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): Why panel shows unknown systems as Other, skips redundant rebuilds, says when a turn had no causes

eyalgolan0392e4e6fcf4
diff --git a/unity/Assets/Scripts/UI/WhyPresenter.cs b/unity/Assets/Scripts/UI/WhyPresenter.cs
index cb81af98d..ec34b43e5 100644
--- a/unity/Assets/Scripts/UI/WhyPresenter.cs
+++ b/unity/Assets/Scripts/UI/WhyPresenter.cs
@@ -1,5 +1,6 @@
#nullable enable
using System.Linq;
+using Statecraft.Sim;
using UnityEngine.UIElements;
namespace Statecraft.Game.UI
@@ -21,19 +22,30 @@ namespace Statecraft.Game.UI
/// <summary>Unsubscribes from the bridge (the owner calls this when disabled).</summary>
public void Dispose() { bridge.Changed -= Render; }
- public void Render()
+ GameSession? shownSession; StepResult? shown;
+
+ /// <summary>Shows the session's last turn. Does nothing when that same result is already on screen.</summary>
+ public void Render() => Show(bridge.Session.Last);
+
+ /// <summary>Shows <paramref name="last"/> (a turn's result, or null for "nothing yet"). The same session and result
+ /// as last time are not rebuilt; a new turn, a new game or a loaded save are.</summary>
+ public void Show(StepResult? last)
{
+ if (shownSession == bridge.Session && shown == last && screen.Q<ScrollView>("why-rows").childCount > 0) return;
+ shownSession = bridge.Session; shown = last;
var rows = screen.Q<ScrollView>("why-rows");
rows.Clear();
- var last = bridge.Session.Last; var prev = bridge.Session.Previous;
+ var prev = bridge.Session.Previous;
screen.Q<Label>("why-turn").text = last == null || prev == null ? "" : text.Format("why.turn", prev.Date, bridge.State.Date);
if (last == null) { rows.Add(new Label(text["why.empty"]) { name = "why-empty" }); return; }
+ if (last.Why.Count == 0) { rows.Add(new Label(text["why.nothing"]) { name = "why-nothing" }); return; }
int i = 0;
+ // Known systems in their fixed order, then any other system last under "Other" (never its raw id).
foreach (var system in Order.Concat(last.Why.Select(w => w.System).Distinct().Where(sys => !Order.Contains(sys))))
{
var lines = last.Why.Where(w => w.System == system).ToList();
if (lines.Count == 0) continue;
- var header = new Label(Order.Contains(system) ? text["why." + system] : system) { name = "why-group-" + system };
+ var header = new Label(Order.Contains(system) ? text["why." + system] : text["why.other"]) { name = "why-group-" + system };
header.AddToClassList("why-group");
rows.Add(header);
foreach (var w in lines)
diff --git a/unity/Assets/Tests/EditMode/WhyScreenTests.cs b/unity/Assets/Tests/EditMode/WhyScreenTests.cs
index 78f8ed7fd..0d16b357a 100644
--- a/unity/Assets/Tests/EditMode/WhyScreenTests.cs
+++ b/unity/Assets/Tests/EditMode/WhyScreenTests.cs
@@ -67,4 +67,96 @@ public class WhyScreenTests
bridge.EndTurn();
Assert.IsNotNull(ui.Root.Q<Label>("why-empty"), "still the empty note: the panel did not re-render");
}
+
+ static (UiTestKit ui, SimBridge bridge, WhyPresenter why) Open()
+ {
+ var ui = new UiTestKit();
+ var bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), Text);
+ return (ui, bridge, new WhyPresenter(ui.Root, bridge, Text));
+ }
+
+ [Test]
+ public void ASystemWithoutAGroupShowsAsOtherAndLast()
+ {
+ var (ui, bridge, why) = Open();
+ using (ui)
+ {
+ bridge.EndTurn();
+ var real = bridge.Session.Last!;
+ var why2 = new System.Collections.Generic.List<Cause>(real.Why) { new Cause("zzz", "t", 0, "something odd happened") };
+ why.Show(new StepResult(real.State, why2, real.Rejected));
+ var rows = ui.Q<ScrollView>("why-rows");
+ var headers = rows.Query<Label>(className: "why-group").ToList();
+ Assert.AreEqual("why-group-zzz", headers.Last().name);
+ Assert.AreEqual("Other", headers.Last().text);
+ Assert.IsFalse(rows.Query<Label>().ToList().Any(l => l.text == "zzz"), "the raw id is never shown");
+ Assert.AreEqual("something odd happened", rows.Query<Label>(className: "why-line").ToList().Last().text);
+ }
+ }
+
+ [Test]
+ public void EverySystemNamedInTheSimSourcesHasAGroup()
+ {
+ var text = Text;
+ var found = new System.Collections.Generic.SortedSet<string>();
+ foreach (var f in System.IO.Directory.GetFiles(System.IO.Path.Combine(Paths.RepoRoot, "sim", "Statecraft.Sim"), "*.cs"))
+ foreach (System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches(System.IO.File.ReadAllText(f), "new Cause\\(\"([a-z_]+)\""))
+ found.Add(m.Groups[1].Value);
+ Assert.Greater(found.Count, 5, "the scan found the sim's causes");
+ foreach (var sys in found)
+ {
+ CollectionAssert.Contains(WhyPresenter.Order, sys, sys);
+ Assert.DoesNotThrow(() => { var _ = text["why." + sys]; }, sys);
+ }
+ }
+
+ [Test]
+ public void ATurnWithNoCausesSaysSo()
+ {
+ var (ui, bridge, why) = Open();
+ using (ui)
+ {
+ bridge.EndTurn();
+ var real = bridge.Session.Last!;
+ why.Show(new StepResult(real.State, new Cause[0], real.Rejected));
+ Assert.AreEqual("Nothing notable happened this turn.", ui.Q<Label>("why-nothing").text);
+ }
+ }
+
+ [Test]
+ public void ASecondTurnReplacesTheFirstAndAnUnchangedResultIsNotRebuilt()
+ {
+ var (ui, bridge, why) = Open();
+ using (ui)
+ {
+ bridge.EndTurn();
+ var first = ui.Q<ScrollView>("why-rows").Query<Label>(className: "why-line").ToList();
+ why.Render();
+ Assert.AreSame(first[0], ui.Q<ScrollView>("why-rows").Query<Label>(className: "why-line").ToList()[0], "same result: no rebuild");
+ var r = bridge.EndTurn();
+ var second = ui.Q<ScrollView>("why-rows").Query<Label>(className: "why-line").ToList();
+ Assert.AreEqual(r.Why.Count, second.Count);
+ Assert.AreNotSame(first[0], second[0]);
+ Assert.AreEqual("22 May 1948 to 29 May 1948", ui.Q<Label>("why-turn").text);
+ }
+ }
+
+ [Test]
+ public void ReplacingTheSessionRerenders()
+ {
+ var (ui, bridge, why) = Open();
+ using (ui)
+ {
+ bridge.EndTurn();
+ Assert.IsNull(ui.Root.Q<Label>("why-empty"));
+ var other = GameSession.New(C, GoldenRun.Seed, Mode.Historical);
+ bridge.Replace(other);
+ Assert.IsNotNull(ui.Root.Q<Label>("why-empty"), "a new game has no last turn");
+ var loaded = GameSession.New(C, GoldenRun.Seed, Mode.Historical);
+ loaded.Step(new Command[0]);
+ bridge.Replace(loaded);
+ Assert.IsNull(ui.Root.Q<Label>("why-empty"));
+ Assert.AreEqual(loaded.Last!.Why.Count, ui.Q<ScrollView>("why-rows").Query<Label>(className: "why-line").ToList().Count);
+ }
+ }
}
diff --git a/unity/Assets/UI/strings.en.yaml b/unity/Assets/UI/strings.en.yaml
index d02c34f12..5c9436fea 100644
--- a/unity/Assets/UI/strings.en.yaml
+++ b/unity/Assets/UI/strings.en.yaml
@@ -55,3 +55,5 @@ why.map: "Building"
why.policy: "Policies"
why.effects: "Effects of policies"
why.situations: "Situations"
+why.other: "Other"
+why.nothing: "Nothing notable happened this turn."