A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Add a synthetic arms workshop cycle, test-only (Refs #1701) (#1834)
Eyal Golan9b3ef3b0cc85
diff --git a/changelog.d/1701-workshop-cycle.md b/changelog.d/1701-workshop-cycle.mdnew file mode 100644index 000000000..c525d2e0c--- /dev/null+++ b/changelog.d/1701-workshop-cycle.md@@ -0,0 +1 @@+- Add an isolated synthetic arms workshop cycle contract with catalogue pricing and explicit reservations.diff --git a/sim/Statecraft.Sim.Tests/SyntheticWorkshopCycleTests.cs b/sim/Statecraft.Sim.Tests/SyntheticWorkshopCycleTests.csnew file mode 100644index 000000000..8075ca445--- /dev/null+++ b/sim/Statecraft.Sim.Tests/SyntheticWorkshopCycleTests.cs@@ -0,0 +1,145 @@+using System;+using System.Globalization;+using Statecraft.Sim;++// #1701 / R-LOOP-005: isolated synthetic workshop contract only; no production work or rule activation.+public class SyntheticWorkshopCycleTests+{+ static Gdd03BalanceCatalogue Catalogue() => ContentLoader.Load(RealContentTests.ContentRoot()).Gdd03Balance!;+ static SyntheticWorkshopRules Rules() => SyntheticWorkshopRules.FromCatalogue(Catalogue());+ // IDs and invalid inputs below are test-only synthetic values, never authored facilities or people.+ static SyntheticWorkshopCycle Empty() => SyntheticWorkshopCycle.Empty(Rules(), "fixture-workshop");++ [Theory]+ [InlineData(SyntheticWorkshopTier.Ordinary, 5)]+ [InlineData(SyntheticWorkshopTier.Novice, 5)]+ [InlineData(SyntheticWorkshopTier.Apprentice, 4)]+ [InlineData(SyntheticWorkshopTier.Expert, 3)]+ public void CataloguePricesAndThreeStaffedTurns(SyntheticWorkshopTier tier, int price)+ {+ var c = Empty().WithWorker("fixture-person", tier);+ Assert.Equal((decimal)price, c.NextPrice);+ c = c.ReserveExplicitly(price);+ Assert.Equal(0, c.Progress);+ Assert.Equal((decimal)price, c.PaidCost);+ for (int i = 1; i < 3; i++)+ {+ c = c.StaffedTurn();+ Assert.Equal(i, c.Progress);+ Assert.Equal(0, c.CompletedArms);+ }+ c = c.StaffedTurn();+ Assert.Null(c.PaidCost);+ Assert.Null(c.PaidTier);+ Assert.Equal(0, c.Progress);+ Assert.Equal(1, c.CompletedArms);+ Assert.Same(c, c.StaffedTurn()); // no automatic restart or unpaid work+ }++ [Theory]+ [InlineData(0)] [InlineData(1)] [InlineData(2)]+ public void PauseAndReplacementPreservePaidCycle(int ticks)+ {+ var original = Empty().WithWorker("fixture-a", SyntheticWorkshopTier.Ordinary).ReserveExplicitly(5m);+ var c = original;+ for (int i = 0; i < ticks; i++) c = c.StaffedTurn();+ c = c.WithoutWorker();+ Assert.Same(c, c.StaffedTurn());+ Assert.Same(c, c.StaffedTurn());+ Assert.Equal(ticks, c.Progress);+ Assert.Equal(5m, c.PaidCost);+ Assert.Throws<InvalidOperationException>(() => c.ReserveExplicitly(5m));+ c = c.WithWorker("fixture-b", SyntheticWorkshopTier.Expert);+ Assert.Equal(ticks, c.Progress);+ Assert.Equal(5m, c.PaidCost);+ Assert.Equal(SyntheticWorkshopTier.Ordinary, c.PaidTier);+ Assert.Equal(3m, c.NextPrice);+ for (int remaining = 3 - ticks; remaining > 0; remaining--) c = c.StaffedTurn();+ Assert.Null(c.PaidCost);+ Assert.Equal(1, c.CompletedArms);+ c = c.ReserveExplicitly(3m);+ Assert.Equal(3m, c.PaidCost);+ Assert.Equal(SyntheticWorkshopTier.Expert, c.PaidTier);+ Assert.Equal(0, original.Progress); // immutable source+ Assert.Equal("fixture-a", original.WorkerId);+ }++ [Fact]+ public void ExpertToOrdinaryReplacementDoesNotRepriceCurrentCycle()+ {+ var c = Empty().WithWorker("fixture-expert", SyntheticWorkshopTier.Expert).ReserveExplicitly(3m).StaffedTurn();+ c = c.WithWorker("fixture-ordinary", SyntheticWorkshopTier.Ordinary);+ Assert.Equal(3m, c.PaidCost);+ Assert.Equal(1, c.Progress);+ c = c.StaffedTurn().StaffedTurn();+ Assert.Equal(1, c.CompletedArms);+ c = c.ReserveExplicitly(5m);+ Assert.Equal(5m, c.PaidCost);+ Assert.Equal(0, c.Progress);+ }++ [Fact]+ public void ExplicitReservationRequiresWorkerAndExactAcknowledgement()+ {+ var empty = Empty();+ Assert.Throws<InvalidOperationException>(() => empty.ReserveExplicitly(5m));+ Assert.Same(empty, empty.StaffedTurn());+ var c = empty.WithWorker("fixture-a", SyntheticWorkshopTier.Expert);+ // No Money or affordability policy here: reject malformed external reservation facts only.+ Assert.Throws<ArgumentException>(() => c.ReserveExplicitly(2m));+ Assert.Throws<ArgumentException>(() => c.ReserveExplicitly(4m));+ Assert.Null(c.PaidCost);+ var paid = c.ReserveExplicitly(3m);+ Assert.Throws<InvalidOperationException>(() => paid.ReserveExplicitly(3m));+ Assert.Equal(0, paid.Progress);+ Assert.Throws<ArgumentException>(() => c.WithWorker(" ", SyntheticWorkshopTier.Novice));+ Assert.Throws<ArgumentOutOfRangeException>(() => c.WithWorker("fixture-a", (SyntheticWorkshopTier)99));+ }++ [Theory]+ [InlineData("he-IL")] [InlineData("en-US")]+ public void NumberTextUsesInvariantCulture(string locale)+ {+ var old = CultureInfo.CurrentCulture;+ var oldUi = CultureInfo.CurrentUICulture;+ try+ {+ CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(locale);+ CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(locale);+ var c = Empty().WithWorker("fixture-a", SyntheticWorkshopTier.Expert).ReserveExplicitly(3m).StaffedTurn();+ Assert.Equal("1/3; paid=3; arms=0", c.ProgressText());+ }+ finally { CultureInfo.CurrentCulture = old; CultureInfo.CurrentUICulture = oldUi; }+ }++ [Theory]+ [InlineData("workshop.staffed_turns")] [InlineData("workshop.cost")]+ public void MissingAndDeferredRulesHaveNoFallback(string key)+ {+ var c = Catalogue();+ c.Values.Remove(key);+ Assert.Throws<ArgumentException>(() => SyntheticWorkshopRules.FromCatalogue(c));+ c = Catalogue();+ c.Values[key].Basis = "Deferred";+ Assert.Throws<ArgumentException>(() => SyntheticWorkshopRules.FromCatalogue(c));+ }++ [Fact]+ public void MalformedRulesAreRejectedAndRulesSnapshotCatalogue()+ {+ var c = Catalogue();+ var rules = SyntheticWorkshopRules.FromCatalogue(c);+ c.Values["workshop.cost"].Values[0] = 99m; // test-only mutation+ Assert.Equal(5m, rules.Price(SyntheticWorkshopTier.Ordinary));+ c = Catalogue(); c.Values["workshop.staffed_turns"].Values[0] = 1.5m;+ Assert.Throws<ArgumentException>(() => SyntheticWorkshopRules.FromCatalogue(c));+ c = Catalogue(); c.Values["workshop.cost"].Values.RemoveAt(0);+ Assert.Throws<ArgumentException>(() => SyntheticWorkshopRules.FromCatalogue(c));+ c = Catalogue(); c.Values["workshop.cost"].Values[0] = 0m;+ Assert.Throws<ArgumentException>(() => SyntheticWorkshopRules.FromCatalogue(c));+ c = Catalogue(); c.Values["workshop.cost"].Expression = "unknown";+ Assert.Throws<ArgumentException>(() => SyntheticWorkshopRules.FromCatalogue(c));+ c = Catalogue(); c.Ruleset = SaveGame.GddRuleset;+ Assert.Throws<ArgumentException>(() => SyntheticWorkshopRules.FromCatalogue(c));+ }+}diff --git a/sim/Statecraft.Sim/SyntheticWorkshopCycle.cs b/sim/Statecraft.Sim/SyntheticWorkshopCycle.csnew file mode 100644index 000000000..bd7eced29--- /dev/null+++ b/sim/Statecraft.Sim/SyntheticWorkshopCycle.cs@@ -0,0 +1,125 @@+using System;+using System.Globalization;++namespace Statecraft.Sim+{+ // Pricing categories only, NOT an eligibility matrix or a profession/qualification mutation.+ public enum SyntheticWorkshopTier { Ordinary, Novice, Apprentice, Expert }++ /// <summary>Explicit opt-in synthetic rules snapshot; no loader registration or game-loop caller.</summary>+ public sealed class SyntheticWorkshopRules+ {+ public int StaffedTurns { get; }+ readonly decimal ordinaryCost, apprenticeCost, expertCost;++ SyntheticWorkshopRules(int turns, decimal ordinary, decimal apprentice, decimal expert)+ {+ StaffedTurns = turns;+ ordinaryCost = ordinary; apprenticeCost = apprentice; expertCost = expert;+ }++ public static SyntheticWorkshopRules FromCatalogue(Gdd03BalanceCatalogue catalogue)+ {+ if (catalogue == null) throw new ArgumentNullException(nameof(catalogue));+ if (catalogue.Ruleset != SaveGame.Gdd03Ruleset || catalogue.Values == null)+ throw new ArgumentException("Requires the gdd-0.3 catalogue.", nameof(catalogue));+ var turns = Read(catalogue, "workshop.staffed_turns", 1)[0];+ if (turns != decimal.Truncate(turns) || turns > int.MaxValue)+ throw new ArgumentException("Staffed duration must be a positive integer.", nameof(catalogue));+ var costs = Read(catalogue, "workshop.cost", 3);+ return new SyntheticWorkshopRules((int)turns, costs[0], costs[1], costs[2]);+ }++ static decimal[] Read(Gdd03BalanceCatalogue catalogue, string key, int count)+ {+ if (!catalogue.Values.TryGetValue(key, out var row) || row == null || row.Basis != "Agreed"+ || row.Values == null || row.Values.Count != count || !string.IsNullOrEmpty(row.Expression)+ || row.Parts == null || row.Parts.Count != 0)+ throw new ArgumentException("Missing or unresolved workshop rule: " + key, nameof(catalogue));+ var result = row.Values.ToArray();+ foreach (var value in result)+ if (value <= 0) throw new ArgumentException("Workshop quantities must be positive: " + key, nameof(catalogue));+ return result;+ }++ public decimal Price(SyntheticWorkshopTier tier)+ {+ switch (tier)+ {+ case SyntheticWorkshopTier.Ordinary:+ case SyntheticWorkshopTier.Novice: return ordinaryCost;+ case SyntheticWorkshopTier.Apprentice: return apprenticeCost;+ case SyntheticWorkshopTier.Expert: return expertCost;+ default: throw new ArgumentOutOfRangeException(nameof(tier));+ }+ }+ }++ /// <summary>+ /// Immutable synthetic cycle, unrelated to GameState, facility validity, assignment or Money ledgers.+ /// Caller supplies reservation and staffed-turn facts. This is not a command, receipt validator,+ /// monthly scheduler, replay format, affordability evaluator or national spare-stock writer.+ /// </summary>+ public sealed class SyntheticWorkshopCycle+ {+ readonly SyntheticWorkshopRules rules;+ public string FixtureFacilityId { get; }+ public string? WorkerId { get; }+ public SyntheticWorkshopTier? WorkerTier { get; }+ public SyntheticWorkshopTier? PaidTier { get; }+ public decimal? PaidCost { get; }+ public int Progress { get; }+ public int CompletedArms { get; }+ public decimal? NextPrice => WorkerTier.HasValue ? rules.Price(WorkerTier.Value) : (decimal?)null;++ SyntheticWorkshopCycle(SyntheticWorkshopRules rules, string facilityId, string? workerId,+ SyntheticWorkshopTier? workerTier, SyntheticWorkshopTier? paidTier, decimal? paidCost,+ int progress, int completedArms)+ {+ this.rules = rules; FixtureFacilityId = facilityId; WorkerId = workerId; WorkerTier = workerTier;+ PaidTier = paidTier; PaidCost = paidCost; Progress = progress; CompletedArms = completedArms;+ }++ public static SyntheticWorkshopCycle Empty(SyntheticWorkshopRules rules, string fixtureFacilityId)+ {+ if (rules == null) throw new ArgumentNullException(nameof(rules));+ if (string.IsNullOrWhiteSpace(fixtureFacilityId)) throw new ArgumentException("Synthetic facility ID required.", nameof(fixtureFacilityId));+ return new SyntheticWorkshopCycle(rules, fixtureFacilityId, null, null, null, null, 0, 0);+ }++ public SyntheticWorkshopCycle WithWorker(string fixturePersonId, SyntheticWorkshopTier tier)+ {+ if (string.IsNullOrWhiteSpace(fixturePersonId)) throw new ArgumentException("Synthetic person ID required.", nameof(fixturePersonId));+ rules.Price(tier); // validate category, without deciding eligibility+ return new SyntheticWorkshopCycle(rules, FixtureFacilityId, fixturePersonId, tier, PaidTier, PaidCost, Progress, CompletedArms);+ }++ public SyntheticWorkshopCycle WithoutWorker() =>+ new SyntheticWorkshopCycle(rules, FixtureFacilityId, null, null, PaidTier, PaidCost, Progress, CompletedArms);++ /// <summary>Acknowledges a caller-supplied synthetic reservation; never debits or borrows Money.+ /// Active cycles refuse a second reservation. No timing or automatic retry policy is implied.</summary>+ public SyntheticWorkshopCycle ReserveExplicitly(decimal reservedCost)+ {+ if (PaidCost.HasValue || !WorkerTier.HasValue)+ throw new InvalidOperationException("Requires a worker and no paid cycle.");+ if (reservedCost != NextPrice) throw new ArgumentException("Reservation must match the current tier quote.", nameof(reservedCost));+ return new SyntheticWorkshopCycle(rules, FixtureFacilityId, WorkerId, WorkerTier, WorkerTier, reservedCost, 0, CompletedArms);+ }++ /// <summary>One explicit synthetic staffed-turn event. Caller owns event uniqueness/timing.+ /// Empty or paused cycles do nothing. Completion reports one synthetic Arms, with no restart.</summary>+ public SyntheticWorkshopCycle StaffedTurn()+ {+ if (!PaidCost.HasValue || WorkerId == null) return this;+ int next = checked(Progress + 1);+ if (next == rules.StaffedTurns)+ return new SyntheticWorkshopCycle(rules, FixtureFacilityId, WorkerId, WorkerTier, null, null, 0, checked(CompletedArms + 1));+ return new SyntheticWorkshopCycle(rules, FixtureFacilityId, WorkerId, WorkerTier, PaidTier, PaidCost, next, CompletedArms);+ }++ // Diagnostic fixture text only; not a save, canonical state or player-facing string.+ public string ProgressText() => string.Format(CultureInfo.InvariantCulture, "{0}/{1}; paid={2}; arms={3}",+ Progress, rules.StaffedTurns, PaidCost.HasValue ? PaidCost.Value.ToString(CultureInfo.InvariantCulture) : "none", CompletedArms);+ }+}