feat(arena-colosseum): playable bracket + rank-match promotion (phase 2)

Closes 4 more arena-colosseum spec shapes (6/16 → 10/16): the dual-prefix
battle URLs (colosseum_battle/* and colosseum_rank_battle/*) for both
do_matching and per-match finish, plus the bracket-end /finish + /retire
pair on /arena_colosseum.

* ColosseumProgressionService: pure-logic decisions for win-threshold
  advancement, 3008 promotion trigger, and per-round reward bundles.
  Extends ColosseumRoundsConfig with FinishRewards/RetireRewards per
  round + a top-level ChampionRewards list.
* MatchContextBuilder.BuildForColosseumAsync: lifts the registered deck
  via IDeckRepository (NOT viewer-graph) per project_ef_nav_include_pitfall.
* ArenaColosseumBattleController: dual route prefixes off one controller
  via explicit absolute [HttpPost("/...")] routes — does NOT extend
  SVSimController's [Route("[controller]")]. Same pattern as FreeBattle.
  URL-vs-IsRankMatching mismatch returns 400 with a phase-mismatch error;
  no-deck triggers the standard 3001 matchmaking-illegal state. 3008
  flips run.IsRankMatching exactly once via the progression service.
* /arena_colosseum/finish + /retire share FinishResponse (rewards +
  reward_list + colosseum_status). Champion finish appends ChampionRewards
  and surfaces colosseum_status.is_champion = true. Both delete the run
  row after granting via IInventoryTransaction.GrantAsync.
* DI: ModePolicy entries for "colosseum_battle" and "colosseum_rank_battle".
* Tests: 8 progression-service unit tests (pure logic), 5 battle-controller
  HTTP tests, 5 bracket-terminate HTTP tests, 2 full-bracket E2E walks
  (champion path + retire mid-round). Full suite: 1331/1331.
This commit is contained in:
gamer147
2026-06-13 12:32:56 -04:00
parent 110867358c
commit cbee0f9a50
16 changed files with 1436 additions and 2 deletions

View File

@@ -0,0 +1,69 @@
using SVSim.Database.Models;
using SVSim.Database.Models.Config;
namespace SVSim.EmulatedEntrypoint.Services.ArenaColosseum;
public class ColosseumProgressionService : IColosseumProgressionService
{
/// <summary>Colosseum-specific node signal — see do-matching.md §matching_state 3008.</summary>
public const int PromoteToRankMatchingState = 3008;
public bool ShouldPromoteToRankMatching(ViewerArenaColosseumRun run, int matchingState) =>
matchingState == PromoteToRankMatchingState && !run.IsRankMatching;
public AdvancementDecision DecideAdvancement(ViewerArenaColosseumRun run, ColosseumRoundsConfig rounds)
{
var currentRound = rounds.Rounds.FirstOrDefault(r => r.RoundId == run.RoundId);
var currentGroup = currentRound?.Groups.FirstOrDefault();
var maxRoundId = rounds.Rounds.Count == 0 ? run.RoundId : rounds.Rounds.Max(r => r.RoundId);
// No matching round config (e.g. content not seeded) — treat current state as terminal
// so the controller doesn't loop forever. Champion=false because we can't tell.
if (currentGroup is null)
{
return new AdvancementDecision(run.RoundId, IsBracketEnd: true, IsChampion: false);
}
// Cleared the breakthrough threshold this round.
if (run.WinCount >= currentGroup.BreakthroughNumber)
{
bool isFinal = run.RoundId >= maxRoundId;
return new AdvancementDecision(
NextRoundId: isFinal ? run.RoundId : run.RoundId + 1,
IsBracketEnd: isFinal,
IsChampion: isFinal);
}
// Exhausted the per-round battle cap without clearing — bracket ends at current round.
if (run.BattleCountThisRound >= currentGroup.MaxBattleCount)
{
return new AdvancementDecision(run.RoundId, IsBracketEnd: true, IsChampion: false);
}
// Mid-round, still playing.
return new AdvancementDecision(run.RoundId, IsBracketEnd: false, IsChampion: false);
}
public IReadOnlyList<ColosseumRoundsConfig.RewardEntry> BuildRetireRewards(
ViewerArenaColosseumRun run, ColosseumRoundsConfig rounds)
{
var round = rounds.Rounds.FirstOrDefault(r => r.RoundId == run.RoundId);
return round?.RetireRewards ?? new();
}
public IReadOnlyList<ColosseumRoundsConfig.RewardEntry> BuildFinishRewards(
ViewerArenaColosseumRun run, ColosseumRoundsConfig rounds)
{
var round = rounds.Rounds.FirstOrDefault(r => r.RoundId == run.RoundId);
var bundle = new List<ColosseumRoundsConfig.RewardEntry>();
if (round is not null)
{
bundle.AddRange(round.FinishRewards);
}
if (run.IsChampion)
{
bundle.AddRange(rounds.ChampionRewards);
}
return bundle;
}
}

View File

@@ -0,0 +1,39 @@
using SVSim.Database.Enums;
using SVSim.Database.Models;
using SVSim.Database.Models.Config;
namespace SVSim.EmulatedEntrypoint.Services.ArenaColosseum;
/// <summary>
/// Pure-logic decisions for the Colosseum bracket lifecycle. Reads a
/// <see cref="ViewerArenaColosseumRun"/> + <see cref="ColosseumRoundsConfig"/> snapshot and
/// returns advancement / promotion / reward decisions. Side-effectful (debits, grants,
/// run-row writes) live on the controllers — this service just computes.
/// </summary>
public interface IColosseumProgressionService
{
/// <summary>True when the node signal <c>matching_state == 3008</c> indicates the run
/// has been promoted to the ranked bracket and we haven't already flipped the flag.
/// Subsequent battle URLs route to <c>colosseum_rank_battle/*</c>.</summary>
bool ShouldPromoteToRankMatching(ViewerArenaColosseumRun run, int matchingState);
/// <summary>Triggered post-match-finish when wins/losses cross thresholds. Returns the
/// next round id (or current if no change), whether the bracket has ended, and the
/// champion flag for the final-round-cleared case.</summary>
AdvancementDecision DecideAdvancement(ViewerArenaColosseumRun run, ColosseumRoundsConfig rounds);
/// <summary>Bundle to grant on <c>/retire</c>. Reads
/// <see cref="ColosseumRoundsConfig.RoundEntry.RetireRewards"/> for the run's
/// <see cref="ViewerArenaColosseumRun.RoundId"/>. Empty when no matching round.</summary>
IReadOnlyList<ColosseumRoundsConfig.RewardEntry> BuildRetireRewards(
ViewerArenaColosseumRun run, ColosseumRoundsConfig rounds);
/// <summary>Bundle to grant on <c>/finish</c>. Combines the current round's
/// <see cref="ColosseumRoundsConfig.RoundEntry.FinishRewards"/> with
/// <see cref="ColosseumRoundsConfig.ChampionRewards"/> when the run is a champion.</summary>
IReadOnlyList<ColosseumRoundsConfig.RewardEntry> BuildFinishRewards(
ViewerArenaColosseumRun run, ColosseumRoundsConfig rounds);
}
/// <summary>Output of <see cref="IColosseumProgressionService.DecideAdvancement"/>.</summary>
public sealed record AdvancementDecision(int NextRoundId, bool IsBracketEnd, bool IsChampion);

View File

@@ -24,4 +24,13 @@ public interface IMatchContextBuilder
/// no deck at that slot.
/// </summary>
Task<MatchContext> BuildForRankBattleAsync(long viewerId, Format format, int deckNo);
/// <summary>
/// Build a context for an Arena Colosseum bracket match — reads the active
/// <c>ViewerArenaColosseumRun</c>'s registered deck slot (single-deck v1) via
/// <c>IDeckRepository.GetDeck</c> (NOT viewer-graph traversal per
/// <c>project_ef_nav_include_pitfall</c>). Throws when the run is missing or no deck
/// has been registered yet.
/// </summary>
Task<MatchContext> BuildForColosseumAsync(long viewerId);
}

View File

@@ -11,17 +11,20 @@ namespace SVSim.EmulatedEntrypoint.Services;
public class MatchContextBuilder : IMatchContextBuilder
{
private readonly IArenaTwoPickRunRepository _runs;
private readonly IArenaColosseumRunRepository _colosseumRuns;
private readonly IViewerRepository _viewers;
private readonly IDeckRepository _decks;
private readonly IGameConfigService _config;
public MatchContextBuilder(
IArenaTwoPickRunRepository runs,
IArenaColosseumRunRepository colosseumRuns,
IViewerRepository viewers,
IDeckRepository decks,
IGameConfigService config)
{
_runs = runs;
_colosseumRuns = colosseumRuns;
_viewers = viewers;
_decks = decks;
_config = config;
@@ -120,4 +123,58 @@ public class MatchContextBuilder : IMatchContextBuilder
IsOfficial: viewer.Info.IsOfficial ? 1 : 0,
BattleModeId: BattleModes.TakeTwo);
}
public async Task<MatchContext> BuildForColosseumAsync(long viewerId)
{
var run = await _colosseumRuns.GetByViewerIdAsync(viewerId)
?? throw new InvalidOperationException("arena_colosseum_no_active_run");
// v1 single-deck slot — Round-3 multi-deck format is deferred per plan.
var deckNos = JsonSerializer.Deserialize<List<int>>(run.RegisteredDeckNoListJson) ?? new();
if (deckNos.Count == 0)
{
throw new InvalidOperationException("arena_colosseum_no_deck_registered");
}
var deckNo = deckNos[0];
var viewer = await _viewers.LoadForMatchContextAsync(viewerId)
?? throw new InvalidOperationException($"viewer {viewerId} not found");
var deck = await _decks.GetDeck(viewerId, run.DeckFormat, deckNo)
?? throw new InvalidOperationException(
$"viewer {viewerId} has no deck #{deckNo} for format {run.DeckFormat}");
var defaults = _config.Get<DefaultLoadoutConfig>();
var emblemId = viewer.Info.SelectedEmblem.Id != 0
? viewer.Info.SelectedEmblem.Id.ToString()
: defaults.EmblemId.ToString();
var degreeId = viewer.Info.SelectedDegree.Id != 0
? viewer.Info.SelectedDegree.Id.ToString()
: defaults.DegreeId.ToString();
var charaId = run.LeaderSkinId != 0
? run.LeaderSkinId.ToString()
: deck.LeaderSkin.Id != 0
? deck.LeaderSkin.Id.ToString()
: deck.Class.Id.ToString();
var sleeveId = deck.Sleeve.Id != 0
? deck.Sleeve.Id.ToString()
: defaults.SleeveId.ToString();
var deckCardIds = deck.Cards
.SelectMany(c => Enumerable.Repeat(c.Card.Id, c.Count))
.ToList();
return new MatchContext(
SelfDeckCardIds: deckCardIds,
ClassId: (CardClass)deck.Class.Id,
CharaId: charaId,
CardMasterName: "card_master_node_10015",
CountryCode: viewer.Info.CountryCode ?? string.Empty,
UserName: viewer.DisplayName,
SleeveId: sleeveId,
EmblemId: emblemId,
DegreeId: degreeId,
FieldId: 43,
IsOfficial: viewer.Info.IsOfficial ? 1 : 0,
BattleModeId: BattleModes.TakeTwo);
}
}