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

@@ -65,15 +65,15 @@ public class ServerBattleFramesTests
Assert.That(body.SelfInfo.EmblemId, Is.EqualTo("888"));
Assert.That(body.SelfInfo.DegreeId, Is.EqualTo("777"));
Assert.That(body.SelfInfo.FieldId, Is.EqualTo(42));
Assert.That(body.SelfInfo.IsOfficial, Is.EqualTo(1));
Assert.That(body.SelfInfo.IsOfficial, Is.True);
}
[Test]
public void BuildBattleStart_HasTurnStateZero_AndUsesContextBattleType()
{
var env = ServerBattleFrames.BuildBattleStart(FixtureCtx(), FakeOpponentCtx(), selfViewerId: 1, turnState: 0);
var env = ServerBattleFrames.BuildBattleStart(FixtureCtx(), FakeOpponentCtx(), selfViewerId: 1, turnState: TurnState.First);
var body = (BattleStartBody)env.Body;
Assert.That(body.TurnState, Is.EqualTo(0));
Assert.That(body.TurnState, Is.EqualTo(TurnState.First));
Assert.That(body.BattleType, Is.EqualTo(11));
}
@@ -87,7 +87,7 @@ public class ServerBattleFramesTests
BattleType = 42,
};
var env = ServerBattleFrames.BuildBattleStart(ctx, FakeOpponentCtx(), selfViewerId: 1, turnState: 0);
var env = ServerBattleFrames.BuildBattleStart(ctx, FakeOpponentCtx(), selfViewerId: 1, turnState: TurnState.First);
var body = (BattleStartBody)env.Body;
Assert.That(body.SelfInfo.ClassId, Is.EqualTo("7"));

View File

