- AiDeckResolver: resolves which prebuilt deck an AI uses for a given difficulty. Checks AiDeckAssignment table first (admin-configured), then falls back to a random active starter deck. Creates/updates a real Deck row in CollectionDb so GameReadyHandler's standard path works unchanged. - AiDeckAssignment entity + EF migration (ai_deck_assignments table): admin maps Difficulty 1-10 → PrebuiltDeckId; null = "random starter". - GameAddAiHandler: replaced direct Deck lookup with AiDeckResolver.ResolveAsync. - AiSeedStartupService: removed AiDeckSeeder call — decks are resolved per-game from prebuilt decks, not pre-seeded with placeholder cards. - ServiceRegistration: registers AiDeckResolver as Scoped. - Admin: new /ai/deck-config page (AiDeckConfigPage) showing a D1-D10 table with prebuilt deck dropdowns; saved via upsert. Added to AI nav section. 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>
52 lines
1.9 KiB
C#
52 lines
1.9 KiB
C#
namespace SOR.Ai.Seeding;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using SOR.Ai.Core;
|
|
using SOR.Database.Contexts; // SorIdentityDbContext
|
|
|
|
/// <summary>
|
|
/// One-shot IHostedService that runs at startup before the server accepts traffic.
|
|
/// Seeds AI accounts and decks, then loads the AiAccountRegistry so other services
|
|
/// can check IsAi without hitting the database on every turn.
|
|
/// </summary>
|
|
internal sealed class AiSeedStartupService : IHostedService
|
|
{
|
|
private readonly IServiceScopeFactory _scopeFactory;
|
|
private readonly AiAccountRegistry _registry;
|
|
private readonly ILogger<AiSeedStartupService> _logger;
|
|
|
|
public AiSeedStartupService(
|
|
IServiceScopeFactory scopeFactory,
|
|
AiAccountRegistry registry,
|
|
ILogger<AiSeedStartupService> logger)
|
|
{
|
|
_scopeFactory = scopeFactory;
|
|
_registry = registry;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
await using var scope = _scopeFactory.CreateAsyncScope();
|
|
var sp = scope.ServiceProvider;
|
|
|
|
var identityDb = sp.GetRequiredService<SorIdentityDbContext>();
|
|
|
|
// 1. Seed AI accounts (idempotent).
|
|
await AiAccountSeeder.SeedAsync(identityDb, _logger, cancellationToken);
|
|
|
|
// 2. Populate in-memory registry for O(1) IsAi checks.
|
|
// Decks are no longer pre-seeded — AiDeckResolver picks a prebuilt deck
|
|
// per-game based on admin-configured AiDeckAssignments.
|
|
_registry.Load(identityDb);
|
|
|
|
_logger.LogInformation(
|
|
"SOR.Ai: startup seed complete. {Count} AI accounts registered.",
|
|
_registry.Count);
|
|
}
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
|
}
|