diff --git a/Server/src/SOR.Ai/ServiceRegistration.cs b/Server/src/SOR.Ai/ServiceRegistration.cs index b2166db..6635416 100644 --- a/Server/src/SOR.Ai/ServiceRegistration.cs +++ b/Server/src/SOR.Ai/ServiceRegistration.cs @@ -51,10 +51,14 @@ public static class ServiceRegistration services.AddSingleton(); services.AddSingleton(sp => sp.GetRequiredService()); - // ── Session manager (singleton hosted service) ──────────────────────── - // Listens for game-start events and orchestrates AI turns. + // ── Session manager (singleton, NOT started here) ──────────────────── + // Registered as Singleton so it can be resolved by DI, but AddHostedService + // is intentionally NOT called here. SOR.Host/Program.cs calls AddHostedService + // only when ENABLED_SERVICES includes "ai", ensuring that exactly one pod + // (the AI pod) listens for GameStartedEvent / AiTurnStartedEvent. + // If every pod started this service, each would receive every NATS event + // (queueGroup=null) and concurrently execute the AI's turns. services.AddSingleton(); - services.AddHostedService(sp => sp.GetRequiredService()); // ── Lobby scoped services ───────────────────────────────────────────── services.AddScoped(); diff --git a/Server/src/SOR.Host/Program.cs b/Server/src/SOR.Host/Program.cs index 71fb69e..d85df19 100644 --- a/Server/src/SOR.Host/Program.cs +++ b/Server/src/SOR.Host/Program.cs @@ -179,8 +179,13 @@ if (IsEnabled("content")) builder.Services.AddContentService(builder.Configur if (IsEnabled("versioning")) builder.Services.AddVersioningService(builder.Configuration); if (IsEnabled("matchmaking")) builder.Services.AddMatchmakingService(builder.Configuration); if (IsEnabled("game")) builder.Services.AddGameService(builder.Configuration); -// AI is always co-located with game: it invokes game handlers in-process. +// AI handlers and infrastructure are co-located with game (invoke game handlers in-process). if (IsEnabled("game")) builder.Services.AddAiService(builder.Configuration); +// AiPlayerSessionManager listens for NATS events (no queue group = every subscriber receives +// every event). Only start it on the dedicated AI pod so exactly one process drives AI turns. +if (IsEnabled("ai")) + builder.Services.AddHostedService(sp => + sp.GetRequiredService()); if (IsEnabled("support")) builder.Services.AddSupportService(builder.Configuration); // ── BUILD ─────────────────────────────────────────────────────────────────────