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 = [];
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];
// TODO does this add every order to every season??
history.Orders.Add(unitOrder);

View File

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

View File

@ -1,3 +1,5 @@
using System.Text.Json.Serialization;
using MultiversalDiplomacy.Model;
namespace MultiversalDiplomacy.Orders;
@ -5,6 +7,16 @@ namespace MultiversalDiplomacy.Orders;
/// <summary>
/// A submitted action by a power.
/// </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
{
/// <summary>

View File

@ -1,3 +1,5 @@
using System.Text.Json.Serialization;
using MultiversalDiplomacy.Model;
namespace MultiversalDiplomacy.Orders;
@ -5,6 +7,14 @@ namespace MultiversalDiplomacy.Orders;
/// <summary>
/// An order given to a specific unit.
/// </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
{
/// <summary>

View File

@ -1,6 +1,7 @@
using System.Text.Json;
using MultiversalDiplomacy.Adjudicate;
using MultiversalDiplomacy.Adjudicate.Decision;
using MultiversalDiplomacy.Model;
using NUnit.Framework;
@ -64,19 +65,20 @@ public class SerializationTest
Assert.That(tyr0, Is.NotDislodged);
setup.UpdateWorld();
Assert.That(setup.World.OrderHistory["a0"].Orders.Count, Is.GreaterThan(0));
Assert.That(setup.World.OrderHistory["a0"].DoesMoveOutcomes.Count, Is.GreaterThan(0));
Assert.That(setup.World.OrderHistory["a0"].IsDislodgedOutcomes.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), "Missing moves");
Assert.That(setup.World.OrderHistory["a0"].IsDislodgedOutcomes.Count, Is.GreaterThan(0), "Missing dislodges");
// Serialize and deserialize the world
string serialized = JsonSerializer.Serialize(setup.World, Options);
// Console.WriteLine(serialized);
World reserialized = JsonSerializer.Deserialize<World>(serialized, Options)
?? throw new AssertionException("Failed to reserialize world");
Assert.That(reserialized.OrderHistory["a0"].Orders.Count, Is.GreaterThan(0));
Assert.That(reserialized.OrderHistory["a0"].DoesMoveOutcomes.Count, Is.GreaterThan(0));
Assert.That(reserialized.OrderHistory["a0"].IsDislodgedOutcomes.Count, Is.GreaterThan(0));
Assert.Multiple(() => {
Assert.That(reserialized.OrderHistory["a0"].Orders.Count, Is.GreaterThan(0), "Missing orders");
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
setup = new(reserialized, MovementPhaseAdjudicator.Instance);
@ -88,10 +90,13 @@ public class SerializationTest
setup.ValidateOrders();
Assert.That(mun1, Is.Valid);
setup.AdjudicateOrders();
var adjudications = setup.AdjudicateOrders();
Assert.That(mun1, Is.NotCut);
Assert.That(mun0, Is.Victorious);
Assert.That(tyr0, Is.Dislodged);
Console.WriteLine(string.Join(", ", adjudications.Select(a => a.ToString())));
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.
World world = setup.UpdateWorld();