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.

fix(sim): require event office placeholders to have a holder across the event window

eyalgolan8631a49c0b71
diff --git a/sim/Statecraft.Sim.Tests/EventTests.cs b/sim/Statecraft.Sim.Tests/EventTests.cs
index db9cb2266..e2945fcf4 100644
--- a/sim/Statecraft.Sim.Tests/EventTests.cs
+++ b/sim/Statecraft.Sim.Tests/EventTests.cs
@@ -124,6 +124,51 @@ options:
Assert.Equal("events[shortage].text", ex.Field);
}
+ // def_two takes defense only on 1949-02-01: nobody holds it in [1949-01-01, 1949-02-01).
+ static readonly string GapPeople = Fixture.Files["people.yaml"].Replace(
+ "office: defense_minister\n from: 1949-01-01", "office: defense_minister\n from: 1949-02-01");
+
+ static Content LoadWithGap(string ev) =>
+ ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["people.yaml"] = GapPeople },
+ new Dictionary<string, string> { ["shortage.yaml"] = ev }));
+
+ [Fact]
+ public void PlaceholderOfficeWithoutHolderForPartOfWindowFailsToLoad()
+ {
+ var bad = Shortage.Replace("{finance_minister}", "{defense_minister}");
+ var ex = Assert.Throws<ContentException>(() => LoadWithGap(bad));
+ Assert.Equal("events/shortage.yaml", ex.File);
+ Assert.Equal("events[shortage].text", ex.Field);
+ Assert.Contains("defense_minister", ex.Message);
+ Assert.Contains("1949-01-01", ex.Message);
+ }
+
+ [Fact]
+ public void PlaceholderUncoveredAtWindowStartNamesThatDate()
+ {
+ var bad = Shortage.Replace("text: Ration food", "text: '{defense_minister} rations food'")
+ .Replace("from: 1948-06-01", "from: 1949-01-15\nto: 1949-03-01");
+ var ex = Assert.Throws<ContentException>(() => LoadWithGap(bad));
+ Assert.Equal("events[shortage].options[ration].text", ex.Field);
+ Assert.Contains("1949-01-15", ex.Message);
+ }
+
+ [Fact]
+ public void PlaceholderFullyCoveredWindowLoads()
+ {
+ var ok = Shortage.Replace("{finance_minister}", "{defense_minister}")
+ .Replace("from: 1948-06-01", "from: 1948-06-01\nto: 1949-01-01"); // window ends where the gap starts
+ Assert.Single(LoadWithGap(ok).Events);
+ }
+
+ [Fact]
+ public void PlaceholderCoveredAcrossAHandoverLoads()
+ {
+ // Fixture: pm_one holds defense until 1949-01-01, def_two from then on (open-ended).
+ var ok = Shortage.Replace("{finance_minister}", "{defense_minister}");
+ Assert.Single(Load(new Dictionary<string, string> { ["shortage.yaml"] = ok }).Events);
+ }
+
[Fact]
public void EffectTargetMustExist()
{
diff --git a/sim/Statecraft.Sim/EventRules.cs b/sim/Statecraft.Sim/EventRules.cs
index 527716507..f77dab462 100644
--- a/sim/Statecraft.Sim/EventRules.cs
+++ b/sim/Statecraft.Sim/EventRules.cs
@@ -1,3 +1,4 @@
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
@@ -7,7 +8,7 @@ namespace Statecraft.Sim
/// <summary>Load-time checks for events, including the sensitive-content rule.</summary>
static class EventRules
{
- public static readonly HashSet<string> SensitiveTags = new HashSet<string>(System.StringComparer.OrdinalIgnoreCase) { "holocaust", "terror", "massacre", "war-casualties" };
+ public static readonly HashSet<string> SensitiveTags = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "holocaust", "terror", "massacre", "war-casualties" };
public static readonly HashSet<string> SpecialTargets = new HashSet<string> { "treasury", "fx_reserves" };
public static void Validate(Content c, HashSet<string> offices, HashSet<string> indicators)
@@ -18,8 +19,8 @@ namespace Statecraft.Sim
string F(string field) => $"events[{e.Id}].{field}";
if (!ids.Add(e.Id)) throw new ContentException(e.File, F("id"), "duplicate event id");
if (e.Office != "" && !offices.Contains(e.Office)) throw new ContentException(e.File, F("office"), $"unknown office '{e.Office}'");
- CheckPlaceholders(e.File, F("title"), e.Title, offices);
- CheckPlaceholders(e.File, F("text"), e.Text, offices);
+ CheckPlaceholders(c, e, F("title"), e.Title, offices);
+ CheckPlaceholders(c, e, F("text"), e.Text, offices);
foreach (var cond in e.Conditions)
if (!indicators.Contains(cond.Indicator)) throw new ContentException(e.File, F($"conditions.{cond.Indicator}"), "unknown indicator");
if (e.Options.Count == 0) throw new ContentException(e.File, F("options"), "at least one option required");
@@ -29,7 +30,7 @@ namespace Statecraft.Sim
foreach (var o in e.Options)
{
if (!optionIds.Add(o.Id)) throw new ContentException(e.File, F($"options[{o.Id}].id"), "duplicate option id");
- CheckPlaceholders(e.File, F($"options[{o.Id}].text"), o.Text, offices);
+ CheckPlaceholders(c, e, F($"options[{o.Id}].text"), o.Text, offices);
if (o.SetRung.HasValue && (o.SetRung < 1 || o.SetRung > 5)) throw new ContentException(e.File, F($"options[{o.Id}].set_rung"), "must be 1-5");
foreach (var fx in o.Effects)
{
@@ -42,10 +43,35 @@ namespace Statecraft.Sim
}
}
- static void CheckPlaceholders(string file, string field, string text, HashSet<string> offices)
+ static void CheckPlaceholders(Content c, GameEvent e, string field, string text, HashSet<string> offices)
{
foreach (Match m in Offices.Placeholder.Matches(text))
- if (!offices.Contains(m.Groups[1].Value)) throw new ContentException(file, field, $"unknown office placeholder '{m.Value}'");
+ {
+ var office = m.Groups[1].Value;
+ if (!offices.Contains(office)) throw new ContentException(e.File, field, $"unknown office placeholder '{m.Value}'");
+ var gap = FirstUncovered(c, office, e.From, e.To);
+ if (gap.HasValue)
+ throw new ContentException(e.File, field, FormattableString.Invariant($"office '{office}' has no holder on {gap.Value:yyyy-MM-dd}, inside the event window"));
+ }
+ }
+
+ /// <summary>First date in [from, to) with nobody holding office, or null. Holders only change at term
+ /// starts and ends, so checking those dates (plus from) covers the whole window.</summary>
+ static DateTime? FirstUncovered(Content c, string office, DateTime from, DateTime? to)
+ {
+ bool Inside(DateTime d) => d >= from && (to == null || d < to.Value);
+ var terms = c.People.SelectMany(p => p.Offices).Where(t => t.Office == office).ToList();
+ var points = new SortedSet<DateTime> { from };
+ foreach (var t in terms)
+ {
+ if (Inside(t.From)) points.Add(t.From);
+ if (t.To.HasValue && Inside(t.To.Value)) points.Add(t.To.Value);
+ }
+ foreach (var d in points)
+ if (Offices.Holder(c, office, d) == null) return d;
+ if (to == null && !terms.Any(t => t.To == null))
+ return terms.Select(t => t.To!.Value).DefaultIfEmpty(from).Max();
+ return null;
}
}
}