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,