Skip to content

Refactoring of the mode system

Niklas Krieger requested to merge stateRefactoring into master

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:
  • reset is replaced by both reset and hardReset:
    • reset performs 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
    • hardReset performs 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 a hardReset, 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, hardReset does nothing
  • all load methods on Territory require that the corresponding game is in the initializing mode
  • it is possible to call initialize on HamsterGame in all modes. If the current mode is not initializing, a hard reset is performed
  • new methods pauseGame and resumeGame are added to HamsterGame. They allow to pause / resume the game via code, which makes debugging easier. pauseGame requires the running mode, resumeGame requires the paused mode
  • startGameIfNotStarted only starts the game if the mode is initializing. If the mode is stopped, a manual hard reset is necessary
  • pause on GameCommandStack used to work async. If executed via pauseGame, this can result in weird bugs, where an additional command gets executed. To prevent this, an new method pauseAsync was introduced, which is used by the UI. The old pause method now works sync and waits until the pause is possible.
  • added precondition functionality to TerritoryBuilder
Open questions
  • Should startGameIfNotStarted perform a hardReset and 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 called getCurrentGameMode.

Merge request reports

Loading