refactor(battle-node): de-magic wire flags and scattered constants

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>
This commit is contained in:
gamer147
2026-06-04 20:46:09 -04:00
parent ed88683fa0
commit 24180d5b4b
38 changed files with 304 additions and 95 deletions

View File

@@ -10,6 +10,11 @@ namespace SVSim.BattleNode.Bridge;
/// </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;
@@ -23,12 +28,13 @@ public sealed class MatchingBridge : IMatchingBridge
{
ValidateContract(p1, p2, type);
// 12-digit decimal battle id mirrors the captures (e.g. "975695075012").
// Two unbiased 6-digit draws concatenated RandomNumberGenerator.GetInt32 uses
// rejection sampling so the result is uniform on [0, 10^6).
var hi = RandomNumberGenerator.GetInt32(0, 1_000_000);
var lo = RandomNumberGenerator.GetInt32(0, 1_000_000);
var battleId = $"{hi:D6}{lo:D6}";
// 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);