From 27e1b7c8a3f0f10090763f32f58bd330e431f2a0 Mon Sep 17 00:00:00 2001 From: dingo Date: Sun, 12 Apr 2026 18:35:23 -0400 Subject: [PATCH] Implement AI Phase 3: MCTS engine, ELO tracking, self-play, calibration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 Co-Authored-By: Happy --- .../SOR.Admin/Components/Layout/NavMenu.razor | 17 + .../Pages/Ai/AiCalibrationPage.razor | 202 ++++++ .../Components/Pages/Ai/AiEloPage.razor | 86 +++ Server/src/SOR.Admin/Program.cs | 7 + Server/src/SOR.Admin/SOR.Admin.csproj | 1 + .../SOR.Ai/Calibration/AiCalibrationJob.cs | 111 ++++ Server/src/SOR.Ai/Core/AiPlayerSession.cs | 1 + .../src/SOR.Ai/Core/AiPlayerSessionManager.cs | 43 ++ .../src/SOR.Ai/Engine/AiDifficultyProfiles.cs | 9 +- Server/src/SOR.Ai/Engine/MonteCarloEngine.cs | 152 +++++ Server/src/SOR.Ai/MCTS/MctsNode.cs | 60 ++ Server/src/SOR.Ai/MCTS/MctsSimulator.cs | 77 +++ Server/src/SOR.Ai/ServiceRegistration.cs | 8 + Server/src/SOR.Ai/Tracking/AiEloService.cs | 82 +++ .../src/SOR.Ai/Training/FeatureExtractor.cs | 122 ++++ .../src/SOR.Ai/Training/GameReplayExporter.cs | 100 +++ .../SOR.Ai/Training/SelfPlayOrchestrator.cs | 147 +++++ Server/src/SOR.Ai/Training/SelfPlayRunner.cs | 152 +++++ .../src/SOR.Ai/Training/TrainingDataWriter.cs | 32 + Server/src/SOR.Ai/Training/TrainingRecord.cs | 16 + .../SOR.Database/Contexts/GameDbContext.cs | 30 +- .../Entities/Game/AiCalibrationRun.cs | 20 + .../SOR.Database/Entities/Game/AiEloRecord.cs | 20 + ...2214256_AddAiEloAndCalibration.Designer.cs | 616 ++++++++++++++++++ .../20260412214256_AddAiEloAndCalibration.cs | 75 +++ .../Game/GameDbContextModelSnapshot.cs | 60 ++ .../src/SOR.Game/Handlers/GameHandlerBase.cs | 15 +- 27 files changed, 2242 insertions(+), 19 deletions(-) create mode 100644 Server/src/SOR.Admin/Components/Pages/Ai/AiCalibrationPage.razor create mode 100644 Server/src/SOR.Admin/Components/Pages/Ai/AiEloPage.razor create mode 100644 Server/src/SOR.Ai/Calibration/AiCalibrationJob.cs create mode 100644 Server/src/SOR.Ai/Engine/MonteCarloEngine.cs create mode 100644 Server/src/SOR.Ai/MCTS/MctsNode.cs create mode 100644 Server/src/SOR.Ai/MCTS/MctsSimulator.cs create mode 100644 Server/src/SOR.Ai/Tracking/AiEloService.cs create mode 100644 Server/src/SOR.Ai/Training/FeatureExtractor.cs create mode 100644 Server/src/SOR.Ai/Training/GameReplayExporter.cs create mode 100644 Server/src/SOR.Ai/Training/SelfPlayOrchestrator.cs create mode 100644 Server/src/SOR.Ai/Training/SelfPlayRunner.cs create mode 100644 Server/src/SOR.Ai/Training/TrainingDataWriter.cs create mode 100644 Server/src/SOR.Ai/Training/TrainingRecord.cs create mode 100644 Server/src/SOR.Database/Entities/Game/AiCalibrationRun.cs create mode 100644 Server/src/SOR.Database/Entities/Game/AiEloRecord.cs create mode 100644 Server/src/SOR.Database/Migrations/Game/20260412214256_AddAiEloAndCalibration.Designer.cs create mode 100644 Server/src/SOR.Database/Migrations/Game/20260412214256_AddAiEloAndCalibration.cs diff --git a/Server/src/SOR.Admin/Components/Layout/NavMenu.razor b/Server/src/SOR.Admin/Components/Layout/NavMenu.razor index d1f1564..912506d 100644 --- a/Server/src/SOR.Admin/Components/Layout/NavMenu.razor +++ b/Server/src/SOR.Admin/Components/Layout/NavMenu.razor @@ -163,6 +163,23 @@ +