using SVSim.BattleNode.Sessions;
namespace SVSim.BattleNode.Sessions.Dispatch;
/// 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 knownList. Generated tokens (cardIds mined from
/// orderList add ops) are recorded into the SAME
/// map via ; a reveal-gate set is still future.
internal sealed class BattleSessionState
{
public BattleSessionPhase SessionPhase { get; set; } = BattleSessionPhase.AwaitingInitNetwork;
public Dictionary PostSwapHands { get; } = new();
/// Per-side idx->cardId, seeded lazily from .
/// Holds deck cards (idx 1..deckCount, seeded) and generated tokens (idx>deckCount, recorded
/// from add ops via ).
public Dictionary> IdxToCardId { get; } = new();
/// The sender's idx->cardId map, seeding it from its on first
/// use. BuildPlayerDeck assigns deck idx = position+1, so entry (i+1) -> cardIds[i].
public IReadOnlyDictionary GetOrSeedDeckMap(IBattleParticipant side)
{
if (!IdxToCardId.TryGetValue(side, out var map))
{
map = new Dictionary();
var deck = side.Context.SelfDeckCardIds;
for (var i = 0; i < deck.Count; i++) map[i + 1] = deck[i];
IdxToCardId[side] = map;
}
return map;
}
/// Record a generated token's identity into the side's idx->cardId map (the same map
/// that holds deck cards). Mined from the sender's orderList add ops by
/// ; surfaced later by BuildPlayedCard when the
/// token is the played card. Deck idxs (1..deckCount) and token idxs (>deckCount) don't
/// collide — the client allocates token idxs after the deck.
public void RecordToken(IBattleParticipant side, int idx, long cardId)
{
GetOrSeedDeckMap(side); // ensure the per-side map exists (deck-seeded)
IdxToCardId[side][idx] = cardId; // overwrite-on-conflict: latest identity wins
}
}