refactor(battlenode): close §A boolean-blindness items (MinedToken, Stock, KeyActionType)

Behavior-preserving; 231 BattleNode tests green.

- MinedToken record struct replaces the transpose-prone (int Idx, long CardId,
  CardOwner IsSelf) tuple returned by KnownListBuilder.Mine*. Positional deconstruct
  keeps the Record*From call sites unchanged.
- enum Stock { Normal, Bypass } replaces the negative `bool noStock` on
  IBattleParticipant.PushAsync and DispatchRoute, threaded through both participants,
  BattleSession, and all handler construction sites.
- enum KeyActionType mirrors the client's SendKeyActionDataManager.KeyActionType;
  the StripKeyActionForOpponent guard compares named values, KeyActionEntry.Type is
  the enum (wire-identical via JsonNumberEnumConverter).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-04 22:53:32 -04:00
parent a3e445cf2f
commit e70f32db79
30 changed files with 124 additions and 67 deletions

View File

@@ -3,6 +3,7 @@ using SVSim.BattleNode.Protocol;
namespace SVSim.BattleNode.Sessions.Dispatch;
/// <summary>One routing decision: deliver <paramref name="Frame"/> to <paramref name="Target"/>.
/// Named form of the tuple <c>ComputeFrames</c> historically returned. <paramref name="NoStock"/>
/// true for control frames (BattleFinish, ack) — bypasses playSeq assignment + archive.</summary>
internal readonly record struct DispatchRoute(IBattleParticipant Target, MsgEnvelope Frame, bool NoStock);
/// Named form of the tuple <c>ComputeFrames</c> historically returned. <paramref name="Stock"/>
/// is <see cref="Sessions.Stock.Bypass"/> for control frames (BattleFinish, ack) — bypasses
/// playSeq assignment + archive — and <see cref="Sessions.Stock.Normal"/> for gameplay frames.</summary>
internal readonly record struct DispatchRoute(IBattleParticipant Target, MsgEnvelope Frame, Stock Stock);

View File

@@ -7,7 +7,7 @@ internal sealed class ForwardWhenBothReadyHandler : IFrameHandler
public IReadOnlyList<DispatchRoute> Handle(FrameDispatchContext ctx)
{
if (ctx.BothAfterReady())
return new[] { new DispatchRoute(ctx.Other, ctx.Env, false) };
return new[] { new DispatchRoute(ctx.Other, ctx.Env, Stock.Normal) };
return Array.Empty<DispatchRoute>();
}
}

View File

@@ -12,7 +12,7 @@ internal sealed class InitBattleHandler : IFrameHandler
{
var r = new List<DispatchRoute>
{
new(ctx.From, BattleFrames.BuildAck(NetworkBattleUri.InitBattle), true),
new(ctx.From, BattleFrames.BuildAck(NetworkBattleUri.InitBattle), Stock.Bypass),
};
ctx.SenderPhase = BattleSessionPhase.AwaitingLoaded;
return r;
@@ -26,7 +26,7 @@ internal sealed class InitBattleHandler : IFrameHandler
new(ctx.From, ServerBattleFrames.BuildMatched(
ctx.From.Context, ctx.Other.Context, ctx.From.ViewerId, ctx.Other.ViewerId,
ctx.BattleId, BattleSeeds.Stable(ctx.State.MasterSeed),
ctx.State.GetShuffledDeck(ctx.From)), false),
ctx.State.GetShuffledDeck(ctx.From)), Stock.Normal),
};
ctx.SenderPhase = BattleSessionPhase.AwaitingLoaded;
return r;

View File

@@ -12,7 +12,7 @@ internal sealed class InitNetworkHandler : IFrameHandler
var routes = new List<DispatchRoute>
{
new(ctx.From, BattleFrames.BuildAck(NetworkBattleUri.InitNetwork), true),
new(ctx.From, BattleFrames.BuildAck(NetworkBattleUri.InitNetwork), Stock.Bypass),
};
ctx.SenderPhase = BattleSessionPhase.AwaitingInitBattle;
return routes;

View File

