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(sim): format numbers and dates in cause texts with the invariant culture

eyalgolan529d7bfea3e7
diff --git a/sim/Statecraft.Sim.Tests/CultureTests.cs b/sim/Statecraft.Sim.Tests/CultureTests.cs
new file mode 100644
index 000000000..c67e34f97
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/CultureTests.cs
@@ -0,0 +1,66 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Linq;
+using Statecraft.Sim;
+
+public class CultureTests
+{
+ const string Event = @"
+id: levy
+title: Emergency levy
+text: '{finance_minister} asks for a levy.'
+from: 1948-06-01
+options:
+ - id: pay
+ text: Pay it
+ effects:
+ - target: treasury
+ delta: -10.5
+";
+
+ static readonly Content C = ContentLoader.Load(Fixture.Write(
+ new Dictionary<string, string>
+ {
+ ["aliyah.yaml"] = Fixture.Files["aliyah.yaml"].Replace("europe: 230", "europe: 23000"),
+ ["legislatures.yaml"] = Fixture.Files["legislatures.yaml"].Replace(" - id: house\n", " - id: house\n elected: 1949-01-25\n"),
+ },
+ new Dictionary<string, string> { ["levy.yaml"] = Event }));
+
+ /// <summary>Runs every system that writes numbers or dates into causes or rejection reasons, under one culture.</summary>
+ static List<string> Run(CultureInfo culture)
+ {
+ var prev = CultureInfo.CurrentCulture;
+ try
+ {
+ CultureInfo.CurrentCulture = culture;
+ var why = new List<Cause>();
+ var s = new GameState { Date = new DateTime(1948, 6, 1), RngState = 1 };
+ EffectsSystem.Init(C, s); EconomySystem.Init(C, s); PopulationSystem.Init(C, s);
+ SecuritySystem.Init(C, s, why); // call-up floor (percent)
+ s.Policies["rationing"] = 1.0;
+ EffectsSystem.Apply(C, s, why); // indicator deltas
+ s.Indicators["food_supply"] = 0.25; EffectsSystem.UpdateSituations(C, s, why); // situation value
+ PopulationSystem.Apply(C, s, new DateTime(1948, 9, 1), 0.25, why); // thousands
+ s.Mobilization = 1.0; s.Treasury = 1;
+ EconomySystem.Apply(C, s, 0.25, why); // treasury, debt, fx
+ PoliticsSystem.UpdateStability(C, s, 0.25, why);
+ PoliticsSystem.ReportChanges(C, new DateTime(1949, 2, 1), new DateTime(1949, 5, 1), why); // election date
+ EventSystem.Trigger(C, s, why);
+ EventSystem.Choose(C, s, "levy", "pay", why); // event delta
+ return why.Select(w => w.Message).ToList();
+ }
+ finally { CultureInfo.CurrentCulture = prev; }
+ }
+
+ [Theory]
+ [InlineData("de-DE")]
+ [InlineData("th-TH")]
+ public void CauseTextsAreCultureInvariant(string culture)
+ {
+ var expected = Run(CultureInfo.InvariantCulture);
+ Assert.Contains(expected, m => m.Contains("9,160 immigrants")); // grouping is exercised
+ Assert.Contains(expected, m => m.Contains("elected 1949-01-25")); // calendar is exercised
+ Assert.Equal(expected, Run(new CultureInfo(culture)));
+ }
+}
diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.cs
index 49f582118..8b0b06130 100644
--- a/sim/Statecraft.Sim/ContentLoader.cs
+++ b/sim/Statecraft.Sim/ContentLoader.cs
@@ -104,7 +104,7 @@ namespace Statecraft.Sim
{
if (string.IsNullOrEmpty(t.Office)) throw new ContentException("people.yaml", $"people[{p.Id}].offices", "office term without an office");
if (!offices.Contains(t.Office)) throw new ContentException("people.yaml", $"people[{p.Id}].offices.{t.Office}", "unknown office");
- if (t.To.HasValue && t.To.Value <= t.From) throw new ContentException("people.yaml", $"people[{p.Id}].offices.{t.Office}", $"term ends ({t.To:yyyy-MM-dd}) before it starts ({t.From:yyyy-MM-dd})");
+ if (t.To.HasValue && t.To.Value <= t.From) throw new ContentException("people.yaml", $"people[{p.Id}].offices.{t.Office}", FormattableString.Invariant($"term ends ({t.To:yyyy-MM-dd}) before it starts ({t.From:yyyy-MM-dd})"));
}
}
var terms = c.People.SelectMany(p => p.Offices.Select(t => (person: p.Id, term: t))).ToList();
@@ -120,7 +120,7 @@ namespace Statecraft.Sim
{
foreach (var kv in l.Seats)
if (!parties.Contains(kv.Key)) throw new ContentException("legislatures.yaml", $"legislatures[{l.Id}].seats.{kv.Key}", "unknown party");
- if (l.Seats.Values.Sum() != l.Size) throw new ContentException("legislatures.yaml", $"legislatures[{l.Id}].size", $"seats sum to {l.Seats.Values.Sum()}, size is {l.Size}");
+ if (l.Seats.Values.Sum() != l.Size) throw new ContentException("legislatures.yaml", $"legislatures[{l.Id}].size", FormattableString.Invariant($"seats sum to {l.Seats.Values.Sum()}, size is {l.Size}"));
}
foreach (var g in c.Governments)
foreach (var p in g.Coalition)
diff --git a/sim/Statecraft.Sim/EconomySystem.cs b/sim/Statecraft.Sim/EconomySystem.cs
index 1c198425f..857aae12a 100644
--- a/sim/Statecraft.Sim/EconomySystem.cs
+++ b/sim/Statecraft.Sim/EconomySystem.cs
@@ -23,18 +23,18 @@ namespace Statecraft.Sim
var mobilization = r.MobilizationCostPerYear * s.Mobilization * yearFraction;
s.Treasury += revenue - spending - mobilization;
why.Add(new Cause("economy", "treasury", revenue - spending - mobilization,
- $"treasury {revenue - spending - mobilization:+0.00;-0.00} (tax {revenue:0.00}, spending {spending:0.00}, reserve call-up {mobilization:0.00})"));
+ FormattableString.Invariant($"treasury {revenue - spending - mobilization:+0.00;-0.00} (tax {revenue:0.00}, spending {spending:0.00}, reserve call-up {mobilization:0.00})")));
if (s.Treasury < 0)
{
s.Debt += -s.Treasury;
- why.Add(new Cause("economy", "debt", -s.Treasury, $"borrowed {-s.Treasury:0.00} to cover the deficit"));
+ why.Add(new Cause("economy", "debt", -s.Treasury, FormattableString.Invariant($"borrowed {-s.Treasury:0.00} to cover the deficit")));
s.Treasury = 0;
}
var exports = r.ExportsPerYear * yearFraction;
var imports = r.ImportsPerCapitaPerYear * pop * yearFraction;
s.FxReserves += exports - imports;
- why.Add(new Cause("economy", "fx_reserves", exports - imports, $"foreign currency {exports - imports:+0.00;-0.00} (exports {exports:0.00}, imports {imports:0.00})"));
+ why.Add(new Cause("economy", "fx_reserves", exports - imports, FormattableString.Invariant($"foreign currency {exports - imports:+0.00;-0.00} (exports {exports:0.00}, imports {imports:0.00})")));
bool on = s.ActiveSituations.Contains(FxCrisis);
if (!on && s.FxReserves < 0) { s.ActiveSituations.Add(FxCrisis); why.Add(new Cause("economy", FxCrisis, 1, "Foreign currency reserves are exhausted")); }
diff --git a/sim/Statecraft.Sim/EffectsSystem.cs b/sim/Statecraft.Sim/EffectsSystem.cs
index 17aaab47e..8b58c5af6 100644
--- a/sim/Statecraft.Sim/EffectsSystem.cs
+++ b/sim/Statecraft.Sim/EffectsSystem.cs
@@ -69,8 +69,8 @@ namespace Statecraft.Sim
bool on = s.ActiveSituations.Contains(sit.Id);
bool turnOn = sit.OnBelow.HasValue ? v < sit.OnBelow.Value : v > sit.OnAbove!.Value;
bool turnOff = sit.OffAbove.HasValue ? v > sit.OffAbove.Value : v < sit.OffBelow!.Value;
- if (!on && turnOn) { s.ActiveSituations.Add(sit.Id); why.Add(new Cause("situations", sit.Id, 1, $"{sit.Id} began ({sit.Indicator} = {v:0.00})")); }
- else if (on && turnOff) { s.ActiveSituations.Remove(sit.Id); why.Add(new Cause("situations", sit.Id, -1, $"{sit.Id} ended ({sit.Indicator} = {v:0.00})")); }
+ if (!on && turnOn) { s.ActiveSituations.Add(sit.Id); why.Add(new Cause("situations", sit.Id, 1, FormattableString.Invariant($"{sit.Id} began ({sit.Indicator} = {v:0.00})"))); }
+ else if (on && turnOff) { s.ActiveSituations.Remove(sit.Id); why.Add(new Cause("situations", sit.Id, -1, FormattableString.Invariant($"{sit.Id} ended ({sit.Indicator} = {v:0.00})"))); }
}
}
diff --git a/sim/Statecraft.Sim/EventSystem.cs b/sim/Statecraft.Sim/EventSystem.cs
index 59b1f0c12..b82fce3cb 100644
--- a/sim/Statecraft.Sim/EventSystem.cs
+++ b/sim/Statecraft.Sim/EventSystem.cs
@@ -45,7 +45,7 @@ namespace Statecraft.Sim
if (fx.Target == "treasury") s.Treasury += fx.Delta;
else if (fx.Target == "fx_reserves") s.FxReserves += fx.Delta;
else s.Indicators[fx.Target] = Math.Max(0, Math.Min(1, s.Indicators[fx.Target] + fx.Delta));
- why.Add(new Cause("events", fx.Target, fx.Delta, $"{fx.Target} {fx.Delta:+0.00;-0.00} ({eventId}/{o.Id})"));
+ why.Add(new Cause("events", fx.Target, fx.Delta, FormattableString.Invariant($"{fx.Target} {fx.Delta:+0.00;-0.00} ({eventId}/{o.Id})")));
}
if (o.SetRung.HasValue) SecuritySystem.SetRung(c, s, o.SetRung.Value, why);
}
diff --git a/sim/Statecraft.Sim/Offices.cs b/sim/Statecraft.Sim/Offices.cs
index 595df24be..454a37e52 100644
--- a/sim/Statecraft.Sim/Offices.cs
+++ b/sim/Statecraft.Sim/Offices.cs
@@ -26,6 +26,6 @@ namespace Statecraft.Sim
public static string Fill(Content c, string text, DateTime date) =>
Placeholder.Replace(text, m =>
Holder(c, m.Groups[1].Value, date)?.Name
- ?? throw new InvalidOperationException($"no holder of '{m.Groups[1].Value}' on {date:yyyy-MM-dd}"));
+ ?? throw new InvalidOperationException(FormattableString.Invariant($"no holder of '{m.Groups[1].Value}' on {date:yyyy-MM-dd}")));
}
}
diff --git a/sim/Statecraft.Sim/PoliticsSystem.cs b/sim/Statecraft.Sim/PoliticsSystem.cs
index 82b59f860..33f0c34ba 100644
--- a/sim/Statecraft.Sim/PoliticsSystem.cs
+++ b/sim/Statecraft.Sim/PoliticsSystem.cs
@@ -36,7 +36,7 @@ namespace Statecraft.Sim
var old = s.CoalitionStability;
s.CoalitionStability = old + (EffectsSystem.Approval(c, s) - old) * Math.Min(1.0, yearFraction * 2);
if (Math.Abs(s.CoalitionStability - old) > 1e-9)
- why.Add(new Cause("politics", "coalition_stability", s.CoalitionStability - old, $"coalition stability {s.CoalitionStability - old:+0.00;-0.00} (public approval)"));
+ why.Add(new Cause("politics", "coalition_stability", s.CoalitionStability - old, FormattableString.Invariant($"coalition stability {s.CoalitionStability - old:+0.00;-0.00} (public approval)")));
if (s.CoalitionStability < CollapseBelow && s.GameOver == null)
{
s.GameOver = "coalition_collapse";
@@ -50,8 +50,8 @@ namespace Statecraft.Sim
if (l1 != null && l1 != l0)
{
var (seats, size) = CoalitionSeats(c, to);
- var elected = l1.Elected.HasValue ? $", elected {l1.Elected:yyyy-MM-dd}" : "";
- why.Add(new Cause("politics", l1.Id, 0, $"{l1.Body} convened ({l1.Size} seats{elected}); coalition holds {seats}/{size}"));
+ var elected = l1.Elected.HasValue ? FormattableString.Invariant($", elected {l1.Elected:yyyy-MM-dd}") : "";
+ why.Add(new Cause("politics", l1.Id, 0, FormattableString.Invariant($"{l1.Body} convened ({l1.Size} seats{elected}); coalition holds {seats}/{size}")));
}
var g0 = Offices.Government(c, from); var g1 = Offices.Government(c, to);
if (g1 != null && g1 != g0)
diff --git a/sim/Statecraft.Sim/PopulationSystem.cs b/sim/Statecraft.Sim/PopulationSystem.cs
index e9a246aaa..32c6e6afd 100644
--- a/sim/Statecraft.Sim/PopulationSystem.cs
+++ b/sim/Statecraft.Sim/PopulationSystem.cs
@@ -34,7 +34,7 @@ namespace Statecraft.Sim
foreach (var kv in Arrivals(c, s.Date, to).OrderBy(k => k.Key, StringComparer.Ordinal))
{
s.Population[kv.Key] = (s.Population.TryGetValue(kv.Key, out var v) ? v : 0) + kv.Value;
- if (kv.Value > 0) why.Add(new Cause("population", kv.Key, kv.Value, $"{kv.Value:N0} immigrants arrived from {kv.Key}"));
+ if (kv.Value > 0) why.Add(new Cause("population", kv.Key, kv.Value, FormattableString.Invariant($"{kv.Value:N0} immigrants arrived from {kv.Key}")));
}
}
}
diff --git a/sim/Statecraft.Sim/SecuritySystem.cs b/sim/Statecraft.Sim/SecuritySystem.cs
index acda75703..2fc72d448 100644
--- a/sim/Statecraft.Sim/SecuritySystem.cs
+++ b/sim/Statecraft.Sim/SecuritySystem.cs
@@ -18,7 +18,7 @@ namespace Statecraft.Sim
rung = Math.Max(1, Math.Min(5, rung));
if (rung == 5 && s.Rung != 5) s.CeasefireWeeksLeft = c.Rules.CeasefireWeeks;
if (rung != 5) s.CeasefireWeeksLeft = 0;
- if (rung != s.Rung) why.Add(new Cause("security", "rung", rung - s.Rung, $"security level {s.Rung} -> {rung}"));
+ if (rung != s.Rung) why.Add(new Cause("security", "rung", rung - s.Rung, FormattableString.Invariant($"security level {s.Rung} -> {rung}")));
s.Rung = rung;
RaiseToFloor(c, s, why);
}
@@ -37,7 +37,7 @@ namespace Statecraft.Sim
var floor = c.Rules.RungMinMobilization[s.Rung - 1];
if (s.Mobilization < floor)
{
- why.Add(new Cause("security", "mobilization", floor - s.Mobilization, $"reserve call-up raised to {floor:P0}, the minimum for security level {s.Rung}"));
+ why.Add(new Cause("security", "mobilization", floor - s.Mobilization, FormattableString.Invariant($"reserve call-up raised to {floor * 100:0}%, the minimum for security level {s.Rung}")));
s.Mobilization = floor;
}
}