fix(rank-battle): route ai-start through the queue-time MatchContext

Live-smoke bug 2026-06-02: queued Bloodcraft (deck #5), wire showed
classId=2 (Swordcraft) for self_info on the /ai_unlimited_rank_battle/start
response — client rendered the wrong leader.

Two layers of the same bug:

1. MatchContextBuilder.BuildForRankBattleAsync hardcoded deckNo=1 instead
   of taking it from the do_matching request — verified against
   data_dumps/captures/traffic.ndjson L17 where deck_no=5 was on the wire.
   Signature changes to (viewerId, format, deckNo); DoMatchingInternal
   passes req.DeckNo.

2. AiStartInternal rebuilt MatchContext from scratch — but the /ai_*/start
   request body is BaseRequest only, no deck_no on the wire. The fix uses
   the MatchContext the bridge already stored at do_matching resolution time
   (in the Bot PendingBattle), so deck/cosmetic data is consistent end-to-end.
   New IBattleSessionStore.TryFindPendingForViewer(viewerId) finds the
   viewer's pending battle for lookup. The store entry persists across
   ai_start (idempotent reads are fine — the WS handler removes on connect).
   No-pending sentinel: ai_id=-1 surfaces the "no AI assigned" error in the
   client.

Tests: 936 → 939 passing.
- MatchContextBuilderTests.BuildForRankBattle_uses_the_caller_supplied_deck_number
  seeds deck #1 (class 1) and deck #5 (class 6) and asserts the deckNo
  argument picks the right one.
- RankBattleControllerTests.AiStart_self_info_class_matches_queued_deck_number
  is the end-to-end regression: register Bot battle with deck #5, hit
  /ai_unlimited_rank_battle/start, assert self_info.classId == 6.
- RankBattleControllerTests.AiStart_without_pending_battle_returns_neg1_sentinel
  locks the defensive ai_id=-1 path.
- Existing AiStart_* tests bypass do_matching, so adapted to call a new
  RegisterBotBattleAsync helper that mirrors what InProcessPairUp does on
  AI-fallback resolution.

SeedDeckAsync gains an optional classId so test cases can differentiate
decks by class (was always picking Classes.First()).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-02 12:28:42 -04:00
parent 24f9b2240e
commit 898b872edd
9 changed files with 150 additions and 34 deletions

View File

@@ -8,6 +8,16 @@ public interface IBattleSessionStore
/// <summary>Look up the pending battle. Returns null if not present.</summary>
PendingBattle? TryGetPending(string battleId);
/// <summary>
/// Find a pending battle this viewer is a participant in (P1 or P2). Used by the
/// HTTP-side <c>/ai_&lt;fmt&gt;/start</c> endpoint to retrieve the deck/cosmetic
/// context the viewer registered at <c>do_matching</c> time — the <c>/start</c>
/// request body carries no <c>deck_no</c> of its own. Returns null if the viewer
/// has no pending battle (already consumed by WS connect, never registered, or
/// evicted by timeout).
/// </summary>
PendingBattle? TryFindPendingForViewer(long viewerId);
/// <summary>Mark a battle as no longer pending (e.g. on successful connect or explicit close).</summary>
bool RemovePending(string battleId);
}

View File

@@ -12,6 +12,21 @@ public sealed class InMemoryBattleSessionStore : IBattleSessionStore
public PendingBattle? TryGetPending(string battleId) =>
_pending.TryGetValue(battleId, out var b) ? b : null;
public PendingBattle? TryFindPendingForViewer(long viewerId)
{
// Linear scan — _pending is bounded by concurrent in-flight matches (low
// double digits at most), so this stays cheap. Returns whichever match the
// dictionary's enumerator yields first; in practice a viewer has at most one
// pending battle since each /do_matching either pairs/falls-back the existing
// slot or parks without registering.
foreach (var b in _pending.Values)
{
if (b.P1.ViewerId == viewerId) return b;
if (b.P2 is not null && b.P2.ViewerId == viewerId) return b;
}
return null;
}
public bool RemovePending(string battleId) =>
_pending.TryRemove(battleId, out _);
}