Harden lobby/game ready flow for out-of-order joins and stale clicks

This commit is contained in:
Shard
2026-03-27 01:02:26 +00:00
parent 32aa8165bb
commit d7aa61a9ad
5 changed files with 32 additions and 5 deletions
@@ -60,7 +60,11 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
?? throw new GameException("LOBBY_NOT_FOUND");
if (lobby.Status != CustomLobbyStatus.Full)
throw new GameException("LOBBY_NOT_FULL");
{
// Not an error for clients that click ready before final seat is filled.
return ServerMessage.Reply("game.custom.lobby.ready.ack", message.Seq,
new LobbyReadyResponse(LobbyId: lobby.Id, IsReady: false));
}
var isHost = lobby.HostAccountId == accountId;
var isGuest = lobby.GuestAccountId == accountId;
@@ -96,7 +96,13 @@ internal sealed class GameReadyHandler : GameHandlerBase, IMessageHandler
}
if (session.Status != GameSessionStatus.Initializing)
throw new GameException("SESSION_NOT_INITIALIZING");
{
// Session may already be transitioning/started on another client.
// Reply gracefully so clients can recover instead of surfacing hard errors.
await _gameDb.Database.CommitTransactionAsync(ct);
return ServerMessage.Reply("game.ready.ack", message.Seq,
new GameReadyResponse(session.Id, "AlreadyStarted"));
}
// Mark this player ready.
if (session.PlayerOneAccountId == accountId)
@@ -44,7 +44,11 @@ internal sealed class GameUnreadyHandler : GameHandlerBase, IMessageHandler
?? throw new GameException("LOBBY_NOT_FOUND");
if (lobby.Status != CustomLobbyStatus.Full)
throw new GameException("LOBBY_NOT_FULL");
{
// No-op if lobby is not in ready-up state anymore (prevents noisy client errors).
return ServerMessage.Reply("game.custom.lobby.unready.ack", message.Seq,
new UnreadyInLobbyResponse(LobbyId: lobby.Id));
}
if (lobby.HostAccountId == accountId)
{