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): an unparseable or negative -smoke value is off, as on the web

eyalgolanaacdb5a68655
diff --git a/unity/Assets/Scripts/UI/GameRoot.cs b/unity/Assets/Scripts/UI/GameRoot.cs
index 151b79967..6285f7c36 100644
--- a/unity/Assets/Scripts/UI/GameRoot.cs
+++ b/unity/Assets/Scripts/UI/GameRoot.cs
@@ -72,7 +72,8 @@ namespace Statecraft.Game.UI
static readonly Regex SmokeQuery = new Regex(@"[?&]smoke(?:=(\d*))?(?=&|#|$)", RegexOptions.CultureInvariant);
/// <summary>Smoke-test turn count: from the page URL on WebGL, else from the command line. "?smoke=N" / "-smoke N"
- /// play N turns (0 = off); a bare "?smoke" / "-smoke" plays DefaultSmokeTurns. 0 when not asked.</summary>
+ /// play N turns (0 = off); a bare "?smoke" / "-smoke" (or "-smoke" followed by another flag) plays DefaultSmokeTurns;
+ /// a value that is not a non-negative int (too big, negative, not a number) is off on both. 0 when not asked.</summary>
internal static int SmokeTurns(string? url, string[] args, bool webgl)
{
if (webgl)
@@ -84,7 +85,10 @@ namespace Statecraft.Game.UI
}
var i = Array.IndexOf(args, "-smoke");
if (i < 0) return 0;
- return i + 1 < args.Length && int.TryParse(args[i + 1], NumberStyles.None, CultureInfo.InvariantCulture, out var t) ? t : DefaultSmokeTurns;
+ if (i + 1 >= args.Length) return DefaultSmokeTurns;
+ var value = args[i + 1];
+ if (value.StartsWith("-", StringComparison.Ordinal) && !Regex.IsMatch(value, @"^-\d")) return DefaultSmokeTurns; // the next flag
+ return int.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out var t) ? t : 0;
}
public static int SmokeTurns() =>
diff --git a/unity/Assets/Tests/EditMode/GameSceneTests.cs b/unity/Assets/Tests/EditMode/GameSceneTests.cs
index e89d9ea47..bb246264b 100644
--- a/unity/Assets/Tests/EditMode/GameSceneTests.cs
+++ b/unity/Assets/Tests/EditMode/GameSceneTests.cs
@@ -421,6 +421,10 @@ public class GameSceneTests
Assert.AreEqual(0, Desk("Statecraft", "-smoke", "0"), "explicitly off");
Assert.AreEqual(GameRoot.DefaultSmokeTurns, Desk("Statecraft", "-smoke"));
Assert.AreEqual(GameRoot.DefaultSmokeTurns, Desk("Statecraft", "-smoke", "-golden"));
+ Assert.AreEqual(0, Desk("Statecraft", "-smoke", "99999999999"), "unparseable (too big) is off, as on the web");
+ Assert.AreEqual(0, Desk("Statecraft", "-smoke", "-1"), "negative is off");
+ Assert.AreEqual(0, Desk("Statecraft", "-smoke", "five"), "not a number is off, as ?smoke=five");
+ Assert.AreEqual(0, Web("http://localhost:8000/?smoke=99999999999"), "the web agrees");
Assert.AreEqual(0, Desk("Statecraft"));
Assert.AreEqual(0, GameRoot.SmokeTurns("http://localhost/?smoke=5", new[] { "Statecraft" }, false), "desktop reads only the command line");
}