From 73d849e117dc993107cccc828f543455f039bcb4 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 14 Aug 2024 09:51:18 -0700 Subject: [PATCH] Working World roundtrip serialization There are probably still reference equality issues here since Unit still has Season and Power objects. The test case builder also still works on reference equality in some places so the second part of adjudication is broken. --- MultiversalDiplomacy/Model/Map.cs | 4 ++-- MultiversalDiplomacy/Model/Unit.cs | 5 ++++- MultiversalDiplomacyTests/SerializationTest.cs | 6 ++---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/MultiversalDiplomacy/Model/Map.cs b/MultiversalDiplomacy/Model/Map.cs index cdd4a91..0597dc4 100644 --- a/MultiversalDiplomacy/Model/Map.cs +++ b/MultiversalDiplomacy/Model/Map.cs @@ -88,12 +88,12 @@ public class Map /// public Power GetPower(string powerName) => Powers.SingleOrDefault(p => p!.Name == powerName || p.Name.StartsWith(powerName), null) - ?? throw new KeyNotFoundException($"Power {powerName} not found"); + ?? throw new KeyNotFoundException($"Power {powerName} not found (powers: {string.Join(", ", Powers)})"); public static Map FromType(MapType type) => type switch { MapType.Test => Test, - MapType.Classical => Test, + MapType.Classical => Classical, _ => throw new NotImplementedException($"Unknown variant {type}"), }; diff --git a/MultiversalDiplomacy/Model/Unit.cs b/MultiversalDiplomacy/Model/Unit.cs index e1bda17..07ccd17 100644 --- a/MultiversalDiplomacy/Model/Unit.cs +++ b/MultiversalDiplomacy/Model/Unit.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace MultiversalDiplomacy.Model; /// @@ -33,9 +35,10 @@ public class Unit /// /// A unique designation for this unit. /// + [JsonIgnore] public string Designation => $"{Type.ToShort()} {Season.Timeline}-{Location}@{Season.Turn}"; - private Unit(string? past, string location, Season season, Power power, UnitType type) + public Unit(string? past, string location, Season season, Power power, UnitType type) { this.Past = past; this.Location = location; diff --git a/MultiversalDiplomacyTests/SerializationTest.cs b/MultiversalDiplomacyTests/SerializationTest.cs index 9bc1095..8a34f3d 100644 --- a/MultiversalDiplomacyTests/SerializationTest.cs +++ b/MultiversalDiplomacyTests/SerializationTest.cs @@ -64,11 +64,9 @@ public class SerializationTest Assert.That(tyr0, Is.NotDislodged); setup.UpdateWorld(); - // Serialize the world + // Serialize and deserialize the world string serialized = JsonSerializer.Serialize(setup.World, Options); - - // Deserialize the world - Console.WriteLine(serialized); + // Console.WriteLine(serialized); World reserialized = JsonSerializer.Deserialize(serialized, Options) ?? throw new AssertionException("Failed to reserialize world");