-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add game state machine; extract to module
- Loading branch information
Showing
17 changed files
with
232 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<parent> | ||
<groupId>com.recom</groupId> | ||
<artifactId>libs</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>recom-state-machine</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<name>RECOM State Machine</name> | ||
<description>RECOM State Machine</description> | ||
|
||
<dependencies> | ||
|
||
<!-- NEEDED FOR: import org.springframework.lang.Nullable --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-core</artifactId> | ||
</dependency> | ||
<!-- NEEDED FOR: import org.springframework.lang.Nullable --> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>${maven-compiler-plugin.version}</version> | ||
<configuration> | ||
<source>${java.version}</source> | ||
<target>${java.version}</target> | ||
<annotationProcessorPaths> | ||
<path> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>${lombok.version}</version> | ||
</path> | ||
</annotationProcessorPaths> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
2 changes: 1 addition & 1 deletion
2
.../com/recom/statemachine/FSMEventable.java → .../com/recom/statemachine/BaseFSMEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package com.recom.statemachine; | ||
|
||
public interface FSMEventable { | ||
public interface BaseFSMEvent { | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
libs/statemachine/src/main/java/com/recom/statemachine/BaseStateMachine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.recom.statemachine; | ||
|
||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.Setter; | ||
import org.springframework.lang.Nullable; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
|
||
abstract public class BaseStateMachine<STATES extends Enum, TRANSITIONS extends BaseTransition<? extends BaseStateMachine, STATES>, EVENT extends BaseFSMEvent> { | ||
|
||
@Getter | ||
@Setter | ||
@Nullable | ||
protected STATES currentMachineState; | ||
protected TRANSITIONS transitions; | ||
@NonNull | ||
private Instant lastEvent = Instant.now(); | ||
|
||
protected BaseStateMachine() { | ||
currentMachineState = null; | ||
transitions = initTransitions(); | ||
} | ||
|
||
@NonNull | ||
protected abstract TRANSITIONS initTransitions(); | ||
|
||
private boolean triggerReRun = false; | ||
|
||
public abstract void start(); | ||
|
||
public abstract void stop(); | ||
|
||
protected void trackEventTime() { | ||
lastEvent = Instant.now(); | ||
} | ||
|
||
@NonNull | ||
private Duration calculateDeltaTime() { | ||
final Duration deltaTime = Duration.between(lastEvent, Instant.now()); | ||
trackEventTime(); | ||
|
||
return deltaTime; | ||
} | ||
|
||
protected void triggerReRun() { | ||
triggerReRun = true; | ||
} | ||
|
||
public void nextEvent(@NonNull final EVENT event) throws IllegalStateException { | ||
do { | ||
triggerReRun = false; | ||
final Duration deltaTime = calculateDeltaTime(); | ||
handleEvent(event, deltaTime); | ||
} while (triggerReRun); | ||
} | ||
|
||
protected abstract void handleEvent( | ||
@NonNull final EVENT event, | ||
@NonNull final Duration deltaTime | ||
) throws IllegalStateException; | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
libs/statemachine/src/main/java/com/recom/statemachine/BaseTransition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.recom.statemachine; | ||
|
||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
abstract public class BaseTransition<MACHINE extends BaseStateMachine, STATES extends Enum> { | ||
|
||
protected final MACHINE machine; | ||
|
||
@NonNull | ||
protected IllegalStateException getFsmIsNotStarted() { | ||
return new IllegalStateException("FSM is not started"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 10 additions & 1 deletion
11
services/recom-backend/src/main/java/com/recom/service/game/GameMachineService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 0 additions & 17 deletions
17
services/recom-backend/src/main/java/com/recom/statemachine/BaseStateMachine.java
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
services/recom-backend/src/main/java/com/recom/statemachine/game/GameEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.