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

@@ -2,6 +2,10 @@ using System.Text.Json.Serialization;
namespace SVSim.BattleNode.Protocol.Bodies;
/// <summary>Gungnir keepalive push. <c>scs</c> = self connection status, <c>ocs</c> = opponent
/// connection status; both carry <see cref="WireConstants.OnlineStatus"/> ("ONLINE") in v1.
/// Intentionally has no <c>resultCode</c> — the client treats an absent resultCode on alive
/// frames as "no error" (the lone body without one).</summary>
public sealed record AlivePushBody(
[property: JsonPropertyName("scs")] string Scs,
[property: JsonPropertyName("ocs")] string Ocs) : IMsgBody;

View File

@@ -6,4 +6,4 @@ public sealed record BattleFinishBody(
[property: JsonPropertyName("result")]
[property: JsonConverter(typeof(JsonNumberEnumConverter<BattleResult>))]
BattleResult Result,
[property: JsonPropertyName("resultCode")] int ResultCode = 1) : IMsgBody;
[property: JsonPropertyName("resultCode")] int ResultCode = (int)ReceiveNodeResultCode.Success) : IMsgBody;

View File

@@ -3,11 +3,12 @@ using System.Text.Json.Serialization;
namespace SVSim.BattleNode.Protocol.Bodies;
public sealed record BattleStartBody(
[property: JsonPropertyName("turnState")] int TurnState,
[property: JsonPropertyName("turnState")]
[property: JsonConverter(typeof(JsonNumberEnumConverter<TurnState>))] TurnState TurnState,
[property: JsonPropertyName("battleType")] int BattleType,
[property: JsonPropertyName("selfInfo")] BattleStartSelfInfo SelfInfo,
[property: JsonPropertyName("oppoInfo")] BattleStartOppoInfo OppoInfo,
[property: JsonPropertyName("resultCode")] int ResultCode = 1) : IMsgBody;
[property: JsonPropertyName("resultCode")] int ResultCode = (int)ReceiveNodeResultCode.Success) : IMsgBody;
public sealed record BattleStartSelfInfo(
[property: JsonPropertyName("rank")] string Rank,
@@ -18,6 +19,7 @@ public sealed record BattleStartSelfInfo(
// Note: BattlePoint is int on the wire here (not string as on self) — matches the
// captured prod frame at data_dumps/captures/battle-traffic_tk2_regular.ndjson.
// The string-self / int-oppo split is INTENTIONAL; do NOT unify the two for "consistency".
public sealed record BattleStartOppoInfo(
[property: JsonPropertyName("rank")] string Rank,
[property: JsonPropertyName("isMasterRank")] string IsMasterRank,

View File

@@ -5,4 +5,4 @@ namespace SVSim.BattleNode.Protocol.Bodies;
public sealed record DealBody(
[property: JsonPropertyName("self")] IReadOnlyList<PosIdx> Self,
[property: JsonPropertyName("oppo")] IReadOnlyList<PosIdx> Oppo,
[property: JsonPropertyName("resultCode")] int ResultCode = 1) : IMsgBody;
[property: JsonPropertyName("resultCode")] int ResultCode = (int)ReceiveNodeResultCode.Success) : IMsgBody;

View File

@@ -2,6 +2,9 @@ using System.Text.Json.Serialization;
namespace SVSim.BattleNode.Protocol.Bodies;
/// <summary>Server-pushed Judge frame (turn-handover gate; reflected to the sender in PvP).
/// Same wire shape as <see cref="OpponentTurnStartBody"/> — kept distinct because they back
/// different frames/URIs.</summary>
public sealed record JudgeBody(
[property: JsonPropertyName("spin")] int Spin,
[property: JsonPropertyName("resultCode")] int ResultCode = 1) : IMsgBody;
[property: JsonPropertyName("resultCode")] int ResultCode = (int)ReceiveNodeResultCode.Success) : IMsgBody;

View File

@@ -6,8 +6,10 @@ public sealed record MatchedBody(
[property: JsonPropertyName("selfInfo")] MatchedSelfInfo SelfInfo,
[property: JsonPropertyName("oppoInfo")] MatchedOppoInfo OppoInfo,
[property: JsonPropertyName("selfDeck")] IReadOnlyList<DeckCardRef> SelfDeck,
[property: JsonPropertyName("resultCode")] int ResultCode = 1) : IMsgBody;
[property: JsonPropertyName("resultCode")] int ResultCode = (int)ReceiveNodeResultCode.Success) : IMsgBody;
// Note: `country_code` is deliberately snake_case among camelCase siblings — that's what prod
// sends on this frame (verified against the TK2 capture). Do NOT "normalize" it to countryCode.
public sealed record MatchedSelfInfo(
[property: JsonPropertyName("country_code")] string CountryCode,
[property: JsonPropertyName("userName")] string UserName,
@@ -15,7 +17,8 @@ public sealed record MatchedSelfInfo(
[property: JsonPropertyName("emblemId")] string EmblemId,
[property: JsonPropertyName("degreeId")] string DegreeId,
[property: JsonPropertyName("fieldId")] int FieldId,
[property: JsonPropertyName("isOfficial")] int IsOfficial,
[property: JsonPropertyName("isOfficial")]
[property: JsonConverter(typeof(NumericBoolJsonConverter))] bool IsOfficial,
[property: JsonPropertyName("oppoId")] long OppoId,
[property: JsonPropertyName("seed")] long Seed);
@@ -26,7 +29,8 @@ public sealed record MatchedOppoInfo(
[property: JsonPropertyName("emblemId")] string EmblemId,
[property: JsonPropertyName("degreeId")] string DegreeId,
[property: JsonPropertyName("fieldId")] int FieldId,
[property: JsonPropertyName("isOfficial")] int IsOfficial,
[property: JsonPropertyName("isOfficial")]
[property: JsonConverter(typeof(NumericBoolJsonConverter))] bool IsOfficial,
[property: JsonPropertyName("oppoId")] long OppoId,
[property: JsonPropertyName("seed")] long Seed,
[property: JsonPropertyName("oppoDeckCount")] int OppoDeckCount);

View File

@@ -2,6 +2,9 @@ using System.Text.Json.Serialization;
namespace SVSim.BattleNode.Protocol.Bodies;
/// <summary>Server-pushed opponent-turn-open frame (relayed to the non-active player).
/// Same wire shape as <see cref="JudgeBody"/> — kept distinct because they back different
/// frames/URIs.</summary>
public sealed record OpponentTurnStartBody(
[property: JsonPropertyName("spin")] int Spin,
[property: JsonPropertyName("resultCode")] int ResultCode = 1) : IMsgBody;
[property: JsonPropertyName("resultCode")] int ResultCode = (int)ReceiveNodeResultCode.Success) : IMsgBody;

