handleEvent method
Handles a GameEvent and updates the state accordingly
Delegates to the game implementation for a game specific event E
In case of a GenericEvent this handles the implementation of handling the event
Implementation
bool handleEvent(GameEvent event) {
// print('${event.toJson()}');
try {
final game = gameState;
state = event.when(
general: (e) => e.maybeWhen(
undo: () {
// Remove the current state
_previousStates.removeLast();
final lastState = _previousStates.removeLast();
return lastState as GameOrError<G>;
},
readyNextRound: (e) {
if (game.readyPlayers.length > 1) {
_previousStates.remove(game);
}
final newState = game.copyWithGeneric((g) => g.addReadyPlayer(e));
if (newState.readyPlayers.length == game.players.length) {
return game
.moveNextRound(gameConfig)
.copyWithGeneric((g) => g.clearReadyPlayers())
.gameValue();
}
return newState.gameValue();
},
message: (_, __, ___) => game
.copyWithGeneric(
(g) => g.addMessage(e as GameMessage).updateTime())
.gameValue(),
orElse: () =>
GameError('General Event not implemented yet $e', 'programmer'),
),
game: (e) => game.next(e as E) as GameOrError<G>,
);
if (state.isError) {
return false;
} else {
_previousStates.add(state.value!);
return true;
}
// ignore: avoid_catches_without_on_clauses
} catch (error, st) {
_gameStateLogger.severe('$error $st');
}
return false;
}