feat(battle-node): thread MatchContext through bridge to BattleSession

IMatchingBridge.RegisterPendingBattle now takes a MatchContext; PendingBattle
carries it; BattleSession stores it. ArenaTwoPickBattleController builds ctx
from IMatchContextBuilder. ScriptedLifecycle still uses ScriptedProfiles for
the player half — Tasks 5/6 migrate the lifecycle.

Existing tests updated: MatchingBridgeTests, BattleNodeFlowTests,
InMemoryBattleSessionStoreTests, BattleSessionDispatchTests, BattleSession
PumpTests, ArenaTwoPickBattleControllerTests (which now seeds a TK2 run +
adds a no-active-run 400 case).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-01 12:44:42 -04:00
parent a0fdb0f3c5
commit 01f9bb722a
12 changed files with 144 additions and 44 deletions

View File

@@ -3,10 +3,10 @@ namespace SVSim.BattleNode.Bridge;
public interface IMatchingBridge
{
/// <summary>
/// Mint a battle id, register a pending session for the given viewer, and return the
/// URL the client should open a socket to.
/// Mint a battle id, register a pending session for the given viewer with their per-battle
/// MatchContext snapshot, and return the URL the client should open a socket to.
/// </summary>
PendingMatch RegisterPendingBattle(long viewerId);
PendingMatch RegisterPendingBattle(long viewerId, MatchContext context);
}
public sealed record PendingMatch(string BattleId, string NodeServerUrl);

View File

@@ -21,7 +21,7 @@ public sealed class MatchingBridge : IMatchingBridge
_options = options;
}
public PendingMatch RegisterPendingBattle(long viewerId)
public PendingMatch RegisterPendingBattle(long viewerId, MatchContext context)
{
// 12-digit decimal battle id mirrors the captures (e.g. "975695075012").
// Two unbiased 6-digit draws concatenated — RandomNumberGenerator.GetInt32 uses
@@ -31,7 +31,7 @@ public sealed class MatchingBridge : IMatchingBridge
var hi = RandomNumberGenerator.GetInt32(0, 1_000_000);
var lo = RandomNumberGenerator.GetInt32(0, 1_000_000);
var battleId = $"{hi:D6}{lo:D6}";
_store.RegisterPending(new PendingBattle(battleId, viewerId));
_store.RegisterPending(new PendingBattle(battleId, viewerId, context));
return new PendingMatch(battleId, _options.NodeServerUrl);
}
}

View File

@@ -104,7 +104,7 @@ public sealed class BattleNodeWebSocketHandler
var ws = await ctx.WebSockets.AcceptWebSocketAsync();
_store.RemovePending(battleId);
var session = new BattleSession(ws, battleId, viewerId, _loggerFactory.CreateLogger<BattleSession>());
var session = new BattleSession(ws, battleId, viewerId, pending.Context, _loggerFactory.CreateLogger<BattleSession>());
await session.RunAsync(ctx.RequestAborted);
}

View File

@@ -2,6 +2,7 @@ using System.Net.WebSockets;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Extensions.Logging;
using SVSim.BattleNode.Bridge;
using SVSim.BattleNode.Lifecycle;
using SVSim.BattleNode.Protocol;
using SVSim.BattleNode.Protocol.Bodies;
@@ -37,12 +38,19 @@ public sealed class BattleSession
public InboundTracker Inbound { get; } = new();
public OutboundSequencer Outbound { get; } = new();
public BattleSession(WebSocket ws, string battleId, long viewerId, ILogger<BattleSession> log)
/// <summary>
/// Player-side snapshot captured at do_matching time. ScriptedLifecycle reads the player
/// half of Matched/BattleStart frames from here; opponent half stays in ScriptedProfiles.
/// </summary>
internal MatchContext Context { get; }
public BattleSession(WebSocket ws, string battleId, long viewerId, MatchContext context, ILogger<BattleSession> log)
{
_ws = ws;
_log = log;
BattleId = battleId;
ViewerId = viewerId;
Context = context;
}
/// <summary>

View File

@@ -1,7 +1,10 @@
using SVSim.BattleNode.Bridge;
namespace SVSim.BattleNode.Sessions;
/// <summary>
/// Sparse pre-connect record: enough to validate the incoming WS connect and resolve
/// the viewer. Full BattleSession is created on connect.
/// Sparse pre-connect record: viewer id + the per-battle MatchContext snapshot. Enough to
/// validate the incoming WS connect, resolve the viewer, and seed the BattleSession with the
/// player-half lifecycle data. Full BattleSession is created on connect.
/// </summary>
public sealed record PendingBattle(string BattleId, long ViewerId);
public sealed record PendingBattle(string BattleId, long ViewerId, MatchContext Context);