From 7eeb3acbf02a993bbbf4cd44dd7e97c9dda4a726 Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 13 Apr 2026 08:12:35 -0400 Subject: [PATCH] fix: AI deck sync concurrency error and arch spirit placement trigger MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AiDeckResolver (DbUpdateConcurrencyException on re-add): - ReplaceRange via EF navigation property was failing when the same AI account's deck was synced more than once (retry after a failed lobby attempt left EF's change-tracked xmin stale). - Fix: use ExecuteDeleteAsync to delete old DeckCard rows directly (bypasses change tracker), then AddRange new ones without touching the navigation property. Fully idempotent. GamePlaceArchSpiritHandler (AI never places ArchSpirit): - Previous fix checked newState.ActivePlayerAccountId, which stays as P1 throughout the simultaneous placement phase — so AI opponents in seat 1 (guest) were never triggered. - Fix: when !bothPlaced, scan all players for AI accounts that haven't placed yet and publish AiTurnStartedEvent for each. When bothPlaced, keep the existing check for the first active player in Draw/Main phase. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- Server/src/SOR.Ai/Lobby/AiDeckResolver.cs | 22 ++++++++++------- .../Handlers/GamePlaceArchSpiritHandler.cs | 24 ++++++++++++++++--- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/Server/src/SOR.Ai/Lobby/AiDeckResolver.cs b/Server/src/SOR.Ai/Lobby/AiDeckResolver.cs index 6685827..5d7eafe 100644 --- a/Server/src/SOR.Ai/Lobby/AiDeckResolver.cs +++ b/Server/src/SOR.Ai/Lobby/AiDeckResolver.cs @@ -130,15 +130,19 @@ internal sealed class AiDeckResolver existing.ArchSpiritCardDefinitionId = prebuilt.ArchSpiritCardDefinitionId!.Value; existing.UpdatedAt = now; - // Replace card rows. - _collectionDb.RemoveRange(existing.Cards); - existing.Cards = prebuilt.Cards - .Select(c => new DeckCard - { - DeckId = existing.Id, - CardDefinitionId = c.CardDefinitionId, - Quantity = c.Quantity, - }).ToList(); + // Delete old cards directly (bypasses EF tracking / concurrency tokens) + // so a re-add after a previous partial attempt doesn't throw DbUpdateConcurrencyException. + await _collectionDb.DeckCards + .Where(c => c.DeckId == existing.Id) + .ExecuteDeleteAsync(ct); + + // Add new cards directly without going through the navigation property. + _collectionDb.DeckCards.AddRange(prebuilt.Cards.Select(c => new DeckCard + { + DeckId = existing.Id, + CardDefinitionId = c.CardDefinitionId, + Quantity = c.Quantity, + })); await _collectionDb.SaveChangesAsync(ct); return existing.Id; diff --git a/Server/src/SOR.Game/Handlers/GamePlaceArchSpiritHandler.cs b/Server/src/SOR.Game/Handlers/GamePlaceArchSpiritHandler.cs index cbde843..509e4c3 100644 --- a/Server/src/SOR.Game/Handlers/GamePlaceArchSpiritHandler.cs +++ b/Server/src/SOR.Game/Handlers/GamePlaceArchSpiritHandler.cs @@ -77,10 +77,28 @@ internal sealed class GamePlaceArchSpiritHandler : GameHandlerBase, IMessageHand await BroadcastActivityAsync(_clientPush, "started", session.Id, ct); } - // If the next player to act (either still placing, or first turn in Active phase) is an AI, - // signal AiPlayerSessionManager to take over. - if (_aiRegistry?.IsAi(newState.ActivePlayerAccountId) == true) + if (!bothPlaced) { + // Placement phase is simultaneous — ActivePlayerAccountId stays as P1 regardless + // of who just placed. Instead, scan all players to find AI accounts that still + // need to place their ArchSpirit and signal each of them. + foreach (var (playerId, player) in newState.Players) + { + if (player.ArchSpirit?.Position.HasValue != true + && _aiRegistry?.IsAi(playerId) == true) + { + await _eventBus.PublishAsync(NatsSubjects.AiTurnStarted, + new AiTurnStartedEvent( + SessionId : session.Id, + AiAccountId : playerId, + TurnNumber : newState.TurnNumber), + ct); + } + } + } + else if (_aiRegistry?.IsAi(newState.ActivePlayerAccountId) == true) + { + // Both placed; game has advanced to Draw/Main. Signal AI if it goes first. await _eventBus.PublishAsync(NatsSubjects.AiTurnStarted, new AiTurnStartedEvent( SessionId : session.Id,