Fix AI opponents in FreePlay 3+ player lobbies

- Remove max_players == 2 gate; use per-seat Add AI buttons in the
  additional players panel instead of the guest-slot button
- Fix seat-indexed panel rendering: unified loop over all additional
  seats detects AI slots, human players, and empty seats correctly
- Add _ai_adding guard to prevent concurrent add-AI requests that
  caused the server race (second request overwrote first AI slot)
- Fix push handler reading wrong key ("slots" → "currentAiSlots")
  which wiped ai_slots on every push, leaving only the newest AI
- Fix ACK handler reading wrong keys ("aiId"/"displayName" →
  "aiAccountId"/"aiDisplayName")
- Server: fix all-ready check that required GuestAccountId.HasValue,
  blocking start for no-guest lobbies filled via AdditionalPlayersJson
- Server: fix StartGameAsync null-deref on GuestAccountId and add
  no-guest branch (host=P1, first additional=P2, rest=seats 2+)

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-05-26 16:51:01 -04:00
co-authored by Claude Happy
parent 32693d303b
commit dfccd091fd
5 changed files with 137 additions and 75 deletions
@@ -154,16 +154,20 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
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.
bool allReady = lobby.GuestAccountId.HasValue && lobby.HostReady && lobby.GuestReady;
if (allReady && lobby.AdditionalPlayersJson != null)
{
var addl = JsonSerializer.Deserialize<List<AdditionalLobbyPlayer>>(
lobby.AdditionalPlayersJson,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true })
?? new();
allReady = addl.All(p => p.IsReady);
}
// All-ready check: host must be ready; guest must be ready if a human guest is present.
// For 3+ player lobbies filled entirely via AdditionalPlayersJson (no human guest),
// GuestAccountId is null — lobby.Status == Full already guarantees all seats are occupied.
bool guestReady = !lobby.GuestAccountId.HasValue || lobby.GuestReady;
List<AdditionalLobbyPlayer> addlForCheck = string.IsNullOrEmpty(lobby.AdditionalPlayersJson)
? []
: JsonSerializer.Deserialize<List<AdditionalLobbyPlayer>>(
lobby.AdditionalPlayersJson,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ?? [];
bool allReady = lobby.Status == CustomLobbyStatus.Full
&& lobby.HostReady
&& guestReady
&& addlForCheck.All(p => p.IsReady);
if (allReady)
await StartGameAsync(lobby, accountId, ct);
@@ -177,32 +181,59 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
Guid triggeringAccountId,
CancellationToken ct)
{
var guestId = lobby.GuestAccountId!.Value;
// Resolve first-turn: matching preferences → that player goes first; otherwise random.
Guid playerOneId;
if (lobby.HostFirstTurnPref == "host" && lobby.GuestFirstTurnPref == "host")
playerOneId = lobby.HostAccountId;
else if (lobby.HostFirstTurnPref == "guest" && lobby.GuestFirstTurnPref == "guest")
playerOneId = guestId;
else
playerOneId = Random.Shared.Next(2) == 0 ? lobby.HostAccountId : guestId;
var playerTwoId = playerOneId == lobby.HostAccountId ? guestId : lobby.HostAccountId;
var p1DeckId = playerOneId == lobby.HostAccountId ? lobby.HostDeckId : lobby.GuestDeckId!.Value;
var p2DeckId = playerTwoId == lobby.HostAccountId ? lobby.HostDeckId : lobby.GuestDeckId!.Value;
// Additional players (seats 2+).
// Deserialize additional players (seats 1+ when no human guest, 2+ when guest is present).
var additionalPlayers = string.IsNullOrEmpty(lobby.AdditionalPlayersJson)
? new List<AdditionalLobbyPlayer>()
: JsonSerializer.Deserialize<List<AdditionalLobbyPlayer>>(
lobby.AdditionalPlayersJson,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ?? new();
// Determine the two "base" players (P1/P2) and the remaining additional players.
// • Human guest present → host + guest fill seats 0/1; additionalPlayers are seats 2+.
// • No human guest → host fills seat 0; first additional player fills seat 1;
// remaining additional players fill seats 2+.
// (3+ player games have no first-turn preference UI, so seat order = turn order.)
Guid playerOneId, playerTwoId;
Guid p1DeckId, p2DeckId;
int p1LobbySeat, p2LobbySeat;
List<AdditionalLobbyPlayer> remainingAddl;
if (lobby.GuestAccountId.HasValue)
{
var guestId = lobby.GuestAccountId.Value;
// Resolve first-turn preference.
if (lobby.HostFirstTurnPref == "host" && lobby.GuestFirstTurnPref == "host")
playerOneId = lobby.HostAccountId;
else if (lobby.HostFirstTurnPref == "guest" && lobby.GuestFirstTurnPref == "guest")
playerOneId = guestId;
else
playerOneId = Random.Shared.Next(2) == 0 ? lobby.HostAccountId : guestId;
playerTwoId = playerOneId == lobby.HostAccountId ? guestId : lobby.HostAccountId;
p1DeckId = playerOneId == lobby.HostAccountId ? lobby.HostDeckId : lobby.GuestDeckId!.Value;
p2DeckId = playerTwoId == lobby.HostAccountId ? lobby.HostDeckId : lobby.GuestDeckId!.Value;
p1LobbySeat = playerOneId == lobby.HostAccountId ? 0 : 1;
p2LobbySeat = 1 - p1LobbySeat;
remainingAddl = additionalPlayers;
}
else
{
// No human guest: host is P1 (lobby seat 0), first additional player is P2 (lobby seat 1).
var p2 = additionalPlayers[0];
playerOneId = lobby.HostAccountId;
playerTwoId = p2.AccountId;
p1DeckId = lobby.HostDeckId;
p2DeckId = p2.DeckId;
p1LobbySeat = 0;
p2LobbySeat = 1;
remainingAddl = additionalPlayers.Skip(1).ToList();
}
// Build the full ordered player list: [P1, P2, P3, ...].
var allPlayerIds = new List<Guid> { playerOneId, playerTwoId };
var allDeckIds = new List<Guid> { p1DeckId, p2DeckId };
foreach (var ap in additionalPlayers)
var allPlayerIds = new List<Guid> { playerOneId, playerTwoId };
var allDeckIds = new List<Guid> { p1DeckId, p2DeckId };
foreach (var ap in remainingAddl)
{
allPlayerIds.Add(ap.AccountId);
allDeckIds.Add(ap.DeckId);
@@ -221,10 +252,6 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
int ComputeTeamIndex(int gameSeat, int lobbySeat) =>
lobby.GameMode == "FFA" ? gameSeat : seatTeams.GetValueOrDefault(lobbySeat, lobbySeat % teamCount);
// P1/P2 lobby seats (before the turn-order coin flip).
int p1LobbySeat = playerOneId == lobby.HostAccountId ? 0 : 1;
int p2LobbySeat = 1 - p1LobbySeat;
// Load the admin-configured custom-default ruleset.
var dbDefaults = await _gameDb.GameRuleSets
.AsNoTracking()
@@ -296,9 +323,9 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
// Build additional players JSON for the session (seats 2+).
string? sessionAdditionalJson = null;
if (additionalPlayers.Count > 0)
if (remainingAddl.Count > 0)
{
var sessionAddl = additionalPlayers.Select((ap, i) => new
var sessionAddl = remainingAddl.Select((ap, i) => new
{
accountId = ap.AccountId,
deckId = ap.DeckId,
@@ -370,9 +397,9 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
ServerMessage.Push("game.custom.started",
new CustomLobbyGameStartedPush(session.Id, playerOneId, p1Name)), ct),
};
for (int i = 0; i < additionalPlayers.Count; i++)
for (int i = 0; i < remainingAddl.Count; i++)
{
var apId = additionalPlayers[i].AccountId;
var apId = remainingAddl[i].AccountId;
var apName = names.GetValueOrDefault(apId, apId.ToString()[..8]);
startTasks.Add(_clientPush.PushAsync(apId.ToString(),
ServerMessage.Push("game.custom.started",