Add Season.First, replacing FIRST_TURN

This commit is contained in:
Tim Van Baak 2024-08-15 16:36:50 -07:00
parent 25d903d91a
commit 3d664208b5
6 changed files with 28 additions and 30 deletions

View File

@ -10,9 +10,10 @@ namespace MultiversalDiplomacy.Model;
public struct Season(string timeline, int turn) public struct Season(string timeline, int turn)
{ {
/// <summary> /// <summary>
/// The first turn number. This is defined to reduce confusion about whether the first turn is 0 or 1. /// The root season of every game. This is defined to avoid any confusion about what the first turn or timeline
/// should be or what season to use to key into a fresh <see cref="Timelines"/>.
/// </summary> /// </summary>
public const int FIRST_TURN = 0; public static readonly Season First = new(Timelines.IntToString(0), 0);
/// <summary> /// <summary>
/// The timeline to which this season belongs. /// The timeline to which this season belongs.

View File

@ -75,10 +75,7 @@ public class Timelines(int next, Dictionary<string, Season?> pasts)
/// Create a new multiverse with an initial season. /// Create a new multiverse with an initial season.
/// </summary> /// </summary>
public static Timelines Create() public static Timelines Create()
{ => new(StringToInt(Season.First.Timeline) + 1, new() { {Season.First.Key, null} });
Season first = new(IntToString(0), Season.FIRST_TURN);
return new Timelines(1, new() { {first.Key, null} });
}
/// <summary> /// <summary>
/// Create a continuation of a season if it has no futures, otherwise create a fork. /// Create a continuation of a season if it has no futures, otherwise create a fork.

View File

@ -181,7 +181,7 @@ public class World
: splits.Length == 3 : splits.Length == 3
? Map.GetWater(splits[2]) ? Map.GetWater(splits[2])
: Map.GetWater(splits[2], splits[3]); : Map.GetWater(splits[2], splits[3]);
Unit unit = Unit.Build(location.Key, new("a0"), power, type); Unit unit = Unit.Build(location.Key, Season.First, power, type);
return unit; return unit;
}); });
return this.Update(units: units); return this.Update(units: units);
@ -229,7 +229,7 @@ public class World
public Unit GetUnitAt(string provinceName, Season? season = null) public Unit GetUnitAt(string provinceName, Season? season = null)
{ {
Province province = Map.GetProvince(provinceName); Province province = Map.GetProvince(provinceName);
season ??= new("a0"); season ??= Season.First;
Unit? foundUnit = this.Units.SingleOrDefault( Unit? foundUnit = this.Units.SingleOrDefault(
u => Map.GetLocation(u!).Province == province && u!.Season == season, u => Map.GetLocation(u!).Province == province && u!.Season == season,
null) null)

View File

@ -36,12 +36,12 @@ public class TimelineFactoryTest
public void NoSharedFactoryState() public void NoSharedFactoryState()
{ {
Timelines one = Timelines.Create() Timelines one = Timelines.Create()
.WithNewSeason(new Season("a0"), out var s1) .WithNewSeason(Season.First, out var s1)
.WithNewSeason(new Season("a0"), out var s2) .WithNewSeason(Season.First, out var s2)
.WithNewSeason(new Season("a0"), out var s3); .WithNewSeason(Season.First, out var s3);
Timelines two = Timelines.Create() Timelines two = Timelines.Create()
.WithNewSeason(new Season("a0"), out var s4) .WithNewSeason(Season.First, out var s4)
.WithNewSeason(new Season("a0"), out var s5); .WithNewSeason(Season.First, out var s5);
Assert.That(s1.Timeline, Is.EqualTo("a")); Assert.That(s1.Timeline, Is.EqualTo("a"));
Assert.That(s2.Timeline, Is.EqualTo("b")); Assert.That(s2.Timeline, Is.EqualTo("b"));
@ -54,14 +54,14 @@ public class TimelineFactoryTest
public void TimelineForking() public void TimelineForking()
{ {
Timelines timelines = Timelines.Create() Timelines timelines = Timelines.Create()
.WithNewSeason("a0", out var a1) .WithNewSeason(Season.First, out var a1)
.WithNewSeason(a1, out var a2) .WithNewSeason(a1, out var a2)
.WithNewSeason(a2, out var a3) .WithNewSeason(a2, out var a3)
.WithNewSeason(a1, out var b2) .WithNewSeason(a1, out var b2)
.WithNewSeason(b2, out var b3) .WithNewSeason(b2, out var b3)
.WithNewSeason(a1, out var c2) .WithNewSeason(a1, out var c2)
.WithNewSeason(a2, out var d3); .WithNewSeason(a2, out var d3);
Season a0 = new("a0"); Season a0 = Season.First;
Assert.That( Assert.That(
timelines.Pasts.Keys, timelines.Pasts.Keys,
@ -76,13 +76,13 @@ public class TimelineFactoryTest
Assert.That(c2.Timeline, Is.EqualTo("c"), "Unexpected second alt"); Assert.That(c2.Timeline, Is.EqualTo("c"), "Unexpected second alt");
Assert.That(d3.Timeline, Is.EqualTo("d"), "Unexpected third alt"); Assert.That(d3.Timeline, Is.EqualTo("d"), "Unexpected third alt");
Assert.That(a1.Turn, Is.EqualTo(Season.FIRST_TURN + 1), "Unexpected a1 turn number"); Assert.That(a1.Turn, Is.EqualTo(Season.First.Turn + 1), "Unexpected a1 turn number");
Assert.That(a2.Turn, Is.EqualTo(Season.FIRST_TURN + 2), "Unexpected a2 turn number"); Assert.That(a2.Turn, Is.EqualTo(Season.First.Turn + 2), "Unexpected a2 turn number");
Assert.That(a3.Turn, Is.EqualTo(Season.FIRST_TURN + 3), "Unexpected a3 turn number"); Assert.That(a3.Turn, Is.EqualTo(Season.First.Turn + 3), "Unexpected a3 turn number");
Assert.That(b2.Turn, Is.EqualTo(Season.FIRST_TURN + 2), "Unexpected b2 turn number"); Assert.That(b2.Turn, Is.EqualTo(Season.First.Turn + 2), "Unexpected b2 turn number");
Assert.That(b3.Turn, Is.EqualTo(Season.FIRST_TURN + 3), "Unexpected b3 turn number"); Assert.That(b3.Turn, Is.EqualTo(Season.First.Turn + 3), "Unexpected b3 turn number");
Assert.That(c2.Turn, Is.EqualTo(Season.FIRST_TURN + 2), "Unexpected c2 turn number"); Assert.That(c2.Turn, Is.EqualTo(Season.First.Turn + 2), "Unexpected c2 turn number");
Assert.That(d3.Turn, Is.EqualTo(Season.FIRST_TURN + 3), "Unexpected d3 turn number"); Assert.That(d3.Turn, Is.EqualTo(Season.First.Turn + 3), "Unexpected d3 turn number");
Assert.That(timelines.GetTimelineRoot(a0), Is.EqualTo(a0), "Expected timeline root to be reflexive"); Assert.That(timelines.GetTimelineRoot(a0), Is.EqualTo(a0), "Expected timeline root to be reflexive");
Assert.That(timelines.GetTimelineRoot(a3), Is.EqualTo(a0), "Expected trunk timeline to have root"); Assert.That(timelines.GetTimelineRoot(a3), Is.EqualTo(a0), "Expected trunk timeline to have root");

View File

@ -65,9 +65,9 @@ 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), "Missing orders"); Assert.That(setup.World.OrderHistory[s0.Key].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[s0.Key].DoesMoveOutcomes.Count, Is.GreaterThan(0), "Missing moves");
Assert.That(setup.World.OrderHistory["a0"].IsDislodgedOutcomes.Count, Is.GreaterThan(0), "Missing dislodges"); Assert.That(setup.World.OrderHistory[s0.Key].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);
@ -75,9 +75,9 @@ public class SerializationTest
?? throw new AssertionException("Failed to reserialize world"); ?? throw new AssertionException("Failed to reserialize world");
Assert.Multiple(() => { Assert.Multiple(() => {
Assert.That(reserialized.OrderHistory["a0"].Orders.Count, Is.GreaterThan(0), "Missing orders"); Assert.That(reserialized.OrderHistory[s0.Key].Orders.Count, Is.GreaterThan(0), "Missing orders");
Assert.That(reserialized.OrderHistory["a0"].DoesMoveOutcomes.Count, Is.GreaterThan(0), "Missing moves"); Assert.That(reserialized.OrderHistory[s0.Key].DoesMoveOutcomes.Count, Is.GreaterThan(0), "Missing moves");
Assert.That(reserialized.OrderHistory["a0"].IsDislodgedOutcomes.Count, Is.GreaterThan(0), "Missing dislodges"); Assert.That(reserialized.OrderHistory[s0.Key].IsDislodgedOutcomes.Count, Is.GreaterThan(0), "Missing dislodges");
}); });
Assert.Ignore("Serialization doesn't fully work yet"); Assert.Ignore("Serialization doesn't fully work yet");
@ -86,7 +86,7 @@ public class SerializationTest
setup = new(reserialized, MovementPhaseAdjudicator.Instance); setup = new(reserialized, MovementPhaseAdjudicator.Instance);
setup[("a", 1)] setup[("a", 1)]
["Germany"] ["Germany"]
.Army("Mun").Supports.Army("Mun", season: new("a0")).MoveTo("Tyr").GetReference(out var mun1) .Army("Mun").Supports.Army("Mun", season: s0).MoveTo("Tyr").GetReference(out var mun1)
["Austria"] ["Austria"]
.Army("Tyr").Holds(); .Army("Tyr").Holds();

View File

@ -13,7 +13,7 @@ public class UnitTests
Location Mun = world.Map.GetLand("Mun"), Location Mun = world.Map.GetLand("Mun"),
Boh = world.Map.GetLand("Boh"), Boh = world.Map.GetLand("Boh"),
Tyr = world.Map.GetLand("Tyr"); Tyr = world.Map.GetLand("Tyr");
Season a0 = new("a0"); Season a0 = Season.First;
Unit u1 = Unit.Build(Mun.Key, a0, "Austria", UnitType.Army); Unit u1 = Unit.Build(Mun.Key, a0, "Austria", UnitType.Army);
world = world.WithNewSeason(a0, out Season a1); world = world.WithNewSeason(a0, out Season a1);