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.

Test harness: scenarios run instant orders first, as live play does; Replace clears the last batch's date (Fixes #2589) (#2606)

Eyal Golan1ce0395b7b07
diff --git a/changelog.d/2589-scenario-order.md b/changelog.d/2589-scenario-order.md
new file mode 100644
index 000000000..77390d32b
--- /dev/null
+++ b/changelog.d/2589-scenario-order.md
@@ -0,0 +1,4 @@
+### Fixed
+
+- Test scenarios now run instant orders before deferred orders like live play, and replacing a game session or refusing a
+ turn clears the previous turn batch's date.
diff --git a/sim/Statecraft.Sim.Tests/ScenarioTests.cs b/sim/Statecraft.Sim.Tests/ScenarioTests.cs
index 358228bbf..f8f706029 100644
--- a/sim/Statecraft.Sim.Tests/ScenarioTests.cs
+++ b/sim/Statecraft.Sim.Tests/ScenarioTests.cs
@@ -58,6 +58,16 @@ public class ScenarioTests
Assert.Contains("no_such_policy", e.Message);
}
+ [Fact]
+ public void InstantCommandsRunBeforeDeferredCommandsAsInLivePlay()
+ {
+ var sc = Scenario.Parse(Head
+ + " - { kind: set_policy, policy: no_such_policy, level: 1.0 }\n"
+ + " - { kind: move, unit: 999, q: 0, r: 0 }\n");
+ var e = Assert.Throws<InvalidOperationException>(() => sc.Play(C));
+ Assert.Contains("rejected move", e.Message);
+ }
+
[Fact]
public void AStepAfterTheRunEndsFailsInsteadOfBeingSkipped()
{
diff --git a/sim/Statecraft.Sim.Tests/golden/manifest.txt b/sim/Statecraft.Sim.Tests/golden/manifest.txt
index d71cf8e58..cf6d45956 100644
--- a/sim/Statecraft.Sim.Tests/golden/manifest.txt
+++ b/sim/Statecraft.Sim.Tests/golden/manifest.txt
@@ -2,6 +2,7 @@
# Tests read this file. To accept a deliberate change: STATECRAFT_UPDATE_GOLDEN=1 dotnet test sim/Statecraft.sln
# rewrites this file, the snapshots and the GoldenRun.cs constants; then update docs/determinism.md and tools/web_check.py
# (GoldenManifestTests names any place that is out of date) and add a row to the rebaseline history.
+# gdd-0.3 has no GoldenRun entries: its reference-player policy is not defined, so GoldenRun refuses that ruleset.
hash1950 a08ff8504d862925563c8523a707b58ebe3bcaac0b504dc9ce7e028fc19eb0e4
hash1952 5561fc51a03a25e7bad499bd2c9d8de156dea241118ac4c80bbf2e0fb50c97ef
smoke5 d12c83ce94e3940f93ed21abe7c2c9957de099b2653961da90e87785e6405e79
diff --git a/sim/Statecraft.Sim/Scenario.cs b/sim/Statecraft.Sim/Scenario.cs
index b7afd8441..7b9d7d8f9 100644
--- a/sim/Statecraft.Sim/Scenario.cs
+++ b/sim/Statecraft.Sim/Scenario.cs
@@ -23,7 +23,8 @@ namespace Statecraft.Sim
/// set_mobilization (level), security_order (delta), appoint (office, person), map_action (action, q, r), assign_workers
/// (city, role, count), train (city, track, count), cancel_training (cohort), return_craftsmen (city, count), reassign_craftsmen (city, count), reassign_pioneers (city, track, count), pioneer_work (unit, action, site), stop_work
/// (unit), move (unit, q, r; GDD content only), unit_order (unit, order, and q, r for an attack; GDD content only). A turn with no
- /// scripted command passes; events nobody answered are delegated, as in the game. A command the sim rejects fails the run.</summary>
+ /// scripted command passes; events nobody answered are delegated, as in the game. Instant commands run before deferred
+ /// commands in each turn, as in live play. A command the sim rejects fails the run.</summary>
public sealed class Scenario
{
public string Name { get; set; } = "";
@@ -140,6 +141,7 @@ namespace Statecraft.Sim
{
var now = new List<Command>();
while (pending.Count > 0 && pending[0].OnDate <= s.Date) { now.AddRange(pending[0].Built); pending.RemoveAt(0); }
+ now = now.Where(Simulation.IsInstant).Concat(now.Where(x => !Simulation.IsInstant(x))).ToList();
var r = Simulation.Step(c, s, now);
if (r.Rejected.Any(j => !CardRefusal(j)))
{
diff --git a/unity/Assets/Scripts/UI/SimBridge.cs b/unity/Assets/Scripts/UI/SimBridge.cs
index 5d1529dfc..7037033cc 100644
--- a/unity/Assets/Scripts/UI/SimBridge.cs
+++ b/unity/Assets/Scripts/UI/SimBridge.cs
@@ -414,7 +414,7 @@ namespace Statecraft.Game.UI
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>();
+ 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);
@@ -429,11 +429,11 @@ namespace Statecraft.Game.UI
return r;
}
- /// <summary>Switches to another session (a loaded save), dropping queued orders.</summary>
+ /// <summary>Switches to another session (a loaded save), dropping queued orders and the last batch's metadata.</summary>
public void Replace(GameSession session)
{
Session = session ?? throw new ArgumentNullException(nameof(session));
- LastBatch = Array.Empty<Command>();
+ LastBatch = Array.Empty<Command>(); LastBatchDate = default(DateTime);
deferred.Clear(); skipped.Clear(); endingCardsSeen.Clear();
Notify(Changed);
}
diff --git a/unity/Assets/Tests/EditMode/SimBridgeGddTests.cs b/unity/Assets/Tests/EditMode/SimBridgeGddTests.cs
index 654884e9b..d266a2559 100644
--- a/unity/Assets/Tests/EditMode/SimBridgeGddTests.cs
+++ b/unity/Assets/Tests/EditMode/SimBridgeGddTests.cs
@@ -36,6 +36,29 @@ public class SimBridgeGddTests
static UiText Text => UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.Strings).text);
static SimBridge Bridge(Content c) => new SimBridge(GameSession.New(c, GoldenRun.Seed, Mode.Historical), Text);
+ [Test]
+ public void ReplaceClearsThePreviousBatchDate()
+ {
+ var bridge = Bridge(Slice);
+ bridge.EndTurn();
+ bridge.Replace(GameSession.New(Slice, GoldenRun.Seed, Mode.Historical));
+ Assert.AreEqual(default(System.DateTime), bridge.LastBatchDate);
+ }
+
+ [Test]
+ public void AFailedEndTurnClearsThePreviousBatchDate()
+ {
+ var calls = 0;
+ var bridge = new SimBridge(GameSession.New(Slice, GoldenRun.Seed, Mode.Historical), Text,
+ (session, commands) => ++calls == 1
+ ? session.EndTurn(commands)
+ : throw new System.ArgumentException("refused by the test"));
+ bridge.EndTurn();
+ Assert.AreNotEqual(default(System.DateTime), bridge.LastBatchDate, "the successful turn records its date");
+ bridge.EndTurn();
+ Assert.AreEqual(default(System.DateTime), bridge.LastBatchDate);
+ }
+
/// <summary>R-TURN-004: a Move given through the bridge shows at once (State), and End turn keeps it.</summary>
[Test]
public void AMoveShowsAtOnce()