Files
SVSimServer/SVSim.BattleNode/Sessions/Dispatch/Handlers/PlayActionsHandler.cs
2026-06-04 15:13:47 -04:00

77 lines
4.6 KiB
C#

using SVSim.BattleNode.Protocol;
using SVSim.BattleNode.Protocol.Bodies;
namespace SVSim.BattleNode.Sessions.Dispatch.Handlers;
/// <summary>PvP PlayActions translator. Synthesizes the opponent-facing knownList from the sender's
/// idx->cardId map + the orderList move op, renames targetList -> oppoTargetList, drops orderList,
/// and forwards a stripped keyAction for choice/Discover plays ({type,cardId}; selectCard dropped
/// for a hidden open:0 pick). Token plays resolve their cardId from add ops (concrete tokens),
/// keyAction.selectCard (choice picks), or a baseIdx copy resolved against the side's map — all mined
/// on earlier (or the same) frames; an un-generated token idx still degrades to {playIdx,type}
/// (no knownList). Bot drop (no rule).</summary>
internal sealed class PlayActionsHandler : IFrameHandler
{
public IReadOnlyList<DispatchRoute> Handle(FrameDispatchContext ctx)
{
if (ctx.Type != BattleType.Pvp || !ctx.BothAfterReady())
return Array.Empty<DispatchRoute>();
var entries = (ctx.Env.Body as RawBody)?.Entries ?? new Dictionary<string, object?>();
var playIdx = (int)KnownListBuilder.AsLong(entries.GetValueOrDefault("playIdx"));
var type = (int)KnownListBuilder.AsLong(entries.GetValueOrDefault("type"));
var orderList = entries.GetValueOrDefault("orderList");
var keyAction = entries.GetValueOrDefault("keyAction");
// Mine generated-token identities from this frame's add ops into the right side's idx->cardId
// map (isSelf:1 → sender; isSelf:0 → opponent, a cross-side gift), so a token played in a LATER
// frame resolves its cardId — by whichever side ends up playing it (bullet-3 audit F1).
ctx.State.RecordTokensFrom(ctx.From, ctx.Other, orderList);
// Choice/Discover-into-hand: the chosen cardId rides keyAction.selectCard (the orderList's
// choiceAdd carries candidates only). Record idx->chosenCardId now so the later play reveals it.
ctx.State.RecordChoicePicksFrom(ctx.From, ctx.Other, orderList, keyAction);
// Copy/clone tokens: card:{baseIdx} points at a card in the actor's own index space; resolve it
// against that side's map and record copyIdx->cardId so the later play reveals it. Ordered after
// the plain/choice mining so a same-frame copy of a just-added token resolves against the live map.
ctx.State.RecordCopyTokensFrom(ctx.From, ctx.Other, orderList);
var deckMap = ctx.State.GetOrSeedDeckMap(ctx.From);
var played = KnownListBuilder.BuildPlayedCard(deckMap, playIdx, orderList);
var oppoTargets = KnownListBuilder.RenameTargets(entries.GetValueOrDefault("targetList"));
// Deck-sourced movements (fetch / search / summon-from-deck) ride the uList — a verbatim,
// separate receive slot the node forwards unchanged (bullet-3 audit F1). The node makes no
// reveal decision; cardId presence is the sender's call. Coexists with the synthesized
// knownList in the same frame (capture line 75).
var uList = KnownListBuilder.RelayUList(entries.GetValueOrDefault("uList"));
// Hidden shared-RNG draws (a random deck→hand fetch the opponent can't reproduce) advance the
// sender's shared _stableRandom; the receiver doesn't re-run them, so it needs a spin crank to
// stay aligned for later visible randoms. Per-frame delivery (real-spin design §3.2): the count
// rides THIS PlayActions. A fetch we relay with an identity (uList cardId present) is already
// known to the opponent — exclude its idxs so it isn't counted. 0 → null (omitted; client
// assumes spin:0).
var revealed = uList is null
? (IReadOnlySet<int>)new HashSet<int>()
: uList.Where(u => u.CardId is not null).SelectMany(u => u.IdxList).ToHashSet();
var spin = KnownListBuilder.CountHiddenDraws(orderList, playIdx, revealed);
var body = new PlayActionsBroadcastBody(
PlayIdx: playIdx,
Type: type,
KnownList: played is null ? null : new[] { played },
OppoTargetList: oppoTargets,
UList: uList,
// {type,cardId} forwarded so the opponent renders the choice token; selectCard dropped
// when open==0 (hidden draw-to-hand pick). Null for a vanilla play (no keyAction).
KeyAction: KnownListBuilder.StripKeyActionForOpponent(keyAction),
Spin: spin > 0 ? spin : null);
var frame = ctx.Env with { Body = body };
return new[] { new DispatchRoute(ctx.Other, frame, false) };
}
}