Remove reference from Unit.Past

This commit is contained in:
Tim Van Baak 2024-08-14 08:39:19 -07:00
parent 53e208ec31
commit 7749d8df4e
6 changed files with 23 additions and 14 deletions

View File

@ -8,7 +8,7 @@ public class Unit
/// <summary>
/// The previous iteration of a unit. This is null if the unit was just built.
/// </summary>
public Unit? Past { get; }
public string? Past { get; }
/// <summary>
/// The location on the map where the unit is.
@ -30,7 +30,12 @@ public class Unit
/// </summary>
public UnitType Type { get; }
private Unit(Unit? past, string location, Season season, Power power, UnitType type)
/// <summary>
/// A unique designation for this unit.
/// </summary>
public string Designation => $"{Type.ToShort()} {Season.Timeline}-{Location}@{Season.Turn}";
private Unit(string? past, string location, Season season, Power power, UnitType type)
{
this.Past = past;
this.Location = location;
@ -53,5 +58,5 @@ public class Unit
/// Advance this unit's timeline to a new location and season.
/// </summary>
public Unit Next(string location, Season season)
=> new(past: this, location, season, this.Power, this.Type);
=> new(past: this.Designation, location, season, this.Power, this.Type);
}

View File

@ -332,4 +332,8 @@ public class World
?? throw new KeyNotFoundException($"Unit at {province} at {season} not found");
return foundUnit;
}
public Unit GetUnitByDesignation(string designation)
=> Units.SingleOrDefault(u => u!.Designation == designation, null)
?? throw new KeyNotFoundException($"Unit {designation} not found");
}

View File

@ -46,12 +46,12 @@ public class TimeTravelTest
Unit originalUnit = world.GetUnitAt("Mun", s0);
Unit aMun0 = world.GetUnitAt("Mun", s1);
Unit aTyr = world.GetUnitAt("Tyr", fork);
Assert.That(aTyr.Past, Is.EqualTo(mun1.Order.Unit));
Assert.That(aTyr.Past?.Past, Is.EqualTo(mun0.Order.Unit));
Assert.That(aTyr.Past, Is.EqualTo(mun1.Order.Unit.Designation));
Assert.That(world.GetUnitByDesignation(aTyr.Past!).Past, Is.EqualTo(mun0.Order.Unit.Designation));
// Confirm that there is a unit in Mun b1 originating from Mun a0
Unit aMun1 = world.GetUnitAt("Mun", fork);
Assert.That(aMun1.Past, Is.EqualTo(originalUnit));
Assert.That(aMun1.Past, Is.EqualTo(originalUnit.Designation));
}
[Test]
@ -95,7 +95,7 @@ public class TimeTravelTest
Unit tyr1 = world.GetUnitAt("Tyr", fork);
Assert.That(
tyr1.Past,
Is.EqualTo(mun0.Order.Unit),
Is.EqualTo(mun0.Order.Unit.Designation),
"Expected A Mun a0 to advance to Tyr b1");
Assert.That(
world.RetreatingUnits.Count,

View File

@ -209,7 +209,7 @@ public class MovementAdjudicatorTest
Unit u2 = updated.GetUnitAt("Mun", s2);
Assert.That(updated.Units.Count, Is.EqualTo(2));
Assert.That(u2, Is.Not.EqualTo(mun1.Order.Unit));
Assert.That(u2.Past, Is.EqualTo(mun1.Order.Unit));
Assert.That(u2.Past, Is.EqualTo(mun1.Order.Unit.Designation));
Assert.That(u2.Season, Is.EqualTo(s2));
setup[("a", 1)]
@ -229,7 +229,7 @@ public class MovementAdjudicatorTest
updated = setup.UpdateWorld();
Season s3 = updated.GetSeason(s2.Timeline, s2.Turn + 1);
Unit u3 = updated.GetUnitAt("Mun", s3);
Assert.That(u3.Past, Is.EqualTo(mun2.Order.Unit));
Assert.That(u3.Past, Is.EqualTo(mun2.Order.Unit.Designation));
}
[Test]
@ -259,7 +259,7 @@ public class MovementAdjudicatorTest
Unit u2 = updated.GetUnitAt("Tyr", s2);
Assert.That(updated.Units.Count, Is.EqualTo(2));
Assert.That(u2, Is.Not.EqualTo(mun1.Order.Unit));
Assert.That(u2.Past, Is.EqualTo(mun1.Order.Unit));
Assert.That(u2.Past, Is.EqualTo(mun1.Order.Unit.Designation));
Assert.That(u2.Season, Is.EqualTo(s2));
setup[("a", 1)]
@ -279,6 +279,6 @@ public class MovementAdjudicatorTest
updated = setup.UpdateWorld();
Season s3 = updated.GetSeason(s2.Timeline, s2.Turn + 1);
Unit u3 = updated.GetUnitAt("Mun", s3);
Assert.That(u3.Past, Is.EqualTo(u2));
Assert.That(u3.Past, Is.EqualTo(u2.Designation));
}
}

View File

@ -94,7 +94,7 @@ public class SerializationTest
Unit tyr1 = world.GetUnitAt("Tyr", fork);
Assert.That(
tyr1.Past,
Is.EqualTo(mun0.Order.Unit),
Is.EqualTo(mun0.Order.Unit.Designation),
"Expected A Mun a0 to advance to Tyr b1");
Assert.That(
world.RetreatingUnits.Count,

View File

@ -23,8 +23,8 @@ public class UnitTests
_ = world.ContinueOrFork(a1, out Season a2);
Unit u3 = u2.Next(Tyr.Designation, a2);
Assert.That(u3.Past, Is.EqualTo(u2), "Missing unit past");
Assert.That(u2.Past, Is.EqualTo(u1), "Missing unit past");
Assert.That(u3.Past, Is.EqualTo(u2.Designation), "Missing unit past");
Assert.That(u2.Past, Is.EqualTo(u1.Designation), "Missing unit past");
Assert.That(u1.Past, Is.Null, "Unexpected unit past");
Assert.That(u1.Season, Is.EqualTo(a0), "Unexpected unit season");