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)
{
/// <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>
public const int FIRST_TURN = 0;
public static readonly Season First = new(Timelines.IntToString(0), 0);
/// <summary>
/// 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.
/// </summary>
public static Timelines Create()
{
Season first = new(IntToString(0), Season.FIRST_TURN);
return new Timelines(1, new() { {first.Key, null} });
}
=> new(StringToInt(Season.First.Timeline) + 1, new() { {Season.First.Key, null} });
/// <summary>
/// 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
? Map.GetWater(splits[2])
: 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 this.Update(units: units);
@ -229,7 +229,7 @@ public class World
public Unit GetUnitAt(string provinceName, Season? season = null)
{
Province province = Map.GetProvince(provinceName);
season ??= new("a0");
season ??= Season.First;
Unit? foundUnit = this.Units.SingleOrDefault(
u => Map.GetLocation(u!).Province == province && u!.Season == season,
null)

View File

@ -36,12 +36,12 @@ public class TimelineFactoryTest
public void NoSharedFactoryState()
{
Timelines one = Timelines.Create()
.WithNewSeason(new Season("a0"), out var s1)
.WithNewSeason(new Season("a0"), out var s2)
.WithNewSeason(new Season("a0"), out var s3);
.WithNewSeason(Season.First, out var s1)
.WithNewSeason(Season.First, out var s2)
.WithNewSeason(Season.First, out var s3);
Timelines two = Timelines.Create()
.WithNewSeason(new Season("a0"), out var s4)
.WithNewSeason(new Season("a0"), out var s5);
.WithNewSeason(Season.First, out var s4)
.WithNewSeason(Season.First, out var s5);
Assert.That(s1.Timeline, Is.EqualTo("a"));
Assert.That(s2.Timeline, Is.EqualTo("b"));
@ -54,14 +54,14 @@ public class TimelineFactoryTest
public void TimelineForking()
{
Timelines timelines = Timelines.Create()
.WithNewSeason("a0", out var a1)
.WithNewSeason(Season.First, out var a1)
.WithNewSeason(a1, out var a2)
.WithNewSeason(a2, out var a3)
.WithNewSeason(a1, out var b2)
.WithNewSeason(b2, out var b3)
.WithNewSeason(a1, out var c2)
.WithNewSeason(a2, out var d3);
Season a0 = new("a0");
Season a0 = Season.First;
Assert.That(
timelines.Pasts.Keys,
@ -76,13 +76,13 @@ public class TimelineFactoryTest
Assert.That(c2.Timeline, Is.EqualTo("c"), "Unexpected second 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(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(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(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(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(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(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(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(a3), Is.EqualTo(a0), "Expected trunk timeline to have root");

View File

@ -65,9 +65,9 @@ public class SerializationTest
Assert.That(tyr0, Is.NotDislodged);
setup.UpdateWorld();
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");
Assert.That(setup.World.OrderHistory[s0.Key].Orders.Count, Is.GreaterThan(0), "Missing orders");
Assert.That(setup.World.OrderHistory[s0.Key].DoesMoveOutcomes.Count, Is.GreaterThan(0), "Missing moves");
Assert.That(setup.World.OrderHistory[s0.Key].IsDislodgedOutcomes.Count, Is.GreaterThan(0), "Missing dislodges");
// Serialize and deserialize the world
string serialized = JsonSerializer.Serialize(setup.World, Options);
@ -75,9 +75,9 @@ public class SerializationTest
?? throw new AssertionException("Failed to reserialize world");
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");
Assert.That(reserialized.OrderHistory[s0.Key].Orders.Count, Is.GreaterThan(0), "Missing orders");
Assert.That(reserialized.OrderHistory[s0.Key].DoesMoveOutcomes.Count, Is.GreaterThan(0), "Missing moves");
Assert.That(reserialized.OrderHistory[s0.Key].IsDislodgedOutcomes.Count, Is.GreaterThan(0), "Missing dislodges");
});
Assert.Ignore("Serialization doesn't fully work yet");
@ -86,7 +86,7 @@ public class SerializationTest
setup = new(reserialized, MovementPhaseAdjudicator.Instance);
setup[("a", 1)]
["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"]
.Army("Tyr").Holds();

View File

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