Files
SVSimServer/SVSim.EmulatedEntrypoint/Matching/BotRoster.cs
gamer147 24f9b2240e feat(matching): move BotRoster from hardcoded fixture to DB-backed seed
Phase 3 shipped the AI rank battle bot pool as a hardcoded 8-entry list
inlined in SVSim.EmulatedEntrypoint/Matching/BotRoster.cs — editing meant
recompiling. Per PLAN.md 2026-06-02 item (d), move it to a Bootstrap
importer so the roster lives in seeds/bot-roster.json and the DB.

Shape mirrors PracticeOpponent end-to-end:
- BotRosterEntry (SVSim.Database/Models) — PK = AiId via the Id passthrough
  pattern. DbSet<BotRosterEntry> BotRoster on SVSimDbContext.
- AddBotRoster migration (DDL only, per migrations-are-DDL-only rule).
- seeds/bot-roster.json — 8 rows preserving the current prod-verified
  cosmetic ids (sleeve 704141010 / emblem 400001100 / degree 120027 /
  field 5) and series-1 ai_ids from rm_ai_setting.csv (1111..1181).
- BotRosterSeed POCO + BotRosterImporter (idempotent upsert keyed by AiId,
  leaves seed-missing rows intact). Wired into SVSim.Bootstrap/Program.cs
  next to PracticeOpponentImporter.
- IGlobalsRepository.GetBotRoster() + impl.

IBotRoster.Pick → PickAsync because BotRoster now depends on the transient
IGlobalsRepository. RankBattleController awaits the new signature. The
deterministic hash-on-ctx invariant (same ctx → same bot, so /ai_<fmt>/start
retries pick the same opponent) is preserved.

DI: AddSingleton<IBotRoster> → AddTransient (matches IGlobalsRepository's
lifetime). Test fixture's SeedGlobalsAsync also runs the importer so
RankBattleControllerTests + the rewritten BotRosterTests both see seeded
rows.

Tests: 931 → 936 passing. Existing 3 BotRosterTests reshaped for the DB
backing + 1 new "throws on empty roster" guard; 4 new
BotRosterImporterTests mirror PracticeOpponentImporterTests
(round-trip / idempotent / seed-missing-row-intact / ai_id=0 skip).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-02 11:58:19 -04:00

56 lines
1.9 KiB
C#

using SVSim.BattleNode.Bridge;
using SVSim.Database.Models;
using SVSim.Database.Repositories.Globals;
namespace SVSim.EmulatedEntrypoint.Matching;
/// <summary>
/// DB-backed bot roster. Reads <c>BotRoster</c> rows (seeded from
/// <c>seeds/bot-roster.json</c>) and picks one deterministically per
/// <see cref="MatchContext"/>. See <see cref="IBotRoster"/> for the contract.
/// </summary>
public sealed class BotRoster : IBotRoster
{
private readonly IGlobalsRepository _globals;
public BotRoster(IGlobalsRepository globals)
{
_globals = globals;
}
public async Task<AIBotProfile> PickAsync(MatchContext selfCtx, CancellationToken ct = default)
{
var roster = await _globals.GetBotRoster();
if (roster.Count == 0)
{
throw new InvalidOperationException(
"BotRoster is empty. Run SVSim.Bootstrap to import seeds/bot-roster.json.");
}
// Deterministic: hash the ctx and pick from the roster. Same ctx →
// same bot so a mid-flight retry of /ai_<fmt>/start returns the same
// opponent (no fresh roster pick on each call).
var hash = StringComparer.Ordinal.GetHashCode(selfCtx.UserName)
^ StringComparer.Ordinal.GetHashCode(selfCtx.ClassId);
var index = (int)((uint)hash % roster.Count);
var row = roster[index];
return ToProfile(row);
}
private static AIBotProfile ToProfile(BotRosterEntry row) => new(
AiId: row.AiId,
CountryCode: row.CountryCode,
UserName: row.UserName,
SleeveId: row.SleeveId,
EmblemId: row.EmblemId,
DegreeId: row.DegreeId,
FieldId: row.FieldId,
IsOfficial: row.IsOfficial,
ClassId: row.ClassId,
CharaId: row.CharaId,
Rank: row.Rank,
BattlePoint: row.BattlePoint,
IsMasterRank: row.IsMasterRank,
MasterPoint: row.MasterPoint);
}