From 2d32051cc04886182449e6671aeec9dfcb32d33d Mon Sep 17 00:00:00 2001 From: gamer147 Date: Fri, 5 Jun 2026 08:20:56 -0400 Subject: [PATCH] refactor(battlenode): key dispatch on OpponentIsAckOnly, drop per-frame BattleType switch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Behavior-identical; 231 BattleNode tests green with ZERO test changes. The 10 handler arms no longer switch on BattleType: - 4 Bot arms gate on the new FrameDispatchContext.OpponentIsAckOnly (Other is not IHasHandshakePhase) — the participant property the audit asked for. - 6 relay arms drop the Type == Pvp guard; it was redundant with BothSidesAfterReady() (only a two-real-player session has both handshake phases). Its doc now records that. - FrameDispatchContext.Type removed (+ the Type = Type in BuildContext). BattleSession.Type stays for the session-level drop cascade. Zero test churn because the stubs already encode the split: FakeRealParticipant/ProbeParticipant implement IHasHandshakePhase, the bot stub FakeParticipant doesn't, and NewBotSession uses it as the opponent. Co-Authored-By: Claude Opus 4.8 --- SVSim.BattleNode/Sessions/BattleSession.cs | 2 +- .../Sessions/Dispatch/FrameDispatchContext.cs | 14 ++++++++++++-- .../Sessions/Dispatch/Handlers/EchoHandler.cs | 2 +- .../Dispatch/Handlers/InitBattleHandler.cs | 2 +- .../Sessions/Dispatch/Handlers/JudgeHandler.cs | 2 +- .../Sessions/Dispatch/Handlers/LoadedHandler.cs | 2 +- .../Dispatch/Handlers/PlayActionsHandler.cs | 2 +- .../Dispatch/Handlers/TurnEndActionsHandler.cs | 2 +- .../Dispatch/Handlers/TurnEndFinalHandler.cs | 2 +- .../Sessions/Dispatch/Handlers/TurnEndHandler.cs | 4 ++-- .../Sessions/Dispatch/Handlers/TurnStartHandler.cs | 2 +- 11 files changed, 23 insertions(+), 13 deletions(-) diff --git a/SVSim.BattleNode/Sessions/BattleSession.cs b/SVSim.BattleNode/Sessions/BattleSession.cs index cd2853c..b70d967 100644 --- a/SVSim.BattleNode/Sessions/BattleSession.cs +++ b/SVSim.BattleNode/Sessions/BattleSession.cs @@ -71,7 +71,7 @@ public sealed class BattleSession new() { A = A, B = B, From = from, Other = ReferenceEquals(from, A) ? B : A, - Env = env, Type = Type, BattleId = BattleId, State = _state, + Env = env, BattleId = BattleId, State = _state, }; public BattleSession(string battleId, BattleType type, IBattleParticipant a, IBattleParticipant b, diff --git a/SVSim.BattleNode/Sessions/Dispatch/FrameDispatchContext.cs b/SVSim.BattleNode/Sessions/Dispatch/FrameDispatchContext.cs index 8fe1a59..3cce6e4 100644 --- a/SVSim.BattleNode/Sessions/Dispatch/FrameDispatchContext.cs +++ b/SVSim.BattleNode/Sessions/Dispatch/FrameDispatchContext.cs @@ -14,10 +14,17 @@ internal sealed class FrameDispatchContext internal required IBattleParticipant From { get; init; } internal required IBattleParticipant Other { get; init; } internal required MsgEnvelope Env { get; init; } - internal required BattleType Type { get; init; } internal required string BattleId { get; init; } internal required BattleSessionState State { get; init; } + /// The opponent is an AI-passive (ack-only) bot: it runs no handshake — no + /// — and receives no relayed frames (the client drives its own + /// AI; the server only acks). This is the participant property that replaces the per-handler + /// BattleType.Bot switch: the Bot dispatch arms gate on it. Its inverse — a live relay + /// peer — is what already implies (only real peers have a + /// handshake phase), so the relay arms need no separate opponent check. + internal bool OpponentIsAckOnly => Other is not IHasHandshakePhase; + /// The dispatching participant's handshake phase (null for a non-IHasHandshakePhase /// participant, e.g. NoOpBot). Setting it advances the sender. internal HandshakePhase? SenderPhase @@ -34,7 +41,10 @@ internal sealed class FrameDispatchContext /// BOTH participants have finished the handshake. Reads A/B (not From/Other) so the /// result is identical regardless of which side sent the frame. Contrast - /// (sender only). + /// (sender only). Only a live relay peer (real player) has a + /// handshake phase, so this can only be true in a two-real-player (PvP) session — the relay + /// dispatch arms gate on this instead of a BattleType check (an ack-only bot opponent, + /// , can never satisfy it). internal bool BothSidesAfterReady() => (A as IHasHandshakePhase)?.Phase == HandshakePhase.AfterReady && (B as IHasHandshakePhase)?.Phase == HandshakePhase.AfterReady; diff --git a/SVSim.BattleNode/Sessions/Dispatch/Handlers/EchoHandler.cs b/SVSim.BattleNode/Sessions/Dispatch/Handlers/EchoHandler.cs index 4aa4da3..0f4e015 100644 --- a/SVSim.BattleNode/Sessions/Dispatch/Handlers/EchoHandler.cs +++ b/SVSim.BattleNode/Sessions/Dispatch/Handlers/EchoHandler.cs @@ -13,7 +13,7 @@ internal sealed class EchoHandler : IFrameHandler { public IReadOnlyList Handle(FrameDispatchContext ctx) { - if (ctx.Type == BattleType.Pvp && ctx.BothSidesAfterReady()) + if (ctx.BothSidesAfterReady()) { var orderList = (ctx.Env.Body as RawBody)?.Entries.GetValueOrDefault(WireKeys.OrderList); ctx.State.RecordTokensFrom(ctx.From, ctx.Other, orderList); diff --git a/SVSim.BattleNode/Sessions/Dispatch/Handlers/InitBattleHandler.cs b/SVSim.BattleNode/Sessions/Dispatch/Handlers/InitBattleHandler.cs index 211abba..66144c1 100644 --- a/SVSim.BattleNode/Sessions/Dispatch/Handlers/InitBattleHandler.cs +++ b/SVSim.BattleNode/Sessions/Dispatch/Handlers/InitBattleHandler.cs @@ -8,7 +8,7 @@ internal sealed class InitBattleHandler : IFrameHandler public IReadOnlyList Handle(FrameDispatchContext ctx) { // case 2: Bot — ack only, NO Matched (Matched would corrupt client opponent info). - if (ctx.Type == BattleType.Bot && ctx.SenderPhase == HandshakePhase.AwaitingInitBattle) + if (ctx.OpponentIsAckOnly && ctx.SenderPhase == HandshakePhase.AwaitingInitBattle) { var r = new List { diff --git a/SVSim.BattleNode/Sessions/Dispatch/Handlers/JudgeHandler.cs b/SVSim.BattleNode/Sessions/Dispatch/Handlers/JudgeHandler.cs index b8a92a1..87019c4 100644 --- a/SVSim.BattleNode/Sessions/Dispatch/Handlers/JudgeHandler.cs +++ b/SVSim.BattleNode/Sessions/Dispatch/Handlers/JudgeHandler.cs @@ -15,7 +15,7 @@ internal sealed class JudgeHandler : IFrameHandler // start another one, stalling the loop; confirmed by the 2026-06-03 two-client capture). // The sender then emits TurnStart, which TurnStartHandler relays to the opponent as {spin}. // battleCode is dropped; spin=0 for the deterministic-turn slice. - if (ctx.Type == BattleType.Pvp && ctx.BothSidesAfterReady()) + if (ctx.BothSidesAfterReady()) { var frame = ctx.Env with { Body = new JudgeBody(Spin: BattleFrameDefaults.DeterministicTurnSpin) }; return new[] { new DispatchRoute(ctx.From, frame, Stock.Normal) }; diff --git a/SVSim.BattleNode/Sessions/Dispatch/Handlers/LoadedHandler.cs b/SVSim.BattleNode/Sessions/Dispatch/Handlers/LoadedHandler.cs index 31c114e..3c31893 100644 --- a/SVSim.BattleNode/Sessions/Dispatch/Handlers/LoadedHandler.cs +++ b/SVSim.BattleNode/Sessions/Dispatch/Handlers/LoadedHandler.cs @@ -8,7 +8,7 @@ internal sealed class LoadedHandler : IFrameHandler public IReadOnlyList Handle(FrameDispatchContext ctx) { // case 3: Bot — silent (client populates opponent state from AIBattleStart HTTP data). - if (ctx.Type == BattleType.Bot && ctx.SenderPhase == HandshakePhase.AwaitingLoaded) + if (ctx.OpponentIsAckOnly && ctx.SenderPhase == HandshakePhase.AwaitingLoaded) { ctx.SenderPhase = HandshakePhase.AwaitingSwap; return Array.Empty(); diff --git a/SVSim.BattleNode/Sessions/Dispatch/Handlers/PlayActionsHandler.cs b/SVSim.BattleNode/Sessions/Dispatch/Handlers/PlayActionsHandler.cs index 58e6e3c..3a5eeaf 100644 --- a/SVSim.BattleNode/Sessions/Dispatch/Handlers/PlayActionsHandler.cs +++ b/SVSim.BattleNode/Sessions/Dispatch/Handlers/PlayActionsHandler.cs @@ -14,7 +14,7 @@ internal sealed class PlayActionsHandler : IFrameHandler { public IReadOnlyList Handle(FrameDispatchContext ctx) { - if (ctx.Type != BattleType.Pvp || !ctx.BothSidesAfterReady()) + if (!ctx.BothSidesAfterReady()) return Array.Empty(); var entries = (ctx.Env.Body as RawBody)?.Entries ?? new Dictionary(); diff --git a/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndActionsHandler.cs b/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndActionsHandler.cs index f685299..dd5ce8d 100644 --- a/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndActionsHandler.cs +++ b/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndActionsHandler.cs @@ -9,7 +9,7 @@ internal sealed class TurnEndActionsHandler : IFrameHandler { public IReadOnlyList Handle(FrameDispatchContext ctx) { - if (ctx.Type == BattleType.Pvp && ctx.BothSidesAfterReady()) + if (ctx.BothSidesAfterReady()) { var frame = ctx.Env with { Body = new RawBody(new Dictionary()) }; return new[] { new DispatchRoute(ctx.Other, frame, Stock.Normal) }; diff --git a/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndFinalHandler.cs b/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndFinalHandler.cs index aadaccd..900da56 100644 --- a/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndFinalHandler.cs +++ b/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndFinalHandler.cs @@ -7,7 +7,7 @@ internal sealed class TurnEndFinalHandler : IFrameHandler public IReadOnlyList Handle(FrameDispatchContext ctx) { // case 4: Bot — Judge to sender only. - if (ctx.Type == BattleType.Bot && ctx.SenderIsAfterReady) + if (ctx.OpponentIsAckOnly && ctx.SenderIsAfterReady) return new[] { new DispatchRoute(ctx.From, BattleFrames.BuildJudgeBroadcast(), Stock.Normal) }; // case 9: general — forward the envelope to other + paired BattleFinish + Terminal. diff --git a/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndHandler.cs b/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndHandler.cs index e33bc11..24d4006 100644 --- a/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndHandler.cs +++ b/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndHandler.cs @@ -8,14 +8,14 @@ internal sealed class TurnEndHandler : IFrameHandler public IReadOnlyList Handle(FrameDispatchContext ctx) { // case 4: Bot — Judge to sender only (no real opponent; client flips back to its local AI). - if (ctx.Type == BattleType.Bot && ctx.SenderIsAfterReady) + if (ctx.OpponentIsAckOnly && ctx.SenderIsAfterReady) 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. if (ctx.SenderIsAfterReady) { - if (ctx.Type == BattleType.Pvp && ctx.BothSidesAfterReady()) + if (ctx.BothSidesAfterReady()) { // Opponent sees {turnState}; receiving TurnEnd drives ITS SendJudge (handover gate): // the opponent (the turn taker-over) then sends a Judge, which JudgeHandler reflects diff --git a/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnStartHandler.cs b/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnStartHandler.cs index ef009d3..74809bc 100644 --- a/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnStartHandler.cs +++ b/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnStartHandler.cs @@ -10,7 +10,7 @@ internal sealed class TurnStartHandler : IFrameHandler { // PvP: the active player's TurnStart{orderList} is dropped; the opponent receives {spin} // (spin=0 for the deterministic-turn slice) and self-generates its turn-open. - if (ctx.Type == BattleType.Pvp && ctx.BothSidesAfterReady()) + if (ctx.BothSidesAfterReady()) { var frame = ctx.Env with { Body = new OpponentTurnStartBody(Spin: BattleFrameDefaults.DeterministicTurnSpin) }; return new[] { new DispatchRoute(ctx.Other, frame, Stock.Normal) };