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.Security.SteamSessionAuthentication;
using SVSim.EmulatedEntrypoint.Services;
namespace SVSim.EmulatedEntrypoint.Controllers;
///
/// 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.
///
[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 _log;
public RankBattleController(
IMatchingPairUpService pairUp,
IMatchingBridge bridge,
IMatchContextBuilder ctxBuilder,
IBotRoster botRoster,
ILogger 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 DoMatchingRotation([FromBody] DoMatchingRequestDto req, CancellationToken ct)
=> DoMatchingInternal("rotation_rank_battle", Format.Rotation, req, ct);
[HttpPost("/unlimited_rank_battle/do_matching")]
public Task DoMatchingUnlimited([FromBody] DoMatchingRequestDto req, CancellationToken ct)
=> DoMatchingInternal("unlimited_rank_battle", Format.Unlimited, req, ct);
[HttpPost("/ai_rotation_rank_battle/start")]
public Task AiStartRotation(CancellationToken ct)
=> AiStartInternal(Format.Rotation, ct);
[HttpPost("/ai_unlimited_rank_battle/start")]
public Task AiStartUnlimited(CancellationToken ct)
=> AiStartInternal(Format.Unlimited, ct);
///
/// 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.
///
[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).
});
}
[HttpPost("/rank_battle/force_finish")]
public IActionResult ForceFinish()
{
if (!TryGetViewerId(out var _)) 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()
{
if (!TryGetViewerId(out var _)) return Unauthorized();
return Ok(new { });
}
[HttpPost("/rank_battle/get_latest_master_point")]
public IActionResult GetLatestMasterPoint()
{
if (!TryGetViewerId(out var _)) return Unauthorized();
return Ok(new { });
}
// Filled in by Task 9.
private Task DoMatchingInternal(string mode, Format format, DoMatchingRequestDto req, CancellationToken ct)
{
if (!TryGetViewerId(out var _)) return Task.FromResult(Unauthorized());
// Placeholder; real impl arrives in Task 9.
return Task.FromResult(Ok(new DoMatchingResponseDto { MatchingState = 3002 }));
}
// Filled in by Task 10.
private Task AiStartInternal(Format format, CancellationToken ct)
{
if (!TryGetViewerId(out var _)) return Task.FromResult(Unauthorized());
// Placeholder; real impl arrives in Task 10.
return Task.FromResult(Ok(new AiBattleStartResponseDto { AiId = -1 }));
}
}