Class GameEventProcessor
java.lang.Object
it.polimi.ingsw.gc14.GameEventProcessor
Processes game events from the action queue and routes each one
based on the current game state.
The three top-level states that determine routing are:
- Inactive – no game model is present, or the game has ended. Only disconnection cleanup is performed.
- Suspended – a forfeit timer is running because exactly one player remains online. Only reconnection events are accepted.
- Active – normal gameplay; every event is applied, saved, and broadcast.
This class is not thread-safe by itself: it relies on the caller
(the game loop in ServerLauncher) to drive it from a single
thread via doFirstEvent().
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final BlockingQueue<NetworkEvent> Queue from which incoming client events are consumed one at a time.private final CompositeClientBroadcasterBroadcaster used to push events and model snapshots to all connected clients.private ScheduledFuture<?> Handle to the running forfeit timer, ornullwhen no timer is active.private final GameControllerServer-side game controller; all model mutations go through this.private final LimitedMap<String, Boolean> Shared map tracking each player's online status (true= online).private final SaveManagerPersists and deletes game save files.private final ScheduledExecutorServiceSingle-thread executor used exclusively for the forfeit timer. -
Constructor Summary
ConstructorsConstructorDescriptionGameEventProcessor(BlockingQueue<NetworkEvent> actionQueue, GameController gameController, LimitedMap<String, Boolean> playerList, CompositeClientBroadcaster broadcaster, SaveManager saveManager) Constructs aGameEventProcessorwith all required dependencies. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidCancels the forfeit timer, clears the player list, and resets the model.private voidapplyAndBroadcast(NetworkEvent event) Applies the event to the game controller, saves the updated state, and broadcasts the result to connected clients.private voidbroadcastResult(NetworkEvent event, Game game, int roundBefore) Determines what to broadcast after a successful event application: If the round advanced, anApplyNextRoundevent (with updated card lists) replaces the original event. If the game has ended, anEndedGameevent is sent and the save file is deleted. Otherwise the original event is broadcast as-is.buildDisconnectedList(Game game) Builds the list of usernames of players currently marked as disconnected in the game model.private voidCancels the forfeit timer if the event is a successful reconnection.voidBlocks until one event is available in the queue, then routes it to the appropriate handler based on the current game state.private voidendGameForfeit(Game game) Ends the game by forfeit when the timer expires without a reconnection.private voidenrichEvent(NetworkEvent event, Game game) Populates the event with the current game state so that clients can update their mini-model after receiving it.private voidhandleInactiveGame(NetworkEvent event) Handles events that arrive when no active game exists (not yet started, or already ended).private voidhandleSuspendedGame(NetworkEvent event) Routes events while the game is suspended waiting for a reconnection.private booleanReturnstruewhen there is an ongoing game that has not yet ended.private booleanReturnstruewhen the forfeit timer is running, meaning only one player is currently online and the game is waiting for a reconnection.private longReturns the number of players currently marked as online in the player list.private voidrejectEvent(NetworkEvent event) Marks the event as an error and broadcasts it back to the requesting player.private voidRemoves all offline entries (valuefalse) from the player list.private voidstartForfeitTimerIfNeeded(NetworkEvent event, Game game) Schedules the 60-second forfeit timer if a disconnection has left exactly one player online.
-
Field Details
-
actionQueue
Queue from which incoming client events are consumed one at a time. -
gameController
Server-side game controller; all model mutations go through this. -
playerList
Shared map tracking each player's online status (true= online). -
broadcaster
Broadcaster used to push events and model snapshots to all connected clients. -
saveManager
Persists and deletes game save files. -
timerExecutor
Single-thread executor used exclusively for the forfeit timer. Daemon so it does not block JVM shutdown. -
disconnectionTimer
Handle to the running forfeit timer, ornullwhen no timer is active. A non-null value signals that the game is in the suspended state.
-
-
Constructor Details
-
GameEventProcessor
public GameEventProcessor(BlockingQueue<NetworkEvent> actionQueue, GameController gameController, LimitedMap<String, Boolean> playerList, CompositeClientBroadcaster broadcaster, SaveManager saveManager) Constructs aGameEventProcessorwith all required dependencies.- Parameters:
actionQueue- the queue from which incoming events are consumed.gameController- the server-side game controller.playerList- the shared map tracking each player's online status.broadcaster- the broadcaster used to notify all connected clients.saveManager- the save manager used to persist the game state.
-
-
Method Details
-
doFirstEvent
Blocks until one event is available in the queue, then routes it to the appropriate handler based on the current game state.- Throws:
InterruptedException- if the thread is interrupted while waiting for the next event.
-
isGameActive
private boolean isGameActive()Returnstruewhen there is an ongoing game that has not yet ended. -
isSuspended
private boolean isSuspended()Returnstruewhen the forfeit timer is running, meaning only one player is currently online and the game is waiting for a reconnection. -
handleInactiveGame
Handles events that arrive when no active game exists (not yet started, or already ended). Only disconnection cleanup is relevant here.- Parameters:
event- the incoming event.
-
handleSuspendedGame
Routes events while the game is suspended waiting for a reconnection.- A second disconnection while suspended means no player remains online: the game is aborted entirely.
- Any non-reconnection event is rejected with an error.
- A reconnection event is allowed through to
applyAndBroadcast(NetworkEvent).
- Parameters:
event- the incoming event.
-
abortGame
private void abortGame()Cancels the forfeit timer, clears the player list, and resets the model. Called when the last remaining player disconnects while the game is suspended. -
rejectEvent
Marks the event as an error and broadcasts it back to the requesting player. Used to reject actions that are not permitted in the current state.- Parameters:
event- the event to reject.
-
applyAndBroadcast
Applies the event to the game controller, saves the updated state, and broadcasts the result to connected clients.The entire method body is synchronized on
gameControllerto prevent concurrent modification of the game model by the network threads.- Parameters:
event- the event to apply.
-
cancelForfeitTimerIfReconnect
Cancels the forfeit timer if the event is a successful reconnection.- Parameters:
event- the event that was just successfully applied.
-
enrichEvent
Populates the event with the current game state so that clients can update their mini-model after receiving it.Each event subclass overrides
NetworkEvent.enrichWithGameState(Game, ArrayList)to append any type-specific extra fields (available totems, etc.).- Parameters:
event- the event to enrich.game- the current game model.
-
startForfeitTimerIfNeeded
Schedules the 60-second forfeit timer if a disconnection has left exactly one player online.If a previous timer is still pending it is canceled first to avoid duplicate timers.
- Parameters:
event- the event that was just applied.game- the current game model.
-
endGameForfeit
-
broadcastResult
Determines what to broadcast after a successful event application:- If the round advanced, an
ApplyNextRoundevent (with updated card lists) replaces the original event. - If the game has ended, an
EndedGameevent is sent and the save file is deleted. - Otherwise the original event is broadcast as-is.
- Parameters:
event- the event that was applied.game- the current game model (post-apply).roundBefore- the round number before the event was applied.
- If the round advanced, an
-
onlinePlayerCount
private long onlinePlayerCount()Returns the number of players currently marked as online in the player list. -
buildDisconnectedList
-
removeOfflinePlayers
private void removeOfflinePlayers()Removes all offline entries (valuefalse) from the player list. Online players remain until they disconnect naturally.
-