From 503c38264614fc0c625ecdb8aca1bad09e7ca056 Mon Sep 17 00:00:00 2001 From: gamer147 Date: Wed, 3 Jun 2026 14:33:26 -0400 Subject: [PATCH] refactor(battle-node): extract ForwardWhenBothReadyHandler; share handler instances via BuildHandlers --- SVSim.BattleNode/Sessions/BattleSession.cs | 18 ++++++++++++++---- .../Handlers/ForwardWhenBothReadyHandler.cs | 13 +++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 SVSim.BattleNode/Sessions/Dispatch/Handlers/ForwardWhenBothReadyHandler.cs diff --git a/SVSim.BattleNode/Sessions/BattleSession.cs b/SVSim.BattleNode/Sessions/BattleSession.cs index 374714e..c8630fc 100644 --- a/SVSim.BattleNode/Sessions/BattleSession.cs +++ b/SVSim.BattleNode/Sessions/BattleSession.cs @@ -33,8 +33,13 @@ public sealed class BattleSession // Per-URI handlers consulted before the legacy switch. Populated incrementally (Tasks 5-14); // each registered URI is served by its handler and its legacy switch arm goes dead. - private static readonly IReadOnlyDictionary Handlers = - new Dictionary + private static readonly IReadOnlyDictionary Handlers = BuildHandlers(); + + private static IReadOnlyDictionary BuildHandlers() + { + var retireKill = new RetireKillHandler(); + var forwardWhenReady = new ForwardWhenBothReadyHandler(); + return new Dictionary { [NetworkBattleUri.InitNetwork] = new InitNetworkHandler(), [NetworkBattleUri.InitBattle] = new InitBattleHandler(), @@ -42,11 +47,16 @@ public sealed class BattleSession [NetworkBattleUri.Swap] = new SwapHandler(), [NetworkBattleUri.TurnEnd] = new TurnEndHandler(), [NetworkBattleUri.TurnEndFinal] = new TurnEndFinalHandler(), - [NetworkBattleUri.Retire] = new RetireKillHandler(), - [NetworkBattleUri.Kill] = new RetireKillHandler(), + [NetworkBattleUri.Retire] = retireKill, + [NetworkBattleUri.Kill] = retireKill, [NetworkBattleUri.TurnStart] = new TurnStartHandler(), [NetworkBattleUri.Judge] = new JudgeHandler(), + [NetworkBattleUri.PlayActions] = forwardWhenReady, + [NetworkBattleUri.Echo] = forwardWhenReady, + [NetworkBattleUri.TurnEndActions] = forwardWhenReady, + [NetworkBattleUri.JudgeResult] = forwardWhenReady, }; + } private FrameDispatchContext BuildContext(IBattleParticipant from, MsgEnvelope env) => new() diff --git a/SVSim.BattleNode/Sessions/Dispatch/Handlers/ForwardWhenBothReadyHandler.cs b/SVSim.BattleNode/Sessions/Dispatch/Handlers/ForwardWhenBothReadyHandler.cs new file mode 100644 index 0000000..8ef8f47 --- /dev/null +++ b/SVSim.BattleNode/Sessions/Dispatch/Handlers/ForwardWhenBothReadyHandler.cs @@ -0,0 +1,13 @@ +using SVSim.BattleNode.Protocol; + +namespace SVSim.BattleNode.Sessions.Dispatch.Handlers; + +internal sealed class ForwardWhenBothReadyHandler : IFrameHandler +{ + public IReadOnlyList Handle(FrameDispatchContext ctx) + { + if (ctx.BothAfterReady()) + return new[] { new DispatchRoute(ctx.Other, ctx.Env, false) }; + return Array.Empty(); + } +}