feat(battle-node): typed Deal/Swap/Ready bodies + PosIdx
This commit is contained in:
8
SVSim.BattleNode/Protocol/Bodies/DealBody.cs
Normal file
8
SVSim.BattleNode/Protocol/Bodies/DealBody.cs
Normal 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;
|
||||
7
SVSim.BattleNode/Protocol/Bodies/PosIdx.cs
Normal file
7
SVSim.BattleNode/Protocol/Bodies/PosIdx.cs
Normal 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);
|
||||
10
SVSim.BattleNode/Protocol/Bodies/ReadyBody.cs
Normal file
10
SVSim.BattleNode/Protocol/Bodies/ReadyBody.cs
Normal 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;
|
||||
7
SVSim.BattleNode/Protocol/Bodies/SwapResponseBody.cs
Normal file
7
SVSim.BattleNode/Protocol/Bodies/SwapResponseBody.cs
Normal 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;
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user