refactor(battlenode): split BattleSessionPhase into HandshakePhase + SessionLifecycle
Behavior-preserving; 231 BattleNode tests green. One enum conflated two axes. Split: - HandshakePhase (per participant): AwaitingInitNetwork..AfterReady. On IHasHandshakePhase.Phase, FrameDispatchContext.SenderPhase, the handler gates. - SessionLifecycle (per battle): Active | Terminal. On the renamed BattleSessionState.Lifecycle (was SessionPhase, defaulting to a handshake value) and BattleSession.Lifecycle (was Phase). Reads are only != Terminal, so the Active default is behavior-identical. OpponentTurn was dead (never assigned) -> dropped. BattleSessionPhase deleted; the two axes can no longer be cross-assigned. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -38,7 +38,7 @@ public sealed class BattleSession
|
||||
public BattleType Type { get; }
|
||||
public IBattleParticipant A { get; }
|
||||
public IBattleParticipant B { get; }
|
||||
public BattleSessionPhase Phase => _state.SessionPhase;
|
||||
public SessionLifecycle Lifecycle => _state.Lifecycle;
|
||||
|
||||
// Per-URI dispatch table. All 14 inbound URIs are registered (Tasks 5-14); unknown
|
||||
// URIs are dropped with a LogDebug in ComputeFrames.
|
||||
@@ -104,7 +104,7 @@ public sealed class BattleSession
|
||||
var first = await Task.WhenAny(aTask, bTask).ConfigureAwait(false);
|
||||
var survivor = first == aTask ? B : A;
|
||||
|
||||
if (Phase != BattleSessionPhase.Terminal)
|
||||
if (Lifecycle != SessionLifecycle.Terminal)
|
||||
{
|
||||
// Involuntary drop (no graceful Retire): synthesize BattleFinish(DisconnectWin)
|
||||
// to survivor. DisconnectWin=201 → client renders "opponent disconnected" →
|
||||
@@ -121,7 +121,7 @@ public sealed class BattleSession
|
||||
"BattleSession {Bid}: failed to push BattleFinish to survivor (their WS may also be closed)",
|
||||
BattleId);
|
||||
}
|
||||
_state.SessionPhase = BattleSessionPhase.Terminal;
|
||||
_state.Lifecycle = SessionLifecycle.Terminal;
|
||||
}
|
||||
|
||||
cts.Cancel(); // unblock the survivor's RunAsync read loop
|
||||
@@ -202,8 +202,8 @@ public sealed class BattleSession
|
||||
if (Handlers.TryGetValue(env.Uri, out var handler))
|
||||
return handler.Handle(BuildContext(from, env));
|
||||
|
||||
_log.LogDebug("BattleSession {Bid}: dropping uri={Uri} in phase={Phase} from vid={Vid}",
|
||||
BattleId, env.Uri, Phase, from.ViewerId);
|
||||
_log.LogDebug("BattleSession {Bid}: dropping uri={Uri} in lifecycle={Lifecycle} from vid={Vid}",
|
||||
BattleId, env.Uri, Lifecycle, from.ViewerId);
|
||||
return Array.Empty<DispatchRoute>();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
namespace SVSim.BattleNode.Sessions;
|
||||
|
||||
/// <summary>
|
||||
/// Where we are in the v1 server-authored frame lifecycle. Drives which server-authored frames
|
||||
/// the session pushes in response to inbound emits.
|
||||
/// </summary>
|
||||
public enum BattleSessionPhase
|
||||
{
|
||||
AwaitingInitNetwork,
|
||||
AwaitingInitBattle,
|
||||
AwaitingLoaded,
|
||||
AwaitingSwap,
|
||||
AfterReady,
|
||||
OpponentTurn,
|
||||
Terminal,
|
||||
}
|
||||
@@ -39,7 +39,7 @@ internal sealed class BattleSessionState
|
||||
return deck;
|
||||
}
|
||||
|
||||
public BattleSessionPhase SessionPhase { get; set; } = BattleSessionPhase.AwaitingInitNetwork;
|
||||
public SessionLifecycle Lifecycle { get; set; } = SessionLifecycle.Active;
|
||||
public Dictionary<IBattleParticipant, long[]> PostSwapHands { get; } = new();
|
||||
|
||||
/// <summary>Per-side idx->cardId, seeded lazily from <see cref="MatchContext.SelfDeckCardIds"/>.
|
||||
|
||||
@@ -20,7 +20,7 @@ internal sealed class FrameDispatchContext
|
||||
|
||||
/// <summary>The dispatching participant's handshake phase (null for a non-IHasHandshakePhase
|
||||
/// participant, e.g. NoOpBot). Setting it advances the sender.</summary>
|
||||
internal BattleSessionPhase? SenderPhase
|
||||
internal HandshakePhase? SenderPhase
|
||||
{
|
||||
get => (From as IHasHandshakePhase)?.Phase;
|
||||
set { if (From is IHasHandshakePhase p && value is { } v) p.Phase = v; }
|
||||
@@ -29,6 +29,6 @@ internal sealed class FrameDispatchContext
|
||||
/// <summary>Both participants have completed the handshake. Reads A/B (not From/Other) so the
|
||||
/// result is identical regardless of which side sent the frame — matches legacy BothAfterReady.</summary>
|
||||
internal bool BothAfterReady() =>
|
||||
(A as IHasHandshakePhase)?.Phase == BattleSessionPhase.AfterReady &&
|
||||
(B as IHasHandshakePhase)?.Phase == BattleSessionPhase.AfterReady;
|
||||
(A as IHasHandshakePhase)?.Phase == HandshakePhase.AfterReady &&
|
||||
(B as IHasHandshakePhase)?.Phase == HandshakePhase.AfterReady;
|
||||
}
|
||||
|
||||
@@ -8,18 +8,18 @@ internal sealed class InitBattleHandler : IFrameHandler
|
||||
public IReadOnlyList<DispatchRoute> 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)
|
||||
if (ctx.Type == BattleType.Bot && ctx.SenderPhase == HandshakePhase.AwaitingInitBattle)
|
||||
{
|
||||
var r = new List<DispatchRoute>
|
||||
{
|
||||
new(ctx.From, BattleFrames.BuildAck(NetworkBattleUri.InitBattle), Stock.Bypass),
|
||||
};
|
||||
ctx.SenderPhase = BattleSessionPhase.AwaitingLoaded;
|
||||
ctx.SenderPhase = HandshakePhase.AwaitingLoaded;
|
||||
return r;
|
||||
}
|
||||
|
||||
// case 5: general — push Matched (per-perspective) to the sender only.
|
||||
if (ctx.SenderPhase == BattleSessionPhase.AwaitingInitBattle)
|
||||
if (ctx.SenderPhase == HandshakePhase.AwaitingInitBattle)
|
||||
{
|
||||
var r = new List<DispatchRoute>
|
||||
{
|
||||
@@ -28,7 +28,7 @@ internal sealed class InitBattleHandler : IFrameHandler
|
||||
ctx.BattleId, BattleSeeds.Stable(ctx.State.MasterSeed),
|
||||
ctx.State.GetShuffledDeck(ctx.From)), Stock.Normal),
|
||||
};
|
||||
ctx.SenderPhase = BattleSessionPhase.AwaitingLoaded;
|
||||
ctx.SenderPhase = HandshakePhase.AwaitingLoaded;
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,14 +7,14 @@ internal sealed class InitNetworkHandler : IFrameHandler
|
||||
{
|
||||
public IReadOnlyList<DispatchRoute> Handle(FrameDispatchContext ctx)
|
||||
{
|
||||
if (ctx.SenderPhase != BattleSessionPhase.AwaitingInitNetwork)
|
||||
if (ctx.SenderPhase != HandshakePhase.AwaitingInitNetwork)
|
||||
return Array.Empty<DispatchRoute>();
|
||||
|
||||
var routes = new List<DispatchRoute>
|
||||
{
|
||||
new(ctx.From, BattleFrames.BuildAck(NetworkBattleUri.InitNetwork), Stock.Bypass),
|
||||
};
|
||||
ctx.SenderPhase = BattleSessionPhase.AwaitingInitBattle;
|
||||
ctx.SenderPhase = HandshakePhase.AwaitingInitBattle;
|
||||
return routes;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,14 +8,14 @@ internal sealed class LoadedHandler : IFrameHandler
|
||||
public IReadOnlyList<DispatchRoute> Handle(FrameDispatchContext ctx)
|
||||
{
|
||||
// case 3: Bot — silent (client populates opponent state from AIBattleStart HTTP data).
|
||||
if (ctx.Type == BattleType.Bot && ctx.SenderPhase == BattleSessionPhase.AwaitingLoaded)
|
||||
if (ctx.Type == BattleType.Bot && ctx.SenderPhase == HandshakePhase.AwaitingLoaded)
|
||||
{
|
||||
ctx.SenderPhase = BattleSessionPhase.AwaitingSwap;
|
||||
ctx.SenderPhase = HandshakePhase.AwaitingSwap;
|
||||
return Array.Empty<DispatchRoute>();
|
||||
}
|
||||
|
||||
// case 6: general — BattleStart (per-perspective) + Deal to the sender.
|
||||
if (ctx.SenderPhase == BattleSessionPhase.AwaitingLoaded)
|
||||
if (ctx.SenderPhase == HandshakePhase.AwaitingLoaded)
|
||||
{
|
||||
// A goes first deterministically; B goes second.
|
||||
var turnState = ReferenceEquals(ctx.From, ctx.A) ? TurnState.First : TurnState.Second;
|
||||
@@ -25,7 +25,7 @@ internal sealed class LoadedHandler : IFrameHandler
|
||||
ctx.From.Context, ctx.Other.Context, ctx.From.ViewerId, turnState), Stock.Normal),
|
||||
new(ctx.From, ServerBattleFrames.BuildDeal(), Stock.Normal),
|
||||
};
|
||||
ctx.SenderPhase = BattleSessionPhase.AwaitingSwap;
|
||||
ctx.SenderPhase = HandshakePhase.AwaitingSwap;
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ internal sealed class RetireKillHandler : IFrameHandler
|
||||
{
|
||||
public IReadOnlyList<DispatchRoute> Handle(FrameDispatchContext ctx)
|
||||
{
|
||||
ctx.State.SessionPhase = BattleSessionPhase.Terminal;
|
||||
ctx.State.Lifecycle = SessionLifecycle.Terminal;
|
||||
// Polarity: the SENDER retired, so From LOSES / Other WINS. This is the OPPOSITE of
|
||||
// TurnEndFinalHandler (From WINS there — sender dealt the lethal). Intentional — do NOT
|
||||
// "consistency-fix" the two handlers to match; a swap here silently reverses every retire.
|
||||
|
||||
@@ -8,7 +8,7 @@ internal sealed class SwapHandler : IFrameHandler
|
||||
{
|
||||
public IReadOnlyList<DispatchRoute> Handle(FrameDispatchContext ctx)
|
||||
{
|
||||
if (ctx.SenderPhase != BattleSessionPhase.AwaitingSwap)
|
||||
if (ctx.SenderPhase != HandshakePhase.AwaitingSwap)
|
||||
return Array.Empty<DispatchRoute>();
|
||||
|
||||
var routes = new List<DispatchRoute>();
|
||||
@@ -17,7 +17,7 @@ internal sealed class SwapHandler : IFrameHandler
|
||||
// SwapResponse is always immediate — completes the sender's own mulligan UI.
|
||||
routes.Add(new DispatchRoute(ctx.From, ServerBattleFrames.BuildSwapResponse(hand), Stock.Normal));
|
||||
ctx.State.PostSwapHands[ctx.From] = hand;
|
||||
ctx.SenderPhase = BattleSessionPhase.AfterReady;
|
||||
ctx.SenderPhase = HandshakePhase.AfterReady;
|
||||
|
||||
// Release Ready to every swapper once all handshake-driving participants have swapped.
|
||||
// IHasHandshakePhase membership IS the "participates in mulligan" set.
|
||||
|
||||
@@ -7,13 +7,13 @@ internal sealed class TurnEndFinalHandler : IFrameHandler
|
||||
public IReadOnlyList<DispatchRoute> Handle(FrameDispatchContext ctx)
|
||||
{
|
||||
// case 4: Bot — Judge to sender only.
|
||||
if (ctx.Type == BattleType.Bot && ctx.SenderPhase == BattleSessionPhase.AfterReady)
|
||||
if (ctx.Type == BattleType.Bot && ctx.SenderPhase == HandshakePhase.AfterReady)
|
||||
return new[] { new DispatchRoute(ctx.From, BattleFrames.BuildJudgeBroadcast(), Stock.Normal) };
|
||||
|
||||
// case 9: general — forward the envelope to other + paired BattleFinish + Terminal.
|
||||
if (ctx.SenderPhase == BattleSessionPhase.AfterReady)
|
||||
if (ctx.SenderPhase == HandshakePhase.AfterReady)
|
||||
{
|
||||
ctx.State.SessionPhase = BattleSessionPhase.Terminal;
|
||||
ctx.State.Lifecycle = SessionLifecycle.Terminal;
|
||||
// Polarity: the SENDER dealt the lethal, so From WINS / Other LOSES. This is the
|
||||
// OPPOSITE of RetireKillHandler (From LOSES there — retire is self-inflicted).
|
||||
// Intentional — do NOT "consistency-fix" the two handlers to match; a swap here
|
||||
|
||||
@@ -8,12 +8,12 @@ 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)
|
||||
if (ctx.Type == BattleType.Bot && ctx.SenderPhase == HandshakePhase.AfterReady)
|
||||
return new[] { new DispatchRoute(ctx.From, BattleFrames.BuildJudgeBroadcast(), Stock.Normal) };
|
||||
|
||||
// case 8: general AfterReady arm — PvP forwards a {turnState} TurnEnd to the opponent
|
||||
// (handover gate). Any non-Pvp non-Bot type that reaches AfterReady consumes the frame.
|
||||
if (ctx.SenderPhase == BattleSessionPhase.AfterReady)
|
||||
if (ctx.SenderPhase == HandshakePhase.AfterReady)
|
||||
{
|
||||
if (ctx.Type == BattleType.Pvp && ctx.BothAfterReady())
|
||||
{
|
||||
|
||||
18
SVSim.BattleNode/Sessions/HandshakePhase.cs
Normal file
18
SVSim.BattleNode/Sessions/HandshakePhase.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
namespace SVSim.BattleNode.Sessions;
|
||||
|
||||
/// <summary>
|
||||
/// Per-participant progression through the v1 server-authored setup handshake. Each side advances
|
||||
/// InitNetwork → InitBattle → Loaded → Swap → AfterReady as the session acks its emits. Tracked
|
||||
/// per participant via <see cref="Participants.IHasHandshakePhase"/>; the session reads the
|
||||
/// SENDER's phase (<see cref="Dispatch.FrameDispatchContext.SenderPhase"/>) to gate which setup
|
||||
/// frame to author next. Distinct from the session-global <see cref="SessionLifecycle"/> — this is
|
||||
/// one axis per side, that is one axis per battle.
|
||||
/// </summary>
|
||||
public enum HandshakePhase
|
||||
{
|
||||
AwaitingInitNetwork,
|
||||
AwaitingInitBattle,
|
||||
AwaitingLoaded,
|
||||
AwaitingSwap,
|
||||
AfterReady,
|
||||
}
|
||||
@@ -19,7 +19,7 @@ namespace SVSim.BattleNode.Sessions.Participants;
|
||||
/// </summary>
|
||||
internal interface IHasHandshakePhase
|
||||
{
|
||||
BattleSessionPhase Phase { get; set; }
|
||||
HandshakePhase Phase { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -66,9 +66,9 @@ public sealed class RealParticipant : IBattleParticipant, IHasHandshakePhase
|
||||
/// because they never send the gating URIs. Also satisfies
|
||||
/// <see cref="IHasHandshakePhase"/> (the interface BattleSession uses to gate
|
||||
/// handshake dispatch without depending on the concrete RealParticipant type).</summary>
|
||||
internal BattleSessionPhase Phase { get; set; } = BattleSessionPhase.AwaitingInitNetwork;
|
||||
internal HandshakePhase Phase { get; set; } = HandshakePhase.AwaitingInitNetwork;
|
||||
|
||||
BattleSessionPhase IHasHandshakePhase.Phase
|
||||
HandshakePhase IHasHandshakePhase.Phase
|
||||
{
|
||||
get => Phase;
|
||||
set => Phase = value;
|
||||
|
||||
15
SVSim.BattleNode/Sessions/SessionLifecycle.cs
Normal file
15
SVSim.BattleNode/Sessions/SessionLifecycle.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace SVSim.BattleNode.Sessions;
|
||||
|
||||
/// <summary>
|
||||
/// Session-global lifecycle. A battle stays <see cref="Active"/> until a terminal event — a lethal
|
||||
/// TurnEndFinal, a Retire/Kill, or the disconnect drop cascade — flips it to <see cref="Terminal"/>,
|
||||
/// after which the drop cascade will not synthesize another BattleFinish. Distinct from the
|
||||
/// per-participant <see cref="HandshakePhase"/> (which side reached which setup step); this is one
|
||||
/// axis per battle. Only these two states are load-bearing — the handshake progression lives on the
|
||||
/// other enum.
|
||||
/// </summary>
|
||||
public enum SessionLifecycle
|
||||
{
|
||||
Active,
|
||||
Terminal,
|
||||
}
|
||||
Reference in New Issue
Block a user