Use unit designations for order history instead of references

This commit is contained in:
Tim Van Baak 2024-08-14 18:45:35 -07:00
parent 5b5320b3e2
commit 8f5dc63833
3 changed files with 24 additions and 10 deletions

View File

@ -391,10 +391,10 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
OrderHistory history = newHistory[unitOrder.Unit.Season.Designation];
// TODO does this add every order to every season??
history.Orders.Add(unitOrder);
history.IsDislodgedOutcomes[unitOrder.Unit] = dislodges[unitOrder.Unit].Outcome == true;
history.IsDislodgedOutcomes[unitOrder.Unit.Designation] = dislodges[unitOrder.Unit].Outcome == true;
if (unitOrder is MoveOrder moveOrder)
{
history.DoesMoveOutcomes[moveOrder] = moves[moveOrder].Outcome == true;
history.DoesMoveOutcomes[moveOrder.Unit.Designation] = moves[moveOrder].Outcome == true;
}
}
@ -494,7 +494,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
IEnumerable<MoveOrder> newIncomingMoves = decision.Orders
.OfType<MoveOrder>()
.Where(order => order.Season == decision.Season
&& !world.OrderHistory[order.Season.Designation].DoesMoveOutcomes.ContainsKey(order));
&& !world.OrderHistory[order.Season.Designation].DoesMoveOutcomes.ContainsKey(order.Unit.Designation));
foreach (MoveOrder moveOrder in newIncomingMoves)
{
DoesMove doesMove = decisions.DoesMove[moveOrder];
@ -518,7 +518,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
// TODO these aren't timeline-specific
IsDislodged dislodged = decisions.IsDislodged[order.Unit];
progress |= ResolveDecision(dislodged, world, decisions, depth + 1);
if (history.IsDislodgedOutcomes.TryGetValue(order.Unit, out bool previous)
if (history.IsDislodgedOutcomes.TryGetValue(order.Unit.Designation, out bool previous)
&& dislodged.Resolved
&& dislodged.Outcome != previous)
{
@ -535,7 +535,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
{
DoesMove moves = decisions.DoesMove[moveOrder];
progress |= ResolveDecision(moves, world, decisions, depth + 1);
if (history.DoesMoveOutcomes.TryGetValue(moveOrder, out bool previousMove)
if (history.DoesMoveOutcomes.TryGetValue(moveOrder.Unit.Designation, out bool previousMove)
&& moves.Resolved
&& moves.Outcome != previousMove)
if (moves.Resolved && moves.Outcome != previousMove)

View File

@ -6,11 +6,17 @@ namespace MultiversalDiplomacy.Model;
public class OrderHistory
{
public List<UnitOrder> Orders;
public List<UnitOrder> Orders { get; }
public Dictionary<Unit, bool> IsDislodgedOutcomes;
/// <summary>
/// Map from unit designation to dislodge outcome.
/// </summary>
public Dictionary<string, bool> IsDislodgedOutcomes { get; }
public Dictionary<MoveOrder, bool> DoesMoveOutcomes;
/// <summary>
/// Map from designation of the ordered unit to move outcome.
/// </summary>
public Dictionary<string, bool> DoesMoveOutcomes { get; }
public OrderHistory()
: this([], [], [])
@ -18,8 +24,8 @@ public class OrderHistory
public OrderHistory(
List<UnitOrder> orders,
Dictionary<Unit, bool> isDislodgedOutcomes,
Dictionary<MoveOrder, bool> doesMoveOutcomes)
Dictionary<string, bool> isDislodgedOutcomes,
Dictionary<string, bool> doesMoveOutcomes)
{
this.Orders = new(orders);
this.IsDislodgedOutcomes = new(isDislodgedOutcomes);

View File

@ -64,12 +64,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));
// 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));
// Resume the test case
setup = new(reserialized, MovementPhaseAdjudicator.Instance);
setup[("a", 1)]