fix(ai): draw first then re-evaluate Main phase plan

AiPlayerSession.ExecuteTurnAsync was computing the action plan while the
game state was still in Draw phase. ActionCandidateGenerator.All returns
only [AiDrawAction] during Draw, so the plan was always [draw, end_turn] —
the AI never moved, played cards, or attacked.

Fix: when state is Draw phase, invoke game.draw immediately, reload
StateJson from the DB (post-draw Main state), then compute the full plan
with the actual hand contents. ReloadStateAsync opens a fresh scope so it
sees the committed row the draw handler just wrote.

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 09:30:03 -04:00
co-authored by Claude Happy
parent 0824f7d315
commit 623acf357b
+56 -2
View File
@@ -1,11 +1,13 @@
namespace SOR.Ai.Core;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SOR.Ai.Engine;
using SOR.Ai.Heuristics;
using SOR.Ai.Transport;
using SOR.Database.Contexts;
using SOR.Database.Enums; // GamePhase
using SOR.Game; // GameState, GameEngine, game request records
using SOR.Networking.Abstractions.Messaging;
@@ -99,11 +101,42 @@ internal sealed class AiPlayerSession : IAsyncDisposable
// Resolve difficulty profile + strategy overlay.
var profile = AiDifficultyProfiles.Get(Difficulty).WithStrategy(Strategy);
// Think delay (before first action).
// Draw phase: send game.draw, then reload state so the plan is computed
// with the actual hand contents after drawing (Main phase state).
// ActionCandidateGenerator.All returns only [AiDrawAction] during Draw phase,
// so computing the plan before drawing would always produce [draw, end_turn].
if (state.Phase == GamePhase.Draw)
{
await Task.Delay(Random.Shared.Next(300, 700), ct);
await InvokeHandlerAsync("game.draw",
JsonSerializer.SerializeToUtf8Bytes(new GameDrawRequest(SessionId)),
ct);
// Reload state: the draw handler updated StateJson in the DB.
var reloaded = await ReloadStateAsync(ct);
if (reloaded is null)
{
_logger.LogWarning("AI {Account}: could not reload state after draw.", AiAccountId);
return;
}
state = reloaded;
if (state.Phase != GamePhase.Main)
{
_logger.LogDebug(
"AI {Account}: state is {Phase} after draw — skipping Main phase plan.",
AiAccountId, state.Phase);
return;
}
}
// Think delay (before first Main-phase action).
await Task.Delay(
Random.Shared.Next(profile.MinThinkTimeMs, profile.MaxThinkTimeMs + 1), ct);
// Compute action plan.
// Compute action plan with post-draw Main phase state.
var ctx = new AiTurnContext(state, AiAccountId);
var engine = ResolveEngine(profile);
IReadOnlyList<AiAction> plan;
@@ -138,6 +171,27 @@ internal sealed class AiPlayerSession : IAsyncDisposable
}
}
// ── State reload ──────────────────────────────────────────────────────────
/// <summary>
/// Loads the current game state from the DB.
/// Used after invoking game.draw to get the post-draw Main-phase state.
/// </summary>
private async Task<GameState?> ReloadStateAsync(CancellationToken ct)
{
using var scope = _scopeFactory.CreateScope();
var gameDb = scope.ServiceProvider.GetRequiredService<GameDbContext>();
var session = await gameDb.GameSessions
.AsNoTracking()
.FirstOrDefaultAsync(s => s.Id == SessionId, ct);
if (session?.StateJson is null)
return null;
return GameEngine.Deserialize(session.StateJson);
}
// ── Placement ─────────────────────────────────────────────────────────────
private async Task ExecutePlacementAsync(GameState state, CancellationToken ct)