diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/game/get_game_state/GetGameStateHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/game/get_game_state/GetGameStateHandler.java index d9fb584..517f835 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/game/get_game_state/GetGameStateHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/game/get_game_state/GetGameStateHandler.java @@ -10,9 +10,16 @@ import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.UserRegistry; import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandHandler; import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.ErrorResponse; import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatcher; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import java.util.logging.Logger; +import java.util.logging.Level; /** Handler for GET_GAME_STATE: returns pot, phase, community cards and per-player info. */ public class GetGameStateHandler extends CommandHandler { + private static final Logger LOGGER = Logger.getLogger(GetGameStateHandler.class.getName()); + private static final int FINISHED_CLEANUP_DELAY_SECONDS = 2; + private final LobbyManager lobbyManager; private final UserRegistry userRegistry; @@ -73,11 +80,13 @@ public class GetGameStateHandler extends CommandHandler { return; } - if (game.getState().getPhase() == GamePhase.FINISHED) { - cleanupLobby(lobby, lobbyId); - } - responseDispatcher.dispatch(new GetGameStateResponse(request.getContext(), game, username)); + + if (game.getState().getPhase() == GamePhase.FINISHED) { + CompletableFuture.delayedExecutor( + FINISHED_CLEANUP_DELAY_SECONDS, TimeUnit.SECONDS) + .execute(() -> cleanupLobby(lobby, lobbyId)); + } } /** @@ -85,15 +94,18 @@ public class GetGameStateHandler extends CommandHandler { * when the game reaches FINISHED phase. */ private void cleanupLobby(Lobby lobby, LobbyId lobbyId) { + if (lobby == null || lobbyId == null) { + return; + } + try { - // Remove all players from the lobby for (String playerName : lobby.getPlayerNames()) { lobbyManager.removePlayer(playerName); } - // Reset the game controller so the lobby returns to CREATED state lobby.initGame(null); } catch (RuntimeException e) { - // Log silently to avoid disrupting game state response + LOGGER.log(Level.WARNING, + "Failed to cleanup lobby " + lobbyId + " during FINISHED state", e); } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/game/get_game_state/GetGameStateResponse.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/game/get_game_state/GetGameStateResponse.java index 795a513..f55fd36 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/game/get_game_state/GetGameStateResponse.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/game/get_game_state/GetGameStateResponse.java @@ -69,6 +69,10 @@ public class GetGameStateResponse extends SuccessResponse { return; } + if (!state.tryMarkWinnerPersistedForHand()) { + return; + } + Player winner = findPlayerByIndex(state, winnerIndex); if (winner == null || winner.getId() == null || winner.getId().value() == null) { return;