refactor(battle-node): extract TurnEndHandler

This commit is contained in:
gamer147
2026-06-03 14:17:20 -04:00
parent 477faf3df3
commit 538099ff4b
2 changed files with 42 additions and 0 deletions

View File

@@ -40,6 +40,7 @@ public sealed class BattleSession
[NetworkBattleUri.InitBattle] = new InitBattleHandler(),
[NetworkBattleUri.Loaded] = new LoadedHandler(),
[NetworkBattleUri.Swap] = new SwapHandler(),
[NetworkBattleUri.TurnEnd] = new TurnEndHandler(),
};
private FrameDispatchContext BuildContext(IBattleParticipant from, MsgEnvelope env) =>

View File

@@ -0,0 +1,41 @@
using SVSim.BattleNode.Protocol;
namespace SVSim.BattleNode.Sessions.Dispatch.Handlers;
internal sealed class TurnEndHandler : IFrameHandler
{
public IReadOnlyList<DispatchRoute> 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.SenderPhase == BattleSessionPhase.AfterReady)
return new[] { new DispatchRoute(ctx.From, BattleFrames.BuildJudgeBroadcast(), false) };
// case 8: general AfterReady arm — matches (and consumes) for any non-Bot type once the
// sender is AfterReady, even if it yields no routes (legacy `break;`).
if (ctx.SenderPhase == BattleSessionPhase.AfterReady)
{
if (ctx.Type == BattleType.Pvp && ctx.BothAfterReady())
{
var te = BattleFrames.BuildTurnEndBroadcast();
var jg = BattleFrames.BuildJudgeBroadcast();
return new[]
{
new DispatchRoute(ctx.From, te, false),
new DispatchRoute(ctx.Other, te, false),
new DispatchRoute(ctx.From, jg, false),
new DispatchRoute(ctx.Other, jg, false),
};
}
if (ctx.Type == BattleType.Scripted)
return new[] { new DispatchRoute(ctx.Other, ctx.Env, false) };
return Array.Empty<DispatchRoute>(); // Pvp-not-both-ready → drop (Bot already returned above)
}
// case 11: scripted-bot TurnEnd whose sender has no handshake phase (test stub) → forward.
if (ctx.IsScriptedBot(ctx.From))
return new[] { new DispatchRoute(ctx.Other, ctx.Env, false) };
return Array.Empty<DispatchRoute>();
}
}