Get world serialization round trip kinda working

This commit is contained in:
Tim Van Baak 2024-08-14 21:12:58 -07:00
parent a4002a1081
commit 2484d4f0fd
5 changed files with 40 additions and 16 deletions

View File

@ -387,7 +387,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
Dictionary<string, OrderHistory> newHistory = []; Dictionary<string, OrderHistory> newHistory = [];
foreach (UnitOrder unitOrder in decisions.OfType<IsDislodged>().Select(d => d.Order)) foreach (UnitOrder unitOrder in decisions.OfType<IsDislodged>().Select(d => d.Order))
{ {
newHistory.Ensure(unitOrder.Unit.Season.Designation, () => new()); newHistory.Ensure(unitOrder.Unit.Season.Designation, () => new([], [], []));
OrderHistory history = newHistory[unitOrder.Unit.Season.Designation]; OrderHistory history = newHistory[unitOrder.Unit.Season.Designation];
// TODO does this add every order to every season?? // TODO does this add every order to every season??
history.Orders.Add(unitOrder); history.Orders.Add(unitOrder);

View File

@ -1,4 +1,4 @@
using System.Collections.ObjectModel; using System.Text.Json.Serialization;
using MultiversalDiplomacy.Orders; using MultiversalDiplomacy.Orders;
@ -18,10 +18,7 @@ public class OrderHistory
/// </summary> /// </summary>
public Dictionary<string, bool> DoesMoveOutcomes { get; } public Dictionary<string, bool> DoesMoveOutcomes { get; }
public OrderHistory() [JsonConstructor]
: this([], [], [])
{}
public OrderHistory( public OrderHistory(
List<UnitOrder> orders, List<UnitOrder> orders,
Dictionary<string, bool> isDislodgedOutcomes, Dictionary<string, bool> isDislodgedOutcomes,

View File

@ -1,3 +1,5 @@
using System.Text.Json.Serialization;
using MultiversalDiplomacy.Model; using MultiversalDiplomacy.Model;
namespace MultiversalDiplomacy.Orders; namespace MultiversalDiplomacy.Orders;
@ -5,6 +7,16 @@ namespace MultiversalDiplomacy.Orders;
/// <summary> /// <summary>
/// A submitted action by a power. /// A submitted action by a power.
/// </summary> /// </summary>
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(BuildOrder), typeDiscriminator: "move")]
[JsonDerivedType(typeof(ConvoyOrder), typeDiscriminator: "convoy")]
[JsonDerivedType(typeof(DisbandOrder), typeDiscriminator: "disband")]
[JsonDerivedType(typeof(HoldOrder), typeDiscriminator: "hold")]
[JsonDerivedType(typeof(MoveOrder), typeDiscriminator: "move")]
[JsonDerivedType(typeof(RetreatOrder), typeDiscriminator: "retreat")]
[JsonDerivedType(typeof(SupportHoldOrder), typeDiscriminator: "supportHold")]
[JsonDerivedType(typeof(SupportMoveOrder), typeDiscriminator: "supportMove")]
[JsonDerivedType(typeof(SustainOrder), typeDiscriminator: "sustain")]
public abstract class Order public abstract class Order
{ {
/// <summary> /// <summary>

View File

@ -1,3 +1,5 @@
using System.Text.Json.Serialization;
using MultiversalDiplomacy.Model; using MultiversalDiplomacy.Model;
namespace MultiversalDiplomacy.Orders; namespace MultiversalDiplomacy.Orders;
@ -5,6 +7,14 @@ namespace MultiversalDiplomacy.Orders;
/// <summary> /// <summary>
/// An order given to a specific unit. /// An order given to a specific unit.
/// </summary> /// </summary>
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(ConvoyOrder), typeDiscriminator: "convoy")]
[JsonDerivedType(typeof(DisbandOrder), typeDiscriminator: "disband")]
[JsonDerivedType(typeof(HoldOrder), typeDiscriminator: "hold")]
[JsonDerivedType(typeof(MoveOrder), typeDiscriminator: "move")]
[JsonDerivedType(typeof(RetreatOrder), typeDiscriminator: "retreat")]
[JsonDerivedType(typeof(SupportHoldOrder), typeDiscriminator: "supportHold")]
[JsonDerivedType(typeof(SupportMoveOrder), typeDiscriminator: "supportMove")]
public abstract class UnitOrder : Order public abstract class UnitOrder : Order
{ {
/// <summary> /// <summary>

View File

@ -1,6 +1,7 @@
using System.Text.Json; using System.Text.Json;
using MultiversalDiplomacy.Adjudicate; using MultiversalDiplomacy.Adjudicate;
using MultiversalDiplomacy.Adjudicate.Decision;
using MultiversalDiplomacy.Model; using MultiversalDiplomacy.Model;
using NUnit.Framework; using NUnit.Framework;
@ -64,19 +65,20 @@ public class SerializationTest
Assert.That(tyr0, Is.NotDislodged); Assert.That(tyr0, Is.NotDislodged);
setup.UpdateWorld(); setup.UpdateWorld();
Assert.That(setup.World.OrderHistory["a0"].Orders.Count, Is.GreaterThan(0)); Assert.That(setup.World.OrderHistory["a0"].Orders.Count, Is.GreaterThan(0), "Missing orders");
Assert.That(setup.World.OrderHistory["a0"].DoesMoveOutcomes.Count, Is.GreaterThan(0)); Assert.That(setup.World.OrderHistory["a0"].DoesMoveOutcomes.Count, Is.GreaterThan(0), "Missing moves");
Assert.That(setup.World.OrderHistory["a0"].IsDislodgedOutcomes.Count, Is.GreaterThan(0)); Assert.That(setup.World.OrderHistory["a0"].IsDislodgedOutcomes.Count, Is.GreaterThan(0), "Missing dislodges");
// Serialize and deserialize the world // Serialize and deserialize the world
string serialized = JsonSerializer.Serialize(setup.World, Options); string serialized = JsonSerializer.Serialize(setup.World, Options);
// Console.WriteLine(serialized);
World reserialized = JsonSerializer.Deserialize<World>(serialized, Options) World reserialized = JsonSerializer.Deserialize<World>(serialized, Options)
?? throw new AssertionException("Failed to reserialize world"); ?? throw new AssertionException("Failed to reserialize world");
Assert.That(reserialized.OrderHistory["a0"].Orders.Count, Is.GreaterThan(0)); Assert.Multiple(() => {
Assert.That(reserialized.OrderHistory["a0"].DoesMoveOutcomes.Count, Is.GreaterThan(0)); Assert.That(reserialized.OrderHistory["a0"].Orders.Count, Is.GreaterThan(0), "Missing orders");
Assert.That(reserialized.OrderHistory["a0"].IsDislodgedOutcomes.Count, Is.GreaterThan(0)); Assert.That(reserialized.OrderHistory["a0"].DoesMoveOutcomes.Count, Is.GreaterThan(0), "Missing moves");
Assert.That(reserialized.OrderHistory["a0"].IsDislodgedOutcomes.Count, Is.GreaterThan(0), "Missing dislodges");
});
// Resume the test case // Resume the test case
setup = new(reserialized, MovementPhaseAdjudicator.Instance); setup = new(reserialized, MovementPhaseAdjudicator.Instance);
@ -88,10 +90,13 @@ public class SerializationTest
setup.ValidateOrders(); setup.ValidateOrders();
Assert.That(mun1, Is.Valid); Assert.That(mun1, Is.Valid);
setup.AdjudicateOrders(); var adjudications = setup.AdjudicateOrders();
Assert.That(mun1, Is.NotCut); Assert.That(mun1, Is.NotCut);
Assert.That(mun0, Is.Victorious); Console.WriteLine(string.Join(", ", adjudications.Select(a => a.ToString())));
Assert.That(tyr0, Is.Dislodged); DoesMove mun0move = adjudications.OfType<DoesMove>().Single(move => move.Order.Unit.Designation == mun0.Order.Unit.Designation);
Assert.That(mun0move.Outcome, Is.True);
IsDislodged tyr0dislodge = adjudications.OfType<IsDislodged>().Single(dis => dis.Order.Unit.Designation == tyr0.Order.Unit.Designation);
Assert.That(tyr0dislodge, Is.True);
// Confirm that an alternate future is created. // Confirm that an alternate future is created.
World world = setup.UpdateWorld(); World world = setup.UpdateWorld();