Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

feat(sim): office resolver and text placeholders

eyalgolan27cef22c5ebe
diff --git a/sim/Statecraft.Sim.Tests/OfficesTests.cs b/sim/Statecraft.Sim.Tests/OfficesTests.cs
new file mode 100644
index 000000000..c57e79853
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/OfficesTests.cs
@@ -0,0 +1,47 @@
+using System;
+using Statecraft.Sim;
+
+public class OfficesTests
+{
+ static readonly Content C = ContentLoader.Load(Fixture.Write());
+
+ [Fact]
+ public void ResolvesHolderOnDate()
+ {
+ Assert.Equal("pm_one", Offices.Holder(C, "defense_minister", new DateTime(1948, 12, 31))!.Id);
+ Assert.Equal("def_two", Offices.Holder(C, "defense_minister", new DateTime(1949, 1, 1))!.Id); // 'to' is exclusive
+ }
+
+ [Fact]
+ public void NoHolderBeforeFirstTerm() => Assert.Null(Offices.Holder(C, "prime_minister", new DateTime(1948, 1, 1)));
+
+ [Fact]
+ public void CurrentLegislatureAndGovernment()
+ {
+ Assert.Equal("council", Offices.Legislature(C, new DateTime(1948, 6, 1))!.Id);
+ Assert.Equal("house", Offices.Legislature(C, new DateTime(1949, 2, 14))!.Id);
+ Assert.Equal("gov2", Offices.Government(C, new DateTime(1949, 3, 10))!.Id);
+ }
+
+ [Fact]
+ public void FillsOfficePlaceholders()
+ {
+ var text = Offices.Fill(C, "{defense_minister} briefs {prime_minister}.", new DateTime(1949, 6, 1));
+ Assert.Equal("Def Two briefs Pm One.", text);
+ }
+
+ [Fact]
+ public void UnfilledPlaceholderThrows() =>
+ Assert.Throws<InvalidOperationException>(() => Offices.Fill(C, "{prime_minister}", new DateTime(1947, 1, 1)));
+
+ [Fact]
+ public void TwoPeopleCannotHoldOneOfficeOnOverlappingDates()
+ {
+ var bad = Fixture.Files["people.yaml"].Replace("to: 1949-01-01", "to: 1949-06-01");
+ var ex = Assert.Throws<ContentException>(() => ContentLoader.Load(Fixture.Write(new System.Collections.Generic.Dictionary<string, string> { ["people.yaml"] = bad })));
+ Assert.Equal("people.yaml", ex.File);
+ Assert.Equal("offices.defense_minister", ex.Field);
+ Assert.Contains("pm_one", ex.Message);
+ Assert.Contains("def_two", ex.Message);
+ }
+}
diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.cs
index e21775e52..a7eda84bb 100644
--- a/sim/Statecraft.Sim/ContentLoader.cs
+++ b/sim/Statecraft.Sim/ContentLoader.cs
@@ -97,6 +97,15 @@ namespace Statecraft.Sim
if (t.To.HasValue && t.To.Value <= t.From) throw new ContentException("people.yaml", $"people[{p.Id}].offices.{t.Office}", $"term ends ({t.To:yyyy-MM-dd}) before it starts ({t.From:yyyy-MM-dd})");
}
}
+ var terms = c.People.SelectMany(p => p.Offices.Select(t => (person: p.Id, term: t))).ToList();
+ for (int i = 0; i < terms.Count; i++)
+ for (int j = i + 1; j < terms.Count; j++)
+ {
+ var (a, b) = (terms[i], terms[j]);
+ if (a.term.Office != b.term.Office || a.person == b.person) continue;
+ if (a.term.From < (b.term.To ?? DateTime.MaxValue) && b.term.From < (a.term.To ?? DateTime.MaxValue))
+ throw new ContentException("people.yaml", $"offices.{a.term.Office}", $"held by both {a.person} and {b.person} on overlapping dates");
+ }
foreach (var l in c.Legislatures)
{
foreach (var kv in l.Seats)
diff --git a/sim/Statecraft.Sim/Offices.cs b/sim/Statecraft.Sim/Offices.cs
new file mode 100644
index 000000000..ccd20892f
--- /dev/null
+++ b/sim/Statecraft.Sim/Offices.cs
@@ -0,0 +1,29 @@
+using System;
+using System.Linq;
+using System.Text.RegularExpressions;
+
+namespace Statecraft.Sim
+{
+ /// <summary>"Who held this office / which body sat on this date." Periods are [from, to).</summary>
+ public static class Offices
+ {
+ static bool In(DateTime d, DateTime from, DateTime? to) => d >= from && (to == null || d < to.Value);
+
+ public static Person? Holder(Content c, string office, DateTime date) =>
+ c.People.FirstOrDefault(p => p.Offices.Any(t => t.Office == office && In(date, t.From, t.To)));
+
+ public static Legislature? Legislature(Content c, DateTime date) =>
+ c.Legislatures.FirstOrDefault(l => In(date, l.From, l.To));
+
+ public static Government? Government(Content c, DateTime date) =>
+ c.Governments.FirstOrDefault(g => In(date, g.From, g.To));
+
+ static readonly Regex Placeholder = new Regex(@"\{([a-z_]+)\}");
+
+ /// <summary>Replaces {office_id} with the name of the person holding it on date.</summary>
+ public static string Fill(Content c, string text, DateTime date) =>
+ Placeholder.Replace(text, m =>
+ Holder(c, m.Groups[1].Value, date)?.Name
+ ?? throw new InvalidOperationException($"no holder of '{m.Groups[1].Value}' on {date:yyyy-MM-dd}"));
+ }
+}