Files
SVSimServer/SVSim.EmulatedEntrypoint/Controllers/RankBattleController.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

229 lines
9.2 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SVSim.BattleNode.Bridge;
using SVSim.Database.Enums;
using SVSim.EmulatedEntrypoint.Constants;
using SVSim.EmulatedEntrypoint.Matching;
using SVSim.EmulatedEntrypoint.Models.Dtos.RankBattle;
using SVSim.EmulatedEntrypoint.Models.Dtos.Requests;
using SVSim.EmulatedEntrypoint.Security.SteamSessionAuthentication;
using SVSim.EmulatedEntrypoint.Services;
namespace SVSim.EmulatedEntrypoint.Controllers;
/// <summary>
/// Rank battle family — covers rotation/unlimited human PvP + AI variants. Crossover
/// is out of scope (no AI variant; human-only). Multi-prefix URLs (rotation_rank_battle/,
/// unlimited_rank_battle/, ai_*_rank_battle/, rank_battle/) require explicit absolute
/// route attributes on each action; the controller doesn't extend SVSimController's
/// [Route("[controller]")] convention.
/// </summary>
[ApiController]
[Authorize(AuthenticationSchemes = SteamAuthenticationConstants.SchemeName)]
public sealed class RankBattleController : ControllerBase
{
private readonly IMatchingPairUpService _pairUp;
private readonly IMatchingBridge _bridge;
private readonly IMatchContextBuilder _ctxBuilder;
private readonly IBotRoster _botRoster;
private readonly ILogger<RankBattleController> _log;
public RankBattleController(
IMatchingPairUpService pairUp,
IMatchingBridge bridge,
IMatchContextBuilder ctxBuilder,
IBotRoster botRoster,
ILogger<RankBattleController> log)
{
_pairUp = pairUp;
_bridge = bridge;
_ctxBuilder = ctxBuilder;
_botRoster = botRoster;
_log = log;
}
private bool TryGetViewerId(out long viewerId)
{
viewerId = 0;
var claim = User.Claims.FirstOrDefault(c => c.Type == ShadowverseClaimTypes.ViewerIdClaim)?.Value;
return claim is not null && long.TryParse(claim, out viewerId);
}
[HttpPost("/rotation_rank_battle/do_matching")]
public Task<IActionResult> DoMatchingRotation([FromBody] DoMatchingRequestDto req, CancellationToken ct)
=> DoMatchingInternal("rotation_rank_battle", Format.Rotation, req, ct);
[HttpPost("/unlimited_rank_battle/do_matching")]
public Task<IActionResult> DoMatchingUnlimited([FromBody] DoMatchingRequestDto req, CancellationToken ct)
=> DoMatchingInternal("unlimited_rank_battle", Format.Unlimited, req, ct);
// AIBattleStartTask has no SetParameter override, so the body is just the inherited
// PostParams (viewer_id / steam_id / steam_session_ticket) — but the translation
// middleware requires at least one parameter to bind the decrypted body. Use BaseRequest.
[HttpPost("/ai_rotation_rank_battle/start")]
public Task<IActionResult> AiStartRotation([FromBody] BaseRequest _, CancellationToken ct)
=> AiStartInternal(Format.Rotation, ct);
[HttpPost("/ai_unlimited_rank_battle/start")]
public Task<IActionResult> AiStartUnlimited([FromBody] BaseRequest _, CancellationToken ct)
=> AiStartInternal(Format.Unlimited, ct);
/// <summary>
/// Shared finish handler — RankBattleFinishTask parses the same wire shape for
/// all four URLs and routes server-side by URL (vs IsAINetwork flag in the client).
/// Stubbed for Phase 3: echo battle_result, emit zeros elsewhere. Real rank
/// progression math is a separate spec.
/// </summary>
[HttpPost("/rotation_rank_battle/finish")]
[HttpPost("/unlimited_rank_battle/finish")]
[HttpPost("/ai_rotation_rank_battle/finish")]
[HttpPost("/ai_unlimited_rank_battle/finish")]
public IActionResult Finish([FromBody] RankBattleFinishRequestDto req)
{
if (!TryGetViewerId(out var _)) return Unauthorized();
return Ok(new RankBattleFinishResponseDto
{
BattleResult = req.BattleResult,
// All other fields default to 0 in the DTO (ClassLevel defaults to 1).
});
}
// BaseRequest parameter on every body-less action so the translation middleware can
// bind the decrypted msgpack body (it explicitly requires at least one parameter).
[HttpPost("/rank_battle/force_finish")]
public IActionResult ForceFinish([FromBody] BaseRequest _)
{
if (!TryGetViewerId(out var _u)) return Unauthorized();
return Ok(new { });
}
[HttpPost("/rank_battle/add_client_log")]
[HttpPost("/rank_battle/add_all_client_log")]
[HttpPost("/rank_battle/add_last_turn_log")]
public IActionResult AddClientLog([FromBody] BaseRequest _)
{
if (!TryGetViewerId(out var _u)) return Unauthorized();
return Ok(new { });
}
[HttpPost("/rank_battle/get_latest_master_point")]
public IActionResult GetLatestMasterPoint([FromBody] BaseRequest _)
{
if (!TryGetViewerId(out var _u)) return Unauthorized();
return Ok(new { });
}
private async Task<IActionResult> DoMatchingInternal(string mode, Format format, DoMatchingRequestDto req, CancellationToken ct)
{
if (!TryGetViewerId(out var vid)) return Unauthorized();
MatchContext ctx;
try
{
ctx = await _ctxBuilder.BuildForRankBattleAsync(vid, format);
}
catch (InvalidOperationException ex)
{
// Most likely cause: viewer has no deck for this format. Surface as 3001
// RC_BATTLE_MATCHING_ILLEGAL — the client shows the standard matchmaking-error
// dialog rather than retrying forever.
_log.LogWarning(ex, "BuildForRankBattleAsync failed for viewer {Vid} format {Fmt}; returning 3001.", vid, format);
return Ok(new DoMatchingResponseDto { MatchingState = 3001, NodeServerUrl = "" });
}
var paired = await _pairUp.TryPairAsync(mode, new BattlePlayer(vid, ctx), ct);
if (paired is null)
{
// Parked. 3002 RETRY. node_server_url must be present as empty string —
// client's DoMatchingBase parser calls .ToString() without a guard.
return Ok(new DoMatchingResponseDto
{
MatchingState = 3002,
NodeServerUrl = "",
});
}
// Owner cache-pickup → 3007 (PvP) or 3011 (AI fallback).
// Joiner (only PvP) → 3004.
var state = paired switch
{
{ IsAiFallback: true } => 3011,
{ IsOwner: true } => 3007,
_ => 3004,
};
return Ok(new DoMatchingResponseDto
{
MatchingState = state,
BattleId = paired.Match.BattleId,
NodeServerUrl = paired.Match.NodeServerUrl,
// Placeholder per spec § Out of scope — per-battle card-master split is deferred.
CardMasterId = 0,
});
}
private async Task<IActionResult> AiStartInternal(Format format, CancellationToken ct)
{
if (!TryGetViewerId(out var vid)) return Unauthorized();
MatchContext selfCtx;
try
{
selfCtx = await _ctxBuilder.BuildForRankBattleAsync(vid, format);
}
catch (InvalidOperationException ex)
{
// No deck → can't build a self profile. Surface as the "no AI assigned"
// sentinel; the client treats ai_id=-1 as a fallback/error condition.
_log.LogWarning(ex, "AiStart failed for viewer {Vid} format {Fmt}; returning ai_id=-1.", vid, format);
return Ok(new AiBattleStartResponseDto { AiId = -1 });
}
var bot = await _botRoster.PickAsync(selfCtx, ct);
// Per spec, ai-start.md TODO: turnState semantics unclear. Default 0 (player first).
return Ok(new AiBattleStartResponseDto
{
AiId = bot.AiId,
TurnState = 0,
SelfInfo = new AiBattlePlayerInfo
{
CountryCode = selfCtx.CountryCode,
UserName = selfCtx.UserName,
SleeveId = int.TryParse(selfCtx.SleeveId, out var sId) ? sId : -1,
EmblemId = int.TryParse(selfCtx.EmblemId, out var eId) ? eId : -1,
DegreeId = int.TryParse(selfCtx.DegreeId, out var dId) ? dId : -1,
FieldId = selfCtx.FieldId,
IsOfficial = selfCtx.IsOfficial,
OppoId = bot.AiId,
Seed = 0,
Rank = 0,
BattlePoint = 0,
ClassId = int.TryParse(selfCtx.ClassId, out var cId) ? cId : -1,
CharaId = int.TryParse(selfCtx.CharaId, out var chId) ? chId : -1,
IsMasterRank = 0,
MasterPoint = 0,
},
OppoInfo = new AiBattlePlayerInfo
{
CountryCode = bot.CountryCode,
UserName = bot.UserName,
SleeveId = bot.SleeveId,
EmblemId = bot.EmblemId,
DegreeId = bot.DegreeId,
FieldId = bot.FieldId,
IsOfficial = bot.IsOfficial,
OppoId = (int)vid,
Seed = 0,
Rank = bot.Rank,
BattlePoint = bot.BattlePoint,
ClassId = bot.ClassId,
CharaId = bot.CharaId,
IsMasterRank = bot.IsMasterRank,
MasterPoint = bot.MasterPoint,
},
});
}
}