feat(battle-node): per-side idx->cardId map on BattleSessionState

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-03 17:53:32 -04:00
parent 486f72f4a0
commit b295fd8f09
2 changed files with 74 additions and 4 deletions

View File

@@ -1,11 +1,31 @@
using SVSim.BattleNode.Sessions;
namespace SVSim.BattleNode.Sessions.Dispatch;
/// <summary>Mutable per-session state shared across frame handlers. Today: the session-level
/// phase (only ever advanced to <see cref="BattleSessionPhase.Terminal"/>) and the mulligan
/// barrier's post-swap hands. FUTURE (PvP equivalency, NOT this refactor): per-side idx->cardId
/// maps + reveal-gating state land here.</summary>
/// <summary>Mutable per-session state shared across frame handlers. The mulligan barrier's
/// post-swap hands, plus (PvP-equivalency, vanilla slice) the per-side idx->cardId map used to
/// synthesize the opponent-facing <c>knownList</c>. FUTURE: a token map (cardIds mined from
/// orderList <c>add</c> ops, idx>30) + a reveal-gate set land alongside <see cref="IdxToCardId"/>.</summary>
internal sealed class BattleSessionState
{
public BattleSessionPhase SessionPhase { get; set; } = BattleSessionPhase.AwaitingInitNetwork;
public Dictionary<IBattleParticipant, long[]> PostSwapHands { get; } = new();
/// <summary>Per-side idx->cardId, seeded lazily from <see cref="MatchContext.SelfDeckCardIds"/>.
/// Deck cards only (idx 1..deckCount); tokens (idx>deckCount) are deferred.</summary>
public Dictionary<IBattleParticipant, Dictionary<int, long>> IdxToCardId { get; } = new();
/// <summary>The sender's idx->cardId map, seeding it from its <see cref="MatchContext"/> on first
/// use. <c>BuildPlayerDeck</c> assigns deck idx = position+1, so entry (i+1) -> cardIds[i].</summary>
public IReadOnlyDictionary<int, long> GetOrSeedDeckMap(IBattleParticipant side)
{
if (!IdxToCardId.TryGetValue(side, out var map))
{
map = new Dictionary<int, long>();
var deck = side.Context.SelfDeckCardIds;
for (var i = 0; i < deck.Count; i++) map[i + 1] = deck[i];
IdxToCardId[side] = map;
}
return map;
}
}

View File

@@ -0,0 +1,50 @@
using NUnit.Framework;
using SVSim.BattleNode.Bridge;
using SVSim.BattleNode.Sessions;
using SVSim.BattleNode.Sessions.Dispatch;
namespace SVSim.UnitTests.BattleNode.Sessions;
[TestFixture]
public class BattleSessionStateTests
{
private sealed class StubParticipant : IBattleParticipant
{
public long ViewerId { get; }
public MatchContext Context { get; }
public event Func<SVSim.BattleNode.Protocol.MsgEnvelope, CancellationToken, Task>? FrameEmitted;
public StubParticipant(long id, MatchContext ctx) { ViewerId = id; Context = ctx; }
public Task PushAsync(SVSim.BattleNode.Protocol.MsgEnvelope e, bool n, CancellationToken c) => Task.CompletedTask;
public Task RunAsync(CancellationToken c) => Task.CompletedTask;
public Task TerminateAsync(BattleFinishReason r) => Task.CompletedTask;
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
private void Touch() => FrameEmitted?.Invoke(null!, default);
}
private static MatchContext Ctx(params long[] deck) => new(
SelfDeckCardIds: deck, ClassId: "1", CharaId: "1", CardMasterName: "cm",
CountryCode: "KOR", UserName: "P", SleeveId: "0", EmblemId: "0", DegreeId: "0",
FieldId: 0, IsOfficial: 0, BattleType: 11);
[Test]
public void GetOrSeedDeckMap_maps_idx_1based_to_deck_cardIds()
{
var state = new BattleSessionState();
var p = new StubParticipant(1, Ctx(900L, 901L, 902L));
var map = state.GetOrSeedDeckMap(p);
Assert.That(map[1], Is.EqualTo(900L));
Assert.That(map[2], Is.EqualTo(901L));
Assert.That(map[3], Is.EqualTo(902L));
Assert.That(map.ContainsKey(4), Is.False);
}
[Test]
public void GetOrSeedDeckMap_is_idempotent_same_instance()
{
var state = new BattleSessionState();
var p = new StubParticipant(1, Ctx(900L));
Assert.That(state.GetOrSeedDeckMap(p), Is.SameAs(state.GetOrSeedDeckMap(p)));
}
}