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): range-check content numbers and required dates (I3)
eyalgolane76c7a6ca8e8
diff --git a/sim/Statecraft.Sim.Tests/ValidationTests.cs b/sim/Statecraft.Sim.Tests/ValidationTests.csindex a7dc41b1f..775f27d90 100644--- a/sim/Statecraft.Sim.Tests/ValidationTests.cs+++ b/sim/Statecraft.Sim.Tests/ValidationTests.cs@@ -93,4 +93,72 @@ status: draft[InlineData("sensitivity-reviewed")]public void EveryStatusLoads(string status) =>ContentLoader.Load(Fixture.Write(events: new Dictionary<string, string> { ["drought.yaml"] = Event.Replace("status: draft", "status: " + status) }));++ // I3: numbers and dates out of range.+ [Theory]+ [InlineData("model.yaml", "base: 0.5", "base: .nan", "indicators[morale].base")]+ [InlineData("model.yaml", "base: 0.5", "base: 1.5", "indicators[morale].base")]+ [InlineData("model.yaml", "base: 0.5", "base: -0.1", "indicators[morale].base")]+ [InlineData("model.yaml", "default: 0.0", "default: 1.2", "policies[rationing].default")]+ [InlineData("model.yaml", "default: 0.0", "default: .nan", "policies[rationing].default")]+ [InlineData("model.yaml", "k: 0.4", "k: .inf", "effects[rationing_food].k")]+ [InlineData("model.yaml", "k: 0.4", "k: .nan", "effects[rationing_food].k")]+ [InlineData("model.yaml", "on_below: 0.3", "on_below: .nan", "situations[hunger].on_below")]+ [InlineData("model.yaml", "off_above: 0.45", "off_above: 1.45", "situations[hunger].off_above")]+ [InlineData("model.yaml", "weights: { morale: 1.0 }", "weights: { morale: .nan }", "voter_groups[everyone].weights.morale")]+ [InlineData("model.yaml", "standing: 0.5", "standing: 2", "actors[usa].standing")]+ [InlineData("model.yaml", "standing: 0.5", "standing: .nan", "actors[usa].standing")]+ [InlineData("rules.yaml", "[0.02, 0.05, 0.15, 0.4, 0.8]", "[0.02, 0.05, .nan, 0.4, 0.8]", "rung_min_mobilization[2]")]+ [InlineData("rules.yaml", "[0.02, 0.05, 0.15, 0.4, 0.8]", "[0.02, 0.05, 0.15, 0.4, 7]", "rung_min_mobilization[4]")]+ [InlineData("rules.yaml", "[0.02, 0.05, 0.15, 0.4, 0.8]", "[-0.02, 0.05, 0.15, 0.4, 0.8]", "rung_min_mobilization[0]")]+ [InlineData("rules.yaml", "[0.02, 0.05, 0.15, 0.4, 0.8]", "[0.02, 0.05, 0.15, 0.1, 0.8]", "rung_min_mobilization[3]")]+ [InlineData("rules.yaml", "treasury_start: 100", "treasury_start: .nan", "treasury_start")]+ [InlineData("rules.yaml", "fx_start: 50", "fx_start: .inf", "fx_start")]+ [InlineData("rules.yaml", "tax_per_capita_per_year: 0.0004", "tax_per_capita_per_year: .nan", "tax_per_capita_per_year")]+ [InlineData("rules.yaml", "spending_per_capita_per_year: 0.0005", "spending_per_capita_per_year: -.inf", "spending_per_capita_per_year")]+ [InlineData("rules.yaml", "exports_per_year: 20", "exports_per_year: .nan", "exports_per_year")]+ [InlineData("rules.yaml", "imports_per_capita_per_year: 0.0001", "imports_per_capita_per_year: .inf", "imports_per_capita_per_year")]+ [InlineData("rules.yaml", "mobilization_cost_per_year: 40", "mobilization_cost_per_year: .nan", "mobilization_cost_per_year")]+ [InlineData("rules.yaml", "natural_increase_per_year: 0.017", "natural_increase_per_year: .nan", "natural_increase_per_year")]+ [InlineData("rules.yaml", "initial_population: { veterans: 1000 }", "initial_population: { veterans: -5 }", "initial_population.veterans")]+ [InlineData("rules.yaml", "initial_population: { veterans: 1000 }", "initial_population: { veterans: .inf }", "initial_population.veterans")]+ [InlineData("rules.yaml", "budget_start: { finance_minister: 1.0 }", "budget_start: { finance_minister: .nan }", "budget_start.finance_minister")]+ [InlineData("rules.yaml", "budget_start: { finance_minister: 1.0 }", "budget_start: { finance_minister: 1.5, defense_minister: -0.5 }", "budget_start.finance_minister")]+ [InlineData("rules.yaml", "budget_start: { finance_minister: 1.0 }", "budget_start: { finance_minister: 0.9 }", "budget_start")]+ [InlineData("rules.yaml", "ceasefire_weeks: 4", "ceasefire_weeks: 0", "ceasefire_weeks")]+ [InlineData("rules.yaml", "ceasefire_weeks: 4", "ceasefire_weeks: -5", "ceasefire_weeks")]+ [InlineData("rules.yaml", "start_rung: 5", "start_rung: 0", "start_rung")]+ [InlineData("rules.yaml", "start_rung: 5", "start_rung: 6", "start_rung")]+ [InlineData("aliyah.yaml", "europe: 230", "europe: -230", "series[0].origins.europe")]+ [InlineData("aliyah.yaml", "europe: 230", "europe: .nan", "series[0].origins.europe")]+ [InlineData("aliyah.yaml", " - from: 1948-05-15\n to:", " - to:", "series[0].from")]+ [InlineData("aliyah.yaml", "to: 1948-12-31", "to: 1948-05-01", "series[0].to")]+ [InlineData("population.yaml", "anchors: []", "anchors:\n - date: 1948-11-08\n jews: -1\n", "anchors[0].jews")]+ [InlineData("population.yaml", "anchors: []", "anchors:\n - jews: 700\n", "anchors[0].date")]+ [InlineData("people.yaml", "competence: 8", "competence: 11", "people[pm_one].competence")]+ [InlineData("people.yaml", "competence: 8", "competence: -1", "people[pm_one].competence")]+ [InlineData("people.yaml", "loyalty: 9", "loyalty: 50", "people[pm_one].loyalty")]+ [InlineData("people.yaml", " - office: prime_minister\n from: 1948-05-14\n", " - office: prime_minister\n", "people[pm_one].offices.prime_minister.from")]+ [InlineData("legislatures.yaml", "seats: { alpha: 4, beta: 6 }", "seats: { alpha: -2, beta: 12 }", "legislatures[house].seats.alpha")]+ [InlineData("legislatures.yaml", " size: 5\n", " size: 0\n", "legislatures[council].size")]+ [InlineData("legislatures.yaml", " from: 1949-02-14\n", "", "legislatures[house].from")]+ [InlineData("legislatures.yaml", "to: 1949-02-14", "to: 1948-05-14", "legislatures[council].to")]+ [InlineData("governments.yaml", " from: 1949-03-10\n", "", "governments[gov2].from")]+ [InlineData("governments.yaml", "to: 1949-03-10", "to: 1948-01-01", "governments[gov1].to")]+ [InlineData("events/drought.yaml", "delta: 0.1", "delta: .nan", "events[drought].options[ration].effects.food_supply.delta")]+ [InlineData("events/drought.yaml", "delta: 0.1", "delta: .inf", "events[drought].options[ration].effects.food_supply.delta")]+ [InlineData("events/drought.yaml", "max: 0.9", "max: .nan", "events[drought].conditions.food_supply.max")]+ [InlineData("events/drought.yaml", "from: 1948-06-01\n", "", "events[drought].from")]+ [InlineData("events/drought.yaml", "to: 1948-09-01", "to: 1948-06-01", "events[drought].to")]+ [InlineData("events/drought.yaml", "to: 1948-09-01", "to: 1948-01-01", "events[drought].to")]+ public void NumberOrDateOutOfRangeIsAContentError(string file, string from, string to, string field)+ {+ var ex = Bad(file, from, to);+ Assert.Equal(file, ex.File);+ Assert.Equal(field, ex.Field);+ }++ [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]") }));}diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.csindex 23381b1cc..441cfcc56 100644--- a/sim/Statecraft.Sim/ContentLoader.cs+++ b/sim/Statecraft.Sim/ContentLoader.cs@@ -123,6 +123,30 @@ namespace Statecraft.Simthrow new ContentException(file, field, $"unknown status '{status}' (must be draft, fact-checked, historian-reviewed or sensitivity-reviewed)");}+ static bool IsFinite(double v) => !double.IsNaN(v) && !double.IsInfinity(v);++ internal static void Finite(string file, string field, double v)+ {+ if (!IsFinite(v)) throw new ContentException(file, field, "must be a finite number");+ }++ internal static void Unit(string file, string field, double v)+ {+ if (!(v >= 0 && v <= 1)) throw new ContentException(file, field, "must be a number from 0 to 1");+ }++ static void Count(string file, string field, double v)+ {+ if (!(v >= 0 && IsFinite(v))) throw new ContentException(file, field, "must be a finite number >= 0");+ }++ /// <summary>from is required (a missing date deserialises to DateTime default); to, when given, must be after from.</summary>+ internal static void Period(string file, string path, DateTime from, DateTime? to)+ {+ if (from == default) throw new ContentException(file, path + ".from", "required");+ if (to.HasValue && to.Value <= from) throw new ContentException(file, path + ".to", FormattableString.Invariant($"must be after from ({from:yyyy-MM-dd})"));+ }+static void Validate(Content c){var offices = new HashSet<string>(c.Offices.Select(o => o.Id));@@ -153,11 +177,14 @@ namespace Statecraft.Simforeach (var p in c.People){+ if (p.Competence < 0 || p.Competence > 10) throw new ContentException("people.yaml", $"people[{p.Id}].competence", "must be 0-10");+ if (p.Loyalty < 0 || p.Loyalty > 10) throw new ContentException("people.yaml", $"people[{p.Id}].loyalty", "must be 0-10");if (p.Party != "" && !parties.Contains(p.Party)) throw new ContentException("people.yaml", $"people[{p.Id}].party", $"unknown party '{p.Party}'");foreach (var t in p.Offices){if (string.IsNullOrEmpty(t.Office)) throw new ContentException("people.yaml", $"people[{p.Id}].offices", "office term without an office");if (!offices.Contains(t.Office)) throw new ContentException("people.yaml", $"people[{p.Id}].offices.{t.Office}", "unknown office");+ if (t.From == default) throw new ContentException("people.yaml", $"people[{p.Id}].offices.{t.Office}.from", "required");if (t.Party != "" && !parties.Contains(t.Party)) throw new ContentException("people.yaml", $"people[{p.Id}].offices.{t.Office}.party", $"unknown party '{t.Party}'");if (t.To.HasValue && t.To.Value <= t.From) throw new ContentException("people.yaml", $"people[{p.Id}].offices.{t.Office}", FormattableString.Invariant($"term ends ({t.To:yyyy-MM-dd}) before it starts ({t.From:yyyy-MM-dd})"));}@@ -182,19 +209,38 @@ namespace Statecraft.Sim}foreach (var l in c.Legislatures){+ Period("legislatures.yaml", $"legislatures[{l.Id}]", l.From, l.To);+ if (l.Size <= 0) throw new ContentException("legislatures.yaml", $"legislatures[{l.Id}].size", "must be > 0");foreach (var kv in l.Seats)+ {if (!parties.Contains(kv.Key)) throw new ContentException("legislatures.yaml", $"legislatures[{l.Id}].seats.{kv.Key}", "unknown party");+ if (kv.Value < 0) throw new ContentException("legislatures.yaml", $"legislatures[{l.Id}].seats.{kv.Key}", "must be >= 0");+ }if (l.Seats.Values.Sum() != l.Size) throw new ContentException("legislatures.yaml", $"legislatures[{l.Id}].size", FormattableString.Invariant($"seats sum to {l.Seats.Values.Sum()}, size is {l.Size}"));}foreach (var g in c.Governments)+ {+ Period("governments.yaml", $"governments[{g.Id}]", g.From, g.To);foreach (var p in g.Coalition)if (!parties.Contains(p)) throw new ContentException("governments.yaml", $"governments[{g.Id}].coalition.{p}", "unknown party");+ }+ foreach (var i in c.Indicators)+ {+ Finite("model.yaml", $"indicators[{i.Id}].base", i.Base);+ Unit("model.yaml", $"indicators[{i.Id}].base", i.Base);+ }+ foreach (var p in c.Policies)+ {+ Finite("model.yaml", $"policies[{p.Id}].default", p.Default);+ Unit("model.yaml", $"policies[{p.Id}].default", p.Default);+ }foreach (var p in c.Policies)if (!offices.Contains(p.Office)) throw new ContentException("model.yaml", $"policies[{p.Id}].office", $"unknown office '{p.Office}'");foreach (var e in c.Effects){if (!sources.Contains(e.Source)) throw new ContentException("model.yaml", $"effects[{e.Id}].source", $"unknown policy or indicator '{e.Source}'");if (!indicators.Contains(e.Target)) throw new ContentException("model.yaml", $"effects[{e.Id}].target", $"unknown indicator '{e.Target}'");+ Finite("model.yaml", $"effects[{e.Id}].k", e.K);if (e.Inertia < 1) throw new ContentException("model.yaml", $"effects[{e.Id}].inertia", "must be >= 1");if (e.Office != "" && !offices.Contains(e.Office)) throw new ContentException("model.yaml", $"effects[{e.Id}].office", $"unknown office '{e.Office}'");}@@ -203,10 +249,16 @@ namespace Statecraft.Simif (!indicators.Contains(s.Indicator)) throw new ContentException("model.yaml", $"situations[{s.Id}].indicator", $"unknown indicator '{s.Indicator}'");bool below = s.OnBelow.HasValue && s.OffAbove.HasValue, above = s.OnAbove.HasValue && s.OffBelow.HasValue;if (below == above) throw new ContentException("model.yaml", $"situations[{s.Id}]", "needs exactly one pair: on_below+off_above or on_above+off_below");+ foreach (var (key, v) in new[] { ("on_below", s.OnBelow), ("off_above", s.OffAbove), ("on_above", s.OnAbove), ("off_below", s.OffBelow) })+ if (v.HasValue) Unit("model.yaml", $"situations[{s.Id}].{key}", v.Value);}foreach (var v in c.VoterGroups)- foreach (var k in v.Weights.Keys)- if (!indicators.Contains(k)) throw new ContentException("model.yaml", $"voter_groups[{v.Id}].weights.{k}", "unknown indicator");+ foreach (var kv in v.Weights)+ {+ if (!indicators.Contains(kv.Key)) throw new ContentException("model.yaml", $"voter_groups[{v.Id}].weights.{kv.Key}", "unknown indicator");+ Finite("model.yaml", $"voter_groups[{v.Id}].weights.{kv.Key}", kv.Value);+ }+ foreach (var a in c.Actors) Unit("model.yaml", $"actors[{a.Id}].standing", a.Standing);if (c.Rules.Start == default) throw new ContentException("rules.yaml", "start", "required");if (!(c.Rules.AttentionPerTurn >= 0 && !double.IsInfinity(c.Rules.AttentionPerTurn))) throw new ContentException("rules.yaml", "attention_per_turn", "must be a finite non-negative number");if (!(c.Rules.RungAttentionStep >= 0 && !double.IsInfinity(c.Rules.RungAttentionStep))) throw new ContentException("rules.yaml", "rung_attention_step", "must be a finite non-negative number");@@ -217,8 +269,41 @@ namespace Statecraft.Simforeach (var k in AttentionSystem.CommandKinds)if (!c.Rules.AttentionCost.ContainsKey(k)) throw new ContentException("rules.yaml", $"attention_cost.{k}", "missing cost");if (c.Rules.RungMinMobilization.Count != 5) throw new ContentException("rules.yaml", "rung_min_mobilization", "needs exactly 5 values (rungs 1-5)");- foreach (var k in c.Rules.BudgetStart.Keys)- if (!offices.Contains(k)) throw new ContentException("rules.yaml", $"budget_start.{k}", "unknown office");+ for (int i = 0; i < 5; i++)+ {+ var field = FormattableString.Invariant($"rung_min_mobilization[{i}]");+ Unit("rules.yaml", field, c.Rules.RungMinMobilization[i]);+ if (i > 0 && c.Rules.RungMinMobilization[i] < c.Rules.RungMinMobilization[i - 1])+ throw new ContentException("rules.yaml", field, "must not be below the previous rung's floor");+ }+ if (c.Rules.StartRung < 1 || c.Rules.StartRung > 5) throw new ContentException("rules.yaml", "start_rung", "must be 1-5");+ if (c.Rules.CeasefireWeeks <= 0) throw new ContentException("rules.yaml", "ceasefire_weeks", "must be > 0");+ var r = c.Rules;+ foreach (var (key, v) in new[] { ("treasury_start", r.TreasuryStart), ("fx_start", r.FxStart), ("tax_per_capita_per_year", r.TaxPerCapitaPerYear),+ ("spending_per_capita_per_year", r.SpendingPerCapitaPerYear), ("exports_per_year", r.ExportsPerYear), ("imports_per_capita_per_year", r.ImportsPerCapitaPerYear),+ ("mobilization_cost_per_year", r.MobilizationCostPerYear), ("natural_increase_per_year", r.NaturalIncreasePerYear) })+ Finite("rules.yaml", key, v);+ foreach (var kv in r.InitialPopulation) Count("rules.yaml", $"initial_population.{kv.Key}", kv.Value);+ foreach (var kv in r.BudgetStart)+ {+ if (!offices.Contains(kv.Key)) throw new ContentException("rules.yaml", $"budget_start.{kv.Key}", "unknown office");+ Unit("rules.yaml", $"budget_start.{kv.Key}", kv.Value);+ }+ // The same rule EconomySystem.Allocate applies to every later allocation.+ if (r.BudgetStart.Count > 0 && Math.Abs(r.BudgetStart.Values.Sum() - 1.0) > 1e-6) throw new ContentException("rules.yaml", "budget_start", "shares must sum to 1");+ for (int i = 0; i < c.Aliyah.Count; i++)+ {+ var a = c.Aliyah[i]; var path = FormattableString.Invariant($"series[{i}]");+ if (a.From == default) throw new ContentException("aliyah.yaml", path + ".from", "required");+ if (a.To < a.From) throw new ContentException("aliyah.yaml", path + ".to", "required, on or after from (both dates are inclusive)");+ foreach (var kv in a.Origins) Count("aliyah.yaml", $"{path}.origins.{kv.Key}", kv.Value);+ }+ for (int i = 0; i < c.PopulationAnchors.Count; i++)+ {+ var a = c.PopulationAnchors[i]; var path = FormattableString.Invariant($"anchors[{i}]");+ if (a.Date == default) throw new ContentException("population.yaml", path + ".date", "required");+ Count("population.yaml", path + ".jews", a.Jews);+ }EventRules.Validate(c, offices, indicators);}}diff --git a/sim/Statecraft.Sim/EventRules.cs b/sim/Statecraft.Sim/EventRules.csindex f1700b7a7..3fa5f1816 100644--- a/sim/Statecraft.Sim/EventRules.cs+++ b/sim/Statecraft.Sim/EventRules.cs@@ -20,11 +20,16 @@ namespace Statecraft.Simif (!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);+ ContentLoader.Period(e.File, $"events[{e.Id}]", e.From, e.To);if (e.Office != "" && !offices.Contains(e.Office)) throw new ContentException(e.File, F("office"), $"unknown office '{e.Office}'");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 (cond.Min.HasValue) ContentLoader.Finite(e.File, F($"conditions.{cond.Indicator}.min"), cond.Min.Value);+ if (cond.Max.HasValue) ContentLoader.Finite(e.File, F($"conditions.{cond.Indicator}.max"), cond.Max.Value);+ }if (e.Options.Count == 0) throw new ContentException(e.File, F("options"), "at least one option required");if (e.Options.Count(o => o.Historical) > 1) throw new ContentException(e.File, F("options"), "at most one option can be historical");if (e.Anchor == "hard" && !e.Options.Any(o => o.Historical)) throw new ContentException(e.File, F("options"), "hard anchor needs a historical option");@@ -39,6 +44,7 @@ namespace Statecraft.Sim{if (!indicators.Contains(fx.Target) && !SpecialTargets.Contains(fx.Target))throw new ContentException(e.File, F($"options[{o.Id}].effects.{fx.Target}"), "unknown target");+ ContentLoader.Finite(e.File, F($"options[{o.Id}].effects.{fx.Target}.delta"), fx.Delta);if (sensitive && (fx.Target == "treasury" || fx.Target == "fx_reserves") && fx.Delta > 0)throw new ContentException(e.File, F($"options[{o.Id}].effects.{fx.Target}"), $"sensitive events cannot reward {fx.Target}");}