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:
@@ -71,10 +71,11 @@ func _clear_state() -> void:
|
|||||||
|
|
||||||
|
|
||||||
## Called on every auth_success (initial login AND reconnects).
|
## Called on every auth_success (initial login AND reconnects).
|
||||||
## If we have an active session, resend game.ready so the server
|
## If we have both an active session_id AND live game state, resend game.ready
|
||||||
## re-registers this connection and we get state updates again.
|
## so the server re-registers this connection and we get state updates again.
|
||||||
|
## The state check prevents sending stale session IDs from games that already ended.
|
||||||
func _on_auth_reconnect(_uid: String, _dn: String) -> void:
|
func _on_auth_reconnect(_uid: String, _dn: String) -> void:
|
||||||
if not session_id.is_empty():
|
if not session_id.is_empty() and not state.is_empty():
|
||||||
send_ready()
|
send_ready()
|
||||||
|
|
||||||
|
|
||||||
@@ -584,7 +585,8 @@ func _on_server_push(type: String, payload: Variant) -> void:
|
|||||||
var wd : int = int(d.get("winnerRankDelta", 0))
|
var wd : int = int(d.get("winnerRankDelta", 0))
|
||||||
var ld : int = int(d.get("loserRankDelta", 0))
|
var ld : int = int(d.get("loserRankDelta", 0))
|
||||||
var outcomes : Array = d.get("playerOutcomes", []) as Array
|
var outcomes : Array = d.get("playerOutcomes", []) as Array
|
||||||
state = {} # Clear stale state so a new GameScreen calls send_ready() on the next match
|
session_id = "" # Clear so _on_auth_reconnect doesn't resend game.ready for an ended game
|
||||||
|
state = {} # Clear stale state so a new GameScreen calls send_ready() on the next match
|
||||||
game_over.emit(winner, wc, wd, ld, outcomes)
|
game_over.emit(winner, wc, wd, ld, outcomes)
|
||||||
|
|
||||||
"game.session_abandoned":
|
"game.session_abandoned":
|
||||||
|
|||||||
@@ -81,23 +81,27 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
|
|||||||
throw new GameException("NOT_IN_LOBBY");
|
throw new GameException("NOT_IN_LOBBY");
|
||||||
|
|
||||||
// Update deck and first-turn pref, then mark ready.
|
// 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)
|
if (isHost)
|
||||||
{
|
{
|
||||||
lobby.HostDeckId = req.DeckId;
|
lobby.HostDeckId = req.DeckId;
|
||||||
lobby.HostFirstTurnPref = req.FirstTurnPref;
|
lobby.HostFirstTurnPref = req.FirstTurnPref;
|
||||||
lobby.HostReady = true;
|
lobby.HostReady = true;
|
||||||
}
|
}
|
||||||
else
|
else if (isGuest)
|
||||||
{
|
{
|
||||||
lobby.GuestDeckId = req.DeckId;
|
lobby.GuestDeckId = req.DeckId;
|
||||||
lobby.GuestFirstTurnPref = req.FirstTurnPref;
|
lobby.GuestFirstTurnPref = req.FirstTurnPref;
|
||||||
lobby.GuestReady = true;
|
lobby.GuestReady = true;
|
||||||
}
|
}
|
||||||
|
// For additional players (isAdditional): readiness is tracked in AdditionalPlayersJson below.
|
||||||
|
// Do NOT touch HostReady / GuestReady here.
|
||||||
|
|
||||||
await _gameDb.SaveChangesAsync(ct);
|
await _gameDb.SaveChangesAsync(ct);
|
||||||
|
|
||||||
// Update readiness in AdditionalPlayersJson for players 3+.
|
// Update readiness and deck in AdditionalPlayersJson for players 3+.
|
||||||
if (!isHost && !isGuest && lobby.AdditionalPlayersJson != null)
|
if (isAdditional && lobby.AdditionalPlayersJson != null)
|
||||||
{
|
{
|
||||||
var addl = JsonSerializer.Deserialize<List<AdditionalLobbyPlayerReady>>(
|
var addl = JsonSerializer.Deserialize<List<AdditionalLobbyPlayerReady>>(
|
||||||
lobby.AdditionalPlayersJson,
|
lobby.AdditionalPlayersJson,
|
||||||
@@ -105,12 +109,15 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
|
|||||||
?? new();
|
?? new();
|
||||||
var entry = addl.FirstOrDefault(p => p.AccountId == accountId);
|
var entry = addl.FirstOrDefault(p => p.AccountId == accountId);
|
||||||
if (entry != null)
|
if (entry != null)
|
||||||
|
{
|
||||||
entry.IsReady = true;
|
entry.IsReady = true;
|
||||||
|
entry.DeckId = req.DeckId;
|
||||||
|
}
|
||||||
lobby.AdditionalPlayersJson = JsonSerializer.Serialize(addl);
|
lobby.AdditionalPlayersJson = JsonSerializer.Serialize(addl);
|
||||||
await _gameDb.SaveChangesAsync(ct);
|
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",
|
var readyPush = ServerMessage.Push("game.custom.lobby.ready_status",
|
||||||
new LobbyReadyStatusPush(lobby.Id, lobby.HostReady, lobby.GuestReady));
|
new LobbyReadyStatusPush(lobby.Id, lobby.HostReady, lobby.GuestReady));
|
||||||
|
|
||||||
@@ -120,6 +127,13 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
|
|||||||
};
|
};
|
||||||
if (lobby.GuestAccountId.HasValue)
|
if (lobby.GuestAccountId.HasValue)
|
||||||
pushTasks.Add(_clientPush.PushAsync(lobby.GuestAccountId.Value.ToString(), readyPush, ct));
|
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);
|
await Task.WhenAll(pushTasks);
|
||||||
|
|
||||||
// All-ready check: host + guest must be ready; for 3+ players all must be ready.
|
// All-ready check: host + guest must be ready; for 3+ players all must be ready.
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
namespace SOR.Game.Handlers;
|
namespace SOR.Game.Handlers;
|
||||||
|
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
using SOR.Database.Contexts;
|
using SOR.Database.Contexts;
|
||||||
using SOR.Database.Enums;
|
using SOR.Database.Enums;
|
||||||
using SOR.Networking.Abstractions.Messaging;
|
using SOR.Networking.Abstractions.Messaging;
|
||||||
@@ -45,20 +47,56 @@ internal sealed class GameUnreadyHandler : GameHandlerBase, IMessageHandler
|
|||||||
throw new GameException("LOBBY_NOT_FULL");
|
throw new GameException("LOBBY_NOT_FULL");
|
||||||
|
|
||||||
if (lobby.HostAccountId == accountId)
|
if (lobby.HostAccountId == accountId)
|
||||||
|
{
|
||||||
lobby.HostReady = false;
|
lobby.HostReady = false;
|
||||||
|
}
|
||||||
else if (lobby.GuestAccountId == accountId)
|
else if (lobby.GuestAccountId == accountId)
|
||||||
|
{
|
||||||
lobby.GuestReady = false;
|
lobby.GuestReady = false;
|
||||||
|
}
|
||||||
else
|
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);
|
await _gameDb.SaveChangesAsync(ct);
|
||||||
|
|
||||||
var readyPush = ServerMessage.Push("game.custom.lobby.ready_status",
|
var readyPush = ServerMessage.Push("game.custom.lobby.ready_status",
|
||||||
new LobbyReadyStatusPush(lobby.Id, lobby.HostReady, lobby.GuestReady));
|
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.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,
|
return ServerMessage.Reply("game.custom.lobby.unready.ack", message.Seq,
|
||||||
new UnreadyInLobbyResponse(LobbyId: lobby.Id));
|
new UnreadyInLobbyResponse(LobbyId: lobby.Id));
|
||||||
|
|||||||
Reference in New Issue
Block a user