Files
SVSimServer/SVSim.BattleNode/Sessions/Dispatch/Handlers/TurnEndHandler.cs
2026-06-03 18:07:44 -04:00

39 lines
1.8 KiB
C#

using SVSim.BattleNode.Protocol;
using SVSim.BattleNode.Protocol.Bodies;
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())
{
// 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.
var te = ctx.Env with { Body = new TurnEndBody(TurnState: 0) };
return new[] { new DispatchRoute(ctx.Other, te, 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>();
}
}