A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Tools: the ?turnlog probe times a failed step and always closes its turn (Refs #3057) (#3061)
Eyal Golan6f3d7a966db9
diff --git a/changelog.d/3057-turnlog-failed-step.md b/changelog.d/3057-turnlog-failed-step.mdnew file mode 100644index 000000000..58ec521c5--- /dev/null+++ b/changelog.d/3057-turnlog-failed-step.md@@ -0,0 +1,2 @@+- The `?turnlog` End turn probe now times a refused or failed sim step (its `step_ms` was 0.00), and always closes its+ turn, even when End turn throws, so nothing after it is timed by mistake (Refs #3057). It changes nothing in the game.diff --git a/docs/dev-setup.md b/docs/dev-setup.mdindex 6ed451581..128f5da5a 100644--- a/docs/dev-setup.md+++ b/docs/dev-setup.md@@ -64,7 +64,7 @@ Not possible without Unity and not in CI: the Unity EditMode tests, player build(every UI pointer and submit event, screen toggle and map click, with its frame), `?turnlog` (one line per End turn,`TURNLOG v=1 turn=N date=yyyy-MM-dd total_ms=X step_ms=X handlers=N Event/Handler=X ... @Section=X ...`: the simstep's milliseconds, each handler's of `Changed`, `CombatResolved` and `TurnEnded`, timed only during End turn, and- each map layer's section; `TurnProbe`, #2905), `?vprofile` (the visual frame-time profile per feature), `?shot` (screenshots at the promo poses as `SHOT` console lines) and `?kitscale=N`, `?housescale=N`, `?treescale=N` (the settlement kits' symbolic scale for city blocks, houses and trees, clamped to 5–200; defaults 60, 100, 100), `?noarmisticelines` (turns off the 1949 armistice lines drawn as true polylines over the hex bands, which are on by default, #237 and #196; a release build draws and ships only the signed-off features). In the editor, `MapCapture.Kits` takes `-kitScale`, `-houseScale` and `-treeScale`. Desktop players take `-golden`,+ each map layer's section; `TurnProbe`, #2905; sections nest, so the `@` fields do not add up: `@UnitLayer.Show` contains `@UnitLayer.Rebuild`, and `@SpecialBuildingModels` its `.Replan` and `.Residency`; in WebGL every value #2957 recorded is a multiple of 0.1 ms, so there `=0.00` means under about 0.1 ms, #3057), `?vprofile` (the visual frame-time profile per feature), `?shot` (screenshots at the promo poses as `SHOT` console lines) and `?kitscale=N`, `?housescale=N`, `?treescale=N` (the settlement kits' symbolic scale for city blocks, houses and trees, clamped to 5–200; defaults 60, 100, 100), `?noarmisticelines` (turns off the 1949 armistice lines drawn as true polylines over the hex bands, which are on by default, #237 and #196; a release build draws and ships only the signed-off features). In the editor, `MapCapture.Kits` takes `-kitScale`, `-houseScale` and `-treeScale`. Desktop players take `-golden`,`-smoke N`, `-profile`, `-camlog`, `-bootlog`, `-navlog`, `-turnlog`, `-vprofile`, `-shot` and `-noarmisticelines`. The console also shows `GAME OK <date> turn <n> hash <h>` at start and `TURN <n> <date> hash <h>` per turn.- First card in normal mode (#909): `?firstcard` keeps the start screen and decision video on, presses New Game at onceand logs `FIRST CARD OK event=<id> media=<photo|portrait fallback> wait=<ms> ms budget=<ms> ms`, or `FIRST CARD FAIL`diff --git a/unity/Assets/Scripts/Boot/TurnProbe.cs b/unity/Assets/Scripts/Boot/TurnProbe.csindex fa02df991..3c181ed73 100644--- a/unity/Assets/Scripts/Boot/TurnProbe.cs+++ b/unity/Assets/Scripts/Boot/TurnProbe.cs@@ -17,7 +17,11 @@ namespace Statecraft.Game.Boot/// A handler key is its event and <see cref="HandlerName"/> (a second handler of the same name gets "#2", and so on), in/// the order the handlers ran; a section key is "@" and its name, its time summed over the turn, in the order first seen./// Presentation only: it never changes what runs or in what order, and with the flag off every hook is one static bool- /// read (no Stopwatch read, no allocation).</summary>+ /// read (no Stopwatch read, no allocation).+ /// Reading the line (#3057): sections nest, so do not sum the "@" fields: @UnitLayer.Show contains @UnitLayer.Rebuild,+ /// and @SpecialBuildingModels contains @SpecialBuildingModels.Replan and @SpecialBuildingModels.Residency. In WebGL every+ /// value #2957 recorded is a multiple of 0.1 ms, so there "=0.00" means under about 0.1 ms, not zero (the likely cause,+ /// not checked in a browser: browsers coarsen performance.now(), which the WebGL Stopwatch reads).</summary>public static class TurnProbe{public const string Flag = "turnlog", Prefix = "TURNLOG", Version = "1";diff --git a/unity/Assets/Scripts/UI/SimBridge.cs b/unity/Assets/Scripts/UI/SimBridge.csindex a01683b88..f374bed46 100644--- a/unity/Assets/Scripts/UI/SimBridge.cs+++ b/unity/Assets/Scripts/UI/SimBridge.cs@@ -559,30 +559,33 @@ namespace Statecraft.Game.UIvar date = TurnStart.Date;Boot.TurnProbe.BeginTurn(TurnStart.Turn, date); // ?turnlog (#2905): this call's sim step and handlers, one linevar stepFrom = Boot.TurnProbe.Start();- try { r = step(Session, deferred.ToList()); } // a snapshot: the queue is cleared below- catch (Exception e) // any exception (M3 final review M7): never leave a queue that fails on every End Turn+ try // the finally closes the probed turn on every path, an exception included (#3057); off, it does nothing{- // A refusal (ArgumentException) is a clear warning, anything else the full exception: both leave a trace.- if (e is ArgumentException) UnityEngine.Debug.LogWarning("End Turn refused: " + e.Message);- else UnityEngine.Debug.LogException(e);+ try { r = step(Session, deferred.ToList()); } // a snapshot: the queue is cleared below+ catch (Exception e) // any exception (M3 final review M7): never leave a queue that fails on every End Turn+ {+ Boot.TurnProbe.Step(stepFrom); // the failed step is timed too (#3057)+ // A refusal (ArgumentException) is a clear warning, anything else the full exception: both leave a trace.+ if (e is ArgumentException) UnityEngine.Debug.LogWarning("End Turn refused: " + e.Message);+ else UnityEngine.Debug.LogException(e);+ deferred.Clear(); skipped.Clear();+ LastBatch = Array.Empty<Command>(); LastBatchDate = default(DateTime);+ 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());+ Notify(Changed, nameof(Changed));+ Notify(TurnEnded, failed.Rejected, nameof(TurnEnded));+ return failed;+ }+ Boot.TurnProbe.Step(stepFrom);+ LastBatch = batch; LastBatchDate = date;deferred.Clear(); skipped.Clear();- LastBatch = Array.Empty<Command>(); LastBatchDate = default(DateTime);- 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());+ Notify(Stepped, r, nameof(Stepped));Notify(Changed, nameof(Changed));- Notify(TurnEnded, failed.Rejected, nameof(TurnEnded));- Boot.TurnProbe.EndTurn(UnityEngine.Debug.Log);- return failed;+ Notify(CombatResolved, Engagements(r.Why.Skip(replayed)), nameof(CombatResolved));+ Notify(TurnEnded, r.Rejected, nameof(TurnEnded));+ return r;}- Boot.TurnProbe.Step(stepFrom);- LastBatch = batch; LastBatchDate = date;- deferred.Clear(); skipped.Clear();- Notify(Stepped, r, nameof(Stepped));- Notify(Changed, nameof(Changed));- Notify(CombatResolved, Engagements(r.Why.Skip(replayed)), nameof(CombatResolved));- Notify(TurnEnded, r.Rejected, nameof(TurnEnded));- Boot.TurnProbe.EndTurn(UnityEngine.Debug.Log);- return r;+ finally { Boot.TurnProbe.EndTurn(UnityEngine.Debug.Log); }}/// <summary>Switches to another session (a loaded save), dropping queued orders and the last batch's metadata.</summary>diff --git a/unity/Assets/Tests/EditMode/TurnProbeTests.cs b/unity/Assets/Tests/EditMode/TurnProbeTests.csindex 50def2bdf..7651a3efe 100644--- a/unity/Assets/Tests/EditMode/TurnProbeTests.cs+++ b/unity/Assets/Tests/EditMode/TurnProbeTests.cs@@ -131,6 +131,40 @@ public class TurnProbeTestsAssert.IsEmpty(logged);}+ /// <summary>#3057 (item 2 of #2957's review): a step that throws is still timed, so a refused turn's line shows the+ /// step's time, not step_ms=0.00.</summary>+ [Test]+ public void On_a_step_that_throws_is_still_timed()+ {+ TurnProbe.On = true;+ var bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), En, (s, orders) =>+ {+ Thread.Sleep(20);+ throw new ArgumentException("refused for the test");+ });+ UnityEngine.TestTools.LogAssert.Expect(LogType.Warning, "End Turn refused: refused for the test");+ bridge.EndTurn();+ Assert.AreEqual(1, logged.Count, "the refused turn writes its line");+ var line = logged[0];+ StringAssert.IsMatch(LineFormat.ToString(), line);+ Assert.GreaterOrEqual(double.Parse(Fields(line)["step_ms"], CultureInfo.InvariantCulture), 10, "the failed step's 20 ms: " + line);+ Assert.IsFalse(TurnProbe.Active, "the turn is closed");+ }++ /// <summary>#3057 (item 1): an exception in End turn outside the step's own try (here a step result of null, which the+ /// combat line reads after the step) still reaches the caller, and the probed turn is closed, so nothing after it is+ /// timed until the next End turn.</summary>+ [Test]+ public void On_an_exception_after_the_step_still_closes_the_turn()+ {+ TurnProbe.On = true;+ var bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), En, (s, orders) => null!);+ Assert.Throws<NullReferenceException>(() => bridge.EndTurn(), "the exception still reaches the caller");+ Assert.IsFalse(TurnProbe.Active, "the turn is closed");+ Assert.AreEqual(1, logged.Count, "its line is written");+ Assert.AreEqual(0, TurnProbe.Start(), "nothing after it is timed");+ }+[Test]public void On_the_game_runs_as_it_does_off(){