View File

@@ -32,7 +32,8 @@ public sealed record KeyActionEntry(
/// Only emitted for the open:1 pass-through case (open:0 strips the whole <c>selectCard</c>).</summary>
public sealed record SelectCardEntry(
[property: JsonPropertyName("cardId")] IReadOnlyList<long> CardId,
[property: JsonPropertyName("open")] int Open);
[property: JsonPropertyName("open")]
[property: JsonConverter(typeof(JsonNumberEnumConverter<ChoiceVisibility>))] ChoiceVisibility Open);
/// <summary>One revealed card in a <c>knownList</c>. Vanilla slice fills cardId from the sender's
/// deck map and leaves spellboost 0 / attachTarget "" (cost/clan/tribe deferred to the card-master
@@ -48,7 +49,8 @@ public sealed record KnownCardEntry(
/// verbatim — no perspective flip (bullet-3 audit F2).</summary>
public sealed record OppoTargetEntry(
[property: JsonPropertyName("targetIdx")] int TargetIdx,
[property: JsonPropertyName("isSelf")] int IsSelf);
[property: JsonPropertyName("isSelf")]
[property: JsonConverter(typeof(JsonNumberEnumConverter<CardOwner>))] CardOwner IsSelf);
/// <summary>One entry in a relayed <c>uList</c> (the unapproved-movement list) — a skill-driven
/// card movement (fetch / search / summon-from-deck / discard-reveal) the node forwards VERBATIM
@@ -60,12 +62,14 @@ public sealed record UnapprovedCardEntry(
[property: JsonPropertyName("idxList")] IReadOnlyList<int> IdxList,
[property: JsonPropertyName("from")] int From,
[property: JsonPropertyName("to")] int To,
[property: JsonPropertyName("isSelf")] int IsSelf,
[property: JsonPropertyName("isSelf")]
[property: JsonConverter(typeof(JsonNumberEnumConverter<CardOwner>))] CardOwner IsSelf,
[property: JsonPropertyName("skill")] string Skill,
[property: JsonPropertyName("cardId")] long? CardId = null,
[property: JsonPropertyName("clan")] int? Clan = null,
[property: JsonPropertyName("cost")] int? Cost = null,
[property: JsonPropertyName("skillKeyCardIdx")] IReadOnlyList<int>? SkillKeyCardIdx = null,
[property: JsonPropertyName("randomTargetIdx")] IReadOnlyList<int>? RandomTargetIdx = null,
[property: JsonPropertyName("isInvoke")] int? IsInvoke = null,
[property: JsonPropertyName("isInvoke")]
[property: JsonConverter(typeof(NumericBoolJsonConverter))] bool? IsInvoke = null,
[property: JsonPropertyName("attachTarget")] string? AttachTarget = null);

