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): every content id must match the office-id pattern (M8)

eyalgolan0f3a81d42080
diff --git a/sim/Statecraft.Sim.Tests/ValidationTests.cs b/sim/Statecraft.Sim.Tests/ValidationTests.cs
index 775f27d90..3521b20a2 100644
--- a/sim/Statecraft.Sim.Tests/ValidationTests.cs
+++ b/sim/Statecraft.Sim.Tests/ValidationTests.cs
@@ -161,4 +161,25 @@ status: draft
[Fact]
public void EqualMobilizationFloorsAreAllowed() =>
ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["rules.yaml"] = Fixture.Files["rules.yaml"].Replace("[0.02, 0.05, 0.15, 0.4, 0.8]", "[0.05, 0.05, 0.15, 0.4, 0.8]") }));
+
+ // M8: every content id has the office-id shape.
+ [Theory]
+ [InlineData("events/drought.yaml", "id: drought", "id: 'dro,ught'", "events[dro,ught].id")]
+ [InlineData("events/drought.yaml", "id: wait", "id: 'wa,it'", "events[drought].options[wa,it].id")]
+ [InlineData("parties.yaml", "id: beta", "id: 'be,ta'", "parties[be,ta].id")]
+ [InlineData("people.yaml", "id: def_two", "id: 'def,two'", "people[def,two].id")]
+ [InlineData("legislatures.yaml", "id: house", "id: 'ho,use'", "legislatures[ho,use].id")]
+ [InlineData("governments.yaml", "id: gov2", "id: 'gov,2'", "governments[gov,2].id")]
+ [InlineData("model.yaml", "id: food_supply", "id: 'food,supply'", "indicators[food,supply].id")]
+ [InlineData("model.yaml", "id: rationing\n", "id: 'Rationing'\n", "policies[Rationing].id")]
+ [InlineData("model.yaml", "id: rationing_food", "id: 'rationing,food'", "effects[rationing,food].id")]
+ [InlineData("model.yaml", "id: hunger", "id: 'hun,ger'", "situations[hun,ger].id")]
+ [InlineData("model.yaml", "id: everyone", "id: 'every one'", "voter_groups[every one].id")]
+ [InlineData("model.yaml", "id: usa", "id: 'u,sa'", "actors[u,sa].id")]
+ public void IdOutsideTheIdPatternIsAContentError(string file, string from, string to, string field)
+ {
+ var ex = Bad(file, from, to);
+ Assert.Equal(file, ex.File);
+ Assert.Equal(field, ex.Field);
+ }
}
diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.cs
index 441cfcc56..2af4d4f01 100644
--- a/sim/Statecraft.Sim/ContentLoader.cs
+++ b/sim/Statecraft.Sim/ContentLoader.cs
@@ -108,11 +108,17 @@ namespace Statecraft.Sim
return c;
}
+ internal const string IdRule = "must be lowercase letters, digits and underscores, starting with a letter";
+
+ /// <summary>Every id has the office-id shape (so it is safe in placeholders, CLI input and save files) and is unique in its list.</summary>
static void Unique(string file, string list, IEnumerable<string> ids)
{
var seen = new HashSet<string>();
foreach (var id in ids)
+ {
+ if (!Offices.OfficeId.IsMatch(id)) throw new ContentException(file, $"{list}[{id}].id", IdRule);
if (!seen.Add(id)) throw new ContentException(file, $"{list}[{id}].id", "duplicate id");
+ }
}
static readonly HashSet<string> Statuses = new HashSet<string> { "draft", "fact-checked", "historian-reviewed", "sensitivity-reviewed" };
@@ -154,8 +160,6 @@ namespace Statecraft.Sim
var indicators = new HashSet<string>(c.Indicators.Select(i => i.Id));
var sources = new HashSet<string>(c.Policies.Select(p => p.Id).Concat(indicators));
Unique("offices.yaml", "offices", c.Offices.Select(o => o.Id));
- foreach (var o in c.Offices)
- if (!Offices.OfficeId.IsMatch(o.Id)) throw new ContentException("offices.yaml", $"offices[{o.Id}].id", "must be lowercase letters, digits and underscores, starting with a letter");
Unique("parties.yaml", "parties", c.Parties.Select(p => p.Id));
Unique("people.yaml", "people", c.People.Select(p => p.Id));
Unique("legislatures.yaml", "legislatures", c.Legislatures.Select(l => l.Id));
diff --git a/sim/Statecraft.Sim/EventRules.cs b/sim/Statecraft.Sim/EventRules.cs
index 3fa5f1816..cbd194596 100644
--- a/sim/Statecraft.Sim/EventRules.cs
+++ b/sim/Statecraft.Sim/EventRules.cs
@@ -17,6 +17,7 @@ namespace Statecraft.Sim
foreach (var e in c.Events)
{
string F(string field) => $"events[{e.Id}].{field}";
+ if (!Offices.OfficeId.IsMatch(e.Id)) throw new ContentException(e.File, F("id"), ContentLoader.IdRule);
if (!ids.Add(e.Id)) throw new ContentException(e.File, F("id"), "duplicate event id");
if (e.Anchor != "hard" && e.Anchor != "soft") throw new ContentException(e.File, F("anchor"), $"unknown anchor '{e.Anchor}' (must be hard or soft)");
ContentLoader.Status(e.File, F("status"), e.Status);
@@ -37,6 +38,7 @@ namespace Statecraft.Sim
var optionIds = new HashSet<string>();
foreach (var o in e.Options)
{
+ if (!Offices.OfficeId.IsMatch(o.Id)) throw new ContentException(e.File, F($"options[{o.Id}].id"), ContentLoader.IdRule);
if (!optionIds.Add(o.Id)) throw new ContentException(e.File, F($"options[{o.Id}].id"), "duplicate option id");
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");