fix: AI deck sync concurrency error and arch spirit placement trigger

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 <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-04-13 08:12:35 -04:00
co-authored by Claude Happy
parent 17e1dc12d5
commit 7eeb3acbf0
2 changed files with 34 additions and 12 deletions
+13 -9
View File
@@ -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;
@@ -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,