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): harden event load rules and cover delegation competence
eyalgolan5cef4019a27f
diff --git a/sim/Statecraft.Sim.Tests/EventTests.cs b/sim/Statecraft.Sim.Tests/EventTests.csindex 5cf33999c..db9cb2266 100644--- a/sim/Statecraft.Sim.Tests/EventTests.cs+++ b/sim/Statecraft.Sim.Tests/EventTests.cs@@ -1,5 +1,6 @@using System;using System.Collections.Generic;+using System.Linq;using Statecraft.Sim;public class EventTests@@ -138,4 +139,74 @@ options:Assert.Equal("events/b.yaml", ex.File);Assert.Equal("events[shortage].id", ex.Field);}++ static string Sensitive(string tag, string target, string delta) =>+ Shortage.Replace("title: Bread lines", "title: Bread lines\ntags: [" + tag + "]")+ .Replace("target: fx_reserves\n delta: -10", "target: " + target + "\n delta: " + delta);++ [Theory]+ [InlineData("Massacre")]+ [InlineData("War-Casualties")]+ [InlineData("HOLOCAUST")]+ [InlineData("Terror")]+ public void SensitiveTagMatchingIgnoresCase(string tag)+ {+ var ex = Assert.Throws<ContentException>(() => Load(new Dictionary<string, string> { ["shortage.yaml"] = Sensitive(tag, "treasury", "50") }));+ Assert.Equal("events/shortage.yaml", ex.File);+ Assert.Equal("events[shortage].options[import].effects.treasury", ex.Field);+ }++ [Fact]+ public void SensitiveEventCannotRewardFxReserves()+ {+ var ex = Assert.Throws<ContentException>(() => Load(new Dictionary<string, string> { ["shortage.yaml"] = Sensitive("terror", "fx_reserves", "50") }));+ Assert.Equal("events/shortage.yaml", ex.File);+ Assert.Equal("events[shortage].options[import].effects.fx_reserves", ex.Field);+ }++ [Theory]+ [InlineData("treasury", "0")]+ [InlineData("treasury", "-20")]+ [InlineData("fx_reserves", "0")]+ [InlineData("fx_reserves", "-20")]+ public void SensitiveEventMayCostOrNotChangeMoney(string target, string delta)+ {+ var c = Load(new Dictionary<string, string> { ["shortage.yaml"] = Sensitive("massacre", target, delta) });+ Assert.Single(c.Events);+ }++ [Fact]+ public void DuplicateOptionIdIsAnError()+ {+ var bad = Shortage.Replace("id: import", "id: ration");+ var ex = Assert.Throws<ContentException>(() => Load(new Dictionary<string, string> { ["shortage.yaml"] = bad }));+ Assert.Equal("events/shortage.yaml", ex.File);+ Assert.Equal("events[shortage].options[ration].id", ex.Field);+ }++ static string DelegatedText(Content c, int competence, ulong seed)+ {+ c.People.First(p => p.Id == "def_two").Competence = competence;+ var s = State(c, new DateTime(1948, 6, 1)); s.RngState = seed; var why = new List<Cause>();+ EventSystem.Trigger(c, s, why); why.Clear();+ EventSystem.Delegate(c, s, why);+ return why.First(w => w.Message.StartsWith("delegated")).Message;+ }++ [Fact]+ public void MaximalCompetenceAlwaysPicksRecommendedOption()+ {+ var c = Load(new Dictionary<string, string> { ["shortage.yaml"] = Shortage });+ for (ulong seed = 1; seed <= 30; seed++)+ Assert.EndsWith("Ration food", DelegatedText(c, 10, seed));+ }++ [Fact]+ public void MinimalCompetenceDelegateCanPickOtherOptions()+ {+ var c = Load(new Dictionary<string, string> { ["shortage.yaml"] = Shortage });+ var picks = new HashSet<string>();+ for (ulong seed = 1; seed <= 60; seed++) picks.Add(DelegatedText(c, 0, seed));+ Assert.True(picks.Count > 1, "competence 0 should choose at random among options");+ }}diff --git a/sim/Statecraft.Sim/EventRules.cs b/sim/Statecraft.Sim/EventRules.csindex 86fc68f8f..527716507 100644--- a/sim/Statecraft.Sim/EventRules.cs+++ b/sim/Statecraft.Sim/EventRules.cs@@ -7,7 +7,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> { "holocaust", "terror", "massacre", "war-casualties" };+ public static readonly HashSet<string> SensitiveTags = new HashSet<string>(System.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)@@ -25,16 +25,18 @@ namespace Statecraft.Simif (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");bool sensitive = e.Tags.Any(SensitiveTags.Contains);+ var optionIds = new HashSet<string>();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);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){if (!indicators.Contains(fx.Target) && !SpecialTargets.Contains(fx.Target))throw new ContentException(e.File, F($"options[{o.Id}].effects.{fx.Target}"), "unknown target");- if (sensitive && fx.Target == "treasury" && fx.Delta > 0)- throw new ContentException(e.File, F($"options[{o.Id}].effects.treasury"), "sensitive events cannot reward the treasury");+ 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}");}}}