Remove Unit.Past
Past is only used in tests. It was added speculatively in expectation that it would be useful for clients. Removing this means that all the fields of a Unit are present in the key, so the key can uniquely determine the Unit or even be parsed into one, and therefore multiple Unit instances can be equivalent if their fields match. If unit pasts are useful later, they can be tracked extrinsically the way timelines are tracked.
This commit is contained in:
parent
4ce5faaac2
commit
1eeb5115e6
|
@ -7,11 +7,6 @@ namespace MultiversalDiplomacy.Model;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Unit
|
public class Unit
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// The previous iteration of a unit. This is null if the unit was just built.
|
|
||||||
/// </summary>
|
|
||||||
public string? Past { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The location on the map where the unit is.
|
/// The location on the map where the unit is.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -38,9 +33,8 @@ public class Unit
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public string Key => $"{Type.ToShort()} {Season.Timeline}-{Location}@{Season.Turn}";
|
public string Key => $"{Type.ToShort()} {Season.Timeline}-{Location}@{Season.Turn}";
|
||||||
|
|
||||||
public Unit(string? past, string location, Season season, string power, UnitType type)
|
public Unit(string location, Season season, string power, UnitType type)
|
||||||
{
|
{
|
||||||
this.Past = past;
|
|
||||||
this.Location = location;
|
this.Location = location;
|
||||||
this.Season = season;
|
this.Season = season;
|
||||||
this.Power = power;
|
this.Power = power;
|
||||||
|
@ -55,13 +49,13 @@ public class Unit
|
||||||
/// method after accepting a build order.
|
/// method after accepting a build order.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static Unit Build(string location, Season season, string power, UnitType type)
|
public static Unit Build(string location, Season season, string power, UnitType type)
|
||||||
=> new(past: null, location, season, power, type);
|
=> new(location, season, power, type);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Advance this unit's timeline to a new location and season.
|
/// Advance this unit's timeline to a new location and season.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Unit Next(string location, Season season)
|
public Unit Next(string location, Season season)
|
||||||
=> new(past: this.Key, location, season, this.Power, this.Type);
|
=> new(location, season, this.Power, this.Type);
|
||||||
|
|
||||||
public Location GetLocation(World world) => world.Map.GetLocation(Location);
|
public Location GetLocation(World world) => world.Map.GetLocation(Location);
|
||||||
|
|
||||||
|
|
|
@ -47,12 +47,9 @@ public class TimeTravelTest
|
||||||
Unit originalUnit = world.GetUnitAt("Mun", s0);
|
Unit originalUnit = world.GetUnitAt("Mun", s0);
|
||||||
Unit aMun0 = world.GetUnitAt("Mun", s1);
|
Unit aMun0 = world.GetUnitAt("Mun", s1);
|
||||||
Unit aTyr = world.GetUnitAt("Tyr", fork);
|
Unit aTyr = world.GetUnitAt("Tyr", fork);
|
||||||
Assert.That(aTyr.Past, Is.EqualTo(mun1.Order.Unit.Key));
|
|
||||||
Assert.That(world.GetUnitByKey(aTyr.Past!).Past, Is.EqualTo(mun0.Order.Unit.Key));
|
|
||||||
|
|
||||||
// Confirm that there is a unit in Mun b1 originating from Mun a0
|
// Confirm that there is a unit in Mun b1 originating from Mun a0
|
||||||
Unit aMun1 = world.GetUnitAt("Mun", fork);
|
Unit aMun1 = world.GetUnitAt("Mun", fork);
|
||||||
Assert.That(aMun1.Past, Is.EqualTo(originalUnit.Key));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
@ -94,10 +91,6 @@ public class TimeTravelTest
|
||||||
World world = setup.UpdateWorld();
|
World world = setup.UpdateWorld();
|
||||||
Season fork = new("b1");
|
Season fork = new("b1");
|
||||||
Unit tyr1 = world.GetUnitAt("Tyr", fork);
|
Unit tyr1 = world.GetUnitAt("Tyr", fork);
|
||||||
Assert.That(
|
|
||||||
tyr1.Past,
|
|
||||||
Is.EqualTo(mun0.Order.Unit.Key),
|
|
||||||
"Expected A Mun a0 to advance to Tyr b1");
|
|
||||||
Assert.That(
|
Assert.That(
|
||||||
world.RetreatingUnits.Count,
|
world.RetreatingUnits.Count,
|
||||||
Is.EqualTo(1),
|
Is.EqualTo(1),
|
||||||
|
|
|
@ -171,7 +171,7 @@ public class MovementAdjudicatorTest
|
||||||
|
|
||||||
// Confirm the unit was created
|
// Confirm the unit was created
|
||||||
Assert.That(updated.Units.Count, Is.EqualTo(2));
|
Assert.That(updated.Units.Count, Is.EqualTo(2));
|
||||||
Unit second = updated.Units.Single(u => u.Past != null);
|
Unit second = updated.Units.OrderBy(u => -u.Season.Turn).First();
|
||||||
Assert.That(second.Location, Is.EqualTo(mun.Order.Unit.Location));
|
Assert.That(second.Location, Is.EqualTo(mun.Order.Unit.Location));
|
||||||
Assert.That(second.Season.Timeline, Is.EqualTo(mun.Order.Unit.Season.Timeline));
|
Assert.That(second.Season.Timeline, Is.EqualTo(mun.Order.Unit.Season.Timeline));
|
||||||
|
|
||||||
|
@ -208,7 +208,6 @@ public class MovementAdjudicatorTest
|
||||||
Unit u2 = updated.GetUnitAt("Mun", s2);
|
Unit u2 = updated.GetUnitAt("Mun", s2);
|
||||||
Assert.That(updated.Units.Count, Is.EqualTo(2));
|
Assert.That(updated.Units.Count, Is.EqualTo(2));
|
||||||
Assert.That(u2.Key, Is.Not.EqualTo(mun1.Order.Unit.Key));
|
Assert.That(u2.Key, Is.Not.EqualTo(mun1.Order.Unit.Key));
|
||||||
Assert.That(u2.Past, Is.EqualTo(mun1.Order.Unit.Key));
|
|
||||||
Assert.That(u2.Season, Is.EqualTo(s2));
|
Assert.That(u2.Season, Is.EqualTo(s2));
|
||||||
|
|
||||||
setup[("a", 1)]
|
setup[("a", 1)]
|
||||||
|
@ -228,7 +227,6 @@ public class MovementAdjudicatorTest
|
||||||
updated = setup.UpdateWorld();
|
updated = setup.UpdateWorld();
|
||||||
Season s3 = new(s2.Timeline, s2.Turn + 1);
|
Season s3 = new(s2.Timeline, s2.Turn + 1);
|
||||||
Unit u3 = updated.GetUnitAt("Mun", s3);
|
Unit u3 = updated.GetUnitAt("Mun", s3);
|
||||||
Assert.That(u3.Past, Is.EqualTo(mun2.Order.Unit.Key));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
@ -258,7 +256,6 @@ public class MovementAdjudicatorTest
|
||||||
Unit u2 = updated.GetUnitAt("Tyr", s2);
|
Unit u2 = updated.GetUnitAt("Tyr", s2);
|
||||||
Assert.That(updated.Units.Count, Is.EqualTo(2));
|
Assert.That(updated.Units.Count, Is.EqualTo(2));
|
||||||
Assert.That(u2.Key, Is.Not.EqualTo(mun1.Order.Unit.Key));
|
Assert.That(u2.Key, Is.Not.EqualTo(mun1.Order.Unit.Key));
|
||||||
Assert.That(u2.Past, Is.EqualTo(mun1.Order.Unit.Key));
|
|
||||||
Assert.That(u2.Season, Is.EqualTo(s2));
|
Assert.That(u2.Season, Is.EqualTo(s2));
|
||||||
|
|
||||||
setup[("a", 1)]
|
setup[("a", 1)]
|
||||||
|
@ -278,6 +275,5 @@ public class MovementAdjudicatorTest
|
||||||
updated = setup.UpdateWorld();
|
updated = setup.UpdateWorld();
|
||||||
Season s3 = new(s2.Timeline, s2.Turn + 1);
|
Season s3 = new(s2.Timeline, s2.Turn + 1);
|
||||||
Unit u3 = updated.GetUnitAt("Mun", s3);
|
Unit u3 = updated.GetUnitAt("Mun", s3);
|
||||||
Assert.That(u3.Past, Is.EqualTo(u2.Key));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,10 +133,6 @@ public class SerializationTest
|
||||||
World world = setup.UpdateWorld();
|
World world = setup.UpdateWorld();
|
||||||
Season fork = new("b1");
|
Season fork = new("b1");
|
||||||
Unit tyr1 = world.GetUnitAt("Tyr", fork);
|
Unit tyr1 = world.GetUnitAt("Tyr", fork);
|
||||||
Assert.That(
|
|
||||||
tyr1.Past,
|
|
||||||
Is.EqualTo(mun0.Order.Unit.Key),
|
|
||||||
"Expected A Mun a0 to advance to Tyr b1");
|
|
||||||
Assert.That(
|
Assert.That(
|
||||||
world.RetreatingUnits.Count,
|
world.RetreatingUnits.Count,
|
||||||
Is.EqualTo(1),
|
Is.EqualTo(1),
|
||||||
|
|
|
@ -22,10 +22,6 @@ public class UnitTests
|
||||||
_ = world.WithNewSeason(a1, out Season a2);
|
_ = world.WithNewSeason(a1, out Season a2);
|
||||||
Unit u3 = u2.Next(Tyr.Key, a2);
|
Unit u3 = u2.Next(Tyr.Key, a2);
|
||||||
|
|
||||||
Assert.That(u3.Past, Is.EqualTo(u2.Key), "Missing unit past");
|
|
||||||
Assert.That(u2.Past, Is.EqualTo(u1.Key), "Missing unit past");
|
|
||||||
Assert.That(u1.Past, Is.Null, "Unexpected unit past");
|
|
||||||
|
|
||||||
Assert.That(u1.Season, Is.EqualTo(a0), "Unexpected unit season");
|
Assert.That(u1.Season, Is.EqualTo(a0), "Unexpected unit season");
|
||||||
Assert.That(u2.Season, Is.EqualTo(a1), "Unexpected unit season");
|
Assert.That(u2.Season, Is.EqualTo(a1), "Unexpected unit season");
|
||||||
Assert.That(u3.Season, Is.EqualTo(a2), "Unexpected unit season");
|
Assert.That(u3.Season, Is.EqualTo(a2), "Unexpected unit season");
|
||||||
|
|
Loading…
Reference in New Issue