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>
This commit is contained in:
gamer147
2026-06-02 11:58:19 -04:00
parent 8aead62116
commit 24f9b2240e
18 changed files with 4653 additions and 100 deletions

View File

@@ -180,7 +180,7 @@ public sealed class RankBattleController : ControllerBase
return Ok(new AiBattleStartResponseDto { AiId = -1 });
}
var bot = _botRoster.Pick(selfCtx);
var bot = await _botRoster.PickAsync(selfCtx, ct);
// Per spec, ai-start.md TODO: turnState semantics unclear. Default 0 (player first).
return Ok(new AiBattleStartResponseDto

View File

@@ -1,98 +1,55 @@
using SVSim.BattleNode.Bridge;
using SVSim.Database.Models;
using SVSim.Database.Repositories.Globals;
namespace SVSim.EmulatedEntrypoint.Matching;
/// <summary>
/// Phase 3 hardcoded fixture: one stub bot per class (1..8). <c>ai_id</c> values are
/// the "series 1" tier from the client's <c>rm_ai_setting.csv</c> master table
/// (<c>1111</c>=Forest, <c>1121</c>=Sword, … <c>1181</c>=Portal). The client's
/// <c>RankMatchAISettingList.GetSettingData(aiId)</c> calls <c>.First(...)</c> against
/// this table and throws <c>InvalidOperationException</c> if the id is absent — so the
/// ids MUST match. See <c>data_dumps/client-assets/rm_ai_setting.csv</c> for the full
/// catalog (32 rows: classes 1-8 × tiers 1-2 × deck variants 1-2).
/// 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
{
// Cosmetic ids (sleeve / emblem / degree / field) intentionally use safe
// default values that match the master tables shipped in the project.
// The client-side AI catalog reads ai_id but renders cosmetics from the
// sleeveId/emblemId/etc returned here.
private static readonly IReadOnlyList<AIBotProfile> Roster = new[]
{
new AIBotProfile(AiId: 1111, CountryCode: "JPN", UserName: "Forestcraft AI",
// Cosmetic ids mirror the Scripted bot's prod-verified values
// (ScriptedProfiles / ScriptedBotParticipant.Context). Placeholder
// ids of 1 failed to resolve in ResourcesManager.LoadAssetGroupSync —
// LoadOpponentAssets (SBattleLoad.cs:933) hangs forever waiting for
// missing assets and the "Waiting for opponent" UI never closes.
SleeveId: 704141010, EmblemId: 400001100, DegreeId: 120027, FieldId: 5, IsOfficial: 0,
ClassId: 1, CharaId: 1, Rank: 10, BattlePoint: 0, IsMasterRank: 0, MasterPoint: 0),
new AIBotProfile(AiId: 1121, CountryCode: "JPN", UserName: "Swordcraft AI",
// Cosmetic ids mirror the Scripted bot's prod-verified values
// (ScriptedProfiles / ScriptedBotParticipant.Context). Placeholder
// ids of 1 failed to resolve in ResourcesManager.LoadAssetGroupSync —
// LoadOpponentAssets (SBattleLoad.cs:933) hangs forever waiting for
// missing assets and the "Waiting for opponent" UI never closes.
SleeveId: 704141010, EmblemId: 400001100, DegreeId: 120027, FieldId: 5, IsOfficial: 0,
ClassId: 2, CharaId: 2, Rank: 10, BattlePoint: 0, IsMasterRank: 0, MasterPoint: 0),
new AIBotProfile(AiId: 1131, CountryCode: "JPN", UserName: "Runecraft AI",
// Cosmetic ids mirror the Scripted bot's prod-verified values
// (ScriptedProfiles / ScriptedBotParticipant.Context). Placeholder
// ids of 1 failed to resolve in ResourcesManager.LoadAssetGroupSync —
// LoadOpponentAssets (SBattleLoad.cs:933) hangs forever waiting for
// missing assets and the "Waiting for opponent" UI never closes.
SleeveId: 704141010, EmblemId: 400001100, DegreeId: 120027, FieldId: 5, IsOfficial: 0,
ClassId: 3, CharaId: 3, Rank: 10, BattlePoint: 0, IsMasterRank: 0, MasterPoint: 0),
new AIBotProfile(AiId: 1141, CountryCode: "JPN", UserName: "Dragoncraft AI",
// Cosmetic ids mirror the Scripted bot's prod-verified values
// (ScriptedProfiles / ScriptedBotParticipant.Context). Placeholder
// ids of 1 failed to resolve in ResourcesManager.LoadAssetGroupSync —
// LoadOpponentAssets (SBattleLoad.cs:933) hangs forever waiting for
// missing assets and the "Waiting for opponent" UI never closes.
SleeveId: 704141010, EmblemId: 400001100, DegreeId: 120027, FieldId: 5, IsOfficial: 0,
ClassId: 4, CharaId: 4, Rank: 10, BattlePoint: 0, IsMasterRank: 0, MasterPoint: 0),
new AIBotProfile(AiId: 1151, CountryCode: "JPN", UserName: "Shadowcraft AI",
// Cosmetic ids mirror the Scripted bot's prod-verified values
// (ScriptedProfiles / ScriptedBotParticipant.Context). Placeholder
// ids of 1 failed to resolve in ResourcesManager.LoadAssetGroupSync —
// LoadOpponentAssets (SBattleLoad.cs:933) hangs forever waiting for
// missing assets and the "Waiting for opponent" UI never closes.
SleeveId: 704141010, EmblemId: 400001100, DegreeId: 120027, FieldId: 5, IsOfficial: 0,
ClassId: 5, CharaId: 5, Rank: 10, BattlePoint: 0, IsMasterRank: 0, MasterPoint: 0),
new AIBotProfile(AiId: 1161, CountryCode: "JPN", UserName: "Bloodcraft AI",
// Cosmetic ids mirror the Scripted bot's prod-verified values
// (ScriptedProfiles / ScriptedBotParticipant.Context). Placeholder
// ids of 1 failed to resolve in ResourcesManager.LoadAssetGroupSync —
// LoadOpponentAssets (SBattleLoad.cs:933) hangs forever waiting for
// missing assets and the "Waiting for opponent" UI never closes.
SleeveId: 704141010, EmblemId: 400001100, DegreeId: 120027, FieldId: 5, IsOfficial: 0,
ClassId: 6, CharaId: 6, Rank: 10, BattlePoint: 0, IsMasterRank: 0, MasterPoint: 0),
new AIBotProfile(AiId: 1171, CountryCode: "JPN", UserName: "Havencraft AI",
// Cosmetic ids mirror the Scripted bot's prod-verified values
// (ScriptedProfiles / ScriptedBotParticipant.Context). Placeholder
// ids of 1 failed to resolve in ResourcesManager.LoadAssetGroupSync —
// LoadOpponentAssets (SBattleLoad.cs:933) hangs forever waiting for
// missing assets and the "Waiting for opponent" UI never closes.
SleeveId: 704141010, EmblemId: 400001100, DegreeId: 120027, FieldId: 5, IsOfficial: 0,
ClassId: 7, CharaId: 7, Rank: 10, BattlePoint: 0, IsMasterRank: 0, MasterPoint: 0),
new AIBotProfile(AiId: 1181, CountryCode: "JPN", UserName: "Portalcraft AI",
// Cosmetic ids mirror the Scripted bot's prod-verified values
// (ScriptedProfiles / ScriptedBotParticipant.Context). Placeholder
// ids of 1 failed to resolve in ResourcesManager.LoadAssetGroupSync —
// LoadOpponentAssets (SBattleLoad.cs:933) hangs forever waiting for
// missing assets and the "Waiting for opponent" UI never closes.
SleeveId: 704141010, EmblemId: 400001100, DegreeId: 120027, FieldId: 5, IsOfficial: 0,
ClassId: 8, CharaId: 8, Rank: 10, BattlePoint: 0, IsMasterRank: 0, MasterPoint: 0),
};
private readonly IGlobalsRepository _globals;
public AIBotProfile Pick(MatchContext selfCtx)
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);
return Roster[index];
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);
}

