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(unity): End Turn notifies every presenter even when one throws (Fixes #912) (#920)

Eyal Golaneaf0ed90556c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d73b4ab01..f5bdff040 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIB
- In the Hebrew UI the map's name pills show each row's `name_he`: the game settlements ("נהלל", "תל אביב") and
the 1948 localities that have one. The thirteen localities with no `name_he` keep their English name, and the
English UI is unchanged. The tier badge stays on the left of the pill until the owner rules on mirroring it (#880).
+- End Turn now logs a throwing `Changed` or `TurnEnded` subscriber and continues notifying the remaining presenters,
+ so a successful turn cannot leave later UI or autosave handlers stale (#912).
- The content loader now checks `name_he_review` on people rows the way it does for parties, governments,
legislatures, offices and settlements: the flag is `agent-checked` or left out, and only beside a `name_he`
(CONTENT_POLICY section 2, rule 6; #862). No people row carries the flag yet, so no row changes.
diff --git a/unity/Assets/Scripts/UI/SimBridge.cs b/unity/Assets/Scripts/UI/SimBridge.cs
index 5fcd2ca9a..ae63ff754 100644
--- a/unity/Assets/Scripts/UI/SimBridge.cs
+++ b/unity/Assets/Scripts/UI/SimBridge.cs
@@ -116,6 +116,22 @@ namespace Statecraft.Game.UI
string? Refuse(string reason) { Refused?.Invoke(reason); return reason; }
+ static void Notify(Action? handlers)
+ {
+ if (handlers == null) return;
+ foreach (Action handler in handlers.GetInvocationList())
+ try { handler(); }
+ catch (Exception e) { UnityEngine.Debug.LogException(e); }
+ }
+
+ static void Notify<T>(Action<T>? handlers, T value)
+ {
+ if (handlers == null) return;
+ foreach (Action<T> handler in handlers.GetInvocationList())
+ try { handler(value); }
+ catch (Exception e) { UnityEngine.Debug.LogException(e); }
+ }
+
/// <summary>Queues an order (replacing one with the same key). Returns null, or why it was refused here: an id
/// the content does not have, the game is over, or the order costs more attention than is left. A refusal is
/// also raised as <see cref="Refused"/>.</summary>
@@ -158,14 +174,14 @@ namespace Statecraft.Game.UI
LastBatch = Array.Empty<Command>();
var reason = text["order.failed"]; // the message stays in the log above, not on screen (#622)
var failed = new StepResult(State, new Cause[0], batch.Select(x => new Rejection(x, reason)).ToList());
- Changed?.Invoke();
- TurnEnded?.Invoke(failed.Rejected);
+ Notify(Changed);
+ Notify(TurnEnded, failed.Rejected);
return failed;
}
LastBatch = batch; LastBatchDate = date;
queue.Clear();
- Changed?.Invoke();
- TurnEnded?.Invoke(r.Rejected);
+ Notify(Changed);
+ Notify(TurnEnded, r.Rejected);
return r;
}
diff --git a/unity/Assets/Tests/EditMode/HudTests.cs b/unity/Assets/Tests/EditMode/HudTests.cs
index 983e0cd0d..d8f2cfef7 100644
--- a/unity/Assets/Tests/EditMode/HudTests.cs
+++ b/unity/Assets/Tests/EditMode/HudTests.cs
@@ -471,6 +471,28 @@ public class HudTests
Assert.AreEqual("Not done: The turn could not run. Your orders were cleared. Give them again.", ui.Q<Label>("toast").text); // "boom": the log only (#622)
}
+ /// <summary>#912: the sim and autosave have advanced before End Turn notifies presenters. A broken presenter is
+ /// logged, but cannot leave every subscriber registered after it showing the previous turn.</summary>
+ [Test]
+ public void AThrowingEndTurnSubscriberDoesNotStopTheOthers()
+ {
+ var bridge = NewBridge();
+ var changed = 0;
+ var ended = 0;
+ bridge.Changed += () => throw new System.InvalidOperationException("changed presenter failed");
+ bridge.Changed += () => changed++;
+ bridge.TurnEnded += _ => throw new System.InvalidOperationException("turn-ended presenter failed");
+ bridge.TurnEnded += _ => ended++;
+ LogAssert.Expect(LogType.Exception, new System.Text.RegularExpressions.Regex("changed presenter failed"));
+ LogAssert.Expect(LogType.Exception, new System.Text.RegularExpressions.Regex("turn-ended presenter failed"));
+
+ Assert.DoesNotThrow(() => bridge.EndTurn());
+
+ Assert.AreEqual(1, bridge.State.Turn, "the simulation stepped exactly once");
+ Assert.AreEqual(1, changed, "later Changed subscribers still refresh");
+ Assert.AreEqual(1, ended, "later TurnEnded subscribers still run");
+ }
+
/// <summary>Playtest 1, item 11: pressing the open screen's button closes it, every time, with every presenter
/// GameRoot creates listening, with the button focused (as after a mouse click), and across End Turn.</summary>
[Test]