@@ -18,7 +18,7 @@ internal sealed class JudgeHandler : IFrameHandler
if (ctx.Type == BattleType.Pvp && ctx.BothAfterReady())
{
var frame = ctx.Env with { Body = new JudgeBody(Spin: BattleFrameDefaults.DeterministicTurnSpin) };
return new[] { new DispatchRoute(ctx.From, frame, false) };
return new[] { new DispatchRoute(ctx.From, frame, Stock.Normal) };
}
return Array.Empty<DispatchRoute>();

View File

@@ -22,8 +22,8 @@ internal sealed class LoadedHandler : IFrameHandler
var r = new List<DispatchRoute>
{
new(ctx.From, ServerBattleFrames.BuildBattleStart(
ctx.From.Context, ctx.Other.Context, ctx.From.ViewerId, turnState), false),
new(ctx.From, ServerBattleFrames.BuildDeal(), false),
ctx.From.Context, ctx.Other.Context, ctx.From.ViewerId, turnState), Stock.Normal),
new(ctx.From, ServerBattleFrames.BuildDeal(), Stock.Normal),
};
ctx.SenderPhase = BattleSessionPhase.AwaitingSwap;
return r;

View File

@@ -59,6 +59,6 @@ internal sealed class PlayActionsHandler : IFrameHandler
KeyAction: KnownListBuilder.StripKeyActionForOpponent(keyAction));
var frame = ctx.Env with { Body = body };
return new[] { new DispatchRoute(ctx.Other, frame, false) };
return new[] { new DispatchRoute(ctx.Other, frame, Stock.Normal) };
}
}

View File

@@ -9,8 +9,8 @@ internal sealed class RetireKillHandler : IFrameHandler
ctx.State.SessionPhase = BattleSessionPhase.Terminal;
return new[]
{
new DispatchRoute(ctx.From, BattleFrames.BuildBattleFinish(BattleResult.RetireLose), true),
new DispatchRoute(ctx.Other, BattleFrames.BuildBattleFinish(BattleResult.RetireWin), true),
new DispatchRoute(ctx.From, BattleFrames.BuildBattleFinish(BattleResult.RetireLose), Stock.Bypass),
new DispatchRoute(ctx.Other, BattleFrames.BuildBattleFinish(BattleResult.RetireWin), Stock.Bypass),
};
}
}

View File

@@ -15,7 +15,7 @@ internal sealed class SwapHandler : IFrameHandler
var hand = ServerBattleFrames.ComputeHandAfterSwap(BattleFrames.ExtractIdxList(ctx.Env));
// SwapResponse is always immediate — completes the sender's own mulligan UI.
routes.Add(new DispatchRoute(ctx.From, ServerBattleFrames.BuildSwapResponse(hand), false));
routes.Add(new DispatchRoute(ctx.From, ServerBattleFrames.BuildSwapResponse(hand), Stock.Normal));
ctx.State.PostSwapHands[ctx.From] = hand;
ctx.SenderPhase = BattleSessionPhase.AfterReady;
@@ -32,7 +32,7 @@ internal sealed class SwapHandler : IFrameHandler
&& ctx.State.PostSwapHands.TryGetValue(opponent, out var oppoHand)
? ServerBattleFrames.BuildReady(ctx.State.PostSwapHands[p], oppoHand, idxSeed)
: ServerBattleFrames.BuildReady(ctx.State.PostSwapHands[p], idxSeed);
routes.Add(new DispatchRoute(p, ready, false));
routes.Add(new DispatchRoute(p, ready, Stock.Normal));
}
}
return routes;

View File

@@ -12,7 +12,7 @@ internal sealed class TurnEndActionsHandler : IFrameHandler
if (ctx.Type == BattleType.Pvp && ctx.BothAfterReady())
{
var frame = ctx.Env with { Body = new RawBody(new Dictionary<string, object?>()) };
return new[] { new DispatchRoute(ctx.Other, frame, false) };
return new[] { new DispatchRoute(ctx.Other, frame, Stock.Normal) };
}
return Array.Empty<DispatchRoute>();
}

View File