@@ -87,7 +87,7 @@ public class TypedBodyWireShapeTests
[Test]
public void BuildBattleStart_SerializesAllWireKeysAndPreservesBattlePointAsymmetry()
{
var env = ServerBattleFrames.BuildBattleStart(FixtureCtx(), FakeOpponentCtx(), selfViewerId: 906243102, turnState: 0);
var env = ServerBattleFrames.BuildBattleStart(FixtureCtx(), FakeOpponentCtx(), selfViewerId: 906243102, turnState: TurnState.First);
var json = MsgEnvelope.ToJson(env);
var node = JsonNode.Parse(json)!.AsObject();

View File

@@ -1,6 +1,7 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using NUnit.Framework;
using SVSim.BattleNode.Protocol;
using SVSim.BattleNode.Protocol.Bodies;
namespace SVSim.UnitTests.BattleNode.Protocol.Bodies;
@@ -12,7 +13,7 @@ public class BattleStartBodyTests
public void Serializes_TopLevelFields_WithCorrectWireKeys()
{
var body = new BattleStartBody(
TurnState: 0, BattleType: 11,
TurnState: TurnState.First, BattleType: 11,
SelfInfo: new BattleStartSelfInfo("10", "6270", "1", "1", "card_master_node_10015"),
OppoInfo: new BattleStartOppoInfo("1", "0", 0, "0", "8", "8", "card_master_node_10015"));

View File

@@ -15,11 +15,11 @@ public class MatchedBodyTests
SelfInfo: new MatchedSelfInfo(
CountryCode: "KOR", UserName: "Player", SleeveId: "3000011",
EmblemId: "701441011", DegreeId: "300003", FieldId: 43,
IsOfficial: 0, OppoId: 847666884L, Seed: 17_548_138L),
IsOfficial: false, OppoId: 847666884L, Seed: 17_548_138L),
OppoInfo: new MatchedOppoInfo(
CountryCode: "JPN", UserName: "Opponent", SleeveId: "704141010",
EmblemId: "400001100", DegreeId: "120027", FieldId: 5,
IsOfficial: 0, OppoId: 906243102L, Seed: 17_548_138L, OppoDeckCount: 30),
IsOfficial: false, OppoId: 906243102L, Seed: 17_548_138L, OppoDeckCount: 30),
SelfDeck: new[] { new DeckCardRef(Idx: 1, CardId: 100011010L) });
var node = (JsonObject)JsonSerializer.SerializeToNode(body)!;
@@ -40,8 +40,8 @@ public class MatchedBodyTests
public void OppoInfo_HasOppoDeckCount_OnTheWire()
{
var body = new MatchedBody(
SelfInfo: new MatchedSelfInfo("KOR","P","s","e","d",0,0,1L,1L),
OppoInfo: new MatchedOppoInfo("JPN","O","s","e","d",0,0,1L,1L, OppoDeckCount: 30),
SelfInfo: new MatchedSelfInfo("KOR","P","s","e","d",0,false,1L,1L),
OppoInfo: new MatchedOppoInfo("JPN","O","s","e","d",0,false,1L,1L, OppoDeckCount: 30),
SelfDeck: System.Array.Empty<DeckCardRef>());
var node = (JsonObject)JsonSerializer.SerializeToNode(body)!;
@@ -54,8 +54,8 @@ public class MatchedBodyTests
public void SelfInfo_DoesNotHaveOppoDeckCount_OnTheWire()
{
var body = new MatchedBody(
SelfInfo: new MatchedSelfInfo("KOR","P","s","e","d",0,0,1L,1L),
OppoInfo: new MatchedOppoInfo("JPN","O","s","e","d",0,0,1L,1L,30),
SelfInfo: new MatchedSelfInfo("KOR","P","s","e","d",0,false,1L,1L),
OppoInfo: new MatchedOppoInfo("JPN","O","s","e","d",0,false,1L,1L,30),
SelfDeck: System.Array.Empty<DeckCardRef>());
var node = (JsonObject)JsonSerializer.SerializeToNode(body)!;
@@ -68,8 +68,8 @@ public class MatchedBodyTests
public void ResultCode_DefaultsToOne_OnConstruction()
{
var body = new MatchedBody(
SelfInfo: new MatchedSelfInfo("KOR","P","s","e","d",0,0,1L,1L),
OppoInfo: new MatchedOppoInfo("JPN","O","s","e","d",0,0,1L,1L,30),
SelfInfo: new MatchedSelfInfo("KOR","P","s","e","d",0,false,1L,1L),
OppoInfo: new MatchedOppoInfo("JPN","O","s","e","d",0,false,1L,1L,30),
SelfDeck: System.Array.Empty<DeckCardRef>());
Assert.That(body.ResultCode, Is.EqualTo(1));
@@ -81,8 +81,8 @@ public class MatchedBodyTests
public void SelfDeck_SerializesAsArray_WithIdxAndCardIdKeys()
{
var body = new MatchedBody(
SelfInfo: new MatchedSelfInfo("KOR","P","s","e","d",0,0,1L,1L),
OppoInfo: new MatchedOppoInfo("JPN","O","s","e","d",0,0,1L,1L,30),
SelfInfo: new MatchedSelfInfo("KOR","P","s","e","d",0,false,1L,1L),
OppoInfo: new MatchedOppoInfo("JPN","O","s","e","d",0,false,1L,1L,30),
SelfDeck: new[]
{
new DeckCardRef(Idx: 1, CardId: 100011010L),

View File

@@ -0,0 +1,51 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using NUnit.Framework;
using SVSim.BattleNode.Protocol;
namespace SVSim.UnitTests.BattleNode.Protocol;
[TestFixture]
public class NumericBoolJsonConverterTests
{
private sealed record Probe(
[property: JsonPropertyName("flag")]
[property: JsonConverter(typeof(NumericBoolJsonConverter))]
bool Flag,
[property: JsonPropertyName("opt")]
[property: JsonConverter(typeof(NumericBoolJsonConverter))]
bool? Opt = null);
private static readonly JsonSerializerOptions Options = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
[Test]
public void Writes_true_as_numeric_1_and_false_as_numeric_0()
{
var node = JsonSerializer.SerializeToElement(new Probe(Flag: true), Options);
Assert.That(node.GetProperty("flag").ValueKind, Is.EqualTo(JsonValueKind.Number));
Assert.That(node.GetProperty("flag").GetInt32(), Is.EqualTo(1));
var falseNode = JsonSerializer.SerializeToElement(new Probe(Flag: false), Options);
Assert.That(falseNode.GetProperty("flag").GetInt32(), Is.EqualTo(0));
}
[Test]
public void Reads_numeric_0_and_1_back_to_bool()
{
Assert.That(JsonSerializer.Deserialize<Probe>("{\"flag\":1}", Options)!.Flag, Is.True);
Assert.That(JsonSerializer.Deserialize<Probe>("{\"flag\":0}", Options)!.Flag, Is.False);
}
[Test]
public void Nullable_true_emits_1_and_null_is_omitted()
{
var present = JsonSerializer.SerializeToElement(new Probe(Flag: false, Opt: true), Options);
Assert.That(present.GetProperty("opt").GetInt32(), Is.EqualTo(1));
var absent = JsonSerializer.SerializeToElement(new Probe(Flag: false, Opt: null), Options);
Assert.That(absent.TryGetProperty("opt", out _), Is.False, "null bool? must be omitted, not emitted");
}
}

View File

@@ -21,7 +21,7 @@ public class BattleSessionDispatchTests
var routes = s.ComputeFrames(a, NewEnvelope(NetworkBattleUri.Loaded));
var bs = (BattleStartBody)routes[0].Frame.Body;
Assert.That(bs.TurnState, Is.EqualTo(0), "A (first arriver) goes first.");
Assert.That(bs.TurnState, Is.EqualTo(TurnState.First), "A (first arriver) goes first.");
}
[Test]
@@ -33,7 +33,7 @@ public class BattleSessionDispatchTests
var routes = s.ComputeFrames(b, NewEnvelope(NetworkBattleUri.Loaded));
var bs = (BattleStartBody)routes[0].Frame.Body;
Assert.That(bs.TurnState, Is.EqualTo(1), "B (second arriver) goes second.");
Assert.That(bs.TurnState, Is.EqualTo(TurnState.Second), "B (second arriver) goes second.");
}
[Test]
@@ -281,7 +281,7 @@ public class BattleSessionDispatchTests
Assert.That(pb.OppoTargetList!.Count, Is.EqualTo(1));
Assert.That(pb.OppoTargetList[0].TargetIdx, Is.EqualTo(8));
Assert.That(pb.OppoTargetList[0].IsSelf, Is.EqualTo(0));
Assert.That(pb.OppoTargetList[0].IsSelf, Is.EqualTo(CardOwner.Opponent));
}
[Test]
@@ -316,7 +316,7 @@ public class BattleSessionDispatchTests
Assert.That(pb.UList[0].IdxList, Is.EqualTo(new[] { 16, 22 }));
Assert.That(pb.UList[0].From, Is.EqualTo(0));
Assert.That(pb.UList[0].To, Is.EqualTo(10));
Assert.That(pb.UList[0].IsSelf, Is.EqualTo(1));
Assert.That(pb.UList[0].IsSelf, Is.EqualTo(CardOwner.Self));
Assert.That(pb.UList[0].Skill, Is.EqualTo("37|36|0"));
}
@@ -807,7 +807,7 @@ public class BattleSessionDispatchTests
Assert.That(routes[0].Target, Is.SameAs(b));
Assert.That(routes[0].Frame.Uri, Is.EqualTo(NetworkBattleUri.TurnEnd));
var body = (SVSim.BattleNode.Protocol.Bodies.TurnEndBody)routes[0].Frame.Body;
Assert.That(body.TurnState, Is.EqualTo(0));
Assert.That(body.TurnState, Is.EqualTo(TurnState.First));
}
[Test]

View File

@@ -1,4 +1,5 @@
using NUnit.Framework;
using SVSim.BattleNode.Protocol;
using SVSim.BattleNode.Protocol.Bodies;
using SVSim.BattleNode.Sessions.Dispatch;
@@ -103,7 +104,7 @@ public class KnownListBuilderTests
Assert.That(renamed, Is.Not.Null);
Assert.That(renamed!.Count, Is.EqualTo(1));
Assert.That(renamed[0].TargetIdx, Is.EqualTo(8));
Assert.That(renamed[0].IsSelf, Is.EqualTo(0));
Assert.That(renamed[0].IsSelf, Is.EqualTo(CardOwner.Opponent));
}
[Test]
@@ -316,7 +317,7 @@ public class KnownListBuilderTests
Assert.That(stripped![0].SelectCard, Is.Not.Null);
Assert.That(stripped[0].SelectCard!.CardId, Is.EqualTo(new[] { 810041260L }));
Assert.That(stripped[0].SelectCard.Open, Is.EqualTo(1));
Assert.That(stripped[0].SelectCard.Open, Is.EqualTo(ChoiceVisibility.Open));
}
[Test]
@@ -447,7 +448,7 @@ public class KnownListBuilderTests
Assert.That(e.IdxList, Is.EqualTo(new[] { 16, 22 }));
Assert.That(e.From, Is.EqualTo(0));
Assert.That(e.To, Is.EqualTo(10));
Assert.That(e.IsSelf, Is.EqualTo(1));
Assert.That(e.IsSelf, Is.EqualTo(CardOwner.Self));
Assert.That(e.Skill, Is.EqualTo("37|36|0"));
Assert.That(e.CardId, Is.Null);
Assert.That(e.Clan, Is.Null);
@@ -479,7 +480,7 @@ public class KnownListBuilderTests
Assert.That(e.Cost, Is.EqualTo(2));
Assert.That(e.SkillKeyCardIdx, Is.EqualTo(new[] { 7 }));
Assert.That(e.RandomTargetIdx, Is.EqualTo(new[] { 2, 3 }));
Assert.That(e.IsInvoke, Is.EqualTo(1));
Assert.That(e.IsInvoke, Is.True);
Assert.That(e.AttachTarget, Is.EqualTo("12,13"));
}
@@ -496,7 +497,7 @@ public class KnownListBuilderTests
Assert.That(relayed!.Count, Is.EqualTo(2));
Assert.That(relayed[0].Skill, Is.EqualTo("a"));
Assert.That(relayed[1].Skill, Is.EqualTo("b"));
Assert.That(relayed[1].IsSelf, Is.EqualTo(0));
Assert.That(relayed[1].IsSelf, Is.EqualTo(CardOwner.Opponent));
}
[Test]