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.

Budget: a language switch keeps the unsubmitted draft (Fixes #2708) (#2803)

Beri70fa0e2f6355
diff --git a/changelog.d/2708.md b/changelog.d/2708.md
new file mode 100644
index 000000000..84f704322
--- /dev/null
+++ b/changelog.d/2708.md
@@ -0,0 +1,3 @@
+### Fixed
+
+- Switching the language in Settings keeps the Budget changes you have not submitted yet.
diff --git a/unity/Assets/Scripts/UI/AdvisorPresenter.cs b/unity/Assets/Scripts/UI/AdvisorPresenter.cs
index b9cf071a3..a73355b93 100644
--- a/unity/Assets/Scripts/UI/AdvisorPresenter.cs
+++ b/unity/Assets/Scripts/UI/AdvisorPresenter.cs
@@ -101,6 +101,9 @@ namespace Statecraft.Game.UI
AdvisorCatalog.Message? covered;
/// <summary>The Budget screen's last word on its edit (BudgetUnsubmitted): true while an edit is not submitted.</summary>
bool budgetUnsubmitted;
+ /// <summary>Sets that state without firing budget_unsubmitted again: a Budget screen built with a kept draft (#2708)
+ /// said it before this advisor listened; the old advisor already queued the warning, and the session is kept.</summary>
+ internal bool HoldsBudgetUnsubmitted { get => budgetUnsubmitted; set => budgetUnsubmitted = value; }
public AdvisorPresenter(VisualElement root, SimBridge bridge, UiText text, AdvisorSession session,
PortraitCatalog? portraits = null, AdvisorHooks? hooks = null)
diff --git a/unity/Assets/Scripts/UI/BudgetPresenter.cs b/unity/Assets/Scripts/UI/BudgetPresenter.cs
index a8de2563b..e76ab126e 100644
--- a/unity/Assets/Scripts/UI/BudgetPresenter.cs
+++ b/unity/Assets/Scripts/UI/BudgetPresenter.cs
@@ -23,7 +23,9 @@ namespace Statecraft.Game.UI
Dictionary<string, double> draft = new Dictionary<string, double>();
GameSession session;
- public BudgetPresenter(VisualElement root, SimBridge bridge, UiText text)
+ /// <param name="keptDraft">The draft of the screen this one replaces over the same game (a language switch, #2708);
+ /// copied, so the old screen's edits cannot reach it.</param>
+ public BudgetPresenter(VisualElement root, SimBridge bridge, UiText text, IReadOnlyDictionary<string, double>? keptDraft = null)
{
screen = root.Q<VisualElement>("screen-budget"); this.bridge = bridge; this.text = text;
reset = screen.Q<Button>("budget-reset"); submit = screen.Q<Button>("budget-submit");
@@ -32,7 +34,7 @@ namespace Statecraft.Game.UI
reset.text = text["budget.reset"]; submit.text = text["budget.submit"];
reset.clicked += Reset; submit.clicked += Submit;
session = bridge.Session;
- draft = Current();
+ draft = keptDraft != null ? new Dictionary<string, double>(keptDraft) : Current();
bridge.Changed += OnChanged;
Render();
}
diff --git a/unity/Assets/Scripts/UI/GameRoot.cs b/unity/Assets/Scripts/UI/GameRoot.cs
index 82a0258ef..77bf85959 100644
--- a/unity/Assets/Scripts/UI/GameRoot.cs
+++ b/unity/Assets/Scripts/UI/GameRoot.cs
@@ -94,6 +94,10 @@ namespace Statecraft.Game.UI
bool endClosed;
/// <summary>Set by SwitchLanguage for its Begin (#2054): whether the start screen it rebuilds offered Continue.</summary>
bool keptResumed = true;
+ /// <summary>The Budget screen of the last Begin, whose unsubmitted draft a language switch hands to the next (#2708).</summary>
+ BudgetPresenter? budgetScreen;
+ /// <summary>Set by SwitchLanguage for its Begin (#2708): the old Budget screen's draft, for the rebuilt one.</summary>
+ IReadOnlyDictionary<string, double>? keptBudgetDraft;
/// <summary>The save notices of <see cref="saveNoticeSession"/>, the session Begin last started, as language-independent
/// keys, so a language switch can say them again in the new language (#2552). A New game replaces the bridge's session
/// without Begin, so the keys are said only while the kept session is still the one that raised them.</summary>
@@ -366,7 +370,7 @@ namespace Statecraft.Game.UI
outcomes = video == null ? null : Own(new OutcomePresenter(root, bridge, text, Own(new CardVideo()), mapping.outcomes,
() => card?.Render(), () => nav?.Close()));
Own(new DefencePresenter(root, bridge, text, portraits));
- var budget = Own(new BudgetPresenter(root, bridge, text));
+ var budget = budgetScreen = Own(new BudgetPresenter(root, bridge, text, keptBudgetDraft));
var why = Own(new WhyPresenter(root, bridge, text));
Own(new TimelinePresenter(root, bridge, text));
Own(new ObjectivesPresenter(root, bridge, text));
@@ -753,6 +757,9 @@ namespace Statecraft.Game.UI
},
}));
budget.UnsubmittedChanged += advisor.BudgetUnsubmitted;
+ // A kept draft (#2708) said "unsubmitted" while the screen was built, before this line listened: the advisor
+ // takes that state without a second Notice, so a put-off budget warning is not pruned after a language switch.
+ advisor.HoldsBudgetUnsubmitted = bridge.BudgetUnsubmitted(budget.Draft);
// The advisors' encyclopedia (spec 10), which replaces How to play: after the advisor, whose Show me and opening
// chain its buttons call; an advisor's Tell me more opens its pages (OpenPage above).
encyclopedia = Own(new EncyclopediaPresenter(root, text, AdvisorSession.Catalog, nav!, advisor, bridge.Content.Ruleset));
@@ -817,7 +824,10 @@ namespace Statecraft.Game.UI
endClosed = end != null && !end.IsOpen; // read before Begin disposes it
bool fromStart = start?.IsOpen == true;
keptResumed = start?.Resumed ?? true;
- Begin(inputs, 0, startScreen: fromStart, keep: Bridge);
+ keptBudgetDraft = budgetScreen?.Draft; // read before Begin disposes it; the new screen copies it
+ // Cleared even if Begin throws, so the next ordinary Begin (a New game, a load) never picks it up.
+ try { Begin(inputs, 0, startScreen: fromStart, keep: Bridge); }
+ finally { keptBudgetDraft = null; }
if (!fromStart) nav?.Open("settings");
// Focus the choice again a frame later: a screen shown this same tick cannot take the focus until its
// styles resolve, and a bare Focus() then leaves it wherever it was (#1915's tour test read end-turn).
@@ -1020,7 +1030,7 @@ namespace Statecraft.Game.UI
owned.Clear();
savePersistenceResult = null;
if (hoveredCity != null) { hoveredCity = null; HandCursor(false); } // the next Begin's pills start unlit
- companion = null; minimap = null; unseenSkipped = null; labels = null; nav = null; start = null; opening = null; card = null; outcomes = null; report = null; end = null; advisor = null; cabinet = null; newGame = null; encyclopedia = null; feedback = null; share = null; info = null; rig = null; drawn = null; drawnSites = null; units = null; Regions = null; logCamera = false;
+ companion = null; minimap = null; unseenSkipped = null; labels = null; nav = null; start = null; opening = null; card = null; outcomes = null; report = null; end = null; advisor = null; cabinet = null; budgetScreen = null; newGame = null; encyclopedia = null; feedback = null; share = null; info = null; rig = null; drawn = null; drawnSites = null; units = null; Regions = null; logCamera = false;
// A ?profile run cut short (a stopped coroutine never reaches its restore line) must not leave the terrain at the
// profile's pixel error (Task 24 review M1). Nothing else changes it, so restoring is always right.
if (map != null && map.Terrain != null) map.Terrain.heightmapPixelError = MapBuilder.TerrainPixelError;
diff --git a/unity/Assets/Tests/EditMode/HebrewUiTests.cs b/unity/Assets/Tests/EditMode/HebrewUiTests.cs
index ced075df0..f9818a685 100644
--- a/unity/Assets/Tests/EditMode/HebrewUiTests.cs
+++ b/unity/Assets/Tests/EditMode/HebrewUiTests.cs
@@ -293,6 +293,79 @@ public class HebrewUiTests
CollectionAssert.DoesNotContain(game.Bridge.NeedsOrders.ToList(), skipped);
}
+ /// <summary>#2708: the language switch rebuilds the Budget screen over the same game, and the new screen keeps the
+ /// player's unsubmitted draft (and says it is unsubmitted) instead of starting again from the submitted budget.</summary>
+ [Test]
+ public void TheLanguageSwitchKeepsAnUnsubmittedBudgetDraft()
+ {
+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
+ Object.FindFirstObjectByType<MapScene>().Build(Inputs.Map);
+ var game = Object.FindFirstObjectByType<GameRoot>();
+ game.Store = new SaveStore(saveDir);
+ game.Settings = new SettingsStore(Path.Combine(saveDir, "settings"));
+ game.Begin(Inputs, 0);
+ var office = game.Bridge!.State.Budget.Keys.OrderBy(k => k, StringComparer.Ordinal).First();
+ var expected = BudgetPresenter.Adjust(game.Bridge.State.Budget, office, BudgetPresenter.Step);
+ UiTestKit.Press(Ui.Q<Button>("nav-budget"));
+ UiTestKit.Press(Ui.Q<Button>("budget-" + office + "-up"));
+ Assert.AreEqual(En["budget.unsaved"], Ui.Q<Label>("budget-status").text, "precondition: an unsubmitted draft");
+
+ UiTestKit.Press(Ui.Q<Button>("nav-settings"));
+ UiTestKit.Press(Ui.Q<Button>("settings-language-he"));
+ Assert.IsTrue(Ui.Q("root").ClassListContains(UiLanguage.HebrewClass), "precondition: the UI was rebuilt");
+ UiTestKit.Press(Ui.Q<Button>("nav-budget"));
+ Assert.AreEqual(He["budget.unsaved"], Ui.Q<Label>("budget-status").text, "the draft is still unsubmitted");
+ Assert.IsEmpty(game.Bridge!.Queued, "nothing was submitted by the switch");
+ Assert.IsTrue(game.Advisor!.HoldsBudgetUnsubmitted, "the advisor knows the kept draft is unsubmitted, so a put-off budget warning is not pruned");
+
+ UiTestKit.Press(Ui.Q<Button>("budget-submit"));
+ var queued = (Statecraft.Sim.AllocateBudget)game.Bridge.Queued.Single();
+ foreach (var kv in expected) Assert.AreEqual(kv.Value, queued.Shares[kv.Key], 1e-9, kv.Key + ": the edited draft is what Submit sends");
+ }
+
+ /// <summary>#2708, the other side: the kept draft belongs to the game the switch rebuilt over. A New game or a load right
+ /// after the switch starts from that game's budget, with no draft.</summary>
+ [Test]
+ public void ANewGameOrALoadAfterTheLanguageSwitchStartsWithNoBudgetDraft()
+ {
+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
+ Object.FindFirstObjectByType<MapScene>().Build(Inputs.Map);
+ var game = Object.FindFirstObjectByType<GameRoot>();
+ game.Store = new SaveStore(saveDir);
+ game.Settings = new SettingsStore(Path.Combine(saveDir, "settings"));
+ game.Begin(Inputs, 0);
+ var office = game.Bridge!.State.Budget.Keys.OrderBy(k => k, StringComparer.Ordinal).First();
+ void EditThenSwitch(string language)
+ {
+ UiTestKit.Press(Ui.Q<Button>("nav-budget"));
+ UiTestKit.Press(Ui.Q<Button>("budget-" + office + "-up"));
+ Assert.IsNotEmpty(Ui.Q<Label>("budget-status").text, "precondition: an unsubmitted draft");
+ UiTestKit.Press(Ui.Q<Button>("nav-settings"));
+ UiTestKit.Press(Ui.Q<Button>("settings-language-" + language));
+ Assert.IsNotEmpty(Ui.Q<Label>("budget-status").text, "precondition: the switch kept the draft");
+ }
+ void AssertNoDraft(string what)
+ {
+ Assert.AreEqual("", Ui.Q<Label>("budget-status").text, what + ": no draft");
+ Assert.IsFalse(Ui.Q<Button>("budget-submit").enabledSelf, what + ": nothing to submit");
+ Assert.IsFalse(game.Advisor!.HoldsBudgetUnsubmitted, what + ": the advisor holds no budget warning");
+ }
+
+ EditThenSwitch(UiLanguage.Hebrew);
+ var before = game.Bridge.Session;
+ UiTestKit.Press(Ui.Q<Button>("new-game"));
+ UiTestKit.Press(Ui.Q<Button>("new-game"));
+ Assert.AreNotSame(before, game.Bridge.Session, "precondition: New game replaced the session");
+ AssertNoDraft("a New game");
+
+ EditThenSwitch(UiLanguage.English);
+ Directory.CreateDirectory(saveDir);
+ game.Store!.WriteAutosave(Statecraft.Sim.GameSession.New(Inputs.Content, 9, Statecraft.Sim.Mode.Historical).Save.Write());
+ game.Begin(Inputs, 0, startScreen: true);
+ UiTestKit.Press(Ui.Q<Button>("start-continue"));
+ AssertNoDraft("a load");
+ }
+
/// <summary>A language named in the address or on the command line (the store page's "?lang=he" link) holds for that
/// visit over the stored choice; without one the stored choice holds; with neither, English.</summary>
[Test]