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): shown changes agree with shown values; exact display strings in the guard; stale map actions refused on replay (fix round 3, m-1 m-3 m-6)
eyalgolan00a863f15a13
diff --git a/sim/Statecraft.Sim.Tests/MapActionTests.cs b/sim/Statecraft.Sim.Tests/MapActionTests.csindex c69023238..80aec66df 100644--- a/sim/Statecraft.Sim.Tests/MapActionTests.cs+++ b/sim/Statecraft.Sim.Tests/MapActionTests.cs@@ -58,7 +58,7 @@ public class MapActionTests}[Theory]- [InlineData("airport", 2, 0, "Unknown map action 'airport'")]+ [InlineData("airport", 2, 0, "There is no such map action")][InlineData("transit_camp", 0, 0, "There is no land hex at (0, 0)")][InlineData("transit_camp", 7, 7, "There is no land hex at (7, 7)")][InlineData("transit_camp", 4, 0, "(4, 0) is more than 1 hex from a settlement")]@@ -116,7 +116,7 @@ public class MapActionTestsAssert.Equal("There is no land hex at (7, 7)", MapActions.Check(C, s, "housing", 7, 7));Assert.Equal("(4, 0) is more than 1 hex from a settlement", MapActions.Check(C, s, "housing", 4, 0));Assert.Equal("(2, 0) already has a housing project", MapActions.Check(C, s, "housing", 2, 0));- Assert.Equal("Unknown map action 'airport'", MapActions.Check(C, s, "airport", 0, 1));+ Assert.Equal("There is no such map action", MapActions.Check(C, s, "airport", 0, 1));Assert.Equal("No more than 2 map actions in one turn", MapActions.Check(C, s, "housing", 0, 1));}@@ -128,7 +128,7 @@ public class MapActionTestsvar c = ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["rules.yaml"] = rules }));var s = Simulation.NewGame(c, 42, Mode.Historical);s.Date = new DateTime(1948, 6, 1);- Assert.Equal("Map action 'housing' is not available in this content", MapActions.Check(c, s, "housing", 2, 0));+ Assert.Equal("That map action is not available in this game", MapActions.Check(c, s, "housing", 2, 0));}[Fact]@@ -210,4 +210,27 @@ public class MapActionTestsvar s1949 = GoldenRun.Play(c, new DateTime(1949, 6, 1));Assert.Equal("Transit camps and housing projects can be placed from 1 January 1950", MapActions.Check(c, s1949, "housing", tlv.Q, tlv.R));}++ /// <summary>Fix round 3 (m-6): a save that built something the content no longer defines still loads: the replay+ /// refuses that order with a reason (no id, no exception), so no placement of it exists and no formatter ever meets+ /// the stale id.</summary>+ [Fact]+ public void AStaleMapActionInASaveIsRefusedOnReplayNotACrash()+ {+ var g = GameSession.New(C, 7, Mode.Historical);+ while (g.State.Date < C.Rules.MapActionsFrom) g.Step(new Command[0]);+ var hex = C.Hexes.First(h => MapActions.Check(C, g.State, MapActions.Housing, h.Q, h.R) == null);+ g.Step(new Command[] { new MapAction(MapActions.Housing, hex.Q, hex.R) });+ Assert.NotNull(MapActions.At(g.State, hex.Q, hex.R));+ var save = g.Save.Write();+ var rules = Fixture.Files["rules.yaml"].Replace(" housing: { cost: 8, housing: 0.08, morale: 0.01, name: a housing project }\n", "");+ Assert.NotEqual(Fixture.Files["rules.yaml"], rules);+ var stale = ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["rules.yaml"] = rules }));+ var loaded = GameSession.Load(stale, save, out var warnings);+ Assert.Contains(warnings, w => w.Contains("different game content"));+ Assert.Null(MapActions.At(loaded.State, hex.Q, hex.R));+ var refused = Assert.Single(loaded.Last!.Rejected);+ Assert.Equal("That map action is not available in this game", refused.Reason);+ Assert.Equal("That map action is not available in this game", MapActions.Check(stale, loaded.State, MapActions.Housing, hex.Q, hex.R));+ }}diff --git a/sim/Statecraft.Sim.Tests/PlayerTextTests.cs b/sim/Statecraft.Sim.Tests/PlayerTextTests.csindex 70f660645..f49b51471 100644--- a/sim/Statecraft.Sim.Tests/PlayerTextTests.cs+++ b/sim/Statecraft.Sim.Tests/PlayerTextTests.cs@@ -90,32 +90,37 @@ public class PlayerTextTests.Concat(c.Situations.Select(x => x.Id)).Concat(c.Money.Select(x => x.Id)).Concat(c.Regions.Select(x => x.Id)).Concat(c.Offices.Select(x => x.Id)).Concat(c.Events.Select(x => x.Id)).Concat(c.Rules.MapActions.Keys).Distinct();- /// <summary>Where an id would appear in a Cause: a snake_case id anywhere as a whole word; a one-word id ("morale",- /// "treasury", "asia") at the start, alone in parentheses or a list, before a signed number, "set to", "=",- /// "began"/"ended", or after "from" (case-sensitive: the display names are capitalised or phrased).</summary>- static Regex IdPattern(string id)+ /// <summary>Fix round 3 (m-3): sim prose that legitimately contains a lowercase word that is also a content id. Each+ /// entry is the exact phrase, never the bare word (<see cref="EverySimPhraseIsRealSimText"/>).</summary>+ internal static readonly string[] SimPhrases = { "the minimum for security level" }; // SecuritySystem's ladder, not the indicator++ /// <summary>The exact display strings that may contain such a word: content names (indicators, policies, effects,+ /// situations, map actions, offices, parties, people, governments, legislatures), event titles and option texts (a "{office}" placeholder matches the filled-in name), each+ /// also capitalised to start a sentence, plus <see cref="SimPhrases"/>.</summary>+ static List<Regex> DisplayStrings(Content c){- var e = Regex.Escape(id);- return EnglishWords.ContainsKey(id)- ? new Regex($@"(^|\(|, |/|from ){e}(\)|,|/|:| [+-]?\d| set to| =| began| ended| at \d|$)")- : new Regex($@"\b{e}\b");+ var texts = c.Indicators.Select(x => x.Name).Concat(c.Policies.Select(x => x.Name)).Concat(c.Effects.Select(x => x.Name))+ .Concat(c.Situations.Select(x => x.Name)).Concat(c.Rules.MapActions.Values.Select(x => x.Name))+ .Concat(c.Events.Select(e => e.Title)).Concat(c.Events.SelectMany(e => e.Options.Select(o => o.Text)))+ .Concat(c.Offices.Select(o => o.Title)).Concat(c.Parties.Select(p => p.Name)).Concat(c.People.Select(p => p.Name))+ .Concat(c.Governments.Select(g => g.Name)).Concat(c.Legislatures.Select(l => l.Body)).Concat(SimPhrases);+ return texts.SelectMany(t => new[] { t, PlayerText.Capitalised(t) }).Distinct()+ .OrderByDescending(t => t.Length) // longest first, so a phrase is removed whole+ .Select(t => new Regex(string.Join(".+?", Regex.Split(t, @"\{[a-z_]+\}").Select(Regex.Escape)))).ToList();}- /// <summary>Fix round 2 (N3): ids are matched anywhere as whole words, except these, which are also ordinary English in- /// player text (each with where it legitimately appears; <see cref="EveryAllowedWordIsRealEnglishInPlayerText"/>).- /// They are still caught wherever an id would sit.</summary>- static readonly Dictionary<string, string> EnglishWords = new Dictionary<string, string>- {- ["morale"] = "Homes lift morale", // effect names (model.yaml)- ["housing"] = "a housing project", // the map action's phrase (rules.yaml) and "New housing"- ["rationing"] = "Impose rationing", // event option text (events/austerity_1949.yaml)- ["security"] = "the minimum for security level", // the security ladder (SecuritySystem), not the indicator- };-+ /// <summary>Every content id found as a whole word anywhere in a message once the exact display strings are removed.</summary>static List<string> Leaks(Content c, IEnumerable<string> messages){- var patterns = ContentIds(c).Select(id => (id, re: IdPattern(id))).ToList();- return messages.Distinct().SelectMany(m => patterns.Where(p => p.re.IsMatch(m)).Select(p => $"'{p.id}' in: {m}")).ToList();+ var display = DisplayStrings(c);+ var patterns = ContentIds(c).Select(id => (id, re: new Regex($@"\b{Regex.Escape(id)}\b"))).ToList();+ var leaks = new List<string>();+ foreach (var m in messages.Distinct())+ {+ var rest = display.Aggregate(m, (acc, d) => d.Replace(acc, " "));+ leaks.AddRange(patterns.Where(p => p.re.IsMatch(rest)).Select(p => $"'{p.id}' in: {m}"));+ }+ return leaks;}/// <summary>The player's own commands from mid-1950 on (historical play up to then): policy votes (passed and lost),@@ -212,46 +217,37 @@ public class PlayerTextTests[InlineData("Morale +4.1% (homes_lift_morale)", "homes_lift_morale")][InlineData("Food supply +5.0% (austerity_1949)", "austerity_1949")][InlineData("transit_camp at (3, 4): cost IL 1.5m", "transit_camp")]+ [InlineData("rationing set to 0.50 (Austerity)", "rationing")]public void TheGuardCatchesOneWordIds(string message, string id) =>Assert.Contains(Leaks(Real, new[] { message }), l => l.StartsWith($"'{id}'", StringComparison.Ordinal));+ /// <summary>Fix round 3 (m-3): only the exact display strings are allowed; the bare word is caught anywhere.</summary>[Theory]+ [InlineData("the morale fell", "morale")][InlineData("morale: low", "morale")]- [InlineData("the morale fell", "morale")] // plain English: not an id position, so allowed- public void AllowedWordsAreStillCaughtWhereAnIdWouldSit(string message, string id)- {- var caught = Leaks(Real, new[] { message }).Any(l => l.StartsWith($"'{id}'", StringComparison.Ordinal));- Assert.Equal(message.StartsWith(id, StringComparison.Ordinal), caught);- }-- [Theory]+ [InlineData("housing fell this turn", "housing")]+ [InlineData("a cut in rationing", "rationing")]+ [InlineData("security fell", "security")][InlineData("of asia", "asia")][InlineData("the treasury fell", "treasury")][InlineData("Ends with debt", "debt")]- public void OtherOneWordIdsAreCaughtAnywhere(string message, string id) =>+ public void AnIdIsCaughtAnywhereOutsideADisplayString(string message, string id) =>Assert.Contains(Leaks(Real, new[] { message }), l => l.StartsWith($"'{id}'", StringComparison.Ordinal));- /// <summary>Each allowed English word really is display text from content or sim prose, so allowing it hides nothing.</summary>+ /// <summary>Each sim phrase is what the sim really writes.</summary>[Fact]- public void EveryAllowedWordIsRealEnglishInPlayerText()+ public void EverySimPhraseIsRealSimText(){- var display = Real.Effects.Select(e => e.Name).Concat(Real.Rules.MapActions.Values.Select(a => a.Name))- .Concat(Real.Events.SelectMany(e => e.Options.Select(o => o.Text))).ToList();- foreach (var kv in EnglishWords)- {- Assert.Contains(kv.Key, kv.Value.Split(' '));- if (kv.Key == "security") continue; // sim prose: SecuritySystem's ladder message- Assert.Contains(display, d => Regex.IsMatch(d, $@"\b{kv.Key}\b"));- }var s = Simulation.NewGame(Real, 1, Mode.Historical);var why = new List<Cause>();SecuritySystem.SetMobilization(Real, s, 0, why); // below the floor: the ladder message- Assert.Contains(why, w => w.Message.Contains(EnglishWords["security"]));+ foreach (var phrase in SimPhrases) Assert.Contains(why, w => w.Message.Contains(phrase));}[Theory]- [InlineData("Impose rationing")]- [InlineData("A housing project at (3, 4): cost IL 1.5m")]+ [InlineData("chosen: Impose rationing")]+ [InlineData("A housing project at (3, 4): cost IL 1.5m, Housing +3.0%, Morale +1.0%")]+ [InlineData("Morale -10.9% (Rationing wears on morale, Homes lift morale, Safety lifts morale)")][InlineData("reserve call-up raised to 80%, the minimum for security level 5")]public void TheGuardLeavesPlainEnglishAlone(string message) => Assert.Empty(Leaks(Real, new[] { message }));@@ -398,4 +394,17 @@ public class PlayerTextTestsAssert.Equal(file, ex.File);Assert.Equal(field, ex.Field);}++ // ---- Fix round 3 ----++ /// <summary>m-1: a shown change is the difference of the two shown (rounded) values, so it always agrees with them.</summary>+ [Theory]+ [InlineData(0.626, 0.634, "0%")] // "63%" -> "63%": no change shown (rounded difference would say "+1%")+ [InlineData(0.624, 0.626, "+1%")] // "62%" -> "63%": "+1%" (the raw difference rounds to "0%")+ [InlineData(0.55, 0.50, "-5%")]+ public void TheShownChangeIsTheDifferenceOfTheShownValues(double before, double now, string shown)+ {+ Assert.Equal(shown, PlayerText.PercentChangeShown(before, now));+ Assert.Equal(PlayerText.ShownPoints(now) - PlayerText.ShownPoints(before), double.Parse(shown.TrimEnd('%'), System.Globalization.CultureInfo.InvariantCulture));+ }}diff --git a/sim/Statecraft.Sim/MapActions.cs b/sim/Statecraft.Sim/MapActions.csindex cab5befc4..97b3a4fd8 100644--- a/sim/Statecraft.Sim/MapActions.cs+++ b/sim/Statecraft.Sim/MapActions.cs@@ -39,9 +39,11 @@ namespace Statecraft.Sim/// <summary>Null when <paramref name="action"/> may be placed on (q, r) now, else the reason shown to the player.</summary>public static string? Check(Content c, GameState s, string action, int q, int r){- if (!Actions.Contains(action)) return $"Unknown map action '{action}'";+ // No id in either reason: a command from an old save or a stale UI may name an action this content lacks; the+ // replay refuses it here, before anything is formatted, so a placement always has its rule (fix round 3, m-6).+ if (!Actions.Contains(action)) return "There is no such map action";var rule = c.Rules.MapActions.TryGetValue(action, out var x) ? x : null;- if (rule == null) return $"Map action '{action}' is not available in this content";+ if (rule == null) return "That map action is not available in this game";if (s.Date < c.Rules.MapActionsFrom)return $"Transit camps and housing projects can be placed from {PlayerText.Date(c.Rules.MapActionsFrom)}";if (action == TransitCamp && !(s.Policies.TryGetValue(MaabarotPolicy, out var m) && m >= 0.5))diff --git a/sim/Statecraft.Sim/PlayerText.cs b/sim/Statecraft.Sim/PlayerText.csindex b911deade..7d35a5183 100644--- a/sim/Statecraft.Sim/PlayerText.cs+++ b/sim/Statecraft.Sim/PlayerText.cs@@ -71,10 +71,17 @@ namespace Statecraft.Sim/// <summary>An indicator level (0..1) or a share on the scale the player sees: 0..100.</summary>public static double Scale(double level) => Finite(level, nameof(level)) * 100;+ /// <summary>The value the player sees for a level, in points: 0.626 shows as 63.</summary>+ public static double ShownPoints(double level) => Math.Round(Scale(level), 0, MidpointRounding.AwayFromZero);++ /// <summary>The change between two shown levels: the difference of the two shown (rounded) values, so it always+ /// agrees with them (0.626 -> 0.634 shows "63%" -> "63%" and no change; 0.624 -> 0.626 shows "+1%").</summary>+ public static string PercentChangeShown(double before, double now) => PercentChange((ShownPoints(now) - ShownPoints(before)) / 100, 0);+/// <summary>"55%" for a level of 0.55; zero is "0%".</summary>public static string Percent(double level){- var r = Math.Round(Scale(level), 0, MidpointRounding.AwayFromZero);+ var r = ShownPoints(level); // the same rounding as PercentChangeShownreturn Signed(r, "", Fixed(Math.Abs(r), 0) + "%", signed: false);}