feat(battle-node): typed Deal/Swap/Ready bodies + PosIdx

This commit is contained in:
gamer147
2026-06-01 10:34:44 -04:00
parent 78a6fe93fb
commit 97b9b6fe42
5 changed files with 87 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
using System.Text.Json.Serialization;
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;

View File

@@ -0,0 +1,7 @@
using System.Text.Json.Serialization;
namespace SVSim.BattleNode.Protocol.Bodies;
public sealed record PosIdx(
[property: JsonPropertyName("pos")] int Pos,
[property: JsonPropertyName("idx")] int Idx);

View File

@@ -0,0 +1,10 @@
using System.Text.Json.Serialization;
namespace SVSim.BattleNode.Protocol.Bodies;
public sealed record ReadyBody(
[property: JsonPropertyName("self")] IReadOnlyList<PosIdx> Self,
[property: JsonPropertyName("oppo")] IReadOnlyList<PosIdx> Oppo,
[property: JsonPropertyName("idxChangeSeed")] int IdxChangeSeed,
[property: JsonPropertyName("spin")] int Spin,
[property: JsonPropertyName("resultCode")] int ResultCode = 1) : IMsgBody;

View File

@@ -0,0 +1,7 @@
using System.Text.Json.Serialization;
namespace SVSim.BattleNode.Protocol.Bodies;
public sealed record SwapResponseBody(
[property: JsonPropertyName("self")] IReadOnlyList<PosIdx> Self,
[property: JsonPropertyName("resultCode")] int ResultCode = 1) : IMsgBody;

View File

@@ -0,0 +1,55 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using NUnit.Framework;
using SVSim.BattleNode.Protocol.Bodies;
namespace SVSim.UnitTests.BattleNode.Protocol.Bodies;
[TestFixture]
public class HandBodiesTests
{
[Test]
public void DealBody_SerializesSelfAndOppoArrays_WithPosIdxKeys()
{
var body = new DealBody(
Self: new[] { new PosIdx(0, 1), new PosIdx(1, 2), new PosIdx(2, 3) },
Oppo: new[] { new PosIdx(0, 1), new PosIdx(1, 2), new PosIdx(2, 3) });
var node = (JsonObject)JsonSerializer.SerializeToNode(body)!;
var self = (JsonArray)node["self"]!;
Assert.That(self.Count, Is.EqualTo(3));
Assert.That(((JsonObject)self[0]!)["pos"]!.GetValue<int>(), Is.EqualTo(0));
Assert.That(((JsonObject)self[0]!)["idx"]!.GetValue<int>(), Is.EqualTo(1));
Assert.That(node["resultCode"]!.GetValue<int>(), Is.EqualTo(1)); // default
}
[Test]
public void SwapResponseBody_OnlyContainsSelf_NotOppo()
{
var body = new SwapResponseBody(
Self: new[] { new PosIdx(0, 1), new PosIdx(1, 4), new PosIdx(2, 3) });
var node = (JsonObject)JsonSerializer.SerializeToNode(body)!;
Assert.That(node.ContainsKey("self"), Is.True);
Assert.That(node.ContainsKey("oppo"), Is.False);
Assert.That(((JsonObject)((JsonArray)node["self"]!)[1]!)["idx"]!.GetValue<int>(), Is.EqualTo(4));
}
[Test]
public void ReadyBody_SerializesAllFields_IncludingIdxChangeSeedAndSpin()
{
var body = new ReadyBody(
Self: new[] { new PosIdx(0, 1) },
Oppo: new[] { new PosIdx(0, 1) },
IdxChangeSeed: 771_335_280,
Spin: 243);
var node = (JsonObject)JsonSerializer.SerializeToNode(body)!;
Assert.That(node["idxChangeSeed"]!.GetValue<int>(), Is.EqualTo(771_335_280));
Assert.That(node["spin"]!.GetValue<int>(), Is.EqualTo(243));
Assert.That(node["resultCode"]!.GetValue<int>(), Is.EqualTo(1));
}
}