View File

@@ -7,4 +7,4 @@ public sealed record ReadyBody(
[property: JsonPropertyName("oppo")] IReadOnlyList<PosIdx> Oppo,
[property: JsonPropertyName("idxChangeSeed")] int IdxChangeSeed,
[property: JsonPropertyName("spin")] int Spin,
[property: JsonPropertyName("resultCode")] int ResultCode = 1) : IMsgBody;
[property: JsonPropertyName("resultCode")] int ResultCode = (int)ReceiveNodeResultCode.Success) : IMsgBody;

View File

@@ -3,4 +3,4 @@ using System.Text.Json.Serialization;
namespace SVSim.BattleNode.Protocol.Bodies;
public sealed record ResultCodeOnlyBody(
[property: JsonPropertyName("resultCode")] int ResultCode = 1) : IMsgBody;
[property: JsonPropertyName("resultCode")] int ResultCode = (int)ReceiveNodeResultCode.Success) : IMsgBody;

View File

@@ -4,4 +4,4 @@ namespace SVSim.BattleNode.Protocol.Bodies;
public sealed record SwapResponseBody(
[property: JsonPropertyName("self")] IReadOnlyList<PosIdx> Self,
[property: JsonPropertyName("resultCode")] int ResultCode = 1) : IMsgBody;
[property: JsonPropertyName("resultCode")] int ResultCode = (int)ReceiveNodeResultCode.Success) : IMsgBody;

View File

@@ -3,5 +3,6 @@ using System.Text.Json.Serialization;
namespace SVSim.BattleNode.Protocol.Bodies;
public sealed record TurnEndBody(
[property: JsonPropertyName("turnState")] int TurnState,
[property: JsonPropertyName("resultCode")] int ResultCode = 1) : IMsgBody;
[property: JsonPropertyName("turnState")]
[property: JsonConverter(typeof(JsonNumberEnumConverter<TurnState>))] TurnState TurnState,
[property: JsonPropertyName("resultCode")] int ResultCode = (int)ReceiveNodeResultCode.Success) : IMsgBody;