Refactoring of the mode system
Refactoring of the mode system of HamsterGame to prevent illegal actions and game modes
This is the new contract of the modes:
- initializing: It is possible to load other territories. To execute commands, it is necessary to call
startGame - running: the game runs, new commands can be executed. It is possible to pause the game or to stop it. It is not possible to load a new territory
- paused: Like running, but it is necessary to continue the game to execute new commands
- stopped: The game was stopped on purpose or an exception occurred which stopped the game. It is possible to undo / redo commands, but it is not possible to execute new commands. It is also not possible to load another territory
To achieve this, the following changes were made:
-
resetis replaced by bothresetandhardReset:-
resetperforms a soft reset: it undoes all previous commands and pauses the game if possible. If the game was already stopped, it remains stopped. It is possible to redo the commands, and if the game was not stopped, it is also possible to execute new commands. Reset is not available if the game is in the initializing mode -
hardResetperforms a hard reset: it undoes all previous commands, but also sets the mode to initializing: therefore, it is possible to load a new territory. However, after ahardReset, it is not possible to redo commands, and it is necessary to start the game to execute commands. If the game is in the initializing mode,hardResetdoes nothing
-
- all load methods on
Territoryrequire that the corresponding game is in the initializing mode - it is possible to call
initializeonHamsterGamein all modes. If the current mode is not initializing, a hard reset is performed - new methods
pauseGameandresumeGameare added toHamsterGame. They allow to pause / resume the game via code, which makes debugging easier.pauseGamerequires the running mode,resumeGamerequires the paused mode -
startGameIfNotStartedonly starts the game if the mode is initializing. If the mode is stopped, a manual hard reset is necessary -
pauseonGameCommandStackused to work async. If executed viapauseGame, this can result in weird bugs, where an additional command gets executed. To prevent this, an new methodpauseAsyncwas introduced, which is used by the UI. The oldpausemethod now works sync and waits until the pause is possible. - added precondition functionality to
TerritoryBuilder
Open questions
- Should
startGameIfNotStartedperform ahardResetand start the game if it is in stopped mode? - The difference between mode / state is unclear. For example, the internal property is called
stateProperty, but the query is calledgetCurrentGameMode.