Quality pass from the 2026-06-04 BattleNode review (audit in the outer
repo). All changes are behavior-preserving — identical wire bytes,
verified by the full 1008-test suite staying green.
- Name scattered magic numbers: crypto key/IV lengths, outbound-sequencer
base, WS receive buffer / EIO ping / SID length, polite-close timeout,
upgrade-credential keys, battle-id digit math, deterministic-turn spin.
- resultCode = 1 -> (int)ReceiveNodeResultCode.Success across body records.
- Pong "3" -> EngineIoPacketType.Pong; remove dead NoOpBotParticipant.Touch
(replace with #pragma warning disable CS0067).
- Wire-flag enums, serialized as numbers via JsonNumberEnumConverter:
turnState -> TurnState{First,Second}, isSelf -> CardOwner{Opponent,Self},
open -> ChoiceVisibility{Hidden,Open}.
- isOfficial / isInvoke -> bool / bool? via new NumericBoolJsonConverter
(reads/writes 0/1; TDD'd). Scoped to the BattleNode wire boundary only;
MatchContext and the HTTP/AI-start path stay int (AI-start uses -1 as a
sentinel, so it is not boolean).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
61 lines
2.6 KiB
C#
61 lines
2.6 KiB
C#
using System.Security.Cryptography;
|
|
using SVSim.BattleNode.Sessions;
|
|
|
|
namespace SVSim.BattleNode.Bridge;
|
|
|
|
/// <summary>
|
|
/// In-process implementation of <see cref="IMatchingBridge"/>. The HTTP-side
|
|
/// matching queue calls <see cref="RegisterBattle"/> once it has decided "these two
|
|
/// play each other" or "this viewer is solo (bot)."
|
|
/// </summary>
|
|
public sealed class MatchingBridge : IMatchingBridge
|
|
{
|
|
/// <summary>Battle id is two zero-padded decimal halves concatenated (e.g. "975695" + "075012").
|
|
/// The half-width and the draw bound must stay coupled: bound == 10^digits.</summary>
|
|
private const int BattleIdHalfDigits = 6;
|
|
private const int BattleIdHalfExclusiveMax = 1_000_000; // 10^BattleIdHalfDigits
|
|
|
|
private readonly IBattleSessionStore _store;
|
|
private readonly BattleNodeOptions _options;
|
|
|
|
public MatchingBridge(IBattleSessionStore store, BattleNodeOptions options)
|
|
{
|
|
_store = store;
|
|
_options = options;
|
|
}
|
|
|
|
public PendingMatch RegisterBattle(BattlePlayer p1, BattlePlayer? p2, BattleType type)
|
|
{
|
|
ValidateContract(p1, p2, type);
|
|
|
|
// Decimal battle id mirrors the captures (e.g. "975695075012"): two unbiased
|
|
// BattleIdHalfDigits-wide draws concatenated. RandomNumberGenerator.GetInt32 uses
|
|
// rejection sampling so each half is uniform on [0, BattleIdHalfExclusiveMax).
|
|
var hi = RandomNumberGenerator.GetInt32(0, BattleIdHalfExclusiveMax);
|
|
var lo = RandomNumberGenerator.GetInt32(0, BattleIdHalfExclusiveMax);
|
|
var halfFormat = "D" + BattleIdHalfDigits;
|
|
var battleId = hi.ToString(halfFormat) + lo.ToString(halfFormat);
|
|
|
|
_store.RegisterPending(new PendingBattle(battleId, type, p1, p2));
|
|
return new PendingMatch(battleId, _options.NodeServerUrl);
|
|
}
|
|
|
|
private static void ValidateContract(BattlePlayer p1, BattlePlayer? p2, BattleType type)
|
|
{
|
|
if (p1 is null) throw new ArgumentNullException(nameof(p1));
|
|
switch (type)
|
|
{
|
|
case BattleType.Pvp:
|
|
if (p2 is null) throw new ArgumentException("Pvp requires both p1 and p2.", nameof(p2));
|
|
if (p1.ViewerId == p2.ViewerId)
|
|
throw new ArgumentException("Pvp requires distinct viewer ids.", nameof(p2));
|
|
break;
|
|
case BattleType.Bot:
|
|
if (p2 is not null) throw new ArgumentException("Bot must have p2==null.", nameof(p2));
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(nameof(type), type, "Unknown BattleType.");
|
|
}
|
|
}
|
|
}
|