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): allow digits in office placeholders, validate office ids and stray braces

eyalgolan6704cee8cb30
diff --git a/sim/Statecraft.Sim.Tests/EventTests.cs b/sim/Statecraft.Sim.Tests/EventTests.cs
index e2945fcf4..d66c2cf73 100644
--- a/sim/Statecraft.Sim.Tests/EventTests.cs
+++ b/sim/Statecraft.Sim.Tests/EventTests.cs
@@ -169,6 +169,26 @@ options:
Assert.Single(Load(new Dictionary<string, string> { ["shortage.yaml"] = ok }).Events);
}
+ [Theory]
+ [InlineData("'{Finance} reports shortages.'", "text: Ration food", "events[shortage].text")]
+ [InlineData("'{finance_minister} reports { shortages.'", "text: Ration food", "events[shortage].text")]
+ [InlineData("'{finance_minister} reports shortages.'", "text: 'Ration } food'", "events[shortage].options[ration].text")]
+ public void StrayBracesAreRejected(string text, string optionText, string field)
+ {
+ var bad = Shortage.Replace("'{finance_minister} reports shortages.'", text).Replace("text: Ration food", optionText);
+ var ex = Assert.Throws<ContentException>(() => Load(new Dictionary<string, string> { ["shortage.yaml"] = bad }));
+ Assert.Equal("events/shortage.yaml", ex.File);
+ Assert.Equal(field, ex.Field);
+ }
+
+ [Fact]
+ public void StrayBraceInTitleIsRejected()
+ {
+ var bad = Shortage.Replace("title: Bread lines", "title: 'Bread {lines'");
+ var ex = Assert.Throws<ContentException>(() => Load(new Dictionary<string, string> { ["shortage.yaml"] = bad }));
+ Assert.Equal("events[shortage].title", ex.Field);
+ }
+
[Fact]
public void EffectTargetMustExist()
{
diff --git a/sim/Statecraft.Sim.Tests/OfficesTests.cs b/sim/Statecraft.Sim.Tests/OfficesTests.cs
index c57e79853..23f675de1 100644
--- a/sim/Statecraft.Sim.Tests/OfficesTests.cs
+++ b/sim/Statecraft.Sim.Tests/OfficesTests.cs
@@ -30,6 +30,28 @@ public class OfficesTests
Assert.Equal("Def Two briefs Pm One.", text);
}
+ [Fact]
+ public void OfficeIdWithDigitsWorksAsPlaceholder()
+ {
+ var offices = Fixture.Files["offices.yaml"] + " - id: minister_2\n title: Second Minister\n";
+ var people = Fixture.Files["people.yaml"].Replace(" - office: prime_minister\n",
+ " - office: minister_2\n from: 1948-05-14\n - office: prime_minister\n");
+ var c = ContentLoader.Load(Fixture.Write(new System.Collections.Generic.Dictionary<string, string> { ["offices.yaml"] = offices, ["people.yaml"] = people }));
+ Assert.Equal("Pm One speaks.", Offices.Fill(c, "{minister_2} speaks.", new DateTime(1949, 6, 1)));
+ }
+
+ [Theory]
+ [InlineData("Bad-Id")]
+ [InlineData("2nd_minister")]
+ [InlineData("_minister")]
+ public void OfficeIdMustBeLowercaseSnakeCase(string id)
+ {
+ var offices = Fixture.Files["offices.yaml"] + " - id: " + id + "\n title: Bad\n";
+ var ex = Assert.Throws<ContentException>(() => ContentLoader.Load(Fixture.Write(new System.Collections.Generic.Dictionary<string, string> { ["offices.yaml"] = offices })));
+ Assert.Equal("offices.yaml", ex.File);
+ Assert.Equal($"offices[{id}].id", ex.Field);
+ }
+
[Fact]
public void UnfilledPlaceholderThrows() =>
Assert.Throws<InvalidOperationException>(() => Offices.Fill(C, "{prime_minister}", new DateTime(1947, 1, 1)));
diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.cs
index 5cdba3d0e..762844aa5 100644
--- a/sim/Statecraft.Sim/ContentLoader.cs
+++ b/sim/Statecraft.Sim/ContentLoader.cs
@@ -80,6 +80,8 @@ 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 f77dab462..d6df3a1c6 100644
--- a/sim/Statecraft.Sim/EventRules.cs
+++ b/sim/Statecraft.Sim/EventRules.cs
@@ -45,6 +45,8 @@ namespace Statecraft.Sim
static void CheckPlaceholders(Content c, GameEvent e, string field, string text, HashSet<string> offices)
{
+ if (Offices.Placeholder.Replace(text, "").IndexOfAny(new[] { '{', '}' }) >= 0)
+ throw new ContentException(e.File, field, "stray '{' or '}' outside an {office_id} placeholder");
foreach (Match m in Offices.Placeholder.Matches(text))
{
var office = m.Groups[1].Value;
diff --git a/sim/Statecraft.Sim/Offices.cs b/sim/Statecraft.Sim/Offices.cs
index 405a876b6..595df24be 100644
--- a/sim/Statecraft.Sim/Offices.cs
+++ b/sim/Statecraft.Sim/Offices.cs
@@ -18,7 +18,9 @@ namespace Statecraft.Sim
public static Government? Government(Content c, DateTime date) =>
c.Governments.FirstOrDefault(g => In(date, g.From, g.To));
- internal static readonly Regex Placeholder = new Regex(@"\{([a-z_]+)\}");
+ internal static readonly Regex Placeholder = new Regex(@"\{([a-z0-9_]+)\}");
+
+ internal static readonly Regex OfficeId = new Regex(@"^[a-z][a-z0-9_]*$");
/// <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) =>