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.

feat(ui): Why did this happen? panel grouped by system

eyalgolan0956420e9670
diff --git a/unity/Assets/Scripts/UI/WhyPresenter.cs b/unity/Assets/Scripts/UI/WhyPresenter.cs
new file mode 100644
index 000000000..cb81af98d
--- /dev/null
+++ b/unity/Assets/Scripts/UI/WhyPresenter.cs
@@ -0,0 +1,48 @@
+#nullable enable
+using System.Linq;
+using UnityEngine.UIElements;
+
+namespace Statecraft.Game.UI
+{
+ /// <summary>"Why did this happen?" (spec section 4, step 6): every cause the last turn reported, grouped by the system
+ /// that produced it, in a fixed order (what was decided first, then its consequences).</summary>
+ public sealed class WhyPresenter : System.IDisposable
+ {
+ public static readonly string[] Order = { "events", "politics", "cabinet", "security", "economy", "population", "map", "policy", "effects", "situations" };
+ readonly VisualElement screen; readonly SimBridge bridge; readonly UiText text;
+
+ public WhyPresenter(VisualElement root, SimBridge bridge, UiText text)
+ {
+ screen = root.Q<VisualElement>("screen-why"); this.bridge = bridge; this.text = text;
+ bridge.Changed += Render;
+ Render();
+ }
+
+ /// <summary>Unsubscribes from the bridge (the owner calls this when disabled).</summary>
+ public void Dispose() { bridge.Changed -= Render; }
+
+ public void Render()
+ {
+ var rows = screen.Q<ScrollView>("why-rows");
+ rows.Clear();
+ var last = bridge.Session.Last; 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; }
+ int i = 0;
+ 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 };
+ header.AddToClassList("why-group");
+ rows.Add(header);
+ foreach (var w in lines)
+ {
+ var line = new Label(w.Message) { name = "why-" + i++ };
+ line.AddToClassList("why-line");
+ rows.Add(line);
+ }
+ }
+ }
+ }
+}
diff --git a/unity/Assets/Scripts/UI/WhyPresenter.cs.meta b/unity/Assets/Scripts/UI/WhyPresenter.cs.meta
new file mode 100644
index 000000000..26ce9f281
--- /dev/null
+++ b/unity/Assets/Scripts/UI/WhyPresenter.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: c12d7584b1003465aaf5d4eac00c44fa
\ No newline at end of file
diff --git a/unity/Assets/Tests/EditMode/WhyScreenTests.cs b/unity/Assets/Tests/EditMode/WhyScreenTests.cs
new file mode 100644
index 000000000..78f8ed7fd
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/WhyScreenTests.cs
@@ -0,0 +1,70 @@
+using System.Linq;
+using NUnit.Framework;
+using Statecraft.Game;
+using Statecraft.Game.UI;
+using Statecraft.Sim;
+using UnityEditor;
+using UnityEngine;
+using UnityEngine.UIElements;
+
+public class WhyScreenTests
+{
+ static readonly Content C = ContentLoader.Load(Paths.Content);
+ static UiText Text => UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.Strings).text);
+
+ [Test]
+ public void EveryCauseOfTheLastTurnIsListedUnderItsSystem()
+ {
+ using var ui = new UiTestKit();
+ var bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), Text);
+ new WhyPresenter(ui.Root, bridge, Text);
+ Assert.AreEqual("Nothing has happened yet: end the turn to see its causes.", ui.Q<Label>("why-empty").text);
+ bridge.Queue(new SetMobilization(1.0));
+ var r = bridge.EndTurn();
+ Assert.AreEqual("15 May 1948 to 22 May 1948", ui.Q<Label>("why-turn").text);
+ var lines = ui.Q<ScrollView>("why-rows").Query<Label>(className: "why-line").ToList();
+ Assert.AreEqual(r.Why.Count, lines.Count);
+ Assert.AreEqual("Immigration and population", ui.Q<Label>("why-group-population").text);
+ Assert.IsTrue(lines.Any(l => l.text == "reserve call-up set to 100%"));
+ var groups = ui.Q<ScrollView>("why-rows").Query<Label>(className: "why-group").ToList().Select(l => l.name).ToList();
+ Assert.Less(groups.IndexOf("why-group-security"), groups.IndexOf("why-group-economy"));
+ }
+
+ [Test]
+ public void TheLinesAreTheSimsOwnMessagesInGroupOrder()
+ {
+ using var ui = new UiTestKit();
+ var bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), Text);
+ new WhyPresenter(ui.Root, bridge, Text);
+ var r = bridge.EndTurn();
+ var expected = WhyPresenter.Order.SelectMany(sys => r.Why.Where(w => w.System == sys).Select(w => w.Message)).ToList();
+ var shown = ui.Q<ScrollView>("why-rows").Query<Label>(className: "why-line").ToList().Select(l => l.text).ToList();
+ Assert.AreEqual(expected, shown);
+ }
+
+ [Test]
+ public void EverySystemTheSimReportsHasAGroupName()
+ {
+ var g = GameSession.New(C, GoldenRun.Seed, Mode.Historical);
+ var seen = new System.Collections.Generic.HashSet<string>();
+ for (int i = 0; i < 80 && g.State.GameOver == null; i++)
+ {
+ g.Step(HistoricalAI.Commands(C, g.State));
+ foreach (var w in g.Last!.Why) seen.Add(w.System);
+ }
+ CollectionAssert.IsSubsetOf(seen, WhyPresenter.Order, "a system with no group would show its raw id to players");
+ var text = Text;
+ foreach (var sys in WhyPresenter.Order) Assert.DoesNotThrow(() => { var _ = text["why." + sys]; }, sys);
+ }
+
+ [Test]
+ public void ADisposedPanelHearsNothing()
+ {
+ using var ui = new UiTestKit();
+ var bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), Text);
+ var why = new WhyPresenter(ui.Root, bridge, Text);
+ why.Dispose();
+ bridge.EndTurn();
+ Assert.IsNotNull(ui.Root.Q<Label>("why-empty"), "still the empty note: the panel did not re-render");
+ }
+}
diff --git a/unity/Assets/Tests/EditMode/WhyScreenTests.cs.meta b/unity/Assets/Tests/EditMode/WhyScreenTests.cs.meta
new file mode 100644
index 000000000..4b6ee80dc
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/WhyScreenTests.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 38aad301c5f1644e8ba1f36f530d98fa
\ No newline at end of file
diff --git a/unity/Assets/UI/Game.uxml b/unity/Assets/UI/Game.uxml
index 65860879a..c2d1524f7 100644
--- a/unity/Assets/UI/Game.uxml
+++ b/unity/Assets/UI/Game.uxml
@@ -92,7 +92,10 @@
</ui:VisualElement>
<ui:Label name="mob-cost" class="muted" />
</ui:VisualElement>
- <ui:VisualElement name="screen-why" class="panel screen" />
+ <ui:VisualElement name="screen-why" class="panel screen">
+ <ui:VisualElement class="screen-header"><ui:Label text="Why did this happen?" class="title" /><ui:Label name="why-turn" class="muted" /></ui:VisualElement>
+ <ui:ScrollView name="why-rows" class="grow" />
+ </ui:VisualElement>
<ui:Label name="toast" class="panel toast" />
</ui:VisualElement>
</ui:UXML>
diff --git a/unity/Assets/UI/strings.en.yaml b/unity/Assets/UI/strings.en.yaml
index 71694c80c..d02c34f12 100644
--- a/unity/Assets/UI/strings.en.yaml
+++ b/unity/Assets/UI/strings.en.yaml
@@ -43,3 +43,15 @@ budget.money: "Treasury {0:0.0}m · debt {1:0.0}m · foreign currency ${2:0.0}m"
budget.flow: "Taxes {0:0.0}m, spending {1:0.0}m and reserve call-up {2:0.0}m a year"
budget.queued: "Submitted: takes effect at End Turn"
budget.unsaved: "Changed: submit to put it to the vote"
+why.turn: "{0:d MMMM yyyy} to {1:d MMMM yyyy}"
+why.empty: "Nothing has happened yet: end the turn to see its causes."
+why.events: "Events"
+why.politics: "Politics"
+why.cabinet: "Cabinet"
+why.security: "Security"
+why.economy: "Economy"
+why.population: "Immigration and population"
+why.map: "Building"
+why.policy: "Policies"
+why.effects: "Effects of policies"
+why.situations: "Situations"