Implement AI Phase 3: MCTS engine, ELO tracking, self-play, calibration

- MCTS (UCT) decision engine for difficulty tiers 6-8 (MctsNode, MctsSimulator,
  MonteCarloEngine); tiers 6-8 updated from Heuristic → MCTS in AiDifficultyProfiles
- AiEloService: separate ELO ledger for AI accounts (ai_elo_records table)
  so calibration games don't pollute the human RankScore leaderboard
- FinalizeGameAsync / FinalizeMultiPlayerGameAsync: AI accounts are now guarded
  from RankScore and OriginPoints adjustments (IsAi check)
- AiPlayerSessionManager: async GameEnded handler calls AiEloService
- SelfPlayRunner: pure in-process AI vs AI game loop (no DB, no NATS)
- SelfPlayOrchestrator: batch runner loading real decks from DB, parallel games
- FeatureExtractor: GameState → float[1024] feature vector for ML training
- GameReplayExporter: replays completed sessions → TrainingRecord JSONL
- TrainingDataWriter: async JSONL writer
- AiCalibrationJob: pairwise self-play calibration (D1vD2…D9vD10), writes
  AiCalibrationRun rows to GameDbContext
- EF migration: ai_elo_records + ai_calibration_runs tables applied to remote DB
- SOR.Admin: references SOR.Ai; new AI section in NavMenu with ELO Leaderboard
  and Calibration pages; SelfPlayOrchestrator + AiCalibrationJob registered

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-12 18:35:23 -04:00
co-authored by Claude Happy
parent 57915ae6da
commit 27e1b7c8a3
27 changed files with 2242 additions and 19 deletions
@@ -332,8 +332,9 @@ internal abstract class GameHandlerBase
var (winnerDelta, loserDelta) = GameEngine.ComputeEloDelta(winner.RankScore, loser.RankScore);
winner.RankScore += winnerDelta;
loser.RankScore = Math.Max(0, loser.RankScore - loserDelta);
// AI accounts track ELO separately — don't adjust player RankScore.
if (!winner.IsAi) winner.RankScore += winnerDelta;
if (!loser.IsAi) loser.RankScore = Math.Max(0, loser.RankScore - loserDelta);
// Compute OriginPoints rewards from the rule snapshot.
var snap = JsonSerializer.Deserialize<OpSnapshot>(
@@ -345,8 +346,8 @@ internal abstract class GameHandlerBase
var winnerOp = Math.Max(1, snap.WinnerOpReward + adjustment);
var loserOp = Math.Max(0, snap.LoserOpReward - adjustment);
winner.OriginPoints += winnerOp;
loser.OriginPoints += loserOp;
if (!winner.IsAi) winner.OriginPoints += winnerOp;
if (!loser.IsAi) loser.OriginPoints += loserOp;
await identityDb.SaveChangesAsync(ct);
@@ -470,11 +471,11 @@ internal abstract class GameHandlerBase
}
}
// Apply clamped deltas.
// Apply clamped deltas — AI accounts are excluded from player RankScore/OP.
foreach (var acc in accounts)
{
acc.RankScore = Math.Max(0, acc.RankScore + rankDeltas[acc.Id]);
acc.OriginPoints += Math.Max(0, opDeltas[acc.Id]);
if (!acc.IsAi) acc.RankScore = Math.Max(0, acc.RankScore + rankDeltas[acc.Id]);
if (!acc.IsAi) acc.OriginPoints += Math.Max(0, opDeltas[acc.Id]);
}
await identityDb.SaveChangesAsync(ct);