Fix N-player lobby: premature start, unready for seat 3+, stale reconnect

GameLobbyReadyHandler: change else→else if (isGuest) so additional
players (seat 3+) no longer incorrectly set GuestReady=true.  When
Player 3 called ready the else-branch marked GuestReady, making the
all-ready check fire as soon as the host was also ready — starting
the game without Player 2 ever clicking ready, leaving 2/3 clients
in a starting state and 1 stuck in the lobby.
Also: update DeckId for additional players in AdditionalPlayersJson;
push ready_status to additional player connections.

GameUnreadyHandler: handle additional players (seat 3+) by updating
isReady in AdditionalPlayersJson via JsonNode; push unready status to
all participants including additional players.  Previously threw
NOT_IN_LOBBY for anyone beyond the primary guest.

Game.gd: guard _on_auth_reconnect with 'state is non-empty' so stale
session_id values from ended games don't trigger spurious game.ready
calls.  Also clear session_id in the game.over handler so it never
lingers past the end of a session.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-03-26 09:18:45 -04:00
co-authored by Claude Happy
parent 9c7dc098aa
commit 497e1c3172
3 changed files with 65 additions and 11 deletions
@@ -81,23 +81,27 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
throw new GameException("NOT_IN_LOBBY");
// Update deck and first-turn pref, then mark ready.
// IMPORTANT: additional players (isAdditional) must NOT fall into the else-guest branch,
// as that would incorrectly set GuestReady = true and trigger premature game start.
if (isHost)
{
lobby.HostDeckId = req.DeckId;
lobby.HostFirstTurnPref = req.FirstTurnPref;
lobby.HostReady = true;
}
else
else if (isGuest)
{
lobby.GuestDeckId = req.DeckId;
lobby.GuestFirstTurnPref = req.FirstTurnPref;
lobby.GuestReady = true;
}
// For additional players (isAdditional): readiness is tracked in AdditionalPlayersJson below.
// Do NOT touch HostReady / GuestReady here.
await _gameDb.SaveChangesAsync(ct);
// Update readiness in AdditionalPlayersJson for players 3+.
if (!isHost && !isGuest && lobby.AdditionalPlayersJson != null)
// Update readiness and deck in AdditionalPlayersJson for players 3+.
if (isAdditional && lobby.AdditionalPlayersJson != null)
{
var addl = JsonSerializer.Deserialize<List<AdditionalLobbyPlayerReady>>(
lobby.AdditionalPlayersJson,
@@ -105,12 +109,15 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
?? new();
var entry = addl.FirstOrDefault(p => p.AccountId == accountId);
if (entry != null)
{
entry.IsReady = true;
entry.DeckId = req.DeckId;
}
lobby.AdditionalPlayersJson = JsonSerializer.Serialize(addl);
await _gameDb.SaveChangesAsync(ct);
}
// Push updated ready status to all participants.
// Push updated ready status to all participants (host, guest, and additional players).
var readyPush = ServerMessage.Push("game.custom.lobby.ready_status",
new LobbyReadyStatusPush(lobby.Id, lobby.HostReady, lobby.GuestReady));
@@ -120,6 +127,13 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
};
if (lobby.GuestAccountId.HasValue)
pushTasks.Add(_clientPush.PushAsync(lobby.GuestAccountId.Value.ToString(), readyPush, ct));
if (lobby.AdditionalPlayersJson != null)
{
var addlPush = JsonSerializer.Deserialize<List<AdditionalLobbyPlayer>>(lobby.AdditionalPlayersJson);
if (addlPush != null)
foreach (var ap in addlPush)
pushTasks.Add(_clientPush.PushAsync(ap.AccountId.ToString(), readyPush, ct));
}
await Task.WhenAll(pushTasks);
// All-ready check: host + guest must be ready; for 3+ players all must be ready.
@@ -1,5 +1,7 @@
namespace SOR.Game.Handlers;
using System.Text.Json;
using System.Text.Json.Nodes;
using SOR.Database.Contexts;
using SOR.Database.Enums;
using SOR.Networking.Abstractions.Messaging;
@@ -45,20 +47,56 @@ internal sealed class GameUnreadyHandler : GameHandlerBase, IMessageHandler
throw new GameException("LOBBY_NOT_FULL");
if (lobby.HostAccountId == accountId)
{
lobby.HostReady = false;
}
else if (lobby.GuestAccountId == accountId)
{
lobby.GuestReady = false;
}
else
throw new GameException("NOT_IN_LOBBY");
{
// Additional players (seat 2+): clear isReady in AdditionalPlayersJson.
if (lobby.AdditionalPlayersJson == null)
throw new GameException("NOT_IN_LOBBY");
var jsonArr = JsonNode.Parse(lobby.AdditionalPlayersJson)?.AsArray()
?? throw new GameException("NOT_IN_LOBBY");
bool found = false;
foreach (var node in jsonArr)
{
if (node is not JsonObject obj) continue;
var rawId = obj["accountId"]?.GetValue<string>();
if (rawId != null && Guid.TryParse(rawId, out var aid) && aid == accountId)
{
obj["isReady"] = JsonValue.Create(false);
found = true;
break;
}
}
if (!found) throw new GameException("NOT_IN_LOBBY");
lobby.AdditionalPlayersJson = jsonArr.ToJsonString();
}
await _gameDb.SaveChangesAsync(ct);
var readyPush = ServerMessage.Push("game.custom.lobby.ready_status",
new LobbyReadyStatusPush(lobby.Id, lobby.HostReady, lobby.GuestReady));
await Task.WhenAll(
var pushTasks = new List<Task>
{
_clientPush.PushAsync(lobby.HostAccountId.ToString(), readyPush, ct),
_clientPush.PushAsync(lobby.GuestAccountId!.Value.ToString(), readyPush, ct));
};
if (lobby.GuestAccountId.HasValue)
pushTasks.Add(_clientPush.PushAsync(lobby.GuestAccountId.Value.ToString(), readyPush, ct));
if (lobby.AdditionalPlayersJson != null)
{
var addlList = JsonSerializer.Deserialize<List<AdditionalLobbyPlayer>>(lobby.AdditionalPlayersJson);
if (addlList != null)
foreach (var ap in addlList)
pushTasks.Add(_clientPush.PushAsync(ap.AccountId.ToString(), readyPush, ct));
}
await Task.WhenAll(pushTasks);
return ServerMessage.Reply("game.custom.lobby.unready.ack", message.Seq,
new UnreadyInLobbyResponse(LobbyId: lobby.Id));