@@ -8,7 +8,7 @@ internal sealed class TurnEndFinalHandler : IFrameHandler
{
// case 4: Bot — Judge to sender only.
if (ctx.Type == BattleType.Bot && ctx.SenderPhase == BattleSessionPhase.AfterReady)
return new[] { new DispatchRoute(ctx.From, BattleFrames.BuildJudgeBroadcast(), false) };
return new[] { new DispatchRoute(ctx.From, BattleFrames.BuildJudgeBroadcast(), Stock.Normal) };
// case 9: general — forward the envelope to other + paired BattleFinish + Terminal.
if (ctx.SenderPhase == BattleSessionPhase.AfterReady)
@@ -16,9 +16,9 @@ internal sealed class TurnEndFinalHandler : IFrameHandler
ctx.State.SessionPhase = BattleSessionPhase.Terminal;
return new[]
{
new DispatchRoute(ctx.Other, ctx.Env, false),
new DispatchRoute(ctx.From, BattleFrames.BuildBattleFinish(BattleResult.LifeWin), true),
new DispatchRoute(ctx.Other, BattleFrames.BuildBattleFinish(BattleResult.LifeLose), true),
new DispatchRoute(ctx.Other, ctx.Env, Stock.Normal),
new DispatchRoute(ctx.From, BattleFrames.BuildBattleFinish(BattleResult.LifeWin), Stock.Bypass),
new DispatchRoute(ctx.Other, BattleFrames.BuildBattleFinish(BattleResult.LifeLose), Stock.Bypass),
};
}

View File

@@ -9,7 +9,7 @@ internal sealed class TurnEndHandler : IFrameHandler
{
// case 4: Bot — Judge to sender only (no real opponent; client flips back to its local AI).
if (ctx.Type == BattleType.Bot && ctx.SenderPhase == BattleSessionPhase.AfterReady)
return new[] { new DispatchRoute(ctx.From, BattleFrames.BuildJudgeBroadcast(), false) };
return new[] { new DispatchRoute(ctx.From, BattleFrames.BuildJudgeBroadcast(), Stock.Normal) };
// case 8: general AfterReady arm — PvP forwards a {turnState} TurnEnd to the opponent
// (handover gate). Any non-Pvp non-Bot type that reaches AfterReady consumes the frame.
@@ -21,7 +21,7 @@ internal sealed class TurnEndHandler : IFrameHandler
// the opponent (the turn taker-over) then sends a Judge, which JudgeHandler reflects
// back to it to start its turn. battleCode/actionSeq/cemetery are dropped.
var te = ctx.Env with { Body = new TurnEndBody(TurnState: TurnState.First) };
return new[] { new DispatchRoute(ctx.Other, te, false) };
return new[] { new DispatchRoute(ctx.Other, te, Stock.Normal) };
}
return Array.Empty<DispatchRoute>(); // Pvp-not-both-ready → drop (Bot already returned above)
}

View File

@@ -13,7 +13,7 @@ internal sealed class TurnStartHandler : IFrameHandler
if (ctx.Type == BattleType.Pvp && ctx.BothAfterReady())
{
var frame = ctx.Env with { Body = new OpponentTurnStartBody(Spin: BattleFrameDefaults.DeterministicTurnSpin) };
return new[] { new DispatchRoute(ctx.Other, frame, false) };
return new[] { new DispatchRoute(ctx.Other, frame, Stock.Normal) };
}
return Array.Empty<DispatchRoute>();

View File

