Class GameEventProcessor

java.lang.Object
it.polimi.ingsw.gc14.GameEventProcessor

public class GameEventProcessor extends Object
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 Details

    • actionQueue

      private final BlockingQueue<NetworkEvent> actionQueue
      Queue from which incoming client events are consumed one at a time.
    • gameController

      private final GameController gameController
      Server-side game controller; all model mutations go through this.
    • playerList

      private final LimitedMap<String,Boolean> playerList
      Shared map tracking each player's online status (true = online).
    • broadcaster

      private final CompositeClientBroadcaster broadcaster
      Broadcaster used to push events and model snapshots to all connected clients.
    • saveManager

      private final SaveManager saveManager
      Persists and deletes game save files.
    • timerExecutor

      private final ScheduledExecutorService timerExecutor
      Single-thread executor used exclusively for the forfeit timer. Daemon so it does not block JVM shutdown.
    • disconnectionTimer

      private ScheduledFuture<?> disconnectionTimer
      Handle to the running forfeit timer, or null when 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 a GameEventProcessor with 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

      public void doFirstEvent() throws InterruptedException
      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()
      Returns true when there is an ongoing game that has not yet ended.
    • isSuspended

      private boolean isSuspended()
      Returns true when the forfeit timer is running, meaning only one player is currently online and the game is waiting for a reconnection.
    • handleInactiveGame

      private void handleInactiveGame(NetworkEvent event)
      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

      private void handleSuspendedGame(NetworkEvent event)
      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

      private void rejectEvent(NetworkEvent event)
      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

      private void applyAndBroadcast(NetworkEvent event)
      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 gameController to prevent concurrent modification of the game model by the network threads.

      Parameters:
      event - the event to apply.
    • cancelForfeitTimerIfReconnect

      private void cancelForfeitTimerIfReconnect(NetworkEvent event)
      Cancels the forfeit timer if the event is a successful reconnection.
      Parameters:
      event - the event that was just successfully applied.
    • enrichEvent

      private void enrichEvent(NetworkEvent event, Game game)
      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

      private void startForfeitTimerIfNeeded(NetworkEvent event, Game game)
      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

      private void endGameForfeit(Game game)
      Ends the game by forfeit when the timer expires without a reconnection. Broadcasts an EndedGame event, deletes the save, and resets state.
      Parameters:
      game - the game model captured when the timer was scheduled.
    • broadcastResult

      private void broadcastResult(NetworkEvent event, Game game, int roundBefore)
      Determines what to broadcast after a successful event application:
      • If the round advanced, an ApplyNextRound event (with updated card lists) replaces the original event.
      • If the game has ended, an EndedGame event 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.
    • onlinePlayerCount

      private long onlinePlayerCount()
      Returns the number of players currently marked as online in the player list.
    • buildDisconnectedList

      private ArrayList<String> buildDisconnectedList(Game game)
      Builds the list of usernames of players currently marked as disconnected in the game model.
      Parameters:
      game - the current game model.
      Returns:
      a new ArrayList of disconnected usernames.
    • removeOfflinePlayers

      private void removeOfflinePlayers()
      Removes all offline entries (value false) from the player list. Online players remain until they disconnect naturally.