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:
@@ -130,15 +130,19 @@ internal sealed class AiDeckResolver
|
|||||||
existing.ArchSpiritCardDefinitionId = prebuilt.ArchSpiritCardDefinitionId!.Value;
|
existing.ArchSpiritCardDefinitionId = prebuilt.ArchSpiritCardDefinitionId!.Value;
|
||||||
existing.UpdatedAt = now;
|
existing.UpdatedAt = now;
|
||||||
|
|
||||||
// Replace card rows.
|
// Delete old cards directly (bypasses EF tracking / concurrency tokens)
|
||||||
_collectionDb.RemoveRange(existing.Cards);
|
// so a re-add after a previous partial attempt doesn't throw DbUpdateConcurrencyException.
|
||||||
existing.Cards = prebuilt.Cards
|
await _collectionDb.DeckCards
|
||||||
.Select(c => new DeckCard
|
.Where(c => c.DeckId == existing.Id)
|
||||||
{
|
.ExecuteDeleteAsync(ct);
|
||||||
DeckId = existing.Id,
|
|
||||||
CardDefinitionId = c.CardDefinitionId,
|
// Add new cards directly without going through the navigation property.
|
||||||
Quantity = c.Quantity,
|
_collectionDb.DeckCards.AddRange(prebuilt.Cards.Select(c => new DeckCard
|
||||||
}).ToList();
|
{
|
||||||
|
DeckId = existing.Id,
|
||||||
|
CardDefinitionId = c.CardDefinitionId,
|
||||||
|
Quantity = c.Quantity,
|
||||||
|
}));
|
||||||
|
|
||||||
await _collectionDb.SaveChangesAsync(ct);
|
await _collectionDb.SaveChangesAsync(ct);
|
||||||
return existing.Id;
|
return existing.Id;
|
||||||
|
|||||||
@@ -77,10 +77,28 @@ internal sealed class GamePlaceArchSpiritHandler : GameHandlerBase, IMessageHand
|
|||||||
await BroadcastActivityAsync(_clientPush, "started", session.Id, ct);
|
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,
|
if (!bothPlaced)
|
||||||
// signal AiPlayerSessionManager to take over.
|
|
||||||
if (_aiRegistry?.IsAi(newState.ActivePlayerAccountId) == true)
|
|
||||||
{
|
{
|
||||||
|
// 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,
|
await _eventBus.PublishAsync(NatsSubjects.AiTurnStarted,
|
||||||
new AiTurnStartedEvent(
|
new AiTurnStartedEvent(
|
||||||
SessionId : session.Id,
|
SessionId : session.Id,
|
||||||
|
|||||||
Reference in New Issue
Block a user