View File

@@ -7,15 +7,16 @@ namespace SVSim.EmulatedEntrypoint.Matching;
/// <c>RankBattleController.AiStart</c> to compose <c>oppo_info</c>.
/// </summary>
/// <remarks>
/// Phase 3 ships a hardcoded fixture; future improvement is to migrate the roster
/// to <c>SVSim.Bootstrap/Data/seeds/bot-roster.json</c> for editability without
/// rebuilds. See spec § Future improvements.
/// Backed by the <c>BotRoster</c> table (seeded from
/// <c>SVSim.Bootstrap/Data/seeds/bot-roster.json</c>). Edit the seed + re-run
/// <c>SVSim.Bootstrap</c> to change the pool without recompiling.
/// </remarks>
public interface IBotRoster
{
/// <summary>
/// Returns a bot profile for the calling viewer. Deterministic per
/// <see cref="MatchContext"/> — the same context value returns the same bot.
/// <see cref="MatchContext"/> — the same context value returns the same bot, so a
/// mid-flight retry of <c>/ai_&lt;fmt&gt;/start</c> picks the same opponent.
/// </summary>
AIBotProfile Pick(MatchContext selfCtx);
Task<AIBotProfile> PickAsync(MatchContext selfCtx, CancellationToken ct = default);
}

View File

@@ -138,8 +138,9 @@ public class Program
new ModePolicy("unlimited_rank_battle", PolicyKind.PvpFirstThenAiFallback),
}));
builder.Services.AddSingleton<IMatchingPairUpService, InProcessPairUp>();
// Phase 3: bot fixture used by RankBattleController.AiStart to compose oppo_info.
builder.Services.AddSingleton<IBotRoster, BotRoster>();
// Phase 3: bot roster used by RankBattleController.AiStart to compose oppo_info.
// Transient because BotRoster depends on the transient IGlobalsRepository.
builder.Services.AddTransient<IBotRoster, BotRoster>();
builder.Services.AddTransient<ShadowverseTranslationMiddleware>();
builder.Services.AddTransient<SessionidMappingMiddleware>();