From d35818360f6879a0f3c97e5f1353bd43b2b130df Mon Sep 17 00:00:00 2001 From: gamer147 Date: Wed, 3 Jun 2026 14:21:54 -0400 Subject: [PATCH] refactor(battle-node): extract TurnEndFinalHandler --- SVSim.BattleNode/Sessions/BattleSession.cs | 1 + .../Dispatch/Handlers/TurnEndFinalHandler.cs | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndFinalHandler.cs diff --git a/SVSim.BattleNode/Sessions/BattleSession.cs b/SVSim.BattleNode/Sessions/BattleSession.cs index 33e90b1..16c5932 100644 --- a/SVSim.BattleNode/Sessions/BattleSession.cs +++ b/SVSim.BattleNode/Sessions/BattleSession.cs @@ -41,6 +41,7 @@ public sealed class BattleSession [NetworkBattleUri.Loaded] = new LoadedHandler(), [NetworkBattleUri.Swap] = new SwapHandler(), [NetworkBattleUri.TurnEnd] = new TurnEndHandler(), + [NetworkBattleUri.TurnEndFinal] = new TurnEndFinalHandler(), }; private FrameDispatchContext BuildContext(IBattleParticipant from, MsgEnvelope env) => diff --git a/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndFinalHandler.cs b/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndFinalHandler.cs new file mode 100644 index 0000000..2a13d41 --- /dev/null +++ b/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndFinalHandler.cs @@ -0,0 +1,27 @@ +using SVSim.BattleNode.Protocol; + +namespace SVSim.BattleNode.Sessions.Dispatch.Handlers; + +internal sealed class TurnEndFinalHandler : IFrameHandler +{ + public IReadOnlyList Handle(FrameDispatchContext ctx) + { + // 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) }; + + // case 9: general — forward the envelope to other + paired BattleFinish + Terminal. + if (ctx.SenderPhase == BattleSessionPhase.AfterReady) + { + 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), + }; + } + + return Array.Empty(); + } +}