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): Cabinet screen: lineup, candidates with reasons, Appoint

eyalgolan951f316a2454
diff --git a/unity/Assets/Scripts/UI/CabinetPresenter.cs b/unity/Assets/Scripts/UI/CabinetPresenter.cs
new file mode 100644
index 000000000..6671b307b
--- /dev/null
+++ b/unity/Assets/Scripts/UI/CabinetPresenter.cs
@@ -0,0 +1,100 @@
+#nullable enable
+using System.Linq;
+using Statecraft.Sim;
+using UnityEngine.UIElements;
+
+namespace Statecraft.Game.UI
+{
+ /// <summary>The Cabinet screen: the government of the day, its coalition, and every office with its holder (portrait,
+ /// party, competence and loyalty). Selecting an office lists every person with Appoint, or the reason they cannot be
+ /// appointed (Cabinet.Check). Appointing queues the Appoint order for End Turn.</summary>
+ public sealed class CabinetPresenter : System.IDisposable
+ {
+ readonly VisualElement screen; readonly SimBridge bridge; readonly UiText text; readonly PortraitCatalog? portraits;
+
+ public CabinetPresenter(VisualElement root, SimBridge bridge, UiText text, PortraitCatalog? portraits)
+ {
+ screen = root.Q<VisualElement>("screen-cabinet"); this.bridge = bridge; this.text = text; this.portraits = portraits;
+ bridge.Changed += Render;
+ Render();
+ }
+
+ /// <summary>Unsubscribes from the bridge and removes the rows (the owner calls this when disabled).</summary>
+ public void Dispose()
+ {
+ bridge.Changed -= Render;
+ screen.Q<ScrollView>("cabinet-seats")?.Clear();
+ screen.Q<ScrollView>("cabinet-candidates")?.Clear();
+ }
+
+ /// <summary>The office whose candidates are listed, or null.</summary>
+ public string? Selected { get; private set; }
+
+ public void Select(string? office) { Selected = office; Render(); }
+
+ string Describe(Person p)
+ {
+ var c = bridge.Content; var party = Offices.PartyOf(c, p, bridge.State.Date);
+ var partyName = party == "" ? text["cabinet.no_party"] : c.Parties.First(x => x.Id == party).Name;
+ return text.Format("cabinet.person", p.Name, partyName, p.Competence, p.Loyalty);
+ }
+
+ public void Render()
+ {
+ var c = bridge.Content; var s = bridge.State;
+ var gov = Offices.Government(c, s.Date);
+ var (seats, size) = PoliticsSystem.CoalitionSeats(c, s.Date);
+ screen.Q<Label>("cabinet-government").text = gov?.Name ?? "";
+ screen.Q<Label>("cabinet-coalition").text = text.Format("cabinet.coalition", seats, size, s.CoalitionStability * 100);
+ var list = screen.Q<ScrollView>("cabinet-seats");
+ list.Clear();
+ foreach (var seat in Cabinet.Lineup(c, s))
+ {
+ var office = seat.Office.Id;
+ var row = new Button(() => Select(office)) { name = "seat-" + office };
+ row.AddToClassList("seat");
+ row.EnableInClassList("seat-selected", office == Selected);
+ var portrait = new VisualElement();
+ portrait.AddToClassList("seat-portrait");
+ var tex = portraits?.Get(seat.Holder?.Id);
+ if (tex != null) portrait.style.backgroundImage = new StyleBackground(tex);
+ portrait.style.display = tex != null ? DisplayStyle.Flex : DisplayStyle.None; // never an invented portrait
+ row.Add(portrait);
+ var who = seat.Holder == null ? text["cabinet.vacant"] : Describe(seat.Holder) + (seat.Appointed ? text["cabinet.appointed"] : "");
+ var label = new Label(text.Format("cabinet.seat", seat.Office.Title, who)) { name = "seat-" + office + "-holder" };
+ label.EnableInClassList("appointed", seat.Appointed);
+ row.Add(label);
+ list.Add(row);
+ }
+ var pick = screen.Q<VisualElement>("cabinet-pick");
+ var candidates = screen.Q<ScrollView>("cabinet-candidates");
+ candidates.Clear();
+ pick.style.display = Selected == null ? DisplayStyle.None : DisplayStyle.Flex;
+ if (Selected != null)
+ {
+ var office = c.Offices.First(o => o.Id == Selected);
+ screen.Q<Label>("cabinet-pick-title").text = text.Format("cabinet.pick", office.Title);
+ foreach (var (person, reason) in Cabinet.Candidates(c, s, Selected))
+ {
+ var p = person;
+ var row = new VisualElement { name = "cand-" + p.Id };
+ row.AddToClassList("candidate");
+ var appoint = new Button(() => Appoint(office.Id, p.Id)) { name = "appoint-" + p.Id, text = Describe(p) };
+ appoint.SetEnabled(reason == null);
+ row.Add(appoint);
+ if (reason != null) { var why = new Label(reason) { name = "cand-" + p.Id + "-reason" }; why.AddToClassList("reason"); row.Add(why); }
+ candidates.Add(row);
+ }
+ }
+ var queued = bridge.Queued.OfType<Appoint>().Select(a =>
+ text.Format("cabinet.queued", c.People.First(p => p.Id == a.Person).Name, c.Offices.First(o => o.Id == a.Office).Title));
+ screen.Q<Label>("cabinet-status").text = string.Join("\n", queued);
+ }
+
+ void Appoint(string office, string person)
+ {
+ // The bridge reports a refusal itself (Refused); the sim re-checks legality when the turn runs.
+ bridge.Queue(new Appoint(office, person));
+ }
+ }
+}
diff --git a/unity/Assets/Scripts/UI/CabinetPresenter.cs.meta b/unity/Assets/Scripts/UI/CabinetPresenter.cs.meta
new file mode 100644
index 000000000..6e0110104
--- /dev/null
+++ b/unity/Assets/Scripts/UI/CabinetPresenter.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 7b75f61026b2c4956befc2670fac7140
\ No newline at end of file
diff --git a/unity/Assets/Tests/EditMode/CabinetScreenTests.cs b/unity/Assets/Tests/EditMode/CabinetScreenTests.cs
new file mode 100644
index 000000000..41b1bc31d
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/CabinetScreenTests.cs
@@ -0,0 +1,118 @@
+using System;
+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 CabinetScreenTests
+{
+ static readonly Content C = ContentLoader.Load(Paths.Content);
+ static UiText Text => UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.Strings).text);
+
+ /// <summary>The historical game on 26 February 1950: the First Government, whose next turn ends before it falls.</summary>
+ static SimBridge In1950()
+ {
+ var g = GameSession.New(C, GoldenRun.Seed, Mode.Historical);
+ while (g.State.Date < new DateTime(1950, 2, 1)) g.Step(HistoricalAI.Commands(C, g.State));
+ return new SimBridge(g, Text);
+ }
+
+ [Test]
+ public void TheScreenListsTheGovernmentAndRefusesIllegalAppointmentsWithAReason()
+ {
+ using var ui = new UiTestKit();
+ var bridge = In1950();
+ var cabinet = new CabinetPresenter(ui.Root, bridge, Text, null);
+ Assert.AreEqual("First Government", ui.Q<Label>("cabinet-government").text);
+ StringAssert.StartsWith("Coalition: 73 of 120 seats", ui.Q<Label>("cabinet-coalition").text);
+ Assert.AreEqual("Minister of Finance: Eliezer Kaplan · Mapai · competence 8, loyalty 9", ui.Q<Label>("seat-finance_minister-holder").text);
+ UiTestKit.Press(ui.Q<Button>("seat-finance_minister"));
+ Assert.AreEqual("finance_minister", cabinet.Selected);
+ Assert.IsFalse(ui.Q<Button>("appoint-zisling").enabledSelf);
+ Assert.AreEqual("Aharon Zisling's party (Mapam) is not in the coalition", ui.Q<Label>("cand-zisling-reason").text);
+ Assert.IsTrue(ui.Q<Button>("appoint-golda_meir").enabledSelf);
+ }
+
+ [Test]
+ public void AppointingTakesEffectAtEndTurn()
+ {
+ using var ui = new UiTestKit();
+ var bridge = In1950();
+ var cabinet = new CabinetPresenter(ui.Root, bridge, Text, null);
+ cabinet.Select("finance_minister");
+ UiTestKit.Press(ui.Q<Button>("appoint-golda_meir"));
+ Assert.AreEqual("Ordered: Golda Meir to Minister of Finance, at End Turn", ui.Q<Label>("cabinet-status").text);
+ bridge.EndTurn();
+ Assert.AreEqual("golda_meir", Cabinet.Holder(C, bridge.State, "finance_minister")!.Id);
+ StringAssert.EndsWith("(your appointee)", ui.Q<Label>("seat-finance_minister-holder").text);
+ Assert.IsTrue(bridge.Session.Last!.Why.Any(w => w.Message == "Golda Meir appointed Minister of Finance, replacing Eliezer Kaplan"));
+ }
+
+ [Test]
+ public void APortraitShowsOnlyWhenTheCatalogHasOne()
+ {
+ using var ui = new UiTestKit();
+ var bridge = In1950();
+ var catalog = AssetDatabase.LoadAssetAtPath<PortraitCatalog>("Assets/UI/Portraits/PortraitCatalog.asset");
+ new CabinetPresenter(ui.Root, bridge, Text, catalog);
+ var kaplan = ui.Q<Button>("seat-finance_minister").Q<VisualElement>(className: "seat-portrait");
+ Assert.AreEqual(catalog.Get("kaplan"), kaplan.style.backgroundImage.value.texture);
+ Assert.AreEqual(DisplayStyle.Flex, kaplan.style.display.value);
+ var noPortrait = C.Offices.Select(o => (office: o, holder: Cabinet.Holder(C, bridge.State, o.Id))).First(x => x.holder != null && catalog.Get(x.holder.Id) == null);
+ var shown = ui.Q<Button>("seat-" + noPortrait.office.Id).Q<VisualElement>(className: "seat-portrait");
+ Assert.AreEqual(DisplayStyle.None, shown.style.display.value, "no invented portrait");
+ }
+
+ [Test]
+ public void EveryCandidateIsListedAndOnlyCheckDecidesWhoCanBeAppointed()
+ {
+ using var ui = new UiTestKit();
+ var bridge = In1950();
+ var cabinet = new CabinetPresenter(ui.Root, bridge, Text, null);
+ foreach (var office in new[] { "finance_minister", "chief_of_staff", "president", "prime_minister" })
+ {
+ cabinet.Select(office);
+ foreach (var (person, reason) in Cabinet.Candidates(C, bridge.State, office))
+ {
+ Assert.AreEqual(reason == null, ui.Q<Button>("appoint-" + person.Id).enabledSelf, office + " " + person.Id);
+ if (reason != null) Assert.AreEqual(reason, ui.Q<Label>("cand-" + person.Id + "-reason").text);
+ }
+ }
+ }
+
+ [Test]
+ public void ARefusalAtQueueTimeIsShown()
+ {
+ using var ui = new UiTestKit();
+ var bridge = In1950();
+ new ToastPresenter(ui.Root, bridge, Text);
+ var cabinet = new CabinetPresenter(ui.Root, bridge, Text, null);
+ cabinet.Select("finance_minister");
+ Assert.IsNull(bridge.Queue(new SetPolicy(C.Policies[0].Id, 0.5)));
+ Assert.IsNull(bridge.Queue(new SetPolicy(C.Policies[1].Id, 0.5)));
+ UiTestKit.Press(ui.Q<Button>("appoint-golda_meir"));
+ StringAssert.StartsWith("Not done: Not enough attention", ui.Q<Label>("toast").text);
+ Assert.AreEqual(2, bridge.Queued.Count);
+ }
+
+ [Test]
+ public void SelectingNothingClosesTheListAndADisposedScreenHearsNothing()
+ {
+ using var ui = new UiTestKit();
+ var bridge = In1950();
+ var cabinet = new CabinetPresenter(ui.Root, bridge, Text, null);
+ cabinet.Select("finance_minister");
+ Assert.AreEqual(DisplayStyle.Flex, ui.Q<VisualElement>("cabinet-pick").style.display.value);
+ Assert.IsTrue(ui.Q<Button>("seat-finance_minister").ClassListContains("seat-selected"));
+ cabinet.Select(null);
+ Assert.AreEqual(DisplayStyle.None, ui.Q<VisualElement>("cabinet-pick").style.display.value);
+ cabinet.Dispose();
+ ui.Q<Label>("cabinet-government").text = "untouched";
+ bridge.EndTurn();
+ Assert.AreEqual("untouched", ui.Q<Label>("cabinet-government").text);
+ }
+}
diff --git a/unity/Assets/Tests/EditMode/CabinetScreenTests.cs.meta b/unity/Assets/Tests/EditMode/CabinetScreenTests.cs.meta
new file mode 100644
index 000000000..a7ad83672
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/CabinetScreenTests.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: a3f9c557dd54744038dcf30a9ff9277f
\ No newline at end of file
diff --git a/unity/Assets/UI/Game.uxml b/unity/Assets/UI/Game.uxml
index c2d1524f7..e411fb182 100644
--- a/unity/Assets/UI/Game.uxml
+++ b/unity/Assets/UI/Game.uxml
@@ -56,7 +56,17 @@
</ui:VisualElement>
<ui:VisualElement name="info-panel" class="panel info-panel" />
<ui:VisualElement name="minimap" class="panel minimap" />
- <ui:VisualElement name="screen-cabinet" class="panel screen" />
+ <ui:VisualElement name="screen-cabinet" class="panel screen">
+ <ui:VisualElement class="screen-header"><ui:Label name="cabinet-government" class="title" /><ui:Label name="cabinet-coalition" class="muted" /></ui:VisualElement>
+ <ui:VisualElement class="row grow">
+ <ui:ScrollView name="cabinet-seats" class="grow" />
+ <ui:VisualElement name="cabinet-pick" class="grow">
+ <ui:Label name="cabinet-pick-title" class="meter-name" />
+ <ui:ScrollView name="cabinet-candidates" class="grow" />
+ </ui:VisualElement>
+ </ui:VisualElement>
+ <ui:Label name="cabinet-status" class="muted" />
+ </ui:VisualElement>
<ui:VisualElement name="screen-budget" class="panel screen">
<ui:VisualElement class="screen-header"><ui:Label text="Budget" class="title" /><ui:Label name="budget-vote" class="muted" /></ui:VisualElement>
<ui:Label name="budget-money" />
diff --git a/unity/Assets/UI/strings.en.yaml b/unity/Assets/UI/strings.en.yaml
index 5c9436fea..1782a8de9 100644
--- a/unity/Assets/UI/strings.en.yaml
+++ b/unity/Assets/UI/strings.en.yaml
@@ -57,3 +57,11 @@ why.effects: "Effects of policies"
why.situations: "Situations"
why.other: "Other"
why.nothing: "Nothing notable happened this turn."
+cabinet.coalition: "Coalition: {0} of {1} seats · stability {2:0}"
+cabinet.seat: "{0}: {1}"
+cabinet.vacant: "(vacant)"
+cabinet.person: "{0} · {1} · competence {2}, loyalty {3}"
+cabinet.no_party: "no party"
+cabinet.appointed: " (your appointee)"
+cabinet.pick: "Appoint to {0}"
+cabinet.queued: "Ordered: {0} to {1}, at End Turn"