@@ -55,7 +55,7 @@ internal static class KnownListBuilder
/// <c>idx</c>-is-list guards. This is the only place a freshly-generated card's identity exists on
/// the wire (bullet-3 audit F1; producing code <c>RegisterToken</c>/<c>RegisterActionBase</c>) —
/// the played-card op itself never carries a <c>cardId</c>.</summary>
public static IEnumerable<(int Idx, long CardId, CardOwner IsSelf)> MineAddOps(object? orderList)
public static IEnumerable<MinedToken> MineAddOps(object? orderList)
{
if (orderList is not IEnumerable<object?> ops) yield break;
foreach (var op in ops)
@@ -72,7 +72,7 @@ internal static class KnownListBuilder
if (!add.TryGetValue("idx", out var idxRaw) || idxRaw is not IEnumerable<object?> idxList) continue;
foreach (var i in idxList)
yield return ((int)AsLong(i), cardId, isSelf);
yield return new MinedToken((int)AsLong(i), cardId, isSelf);
}
}
@@ -87,7 +87,7 @@ internal static class KnownListBuilder
/// only gates the strip (<see cref="StripKeyActionForOpponent"/>), not the recording. An add whose
/// candidates contain none of the picks is skipped (defensive — no record, no desync); Echo (no
/// keyAction) yields nothing, leaving it mining-only via <see cref="MineAddOps"/>.</summary>
public static IEnumerable<(int Idx, long CardId, CardOwner IsSelf)> MineChoicePicks(object? orderList, object? keyAction)
public static IEnumerable<MinedToken> MineChoicePicks(object? orderList, object? keyAction)
{
if (orderList is not IEnumerable<object?> ops) yield break;
@@ -127,7 +127,7 @@ internal static class KnownListBuilder
if (!add.TryGetValue("idx", out var idxRaw) || idxRaw is not IEnumerable<object?> idxList) continue;
foreach (var i in idxList)
yield return ((int)AsLong(i), chosen.Value, isSelf);
yield return new MinedToken((int)AsLong(i), chosen.Value, isSelf);
}
}
@@ -144,7 +144,7 @@ internal static class KnownListBuilder
/// <c>candidates</c> (→ MineChoicePicks), a <c>string</c> <c>baseIdx</c> (private-group copy,
/// <c>RegisterCopyToken.cs:19-22</c>), and a <c>baseIdx</c> absent from the chosen map (unknown source
/// → degrade, no desync). <c>isPremium</c> (IsFoil) is cosmetic and ignored.</summary>
public static IEnumerable<(int Idx, long CardId, CardOwner IsSelf)> MineCopyTokens(
public static IEnumerable<MinedToken> MineCopyTokens(
object? orderList,
IReadOnlyDictionary<int, long> selfMap,
IReadOnlyDictionary<int, long> otherMap)
@@ -168,7 +168,7 @@ internal static class KnownListBuilder
if (!add.TryGetValue("idx", out var idxRaw) || idxRaw is not IEnumerable<object?> idxList) continue;
foreach (var i in idxList)
yield return ((int)AsLong(i), cardId, isSelf);
yield return new MinedToken((int)AsLong(i), cardId, isSelf);
}
}
@@ -186,8 +186,8 @@ internal static class KnownListBuilder
{
if (e is not IDictionary<string, object?> d) continue;
d.TryGetValue("type", out var typeRaw);
var type = (int)AsLong(typeRaw);
if (type is not (1 or 5)) continue; // only Choice / HaveBeforeSkillChoice handled
var type = (KeyActionType)(int)AsLong(typeRaw);
if (type is not (KeyActionType.Choice or KeyActionType.HaveBeforeSkillChoice)) continue;
d.TryGetValue("cardId", out var cardIdRaw);
var cardId = AsLong(cardIdRaw);

View File

@@ -0,0 +1,15 @@
using SVSim.BattleNode.Protocol;
namespace SVSim.BattleNode.Sessions.Dispatch;
/// <summary>One generated-token identity mined from a sender's <c>orderList</c> <c>add</c> op:
/// the token's <paramref name="Idx"/> in a side's index space, its resolved
/// <paramref name="CardId"/>, and <paramref name="IsSelf"/> — whose map it belongs to (the
/// sender's own token vs a cross-side gift living in the opponent's index space; routed by
/// <see cref="BattleSessionState.RecordTokensFrom"/>). Replaces the transpose-prone
/// <c>(int Idx, long CardId, CardOwner IsSelf)</c> tuple the <c>Mine*</c> methods returned:
/// <c>Idx</c> and <c>CardId</c> are both numeric, so <c>(cardId, idx, …)</c> silently compiled
/// and corrupted the reveal map. As a positional record struct it keeps the named members and
/// positional deconstruct (call sites stay <c>foreach (var (idx, cardId, isSelf) in …)</c>)
/// while the compiler rejects a transposed construction.</summary>
internal readonly record struct MinedToken(int Idx, long CardId, CardOwner IsSelf);