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(ui): the top bar's percentages through PlayerText only; a guard over all scripts (fix round 2, N4 N5)

eyalgolan4a5e4d7b836a
diff --git a/unity/Assets/Scripts/UI/HudModel.cs b/unity/Assets/Scripts/UI/HudModel.cs
index 6d6a74471..8b7a6adff 100644
--- a/unity/Assets/Scripts/UI/HudModel.cs
+++ b/unity/Assets/Scripts/UI/HudModel.cs
@@ -39,11 +39,10 @@ namespace Statecraft.Game.UI
}
Meter Points(double now, double? before)
{
- var v = Math.Round(PlayerText.Scale(now), MidpointRounding.AwayFromZero);
- var value = t.Format("hud.percent", v);
+ var value = PlayerText.Percent(now); // the one formatter (fix round 2, N4)
if (before == null) return new Meter(value, "", 0);
- var d = v - Math.Round(PlayerText.Scale(before.Value), MidpointRounding.AwayFromZero);
- return d == 0 ? new Meter(value, "", 0) : new Meter(value, t.Format("hud.delta_percent", d), Math.Sign(d));
+ var delta = PlayerText.PercentChange(now - before.Value, 0);
+ return delta == "0%" ? new Meter(value, "", 0) : new Meter(value, delta, Math.Sign(now - before.Value));
}
Meter People(double now, double? before)
{
diff --git a/unity/Assets/Tests/EditMode/HudTests.cs b/unity/Assets/Tests/EditMode/HudTests.cs
index e95bde8a6..bc1a7b8b4 100644
--- a/unity/Assets/Tests/EditMode/HudTests.cs
+++ b/unity/Assets/Tests/EditMode/HudTests.cs
@@ -353,15 +353,32 @@ public class HudTests
Assert.AreEqual("-IL 3.0m", m.Treasury.Delta);
}
- /// <summary>Fix round 1, I2: Unity never rescales a sim number itself; 0..1 values go through PlayerText.Scale or
- /// PlayerText.Percent (the minimap's layout percentages are not numbers the player reads).</summary>
+ /// <summary>Fix round 2 (N4, N5): no script rescales a sim number for display itself: 0..1 values go through
+ /// PlayerText.Scale / Percent / PercentChange. Scans all of Assets/Scripts for "* 100", "100f", "/ 0.01" and "100.0".
+ /// Allowed, each for a reason that is not a displayed number:</summary>
+ static readonly (string file, string contains, string why)[] AllowedScale =
+ {
+ ("MinimapPresenter.cs", "Length.Percent(", "the view frame's position and size in percent of the picture (layout, not text)"),
+ };
+
[Test]
- public void TheUiNeverMultipliesBy100Itself()
+ public void NoScriptRescalesADisplayedNumberItself()
{
- foreach (var f in System.IO.Directory.GetFiles("Assets/Scripts/UI", "*.cs"))
+ var pattern = new System.Text.RegularExpressions.Regex(@"\*\s*100\b|\b100f\b|/\s*0\.01\b|\b100\.0\b|\*\s*100f");
+ foreach (var f in System.IO.Directory.GetFiles("Assets/Scripts", "*.cs", System.IO.SearchOption.AllDirectories))
foreach (var line in System.IO.File.ReadAllLines(f))
- if (System.Text.RegularExpressions.Regex.IsMatch(line, @"\*\s*100\b") && !line.Contains("Length.Percent"))
+ if (pattern.IsMatch(line) && !System.Array.Exists(AllowedScale, a => System.IO.Path.GetFileName(f) == a.file && line.Contains(a.contains)))
Assert.Fail($"{System.IO.Path.GetFileName(f)}: {line.Trim()}");
+ foreach (var a in AllowedScale) Assert.IsNotEmpty(a.why);
+ }
+
+ /// <summary>Fix round 2 (N4): percentages have one formatter, PlayerText; no UI string formats a number as "%" itself.</summary>
+ [Test]
+ public void NoUiStringFormatsAPercentItself()
+ {
+ var yaml = AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.Strings).text;
+ foreach (var line in yaml.Split('\n'))
+ Assert.IsFalse(System.Text.RegularExpressions.Regex.IsMatch(line, @"\{\d+:[^}]*\}%"), "percent format in strings.en.yaml: " + line);
}
/// <summary>Fix round 1, M3: the top-bar captions are the content names in title case (a deliberate casing for
diff --git a/unity/Assets/UI/strings.en.yaml b/unity/Assets/UI/strings.en.yaml
index 4ecfb5ed7..5698fdae4 100644
--- a/unity/Assets/UI/strings.en.yaml
+++ b/unity/Assets/UI/strings.en.yaml
@@ -7,9 +7,7 @@ hud.turn: "Turn {0}"
hud.date: "{0}"
hud.attention: "Attention {0:0.0} of {1:0.0}"
hud.people: "{0:N0}"
-hud.percent: "{0:0}%"
hud.level: "Level {0} of 5"
-hud.delta_percent: "{0:+0;-0}%"
hud.delta_people: "{0:+#,##0;-#,##0}"
toast.refused: "Not done: {0}"
event.office: "{0}: {1}"