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;

View File

@@ -0,0 +1,17 @@
namespace SVSim.BattleNode.Protocol;
/// <summary>
/// Wire value of the actor-relative <c>isSelf</c> flag on relayed lists (<c>targetList</c>,
/// <c>uList</c>): whose side a referenced card belongs to, from the SENDER's perspective. The node
/// forwards it verbatim — no perspective flip (bullet-3 audit F2). The client reads it via
/// <c>ConvertToInt(...) == 1</c> (<c>NetworkBattleReceiver.cs</c>), so it serializes as the
/// underlying int via <see cref="System.Text.Json.Serialization.JsonNumberEnumConverter{T}"/>.
/// </summary>
public enum CardOwner
{
/// <summary>Card belongs to the opponent of the sender.</summary>
Opponent = 0,
/// <summary>Card belongs to the sender.</summary>
Self = 1,
}

View File

@@ -0,0 +1,17 @@
namespace SVSim.BattleNode.Protocol;
/// <summary>
/// Wire value of <c>open</c> on a choice/Discover <c>selectCard</c>: whether the pick is revealed.
/// The client emits it as <c>selectCardIsOpen ? 1 : 0</c> (<c>SendKeyActionDataManager.cs</c>);
/// the node uses it to decide whether to strip the pick for the opponent (<c>Hidden</c> = strip).
/// Serializes as the underlying int via
/// <see cref="System.Text.Json.Serialization.JsonNumberEnumConverter{T}"/>.
/// </summary>
public enum ChoiceVisibility
{
/// <summary>Hidden draw-to-hand pick — the chosen card stays secret until played.</summary>
Hidden = 0,
/// <summary>Visible board choice — the pick is revealed immediately.</summary>
Open = 1,
}

View File

@@ -0,0 +1,29 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace SVSim.BattleNode.Protocol;
/// <summary>
/// Serializes a <see cref="bool"/> as the wire's numeric 0/1. The client reads these flags via
/// <c>Convert.ToInt32</c> / <c>Convert.ToBoolean</c> (e.g. <c>isOfficial</c>, <c>isInvoke</c>) —
/// never as a JSON <c>true</c>/<c>false</c> token — so a real <c>bool</c> property must still emit
/// a number. Read accepts a JSON number (0 = false, non-zero = true) and, defensively, a
/// <c>true</c>/<c>false</c> token or a numeric string. Applied per-field via
/// <c>[JsonConverter(typeof(NumericBoolJsonConverter))]</c>; works on <c>bool?</c> too (System.Text.Json
/// wraps a <c>JsonConverter&lt;bool&gt;</c> for the nullable case).
/// </summary>
public sealed class NumericBoolJsonConverter : JsonConverter<bool>
{
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.TokenType switch
{
JsonTokenType.Number => reader.GetInt64() != 0,
JsonTokenType.True => true,
JsonTokenType.False => false,
JsonTokenType.String => long.TryParse(reader.GetString(), out var n) && n != 0,
_ => throw new JsonException($"Cannot convert token {reader.TokenType} to a numeric bool"),
};
public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
=> writer.WriteNumberValue(value ? 1 : 0);
}

View File

@@ -0,0 +1,16 @@
namespace SVSim.BattleNode.Protocol;
/// <summary>
/// Wire value of <c>turnState</c> on BattleStart / TurnEnd frames: which side acts first.
/// The client reads it via <c>Convert.ToInt32</c> (<c>RealTimeNetworkAgent.cs</c> "turnState"
/// case) into <c>NetworkUserInfoData.TurnState</c>, so it serializes as the underlying int via
/// <see cref="System.Text.Json.Serialization.JsonNumberEnumConverter{T}"/>.
/// </summary>
public enum TurnState
{
/// <summary>This side takes the first turn.</summary>
First = 0,
/// <summary>This side takes the second turn.</summary>
Second = 1,
}