Files
SVSimServer/SVSim.EmulatedEntrypoint/Services/MatchContextBuilder.cs
gamer147 898b872edd 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>
2026-06-02 12:28:42 -04:00

115 lines
4.5 KiB
C#

using System.Text.Json;
using SVSim.BattleNode.Bridge;
using SVSim.Database.Enums;
using SVSim.Database.Models.Config;
using SVSim.Database.Repositories.Deck;
using SVSim.Database.Repositories.Viewer;
using SVSim.Database.Services;
namespace SVSim.EmulatedEntrypoint.Services;
public class MatchContextBuilder : IMatchContextBuilder
{
private readonly IArenaTwoPickRunRepository _runs;
private readonly IViewerRepository _viewers;
private readonly IDeckRepository _decks;
private readonly IGameConfigService _config;
public MatchContextBuilder(
IArenaTwoPickRunRepository runs,
IViewerRepository viewers,
IDeckRepository decks,
IGameConfigService config)
{
_runs = runs;
_viewers = viewers;
_decks = decks;
_config = config;
}
public async Task<MatchContext> BuildForTwoPickAsync(long viewerId)
{
var run = await _runs.GetByViewerIdAsync(viewerId)
?? throw new ArenaTwoPickException("arena_two_pick_no_active_run");
var deck = JsonSerializer.Deserialize<List<long>>(run.SelectedCardIdsJson) ?? new();
if (deck.Count < 30)
throw new ArenaTwoPickException("arena_two_pick_draft_incomplete");
var viewer = await _viewers.LoadForMatchContextAsync(viewerId)
?? throw new ArenaTwoPickException("arena_two_pick_no_active_run");
var challenge = _config.Get<ChallengeConfig>();
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()
: run.ClassId.ToString();
return new MatchContext(
SelfDeckCardIds: deck,
ClassId: run.ClassId.ToString(),
CharaId: charaId,
// Hardcoded v1; see spec §Deferred plumbing.
CardMasterName: "card_master_node_10015",
CountryCode: viewer.Info.CountryCode ?? string.Empty,
UserName: viewer.DisplayName,
// TK2-specific cosmetic source; other modes will use the deck row's SleeveId.
SleeveId: challenge.TwoPickSleeveId.ToString(),
EmblemId: emblemId,
DegreeId: degreeId,
// Hardcoded v1; needs equipped-MyPageBackground lookup (see spec §Deferred).
FieldId: 43,
IsOfficial: viewer.Info.IsOfficial ? 1 : 0,
BattleType: 11);
}
public async Task<MatchContext> BuildForRankBattleAsync(long viewerId, Format format, int deckNo)
{
var viewer = await _viewers.LoadForMatchContextAsync(viewerId)
?? throw new InvalidOperationException($"viewer {viewerId} not found");
// IDeckRepository is the right path here — viewer-graph nav refs (DeckCard.Card)
// don't auto-load (see project_ef_nav_include_pitfall memory), which would
// silently ship card_id=0.
var deck = await _decks.GetDeck(viewerId, format, deckNo)
?? throw new InvalidOperationException(
$"viewer {viewerId} has no deck #{deckNo} for format {format}");
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 = 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.Select(c => c.Card.Id).ToList();
return new MatchContext(
SelfDeckCardIds: deckCardIds,
ClassId: deck.Class.Id.ToString(),
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,
BattleType: 11);
}
}