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.

sim: map actions accept from/to (refs #57) (#335)

agamrafaeli-instinct98b6fac95064
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 26260fbfb..39b1e5b2d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,6 +22,10 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIB
yet. Goldens unchanged.
- Aliyah regions accept `from`/`to` (chapters spec 3.2, refs #57). The loader refuses a window that runs backwards or is
empty, and an aliyah period that counts a region outside its window. With no dates nothing changes (goldens unchanged).
+- A `map_actions` entry in `rules.yaml` accepts `from`/`to` (chapters spec 3.2, refs #57). Outside the window the action
+ is refused with the day it opens or the last day it was open ("A housing project can be placed from 1 January 1950").
+ The loader refuses a window that runs backwards or is empty.
+ With no dates nothing changes (goldens unchanged). This finishes the model-record windows of #57.
- Map: today's name of a 1948 locality by tap and in the info panel. A tap on a locality pill toggles its tooltip, a tap
elsewhere hides it, and a mouse still hovers as before. The info panel lists the selected hex's 1948 localities with
today's name where a living place carries on, by the same rule as the tooltip. Keyboard players reach the panel list
diff --git a/sim/Statecraft.Sim.Tests/MapActionWindowTests.cs b/sim/Statecraft.Sim.Tests/MapActionWindowTests.cs
new file mode 100644
index 000000000..2d57f9bd4
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/MapActionWindowTests.cs
@@ -0,0 +1,82 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using Statecraft.Sim;
+
+/// <summary>Map-action windows (#57): from/to on a rules.yaml map action.</summary>
+public class MapActionWindowTests
+{
+ const string Housing = " housing: { cost: 8, housing: 0.08, morale: 0.01, name: a housing project }\n";
+
+ static Content Load(string housingLine)
+ {
+ var rules = Fixture.Files["rules.yaml"].Replace(Housing, housingLine);
+ return ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["rules.yaml"] = rules }));
+ }
+
+ static string? Check(Content c, DateTime date)
+ {
+ var s = Simulation.NewGame(c, 1, Mode.Historical); s.Date = date;
+ return MapActions.Check(c, s, MapActions.Housing, 2, 0); // a land hex next to a settlement: null means placeable
+ }
+
+ const string Before = "A housing project can be placed from 1 January 1950";
+ const string After = "A housing project could be placed until 31 December 1951";
+
+ [Fact]
+ public void WithNoWindowTheActionIsNeverRefusedForTime()
+ {
+ var c = Load(Housing);
+ Assert.Null(Check(c, new DateTime(1949, 1, 1)));
+ Assert.Null(Check(c, new DateTime(1955, 1, 1)));
+ }
+
+ [Fact]
+ public void AnActionIsRefusedBeforeFromAndOnTo()
+ {
+ var c = Load(" housing: { cost: 8, housing: 0.08, morale: 0.01, name: a housing project, from: 1950-01-01, to: 1952-01-01 }\n");
+ Assert.Equal(Before, Check(c, new DateTime(1949, 12, 31)));
+ Assert.Null(Check(c, new DateTime(1950, 1, 1)));
+ Assert.Null(Check(c, new DateTime(1951, 12, 31)));
+ Assert.Equal(After, Check(c, new DateTime(1952, 1, 1)));
+ }
+
+ [Theory]
+ [InlineData("1950-01-01")] // backwards
+ [InlineData("1952-01-01")] // empty: to is exclusive, so from = to is never open
+ public void AWindowThatRunsBackwardsOrIsEmptyIsRefused(string to)
+ {
+ var e = Assert.Throws<ContentException>(() => Load($" housing: {{ cost: 8, housing: 0.08, morale: 0.01, name: a housing project, from: 1952-01-01, to: {to} }}\n"));
+ Assert.Contains("rules.yaml", e.Message);
+ Assert.Contains("map_actions.housing.to", e.Message);
+ }
+
+ [Fact]
+ public void AnOpenEndedWindowNamesItsOneBound()
+ {
+ Assert.Equal(Before, Check(Load(" housing: { cost: 8, housing: 0.08, morale: 0.01, name: a housing project, from: 1950-01-01 }\n"), new DateTime(1949, 6, 1)));
+ Assert.Equal(After, Check(Load(" housing: { cost: 8, housing: 0.08, morale: 0.01, name: a housing project, to: 1952-01-01 }\n"), new DateTime(1953, 6, 1)));
+ }
+
+ /// <summary>The window is read from RulesAt (#290): an era's rules/&lt;era&gt;.yaml can close an action the base keeps open,
+ /// and a backwards window there is reported against that file.</summary>
+ [Fact]
+ public void AnErasRulesFileCanCloseTheActionAndItsErrorsNameThatFile()
+ {
+ const string eras = "eras:\n - { id: founding, name: The Founding, from: 1948-05-15, to: 1952-01-01 }\n - { id: later, name: Later, from: 1952-01-01, to: 1957-01-01 }\n";
+ static Content WithLater(string later)
+ {
+ var dir = Fixture.Write(new Dictionary<string, string> { ["eras.yaml"] = eras });
+ Directory.CreateDirectory(Path.Combine(dir, "rules"));
+ File.WriteAllText(Path.Combine(dir, "rules", "later.yaml"), later);
+ return ContentLoader.Load(dir);
+ }
+ var c = WithLater("map_actions:\n housing: { to: 1953-01-01 }\n");
+ Assert.Null(Check(c, new DateTime(1951, 6, 1))); // founding: no window
+ Assert.Null(Check(c, new DateTime(1952, 6, 1))); // later: open until 1953
+ Assert.Equal("A housing project could be placed until 31 December 1952", Check(c, new DateTime(1953, 6, 1)));
+ var e = Assert.Throws<ContentException>(() => WithLater("map_actions:\n housing: { from: 1954-01-01, to: 1953-01-01 }\n"));
+ Assert.Contains("rules/later.yaml", e.Message);
+ Assert.Contains("map_actions.housing.to", e.Message);
+ }
+}
diff --git a/sim/Statecraft.Sim/Content.cs b/sim/Statecraft.Sim/Content.cs
index 97a060edb..8d34df0d7 100644
--- a/sim/Statecraft.Sim/Content.cs
+++ b/sim/Statecraft.Sim/Content.cs
@@ -320,6 +320,10 @@ namespace Statecraft.Sim
public double Morale { get; set; }
/// <summary>The phrase the player reads in a sentence ("a transit camp"); required.</summary>
public string Name { get; set; } = "";
+ /// <summary>First day the action may be placed (inclusive); null when it is open from the start (chapters spec 3.2).</summary>
+ public DateTime? From { get; set; }
+ /// <summary>First day it may no longer be placed (exclusive); null while it still may.</summary>
+ public DateTime? To { get; set; }
}
/// <summary>Money the sim tracks (model.yaml money): treasury, debt and fx_reserves, each with the name and currency
diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.cs
index 3241d75d7..088e29522 100644
--- a/sim/Statecraft.Sim/ContentLoader.cs
+++ b/sim/Statecraft.Sim/ContentLoader.cs
@@ -391,6 +391,8 @@ namespace Statecraft.Sim
if (!MapActions.Actions.Contains(kv.Key)) throw new ContentException(file, $"map_actions.{kv.Key}", "unknown map action (transit_camp or housing)");
if (!(kv.Value.Cost >= 0 && IsFinite(kv.Value.Cost))) throw new ContentException(file, $"map_actions.{kv.Key}.cost", "must be a finite number >= 0");
Named(file, $"map_actions.{kv.Key}.name", kv.Value.Name);
+ if (kv.Value.From.HasValue && kv.Value.To.HasValue && kv.Value.To.Value <= kv.Value.From.Value)
+ throw new ContentException(file, $"map_actions.{kv.Key}.to", FormattableString.Invariant($"must be after from ({kv.Value.From:yyyy-MM-dd})"));
foreach (var (key, v) in new[] { ("housing", kv.Value.Housing), ("morale", kv.Value.Morale) })
if (!(v >= -1 && v <= 1)) throw new ContentException(file, $"map_actions.{kv.Key}.{key}", "must be a number from -1 to 1");
}
diff --git a/sim/Statecraft.Sim/MapActions.cs b/sim/Statecraft.Sim/MapActions.cs
index 96d4e2e83..3b6eb29db 100644
--- a/sim/Statecraft.Sim/MapActions.cs
+++ b/sim/Statecraft.Sim/MapActions.cs
@@ -47,6 +47,11 @@ namespace Statecraft.Sim
var rules = c.RulesAt(s.Date); // the era the turn starts in
var rule = rules.MapActions.TryGetValue(action, out var x) ? x : null;
if (rule == null) return "That map action is not available in this game";
+ // rules.yaml map_actions from/to (chapters spec 3.2): [from, to), like every content window. The reason names the day.
+ if (!Window.Active(rule.From, rule.To, s.Date))
+ return s.Date < rule.From.GetValueOrDefault()
+ ? $"{PlayerText.Capitalised(PlayerText.MapAction(c, action))} can be placed from {PlayerText.Date(rule.From!.Value)}"
+ : $"{PlayerText.Capitalised(PlayerText.MapAction(c, action))} could be placed until {PlayerText.Date(rule.To!.Value.AddDays(-1))}";
if (s.Date < rules.MapActionsFrom)
return $"Transit camps and housing projects can be placed from {PlayerText.Date(rules.MapActionsFrom)}";
if (action == TransitCamp && !(s.Policies.TryGetValue(MaabarotPolicy, out var m) && m >= 0.5))