diff --git a/Server/src/SOR.Ai/Core/AiPlayerSession.cs b/Server/src/SOR.Ai/Core/AiPlayerSession.cs index 2447e18..9dc0469 100644 --- a/Server/src/SOR.Ai/Core/AiPlayerSession.cs +++ b/Server/src/SOR.Ai/Core/AiPlayerSession.cs @@ -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 plan; @@ -138,6 +171,27 @@ internal sealed class AiPlayerSession : IAsyncDisposable } } + // ── State reload ────────────────────────────────────────────────────────── + + /// + /// Loads the current game state from the DB. + /// Used after invoking game.draw to get the post-draw Main-phase state. + /// + private async Task ReloadStateAsync(CancellationToken ct) + { + using var scope = _scopeFactory.CreateScope(); + var gameDb = scope.ServiceProvider.GetRequiredService(); + + 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)