Fix: delay lobby cleanup to ensure winner is displayed on all Clients
Refs #125
This commit is contained in:
+19
-7
@@ -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.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.ErrorResponse;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatcher;
|
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. */
|
/** Handler for GET_GAME_STATE: returns pot, phase, community cards and per-player info. */
|
||||||
public class GetGameStateHandler extends CommandHandler<GetGameStateRequest> {
|
public class GetGameStateHandler extends CommandHandler<GetGameStateRequest> {
|
||||||
|
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 LobbyManager lobbyManager;
|
||||||
private final UserRegistry userRegistry;
|
private final UserRegistry userRegistry;
|
||||||
|
|
||||||
@@ -73,11 +80,13 @@ public class GetGameStateHandler extends CommandHandler<GetGameStateRequest> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (game.getState().getPhase() == GamePhase.FINISHED) {
|
|
||||||
cleanupLobby(lobby, lobbyId);
|
|
||||||
}
|
|
||||||
|
|
||||||
responseDispatcher.dispatch(new GetGameStateResponse(request.getContext(), game, username));
|
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<GetGameStateRequest> {
|
|||||||
* when the game reaches FINISHED phase.
|
* when the game reaches FINISHED phase.
|
||||||
*/
|
*/
|
||||||
private void cleanupLobby(Lobby lobby, LobbyId lobbyId) {
|
private void cleanupLobby(Lobby lobby, LobbyId lobbyId) {
|
||||||
|
if (lobby == null || lobbyId == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Remove all players from the lobby
|
|
||||||
for (String playerName : lobby.getPlayerNames()) {
|
for (String playerName : lobby.getPlayerNames()) {
|
||||||
lobbyManager.removePlayer(playerName);
|
lobbyManager.removePlayer(playerName);
|
||||||
}
|
}
|
||||||
// Reset the game controller so the lobby returns to CREATED state
|
|
||||||
lobby.initGame(null);
|
lobby.initGame(null);
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
// Log silently to avoid disrupting game state response
|
LOGGER.log(Level.WARNING,
|
||||||
|
"Failed to cleanup lobby " + lobbyId + " during FINISHED state", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
@@ -69,6 +69,10 @@ public class GetGameStateResponse extends SuccessResponse {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!state.tryMarkWinnerPersistedForHand()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Player winner = findPlayerByIndex(state, winnerIndex);
|
Player winner = findPlayerByIndex(state, winnerIndex);
|
||||||
if (winner == null || winner.getId() == null || winner.getId().value() == null) {
|
if (winner == null || winner.getId() == null || winner.getId().value() == null) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user