From e98bd10dbe317f30eb3fddc6c324c0e6c469f54b Mon Sep 17 00:00:00 2001 From: gamer147 Date: Wed, 3 Jun 2026 18:45:17 -0400 Subject: [PATCH] fix(battle-node): reflect PvP Judge back to its sender (turn handover) Live two-client run (data_dumps/captures/battle_test) exposed a turn-handover stall: ending a turn on client A made BOTH clients show A's turn again; the opponent never got a turn. Root cause: JudgeHandler routed the {spin:0} Judge to ctx.Other. The client rule is 'receive opponent TurnEnd -> SendJudge', so the PASSIVE player (the one taking over the turn) is the Judge sender, and 'receive Judge -> ControlTurnStartPlayer' starts the RECEIVER's turn. Routing to ctx.Other delivered the Judge to the player who had just ended their turn, restarting it in a closed loop while the taker-over sat on 'Opponent's Turn'. Fix: the PvP Judge {spin} reflects back to ctx.From (the sender / turn taker-over), matching the Bot arm's existing 'Judge to sender only' handover. The sender then emits TurnStart, which relays to the opponent as {spin}. Updated the dispatch unit test and the PvpHandshakeAndGameplay integration test to the real handover order (passive sends Judge -> receives it back -> sends TurnStart -> opponent sees it). Co-Authored-By: Claude Opus 4.8 --- .../Dispatch/Handlers/JudgeHandler.cs | 11 +++++++--- .../Dispatch/Handlers/TurnEndHandler.cs | 6 +++--- .../Integration/BattleNodeFlowTests.cs | 20 ++++++++++++------- .../Sessions/BattleSessionDispatchTests.cs | 7 +++++-- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/SVSim.BattleNode/Sessions/Dispatch/Handlers/JudgeHandler.cs b/SVSim.BattleNode/Sessions/Dispatch/Handlers/JudgeHandler.cs index 0e2b973..563d96b 100644 --- a/SVSim.BattleNode/Sessions/Dispatch/Handlers/JudgeHandler.cs +++ b/SVSim.BattleNode/Sessions/Dispatch/Handlers/JudgeHandler.cs @@ -11,12 +11,17 @@ internal sealed class JudgeHandler : IFrameHandler if (ctx.IsScriptedBot(ctx.From)) return new[] { new DispatchRoute(ctx.Other, ctx.Env, false) }; - // PvP: active player's Judge{battleCode} -> opponent {spin} (RNG catch-up; spin=0 for the - // deterministic-turn slice). Receiving Judge starts the opponent's own turn. + // PvP: Judge is the handover gate. The player who sends Judge is the one TAKING OVER the + // turn (the client rule is: receive opponent TurnEnd -> SendJudge). Receiving Judge{spin} + // fires ControlTurnStartPlayer ("start MY turn"), so the {spin} must REFLECT BACK to the + // sender — NOT go to the opponent (that would make the player who just ended their turn + // 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.BothAfterReady()) { var frame = ctx.Env with { Body = new JudgeBody(Spin: 0) }; - return new[] { new DispatchRoute(ctx.Other, frame, false) }; + return new[] { new DispatchRoute(ctx.From, frame, false) }; } return Array.Empty(); diff --git a/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndHandler.cs b/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndHandler.cs index 27c18af..69fb6f0 100644 --- a/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndHandler.cs +++ b/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndHandler.cs @@ -17,9 +17,9 @@ internal sealed class TurnEndHandler : IFrameHandler { if (ctx.Type == BattleType.Pvp && ctx.BothAfterReady()) { - // Opponent sees {turnState}; receiving TurnEnd drives its SendJudge (handover gate). - // battleCode/actionSeq/cemetery are dropped. The paired Judge{spin} is emitted by - // JudgeHandler when the active player sends its own Judge frame. + // Opponent sees {turnState}; receiving TurnEnd drives ITS SendJudge (handover gate): + // 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: 0) }; return new[] { new DispatchRoute(ctx.Other, te, false) }; } diff --git a/SVSim.UnitTests/BattleNode/Integration/BattleNodeFlowTests.cs b/SVSim.UnitTests/BattleNode/Integration/BattleNodeFlowTests.cs index 83d9c40..452af9e 100644 --- a/SVSim.UnitTests/BattleNode/Integration/BattleNodeFlowTests.cs +++ b/SVSim.UnitTests/BattleNode/Integration/BattleNodeFlowTests.cs @@ -220,22 +220,28 @@ public class BattleNodeFlowTests await DrivePvpHandshakeAsync(clientA, vidA, clientB, vidB, key, ct); - // Both are now AfterReady. Deterministic-turn translator contract (per-URI, opponent- - // facing): the active player ends its turn by sending TurnEnd then Judge. The OPPONENT - // (B) receives the translated {turnState:0} TurnEnd and {spin:0} Judge; the sender (A) - // receives nothing back — it already ran the action locally. (Old behavior broadcast - // TurnEnd+Judge to both sides; that 4-route relay was replaced by the translator.) + // Both are now AfterReady. Deterministic-turn handover, mirroring the real two-client + // capture (2026-06-03 battle_test). A ends its turn; the OPPONENT (B) receives the + // translated {turnState:0} TurnEnd. A receives nothing — it already ran the turn locally. await clientA.SendMsgAsync(MakeEnvelopeWith(vidA, NetworkBattleUri.TurnEnd, pubSeq: 5), key, ct); var bTurnEnd = await clientB.ReceiveSynchronizeAsync(ct); Assert.That(bTurnEnd.Uri, Is.EqualTo(NetworkBattleUri.TurnEnd)); - await clientA.SendMsgAsync(MakeEnvelopeWith(vidA, NetworkBattleUri.Judge, pubSeq: 6), key, ct); + // The client rule is: receive opponent TurnEnd -> SendJudge. So B (the taker-over) sends + // Judge. The {spin:0} reflects BACK to B (its own ControlTurnStartPlayer gate), NOT to A — + // routing it to A would restart A's turn and stall the loop (the live-run bug this fixes). + await clientB.SendMsgAsync(MakeEnvelopeWith(vidB, NetworkBattleUri.Judge, pubSeq: 5), key, ct); var bJudge = await clientB.ReceiveSynchronizeAsync(ct); Assert.That(bJudge.Uri, Is.EqualTo(NetworkBattleUri.Judge)); + // B opens its turn: TurnStart relays to the opponent A as {spin:0} ("opponent's turn"). + await clientB.SendMsgAsync(MakeEnvelopeWith(vidB, NetworkBattleUri.TurnStart, pubSeq: 6), key, ct); + var aTurnStart = await clientA.ReceiveSynchronizeAsync(ct); + Assert.That(aTurnStart.Uri, Is.EqualTo(NetworkBattleUri.TurnStart)); + // PlayActions translation: B plays a card; A receives the opponent-facing PlayActions // frame (Uri preserved, body synthesized by PlayActionsHandler). - await clientB.SendMsgAsync(MakeEnvelopeWith(vidB, NetworkBattleUri.PlayActions, pubSeq: 5), key, ct); + await clientB.SendMsgAsync(MakeEnvelopeWith(vidB, NetworkBattleUri.PlayActions, pubSeq: 7), key, ct); var aForwarded = await clientA.ReceiveSynchronizeAsync(ct); Assert.That(aForwarded.Uri, Is.EqualTo(NetworkBattleUri.PlayActions)); } diff --git a/SVSim.UnitTests/BattleNode/Sessions/BattleSessionDispatchTests.cs b/SVSim.UnitTests/BattleNode/Sessions/BattleSessionDispatchTests.cs index 0e449e4..9faf1c6 100644 --- a/SVSim.UnitTests/BattleNode/Sessions/BattleSessionDispatchTests.cs +++ b/SVSim.UnitTests/BattleNode/Sessions/BattleSessionDispatchTests.cs @@ -400,7 +400,7 @@ public class BattleSessionDispatchTests } [Test] - public void Pvp_Judge_from_A_emits_spin0_to_B() + public void Pvp_Judge_from_A_reflects_spin0_back_to_sender() { var (s, a, b) = NewPvpSession(); DriveToAfterReady(s, a); @@ -408,8 +408,11 @@ public class BattleSessionDispatchTests var routes = s.ComputeFrames(a, NewEnvelope(NetworkBattleUri.Judge)); + // Judge reflects BACK to its sender (the turn taker-over), not to the opponent: receiving + // Judge{spin} fires the sender's ControlTurnStartPlayer. Routing to the opponent would + // restart the just-ended player's turn (2026-06-03 two-client capture). Assert.That(routes.Count, Is.EqualTo(1)); - Assert.That(routes[0].Target, Is.SameAs(b)); + Assert.That(routes[0].Target, Is.SameAs(a)); Assert.That(routes[0].Frame.Uri, Is.EqualTo(NetworkBattleUri.Judge)); var body = (SVSim.BattleNode.Protocol.Bodies.JudgeBody)routes[0].Frame.Body; Assert.That(body.Spin, Is.EqualTo(0));