using SVSim.BattleNode.Lifecycle;
using SVSim.BattleNode.Protocol;
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
{
/// The one random value chosen per battle. Every per-battle RNG (shared effect seed,
/// each side's deck shuffle + idxChangeSeed) derives from it via .
/// Logged at session start so a battle's randomness is reproducible (future replay).
public int MasterSeed { get; }
/// Test hook — production uses the random default.
public BattleSessionState(int? masterSeed = null) =>
MasterSeed = masterSeed ?? Random.Shared.Next();
private readonly Dictionary> _shuffledDecks = new();
/// This side's deck, shuffled deterministically from
/// (Fisher–Yates). Cached per side. Both the wire selfDeck (Matched) and the reveal map
/// () read this, so they share one shuffled order.
public IReadOnlyList GetShuffledDeck(IBattleParticipant side)
{
if (_shuffledDecks.TryGetValue(side, out var cached)) return cached;
var deck = side.Context.SelfDeckCardIds.ToArray();
var rng = new Random(BattleSeeds.DeckShuffle(MasterSeed, side.ViewerId));
for (var i = deck.Length - 1; i > 0; i--)
{
var j = rng.Next(i + 1);
(deck[i], deck[j]) = (deck[j], deck[i]);
}
_shuffledDecks[side] = deck;
return deck;
}
public SessionLifecycle Lifecycle { get; set; } = SessionLifecycle.Active;
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 order on
/// first use. Deck idx = position+1 in the shuffled order, so entry (i+1) -> shuffledDeck[i]. The
/// wire selfDeck (Matched) is built from the same shuffled order, so the two agree.
public IReadOnlyDictionary GetOrSeedDeckMap(IBattleParticipant side)
{
if (!IdxToCardId.TryGetValue(side, out var map))
{
map = new Dictionary();
var deck = GetShuffledDeck(side);
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
}
/// Mine generated-token identities from a sender's orderList add ops and
/// record each into the correct side's map. isSelf:1 → the sender's own token (); isSelf:0 → a cross-side gift living at that idx in the OPPONENT's index
/// space () — isSelf is the sender's perspective tag on
/// CardObj.IsPlayer (RegisterToken.cs:22), and a card has a single CardObj.Index, so
/// the gifted idx is the same slot in the recipient's own map (the one consulted when the recipient
/// later plays it). Shared by PlayActionsHandler and EchoHandler — an Echo's orderList
/// carries the same add-op shape (SendCardDataMaker.MakeEchoData), so both mine identically;
/// Echo is mined but never relayed.
public void RecordTokensFrom(IBattleParticipant from, IBattleParticipant other, object? orderList)
{
// TRUST: isSelf is the SENDER's own perspective flag and idx is unbounded, while RecordToken
// overwrites-on-conflict. A buggy/malicious sender could pass isSelf:0 with a deck-range idx to
// rewrite the OPPONENT's card identity at a seeded slot. Acceptable for the current trusted-LAN
// relay; if peers ever become untrusted, gate on `idx > deckCount` here (generated tokens always
// allocate past the deck) so a sender can't forge over seeded deck cards.
foreach (var (idx, cardId, isSelf) in KnownListBuilder.MineAddOps(orderList))
RecordToken(isSelf == CardOwner.Self ? from : other, idx, cardId);
}
/// Mine + record choice/Discover-token picks ()
/// into the correct side's map, by the same isSelf routing as .
/// The chosen cardId rides the generating send's keyAction.selectCard (not the orderList add
/// op, which carries candidates only); recorded regardless of the choice's open visibility —
/// an unplayed idx is never queried, so a stray record is harmless.
public void RecordChoicePicksFrom(IBattleParticipant from, IBattleParticipant other, object? orderList, object? keyAction)
{
foreach (var (idx, cardId, isSelf) in KnownListBuilder.MineChoicePicks(orderList, keyAction))
RecordToken(isSelf == CardOwner.Self ? from : other, idx, cardId);
}
/// Per-side idx->spellboost COUNT, accumulated from orderList alter ops via
/// . Separate from because spellboost is a
/// mutable counter, not an identity. Surfaced by BuildPlayedCard as the played card's
/// knownList.spellboost so the opponent computes its discounted cost (see that method).
public Dictionary> IdxToSpellboost { get; } = new();
private Dictionary SpellboostMap(IBattleParticipant side)
{
if (!IdxToSpellboost.TryGetValue(side, out var map))
IdxToSpellboost[side] = map = new Dictionary();
return map;
}
/// The side's idx->spellboost map (empty if nothing recorded yet). Read by
/// PlayActionsHandler to feed BuildPlayedCard.
public IReadOnlyDictionary GetSpellboostMap(IBattleParticipant side) => SpellboostMap(side);
/// Apply a frame's spellboost alter ops to the per-side maps. Routed by isSelf
/// (the sender's perspective) exactly like : isSelf:1 → the
/// sender's own hand (); isSelf:0 → the opponent's hand
/// () for the rare cross-side spellboost. Ops: 'a' add, 's' set,
/// 'h' half. Call this AFTER BuildPlayedCard for the same frame: a card's cost is fixed
/// when it leaves hand, so the played card's emitted count must reflect state BEFORE this frame's
/// grant (Fate's Hand plays, then spellboosts the rest of the hand). Recorded only from the
/// authoritative PlayActions, never the Echo, to avoid double-counting the same alter.
/// Known gap: a card bounced back to hand keeps its stale count (no reset on zone-exit) — not yet
/// observed in capture, left for when a bounce desync actually shows up.
public void RecordSpellboostFrom(IBattleParticipant from, IBattleParticipant other, object? orderList)
{
foreach (var (idx, isSelf, op, amount) in KnownListBuilder.MineAlterSpellboosts(orderList))
{
var map = SpellboostMap(isSelf == CardOwner.Self ? from : other);
map.TryGetValue(idx, out var cur);
map[idx] = op switch
{
's' => amount, // set
'h' => cur / 2, // half
_ => cur + amount, // 'a' add (the only form seen in capture)
};
}
}
/// Mine + record copy/clone-token identities ()
/// into the correct side's map. A copy's source lives at baseIdx in the actor's own index
/// space, so the resolution side == the record side, both selected by the same isSelf routing
/// as . Passing the LIVE per-side maps (via
/// , not snapshots) lets a copy that references a plain/choice token
/// added earlier THIS frame resolve — provided this runs AFTER
/// / (the handler orders it last).
/// Seeding both maps up front matters because a copy-only frame (no concrete/choice add) would never
/// have hit yet, leaving the maps unseeded.
public void RecordCopyTokensFrom(IBattleParticipant from, IBattleParticipant other, object? orderList)
{
var selfMap = GetOrSeedDeckMap(from);
var otherMap = GetOrSeedDeckMap(other);
foreach (var (idx, cardId, isSelf) in KnownListBuilder.MineCopyTokens(orderList, selfMap, otherMap))
RecordToken(isSelf == CardOwner.Self ? from : other, idx, cardId);
}
}