A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
UI: a new game closes the old turn report; gdd-0.3 order controls are read-only after game over (Fixes #2669) (#2689)
Eyal Golan0b86a29e5de1
diff --git a/changelog.d/2669.md b/changelog.d/2669.mdnew file mode 100644index 000000000..9c3e89092--- /dev/null+++ b/changelog.d/2669.md@@ -0,0 +1,3 @@+### Fixed++- Close the previous turn's report when New Game starts.diff --git a/unity/Assets/Scripts/UI/GddHexPanel.cs b/unity/Assets/Scripts/UI/GddHexPanel.csindex 78daf1cc1..c11e8fbb7 100644--- a/unity/Assets/Scripts/UI/GddHexPanel.cs+++ b/unity/Assets/Scripts/UI/GddHexPanel.cs@@ -124,9 +124,9 @@ namespace Statecraft.Game.UIvar target = queued?.Count ?? city.Employed;var max = Cities.MaxWorkers(s, id, bridge.Queued);var workers = Row(host, "city-workers-row");- Button(workers, "city-workers-less", "−", () => Workers(bridge, id, city.Employed, target - 1)).SetEnabled(target > 0);+ OrderButton(workers, bridge, "city-workers-less", "−", target > 0, () => Workers(bridge, id, city.Employed, target - 1));Line(workers, "city-workers", text.Format(queued == null ? "city.workers" : "city.workers_ordered", Groups(text, target)));- Button(workers, "city-workers-more", "+", () => Workers(bridge, id, city.Employed, target + 1)).SetEnabled(target < max);+ OrderButton(workers, bridge, "city-workers-more", "+", target < max, () => Workers(bridge, id, city.Employed, target + 1));// The Assign order's Attention price (the queue's, at this turn's start) and when it applies (playtest B7): the// Jobs line stays the sim's actual count until End turn. Changing a queued target does not buy another order.Line(host, "city-workers-cost", bridge.Content.UsesAttention@@ -141,8 +141,8 @@ namespace Statecraft.Game.UI{var ordered = (bridge.QueuedLike(new Train(id, type.Id, 1)) as Train)?.Count ?? 0;var row = Row(host, $"city-train-{type.Id}-row");- Button(row, "city-train-" + type.Id, text.Format("city.train_" + type.Id, PlayerText.Money(c, "treasury", type.TrainingCost), Turns(text, type.TrainingTurns)),- () => bridge.Queue(new Train(id, type.Id, ordered + 1))).SetEnabled(free >= 1);+ OrderButton(row, bridge, "city-train-" + type.Id, text.Format("city.train_" + type.Id, PlayerText.Money(c, "treasury", type.TrainingCost), Turns(text, type.TrainingTurns)),+ free >= 1, () => bridge.Queue(new Train(id, type.Id, ordered + 1)));if (ordered > 0) Line(row, $"city-train-{type.Id}-ordered", text.Format("city.train_ordered", ordered));}@@ -151,8 +151,8 @@ namespace Statecraft.Game.UIvar row = Row(host, $"city-cohort-{k.Id}-row");Line(row, $"city-cohort-{k.Id}", text.Format("city.cohort", text["city.track_" + k.Type], Groups(text, k.Count), Turns(text, k.TurnsLeft)));var cancelled = bridge.QueuedLike(new CancelTraining(k.Id)) != null;- Button(row, $"city-cohort-{k.Id}-cancel", text[cancelled ? "city.cancel_ordered" : "city.cancel"],- () => bridge.Queue(new CancelTraining(k.Id))).SetEnabled(!cancelled);+ OrderButton(row, bridge, $"city-cohort-{k.Id}-cancel", text[cancelled ? "city.cancel_ordered" : "city.cancel"],+ !cancelled, () => bridge.Queue(new CancelTraining(k.Id)));}// Return and Reassign (#1511, R-QUAL-002 to R-QUAL-005): one group a press, like Train, shown only where the city has@@ -163,7 +163,8 @@ namespace Statecraft.Game.UI{var back = (bridge.QueuedLike(new ReturnCraftsmen(id, 1)) as ReturnCraftsmen)?.Count ?? 0;var row = Row(host, "city-return-craftsmen-row");- Button(row, "city-return-craftsmen", text["city.return_craftsmen"], () => bridge.Queue(new ReturnCraftsmen(id, back + 1))).SetEnabled(back < city.Craftsmen);+ OrderButton(row, bridge, "city-return-craftsmen", text["city.return_craftsmen"], back < city.Craftsmen,+ () => bridge.Queue(new ReturnCraftsmen(id, back + 1)));if (back > 0) Line(row, "city-return-craftsmen-ordered", text.Format("city.train_ordered", back));}// Training.Available already takes this turn's Train, Reinforce, Assign and Reassign orders off the idle civilians.@@ -181,8 +182,8 @@ namespace Statecraft.Game.UIvar again = bridge.QueuedLike(Order(1));var ordered = again is ReassignCraftsmen rc ? rc.Count : again is ReassignPioneers rp ? rp.Count : 0;var row = Row(host, $"city-reassign-{role}-row");- Button(row, "city-reassign-" + role, text.Format("city.reassign_" + role, have), () => bridge.Queue(Order(ordered + 1)))- .SetEnabled(ordered < left && idleLeft >= 1);+ OrderButton(row, bridge, "city-reassign-" + role, text.Format("city.reassign_" + role, have), ordered < left && idleLeft >= 1,+ () => bridge.Queue(Order(ordered + 1)));if (ordered > 0) Line(row, $"city-reassign-{role}-ordered", text.Format("city.train_ordered", ordered));}var hex = c.SettlementHexes.First(h => h.Id == id); // every settlement has one@@ -198,8 +199,8 @@ namespace Statecraft.Game.UI{var offer = PioneerReturnOrder(bridge, unit);if (!offer.Eligible) return;- Button(host, $"pioneer-return-{unit}", text.Format("pioneer.return", Group(text, bridge.State, unit)),- () => PioneerReturnOrder(bridge, unit, submit: true)).SetEnabled(!offer.WorkOrdered);+ OrderButton(host, bridge, $"pioneer-return-{unit}", text.Format("pioneer.return", Group(text, bridge.State, unit)), !offer.WorkOrdered,+ () => PioneerReturnOrder(bridge, unit, submit: true));}/// <summary>The one guarded Return for both the rows and the pioneer card (#1549): Eligible when@@ -257,10 +258,10 @@ namespace Statecraft.Game.UI{// PioneerWork and StopWork share the unit's key: an order for this unit at this site is this button's.var ordered = bridge.QueuedLike(new PioneerWork(u.Id, action, site.Id)) is PioneerWork p && p.Site == site.Id;- Button(host, $"site-build-{u.Id}", ordered ? text["site.ordered"]+ OrderButton(host, bridge, $"site-build-{u.Id}", ordered ? text["site.ordered"]: text.Format("site.build" + key, Group(text, s, u.Id), PlayerText.Money(c, "treasury", terms.Cost),Turns(text, Projects.TurnsToBuild(c, s, u.Id, site, terms.Turns, 0))),- () => bridge.Queue(new PioneerWork(u.Id, action, site.Id))).SetEnabled(!ordered && !taken);+ !ordered && !taken, () => bridge.Queue(new PioneerWork(u.Id, action, site.Id)));PioneerReturn(host, bridge, text, u.Id);}}@@ -270,7 +271,8 @@ namespace Statecraft.Game.UIGroupLink(host, bridge, text, unit, selectPioneer);Line(host, "site-state", text.Format("site.building" + key, Group(text, s, unit), Turns(text, Projects.TurnsToBuild(c, s, unit, site, work.TurnsLeft, work.Progress))));var stopping = bridge.QueuedLike(new StopWork(unit)) is StopWork;- Button(host, "site-stop", text[stopping ? "site.ordered" : "site.stop" + key], () => bridge.Queue(new StopWork(unit))).SetEnabled(!stopping);+ OrderButton(host, bridge, "site-stop", text[stopping ? "site.ordered" : "site.stop" + key], !stopping,+ () => bridge.Queue(new StopWork(unit)));}else Line(host, "site-state", text.Format("site.built" + key, Date(text, work.BuiltOn.Value)));}@@ -298,10 +300,10 @@ namespace Statecraft.Game.UIforeach (var u in idle){var ordered = bridge.QueuedLike(new PioneerWork(u.Id, action, target)) is PioneerWork p && p.Site == target;- Button(host, $"corridor-build-{u.Id}", ordered ? text["site.ordered"]+ OrderButton(host, bridge, $"corridor-build-{u.Id}", ordered ? text["site.ordered"]: text.Format("corridor.build_" + d.Kind, Group(text, s, u.Id), PlayerText.Money(c, "treasury", terms.Cost),Turns(text, Corridors.TurnsToBuild(c, s, u.Id, terms.Turns, 0))),- () => bridge.Queue(new PioneerWork(u.Id, action, target))).SetEnabled(!ordered && !taken);+ !ordered && !taken, () => bridge.Queue(new PioneerWork(u.Id, action, target)));PioneerReturn(host, bridge, text, u.Id);}}@@ -311,7 +313,8 @@ namespace Statecraft.Game.UIGroupLink(host, bridge, text, unit, selectPioneer);Line(host, "corridor-state", text.Format("site.building", Group(text, s, unit), Turns(text, Corridors.TurnsToBuild(c, s, unit, terms.Turns - work.Progress, work.Carry))));var stopping = bridge.QueuedLike(new StopWork(unit)) is StopWork;- Button(host, "corridor-stop", text[stopping ? "site.ordered" : "site.stop"], () => bridge.Queue(new StopWork(unit))).SetEnabled(!stopping);+ OrderButton(host, bridge, "corridor-stop", text[stopping ? "site.ordered" : "site.stop"], !stopping,+ () => bridge.Queue(new StopWork(unit)));}else Line(host, "corridor-state", text.Format("corridor.built", Date(text, work.BuiltOn.Value)));}@@ -340,8 +343,8 @@ namespace Statecraft.Game.UI{Line(host, "city-project-state", text.Format("city.project_building", Kind(project.Kind), townName(project.City), Turns(text, Projects.CityTurnsToBuild(c, s, project.Kind, project.TurnsLeft, project.Progress))));var cancelled = bridge.QueuedLike(new CancelProject(project.Id)) != null;- Button(host, "city-project-cancel", text[cancelled ? "city.cancel_ordered" : "city.cancel"],- () => bridge.Queue(new CancelProject(project.Id))).SetEnabled(!cancelled);+ OrderButton(host, bridge, "city-project-cancel", text[cancelled ? "city.cancel_ordered" : "city.cancel"],+ !cancelled, () => bridge.Queue(new CancelProject(project.Id)));}// R-HOUSE-006 (#1700): the sim takes a gdd-0.3 housing repair on its city's own hex whatever that hex holds// (Projects.CheckCityReason), so on a city's own hex the repair is still offered under the project; every other@@ -358,6 +361,10 @@ namespace Statecraft.Game.UI// on each frame; an earlier order can make a kind that the current state refuses valid.(string? kind, string? words) Refusal(ProjectKind k, string city){+ // #2669 with #2679: a finished game's preview refuses every order, which would replace the buttons with that+ // refusal; under gdd-0.3 the panel keeps them, disabled by OrderButton (its same rule), so the ended game stays+ // inspectable. Under the other rules OrderButton does not disable them, so the refusal lines stay.+ if (s.GameOver != null && c.Ruleset == SaveGame.Gdd03Ruleset) return (null, null);var press = new StartProject(k.Id, city, q, r);var old = bridge.QueuedLike(press);var queue = bridge.Queued.ToList();@@ -426,9 +433,9 @@ namespace Statecraft.Game.UIvar k = kinds[i];if (words[i] != null) { Line(list, $"city-project-{k.Id}-reason", words[i]!); continue; }var ordered = queued != null && queued.ProjectKind == k.Id && queued.City == city;- Button(list, "city-project-" + k.Id, ordered ? text.Format("city.project_ordered", ContentNames.Of(text, k))+ OrderButton(list, bridge, "city-project-" + k.Id, ordered ? text.Format("city.project_ordered", ContentNames.Of(text, k)): text.Format("city.project_start", ContentNames.Of(text, k), PlayerText.Money(c, "treasury", k.Cost), Turns(text, Projects.CityTurnsToBuild(c, s, k.Id, k.Turns, 0))),- () => bridge.Queue(new StartProject(k.Id, city, q, r))).SetEnabled(!ordered);+ !ordered, () => bridge.Queue(new StartProject(k.Id, city, q, r)));}}@@ -453,9 +460,9 @@ namespace Statecraft.Game.UIvar target = queued?.Count ?? staffed;var refusal = Workshops.Check(c, s, city, target + 1, bridge.Queued);var row = Row(host, "city-workshops-row");- Button(row, "city-workshops-less", "−", () => Staff(bridge, city, staffed, target - 1)).SetEnabled(target > 0);+ OrderButton(row, bridge, "city-workshops-less", "−", target > 0, () => Staff(bridge, city, staffed, target - 1));Line(row, "city-workshops", text.Format(queued == null ? "city.workshops_staffed" : "city.workshops_ordered", target, built));- Button(row, "city-workshops-more", "+", () => Staff(bridge, city, staffed, target + 1)).SetEnabled(refusal == null);+ OrderButton(row, bridge, "city-workshops-more", "+", refusal == null, () => Staff(bridge, city, staffed, target + 1));if (refusal != null && target < built) Line(host, "city-workshops-reason", SimBridge.ReasonText(text, refusal, c, bridge.TownNamesHe));s.Workshops.TryGetValue(project.Id, out var w);@@ -550,5 +557,14 @@ namespace Statecraft.Game.UIb.text = label; // set once on the panel, so UiDirection's change callback gives it its direction (#1915)return b;}++ /// <summary>An order-producing control: in gdd-0.3, after GameOver it is read-only like the other order panels, while+ /// disclosure and navigation controls made with <see cref="Button"/> stay available for inspecting the finished game.</summary>+ static Button OrderButton(VisualElement parent, SimBridge bridge, string name, string label, bool enabled, Action onClick)+ {+ var b = Button(parent, name, label, onClick);+ b.SetEnabled(enabled && (bridge.Content.Ruleset != SaveGame.Gdd03Ruleset || bridge.State.GameOver == null));+ return b;+ }}}diff --git a/unity/Assets/Scripts/UI/TurnReportPresenter.cs b/unity/Assets/Scripts/UI/TurnReportPresenter.csindex 44a40ae8c..06f11d40e 100644--- a/unity/Assets/Scripts/UI/TurnReportPresenter.cs+++ b/unity/Assets/Scripts/UI/TurnReportPresenter.cs@@ -17,7 +17,9 @@ namespace Statecraft.Game.UI/// <para>While it shows, the root has <see cref="OpenClass"/>: ScreenNav.DisableCovered disables the top bar, the panels/// and the decision layer under it, as under an End Turn outcome clip, which is drawn over the report (Game.uxml order)/// and disables it in turn; the report takes the keyboard focus when it is uncovered. A turn the sim refused outright- /// (SimBridge.EndTurn's failure: Session.Last does not move) shows no report; a turn with a refused order does.</para></summary>+ /// (SimBridge.EndTurn's failure: Session.Last does not move) shows no report; a turn with a refused order does. The+ /// session replaced for New Game or a load closes the old game's report. Other bridge changes leave it open (End Turn+ /// opens its new report from TurnEnded; answering the ending turn's card keeps it under the end screen).</para></summary>public sealed class TurnReportPresenter : IDisposable{/// <summary>On the "root" element while the report shows.</summary>@@ -37,6 +39,8 @@ namespace Statecraft.Game.UIreadonly VisualElement hudRoot, report; readonly ScrollView rows; readonly SimBridge bridge; readonly UiText text;readonly ScreenNav nav; readonly Button close, all;readonly Action<IReadOnlyList<Rejection>> onTurn;+ readonly Action onChanged;+ GameSession session; // the session the open report belongs toreadonly EventCallback<NavigationCancelEvent> onCancel;/// <summary>The turn last reported (or the session's last turn when this was made: a loaded game reports nothing).</summary>StepResult? seen;@@ -78,6 +82,11 @@ namespace Statecraft.Game.UIcatch (Exception e) { UnityEngine.Debug.LogException(e); Hide(); }};bridge.TurnEnded += onTurn;+ // Only a replaced session (New Game, a load: SimBridge.Replace) makes the report stale. Other changes keep it:+ // answering the ending turn's card (SimBridge.SeeEndingCard) changes nothing and leaves it under the end screen.+ session = bridge.Session;+ onChanged = () => { if (ReferenceEquals(bridge.Session, session)) return; session = bridge.Session; Hide(); };+ bridge.Changed += onChanged;Hide();}@@ -269,6 +278,7 @@ namespace Statecraft.Game.UIpublic void Dispose(){+ bridge.Changed -= onChanged;bridge.TurnEnded -= onTurn;close.clicked -= OnClose;all.clicked -= SeeAll;diff --git a/unity/Assets/Tests/EditMode/CityPanelTests.cs b/unity/Assets/Tests/EditMode/CityPanelTests.csindex 2a1a01c1c..2c8c0706f 100644--- a/unity/Assets/Tests/EditMode/CityPanelTests.cs+++ b/unity/Assets/Tests/EditMode/CityPanelTests.cs@@ -60,6 +60,36 @@ public class CityPanelTestsstatic VisualElement Host(UiTestKit ui) => ui.Q<VisualElement>(GddHexPanel.HostName);+ /// <summary>#2669: the finished game stays inspectable, but its GDD hex panel cannot offer orders. Red first: these+ /// controls previously used only their local prerequisites and remained enabled after GameOver.</summary>+ [Test]+ public void GameOverDisablesTheHexPanelsOrderControlsButNotItsDisclosure()+ {+ using var ui = new UiTestKit();+ var c = Gdd03Content(); // the shipped eras run gdd-0.1; the read-only rule is gdd-0.3's (OrderButton)+ Assert.AreEqual(SaveGame.Gdd03Ruleset, c.Ruleset, "precondition: the content under test runs gdd-0.3");+ var g = GameSession.New(c, GoldenRun.Seed, Mode.Historical);+ g.State.GameOver = GameOverCodes.MvpEnded; g.State.Policies[MapActions.MaabarotPolicy] = 1;+ var bridge = new SimBridge(g, En);+ using var panel = Panel(ui, bridge, En);+ using var end = new EndScreenPresenter(ui.Root, bridge, En, null);+ Assert.IsTrue(end.IsOpen, "precondition: the ended game opens its end screen");+ UiTestKit.Press(ui.Q<Button>("end-screen-close"));+ Assert.IsFalse(end.IsOpen, "the player has closed the end screen to inspect the map");+ panel.Show(CityHex.q, CityHex.r);++ var orders = Host(ui).Query<Button>().ToList().Concat(ui.Q<VisualElement>(GddHexPanel.ProjectsHostName).Query<Button>().ToList())+ .Where(b => b.name != GddHexPanel.ToggleName+ && !b.name.StartsWith("city-project-city-", StringComparison.Ordinal)+ && !b.name.StartsWith("pioneer-project-", StringComparison.Ordinal)).ToList();+ Assert.GreaterOrEqual(orders.Count, 3, "precondition: the city offers Assign, Train, and project controls");+ Assert.IsTrue(orders.Any(b => b.name.StartsWith("city-workers-", StringComparison.Ordinal)), "the Assign stepper is covered");+ Assert.IsTrue(orders.Any(b => b.name.StartsWith("city-train-", StringComparison.Ordinal)), "Train is covered");+ Assert.IsTrue(orders.Any(b => b.name.StartsWith("city-project-", StringComparison.Ordinal)), "city projects are covered");+ Assert.IsTrue(orders.All(b => !b.enabledSelf), "every order-producing control is disabled after GameOver");+ Assert.IsTrue(ui.Q<Button>(GddHexPanel.ToggleName).enabledSelf, "the project disclosure remains available for inspection");+ }+[Test]public void TheCityHexShowsItsCountsFromTheSim(){diff --git a/unity/Assets/Tests/EditMode/EndScreenTests.cs b/unity/Assets/Tests/EditMode/EndScreenTests.csindex 463693b21..8134015c5 100644--- a/unity/Assets/Tests/EditMode/EndScreenTests.cs+++ b/unity/Assets/Tests/EditMode/EndScreenTests.cs@@ -392,6 +392,45 @@ public class EndScreenTestsAssert.IsNull(resumed.State.GameOver);}+ /// <summary>#2669: the end screen can start New Game while the final turn's report waits underneath it. Red first:+ /// before TurnReportPresenter listened to Changed, the second press replaced the session but left that report open.</summary>+ [Test]+ public void NewGameFromTheEndScreenClosesTheLastTurnsReport()+ {+ using var k = new Kit(En, Exhausted);+ for (int turn = 0; turn < 3 && k.Bridge.State.GameOver == null; turn++) k.Bridge.EndTurn();+ Assert.IsTrue(k.End.IsOpen && k.Report.IsOpen, "precondition: the end screen covers the final turn's report");++ UiTestKit.Press(k.Ui.Q<Button>("end-screen-new-game"));+ Assert.IsTrue(k.NewGame.Armed);+ Assert.IsTrue(k.End.IsOpen && k.Report.IsOpen, "the confirmation press changes no session");++ UiTestKit.Press(k.Ui.Q<Button>("end-screen-new-game"));+ Assert.IsNull(k.Bridge.State.GameOver, "the second press starts the new game");+ Assert.IsFalse(k.End.IsOpen);+ Assert.IsFalse(k.Report.IsOpen, "the old game's report is closed");+ Assert.IsTrue(k.Q("top-bar").enabledSelf, "the old report does not cover the new game");+ }++ /// <summary>#2669 with #2536 (Codex review of 17c491b3d): answering the ending turn's card changes nothing in the game, so+ /// the final turn's report stays open under the end screen; only a replaced session closes it. Red at 17c491b3d: the+ /// report listened to every Changed, which SeeEndingCard raises.</summary>+ [Test]+ public void AnsweringTheEndingTurnsCardKeepsTheLastTurnsReport()+ {+ using var k = new Kit(En, Exhausted);+ for (int turn = 0; turn < 3 && k.Bridge.State.GameOver == null; turn++) k.Bridge.EndTurn();+ Assert.IsTrue(k.Report.IsOpen, "precondition: the final turn's report is open");+ EndedWithACard(k.Bridge.State); // the ending turn also left an objective's card waiting+ Assert.IsTrue(k.Bridge.EndingCardWaiting, "precondition: a card of the ending turn waits");+ var session = k.Bridge.Session;++ k.Bridge.SeeEndingCard("railway_complete");+ Assert.AreSame(session, k.Bridge.Session, "the answer replaces nothing");+ Assert.IsFalse(k.Bridge.EndingCardWaiting);+ Assert.IsTrue(k.Report.IsOpen, "the report stays, under the end screen");+ }+[Test]public void CancelClosesItAndClosedItTakesNoInput(){