From e5ec8a0de1f822f77c037615130e78ae31caf0a2 Mon Sep 17 00:00:00 2001 From: gamer147 Date: Wed, 3 Jun 2026 14:07:49 -0400 Subject: [PATCH] refactor(battle-node): extract InitBattleHandler --- SVSim.BattleNode/Sessions/BattleSession.cs | 1 + .../Dispatch/Handlers/InitBattleHandler.cs | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 SVSim.BattleNode/Sessions/Dispatch/Handlers/InitBattleHandler.cs diff --git a/SVSim.BattleNode/Sessions/BattleSession.cs b/SVSim.BattleNode/Sessions/BattleSession.cs index 808437e..8af0e11 100644 --- a/SVSim.BattleNode/Sessions/BattleSession.cs +++ b/SVSim.BattleNode/Sessions/BattleSession.cs @@ -37,6 +37,7 @@ public sealed class BattleSession new Dictionary { [NetworkBattleUri.InitNetwork] = new InitNetworkHandler(), + [NetworkBattleUri.InitBattle] = new InitBattleHandler(), }; private FrameDispatchContext BuildContext(IBattleParticipant from, MsgEnvelope env) => diff --git a/SVSim.BattleNode/Sessions/Dispatch/Handlers/InitBattleHandler.cs b/SVSim.BattleNode/Sessions/Dispatch/Handlers/InitBattleHandler.cs new file mode 100644 index 0000000..44ba643 --- /dev/null +++ b/SVSim.BattleNode/Sessions/Dispatch/Handlers/InitBattleHandler.cs @@ -0,0 +1,36 @@ +using SVSim.BattleNode.Lifecycle; +using SVSim.BattleNode.Protocol; + +namespace SVSim.BattleNode.Sessions.Dispatch.Handlers; + +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 == BattleSessionPhase.AwaitingInitBattle) + { + var r = new List + { + new(ctx.From, BattleFrames.BuildAck(NetworkBattleUri.InitBattle), true), + }; + ctx.SenderPhase = BattleSessionPhase.AwaitingLoaded; + return r; + } + + // case 5: general — push Matched (per-perspective) to the sender only. + if (ctx.SenderPhase == BattleSessionPhase.AwaitingInitBattle) + { + var r = new List + { + new(ctx.From, ScriptedLifecycle.BuildMatched( + ctx.From.Context, ctx.Other.Context, ctx.From.ViewerId, ctx.Other.ViewerId, + ctx.BattleId, ScriptedProfiles.BattleSeed), false), + }; + ctx.SenderPhase = BattleSessionPhase.AwaitingLoaded; + return r; + } + + return Array.Empty(); + } +}