A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Loader: refuse a window on indicators the code reads by name (#351)
agamrafaeli-instinct84ecba31f3c0
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 7461b2583..20d6cbce1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,8 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIB## [Unreleased]### Added+- The loader refuses a `from`/`to` window on the indicators the code reads by name (`morale`, `food_supply`, `housing`,+ `security`, `intl_standing`), a follow-up from the review of #313 (refs #57).- Turn lengths per security rung are data: `turn_lengths` in `rules.yaml` or an era override, read in the era the turnstarts in (R-TURN-001). The default is today's table, so the goldens do not move. GDD v0.1 Phase 1 uses it for monthlyturns (refs #81).diff --git a/sim/Statecraft.Sim.Tests/ContentWindowsTests.cs b/sim/Statecraft.Sim.Tests/ContentWindowsTests.csindex f8ef5c069..a8c5d0ddf 100644--- a/sim/Statecraft.Sim.Tests/ContentWindowsTests.cs+++ b/sim/Statecraft.Sim.Tests/ContentWindowsTests.cs@@ -10,7 +10,13 @@ public class ContentWindowsTestsstatic readonly DateTime D0 = new DateTime(1948, 5, 15);static readonly DateTime Cut = new DateTime(1948, 8, 1);- static string Model(Func<string, string>? edit = null) => edit == null ? Fixture.Files["model.yaml"] : edit(Fixture.Files["model.yaml"]);+ // The fixture model plus `shelter`, an indicator no code reads by name, so these tests can give it a window (the loader+ // refuses one on morale, food_supply, housing, security and intl_standing). `security` and `intl_standing` are added too,+ // since the fixture model has neither and the refusal test needs both.+ const string Shelter = " - id: shelter\n base: 0.3\n name: Shelter\n";+ const string HudRead = " - id: security\n base: 0.3\n name: Security\n - id: intl_standing\n base: 0.5\n name: Standing\n";+ static string Base => Fixture.Files["model.yaml"].Replace("policies:\n", Shelter + HudRead + "policies:\n");+ static string Model(Func<string, string>? edit = null) => edit == null ? Base : edit(Base);static string Offices(Func<string, string> edit) => edit(Fixture.Files["offices.yaml"]);static Content Load(string model, string? offices = null, Dictionary<string, string>? events = null)@@ -63,7 +69,7 @@ public class ContentWindowsTests// ---- the state follows the windows// The fixture's turns run weekly to 12 June, then every quarter (the ceasefire ends the war rung): 5/15 ... 6/12, 9/12.- static Content Windowed() => Load(Model(m => Window(Window(m, " - id: maabarot\n default: 0.0", "from: 1948-06-20"), " - id: housing\n base: 0.3", "to: 1948-08-01")));+ static Content Windowed() => Load(Model(m => Window(Window(m, " - id: maabarot\n default: 0.0", "from: 1948-06-20"), " - id: shelter\n base: 0.3", "to: 1948-08-01")));[Fact]public void ANewGameHoldsOnlyTheRecordsActiveOnTheStartDate()@@ -71,7 +77,7 @@ public class ContentWindowsTestsvar c = Windowed();var s = Simulation.NewGame(c, 1, Mode.Historical);Assert.DoesNotContain("maabarot", s.Policies.Keys);- Assert.Contains("housing", s.Indicators.Keys);+ Assert.Contains("shelter", s.Indicators.Keys);}static GameState Play(Content c, DateTime until)@@ -87,11 +93,11 @@ public class ContentWindowsTestsvar c = Windowed();var early = Play(c, new DateTime(1948, 6, 10));Assert.DoesNotContain("maabarot", early.Policies.Keys);- Assert.Contains("housing", early.Indicators.Keys);+ Assert.Contains("shelter", early.Indicators.Keys);var later = Play(c, new DateTime(1948, 9, 10));Assert.Equal(0.0, later.Policies["maabarot"]); // added as Init would add it- Assert.DoesNotContain("housing", later.Indicators.Keys); // removed with its shift- Assert.DoesNotContain("housing", later.IndicatorShifts.Keys);+ Assert.DoesNotContain("shelter", later.Indicators.Keys); // removed with its shift+ Assert.DoesNotContain("shelter", later.IndicatorShifts.Keys);Assert.NotEmpty(later.Hash()); // the hash never reads a missing keyAssert.NotEmpty(later.Describe());}@@ -149,7 +155,7 @@ public class ContentWindowsTests[Theory][InlineData(" - id: maabarot\n default: 0.0", "policies[maabarot]")]- [InlineData(" - id: housing\n base: 0.3", "indicators[housing]")]+ [InlineData(" - id: shelter\n base: 0.3", "indicators[shelter]")][InlineData(" - id: rationing_morale\n source: rationing", "effects[rationing_morale]")][InlineData(" - id: hunger\n indicator: food_supply", "situations[hunger]")][InlineData(" - id: everyone", "voter_groups[everyone]")]@@ -165,6 +171,19 @@ public class ContentWindowsTests}}+ [Theory]+ [InlineData("morale", " - id: morale\n base: 0.5")]+ [InlineData("food_supply", " - id: food_supply\n base: 0.4")]+ [InlineData("housing", " - id: housing\n base: 0.3")]+ [InlineData("security", " - id: security\n base: 0.3")]+ [InlineData("intl_standing", " - id: intl_standing\n base: 0.5")]+ public void AWindowOnAnIndicatorTheCodeReadsByNameIsRefused(string id, string anchor)+ {+ foreach (var w in new[] { "from: 1948-08-01", "to: 1949-01-01" })+ Refused(Model(m => Window(m, anchor, w)), $"'{id}' is read by name in the code");+ Assert.NotNull(Load(Model())); // no window: fine+ }+[Fact]public void AnEffectSourceThatIsNotActiveThroughTheEffectIsRefused(){@@ -176,16 +195,15 @@ public class ContentWindowsTests[Fact]public void AnEffectTargetThatIsNotActiveThroughTheEffectIsRefused() =>- Refused(Model(m => Window(m, " - id: food_supply\n base: 0.4", "to: 1948-09-01")), "target 'food_supply' is not active");+ Refused(Model(m => Window(m, " - id: shelter\n base: 0.3", "to: 1948-09-01").Replace("situations:\n", " - id: shelter_effect\n source: rationing\n target: shelter\n k: 0.1\n inertia: 1\n name: Shelter effect\nsituations:\n")), "target 'shelter' is not active");[Fact]public void ASituationOnAnIndicatorThatEndsEarlyIsRefused() =>- Refused(Model(m => Window(m, " - id: food_supply\n base: 0.4", "to: 1948-09-01")- .Replace(" - id: rationing_food\n", " - id: rationing_food\n to: 1948-09-01\n")), "indicator 'food_supply' is not active");+ Refused(Model(m => Window(m, " - id: shelter\n base: 0.3", "to: 1948-09-01").Replace("situations:\n", "situations:\n - id: homeless\n indicator: shelter\n on_below: 0.2\n off_above: 0.3\n name: Homeless\n")), "indicator 'shelter' is not active");[Fact]public void AVoterGroupWeightOnAnIndicatorThatBeginsLateIsRefused() =>- Refused(Model(m => Window(m, " - id: morale\n base: 0.5", "from: 1948-08-01")), "is not active for the whole window");+ Refused(Model(m => Window(m, " - id: shelter\n base: 0.3", "from: 1948-08-01").Replace("weights: { morale: 1.0 }", "weights: { morale: 1.0, shelter: 0.5 }")), "is not active for the whole window");[Fact]public void AWindowedRecordMayUseARecordWithAWiderWindow()@@ -211,8 +229,8 @@ public class ContentWindowsTests[Fact]public void AnEventConditionOnAnIndicatorThatBeginsInsideItsWindowIsRefused() =>- Refused(Model(m => Window(m, " - id: housing\n base: 0.3", "from: 1948-07-01")), "not active for the whole window of this event",- events: new Dictionary<string, string> { ["probe.yaml"] = string.Format(Event, "conditions: [{ indicator: housing, min: 0.1 }]\n", "") });+ Refused(Model(m => Window(m, " - id: shelter\n base: 0.3", "from: 1948-07-01")), "not active for the whole window of this event",+ events: new Dictionary<string, string> { ["probe.yaml"] = string.Format(Event, "conditions: [{ indicator: shelter, min: 0.1 }]\n", "") });[Fact]public void AnEventOptionThatSetsAPolicyOrMovesAnIndicatorOutsideItsWindowIsRefused()@@ -220,22 +238,22 @@ public class ContentWindowsTestsvar model = Model(m => Window(m, " - id: maabarot\n default: 0.0", "from: 1948-07-01"));Refused(model, "not active for the whole window of this event",events: new Dictionary<string, string> { ["probe.yaml"] = string.Format(Event, "", " set_policy: { maabarot: 1.0 }\n") });- Refused(Model(m => Window(m, " - id: housing\n base: 0.3", "to: 1948-09-01")), "not active for the whole window of this event",- events: new Dictionary<string, string> { ["probe.yaml"] = string.Format(Event, "", " effects: [{ target: housing, delta: 0.1 }]\n") });+ Refused(Model(m => Window(m, " - id: shelter\n base: 0.3", "to: 1948-09-01")), "not active for the whole window of this event",+ events: new Dictionary<string, string> { ["probe.yaml"] = string.Format(Event, "", " effects: [{ target: shelter, delta: 0.1 }]\n") });}// ---- a late fire (EventSystem.Trigger) can come after the window of a record the event reaches has ended// The event's window [20 June, 1 July) falls inside the fixture's quarterly turn 12 June to 12 September, so it fires late,- // on 12 September, after housing (and maabarot) have left the state on 1 July.+ // on 12 September, after shelter (and maabarot) have left the state on 1 July.static Content LateFire(string conditions, string option) => Load(- Model(m => Window(Window(m, " - id: housing\n base: 0.3", "to: 1948-07-01"), " - id: maabarot\n default: 0.0", "to: 1948-07-01")),+ Model(m => Window(Window(m, " - id: shelter\n base: 0.3", "to: 1948-07-01"), " - id: maabarot\n default: 0.0", "to: 1948-07-01")),events: new Dictionary<string, string> { ["probe.yaml"] = "id: probe\ntitle: Probe\ntext: A probe.\nanchor: soft\nfrom: 1948-06-20\nto: 1948-07-01\n" + conditions + "options:\n - id: a\n text: Do a\n" + option });[Fact]public void ALateFireDoesNotReadAnIndicatorWhoseWindowHasEnded(){- var c = LateFire("conditions: [{ indicator: housing, min: 0.0 }]\n", "");+ var c = LateFire("conditions: [{ indicator: shelter, min: 0.0 }]\n", "");var s = Play(c, new DateTime(1948, 9, 1));Assert.Equal(new DateTime(1948, 9, 12), s.Date);Assert.DoesNotContain("probe", s.FiredEvents); // its condition reads a record that no longer exists, so it is not met@@ -244,13 +262,13 @@ public class ContentWindowsTests[Fact]public void ALateFiredEventResolvedAfterTheWindowLeavesEndedRecordsOut(){- var c = LateFire("", " effects: [{ target: housing, delta: 0.1 }, { target: treasury, delta: 5 }]\n set_policy: { maabarot: 1.0 }\n");+ var c = LateFire("", " effects: [{ target: shelter, delta: 0.1 }, { target: treasury, delta: 5 }]\n set_policy: { maabarot: 1.0 }\n");var s = Play(c, new DateTime(1948, 9, 1));Assert.Contains("probe", s.PendingEvents);var r = Simulation.Step(c, s, new Command[] { new ChooseEventOption("probe", "a") });Assert.Empty(r.Rejected);- Assert.DoesNotContain("housing", r.State.Indicators.Keys);- Assert.DoesNotContain("housing", r.State.IndicatorShifts.Keys);+ Assert.DoesNotContain("shelter", r.State.Indicators.Keys);+ Assert.DoesNotContain("shelter", r.State.IndicatorShifts.Keys);Assert.DoesNotContain("maabarot", r.State.Policies.Keys); // not added back by the optionAssert.Contains("probe", r.State.ChosenOptions.Keys);Assert.Contains(r.Why, w => w.Target == "treasury" && w.Delta == 5); // treasury is not a dated record: the option still pays itdiff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.csindex 088e29522..1e2f1fd20 100644--- a/sim/Statecraft.Sim/ContentLoader.cs+++ b/sim/Statecraft.Sim/ContentLoader.cs@@ -460,14 +460,22 @@ namespace Statecraft.Simif (r.BudgetStart.Count > 0 && Math.Abs(r.BudgetStart.Values.Sum() - 1.0) > 1e-6) throw new ContentException(file, "budget_start", "shares must sum to 1");}+ // Indicators the code reads by name: food_supply (EconomySystem, FX crisis), housing and morale (MapActions), security+ // and intl_standing (Unity HudModel). A window on one would make those reads miss, so the loader refuses it.+ static readonly string[] CodeNamedIndicators = { "morale", "food_supply", "housing", "security", "intl_standing" };+/// <summary>Chapters design 3.2: a window must run forward, and nothing may reach a record that is not active for the/// whole window of whatever uses it (effect sources and targets, situations, voter-group weights, policy and effect offices).</summary>+static void ValidateWindows(Content c){void Order(string where, DateTime? from, DateTime? to){if (from.HasValue && to.HasValue && from.Value >= to.Value) throw new ContentException("model.yaml", where, "from must be before to");}+ foreach (var x in c.Indicators)+ if ((x.From.HasValue || x.To.HasValue) && CodeNamedIndicators.Contains(x.Id))+ throw new ContentException("model.yaml", $"indicators[{x.Id}]", $"'{x.Id}' is read by name in the code, so it cannot have a from/to window");foreach (var x in c.Indicators) Order($"indicators[{x.Id}]", x.From, x.To);foreach (var x in c.Policies) Order($"policies[{x.Id}]", x.From, x.To);foreach (var x in c.Effects) Order($"effects[{x.Id}]", x.From, x.To);