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): validate attention numbers at load; make CommandKinds read-only
eyalgolan458cc67654f0
diff --git a/sim/Statecraft.Sim.Tests/AttentionTests.cs b/sim/Statecraft.Sim.Tests/AttentionTests.csindex 2c4b1c6eb..13ed15ba6 100644--- a/sim/Statecraft.Sim.Tests/AttentionTests.cs+++ b/sim/Statecraft.Sim.Tests/AttentionTests.cs@@ -31,4 +31,23 @@ public class AttentionTestsvar ex = Assert.Throws<ContentException>(() => ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["rules.yaml"] = bad })));Assert.Equal("attention_cost.set_polcy", ex.Field);}++ static string LoadField(string from, string to)+ {+ var bad = Fixture.Files["rules.yaml"].Replace(from, to);+ Assert.NotEqual(Fixture.Files["rules.yaml"], bad);+ return Assert.Throws<ContentException>(() => ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["rules.yaml"] = bad }))).Field;+ }++ [Fact]+ public void LoaderRejectsNegativeCost() =>+ Assert.Equal("attention_cost.set_policy", LoadField("set_policy: 1,", "set_policy: -1,"));++ [Fact]+ public void LoaderRejectsNaNStep() =>+ Assert.Equal("rung_attention_step", LoadField("rung_attention_step: 0.25", "rung_attention_step: .nan"));++ [Fact]+ public void LoaderRejectsNegativePerTurn() =>+ Assert.Equal("attention_per_turn", LoadField("attention_per_turn: 3", "attention_per_turn: -3"));}diff --git a/sim/Statecraft.Sim/AttentionSystem.cs b/sim/Statecraft.Sim/AttentionSystem.csindex 62ad727d4..1c8ee4f43 100644--- a/sim/Statecraft.Sim/AttentionSystem.cs+++ b/sim/Statecraft.Sim/AttentionSystem.cs@@ -6,9 +6,13 @@ namespace Statecraft.Sim/// <summary>The PM's per-turn capacity. Each command costs attention; costs rise with the security level.</summary>public static class AttentionSystem{- public static readonly HashSet<string> CommandKinds = new HashSet<string>+ static readonly HashSet<string> Kinds = new HashSet<string>{ "set_policy", "allocate_budget", "choose_event_option", "set_mobilization", "security_order" };+ public static IReadOnlyCollection<string> CommandKinds => Kinds;++ public static bool IsCommandKind(string kind) => Kinds.Contains(kind);+public static void Refill(Content c, GameState s) => s.Attention = c.Rules.AttentionPerTurn;public static double Cost(Content c, GameState s, string kind) =>diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.csindex 022828a72..167e56979 100644--- a/sim/Statecraft.Sim/ContentLoader.cs+++ b/sim/Statecraft.Sim/ContentLoader.cs@@ -144,8 +144,12 @@ namespace Statecraft.Simforeach (var k in v.Weights.Keys)if (!indicators.Contains(k)) throw new ContentException("model.yaml", $"voter_groups[{v.Id}].weights.{k}", "unknown indicator");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");+ foreach (var kv in c.Rules.AttentionCost)+ if (!(kv.Value >= 0 && !double.IsInfinity(kv.Value))) throw new ContentException("rules.yaml", $"attention_cost.{kv.Key}", "must be a finite non-negative number");foreach (var k in c.Rules.AttentionCost.Keys)- if (!AttentionSystem.CommandKinds.Contains(k)) throw new ContentException("rules.yaml", $"attention_cost.{k}", "unknown command kind");+ if (!AttentionSystem.IsCommandKind(k)) throw new ContentException("rules.yaml", $"attention_cost.{k}", "unknown command kind");foreach (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)");