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 assignment fixture and typed Industry rates, test-only (Refs #1692) (#1829)
Eyal Golan685917ed8f55
diff --git a/changelog.d/1692-fixture-assignments.md b/changelog.d/1692-fixture-assignments.mdnew file mode 100644index 000000000..0a5e3b585--- /dev/null+++ b/changelog.d/1692-fixture-assignments.md@@ -0,0 +1 @@+- Add isolated population fixture assignments with atomic capacity checks and typed Industry rate quotations.diff --git a/sim/Statecraft.Sim.Tests/Gdd03AssignmentFixtureTests.cs b/sim/Statecraft.Sim.Tests/Gdd03AssignmentFixtureTests.csnew file mode 100644index 000000000..c259adf3b--- /dev/null+++ b/sim/Statecraft.Sim.Tests/Gdd03AssignmentFixtureTests.cs@@ -0,0 +1,156 @@+using Statecraft.Sim;+using System.Globalization;++// Synthetic-only inventories, capacities, ids, labels, XP and random seed below are test data.+public class Gdd03AssignmentFixtureTests+{+ static Content Content(string identity = SaveGame.Gdd03Ruleset)+ {+ var c = PopulationLedgerTests.Content(identity);+ c.Gdd03Balance!.Values["industry.income"] = new Gdd03DesignValue {+ Basis = "Agreed", Note = "content/gdd03-balance.yaml:138-141", Values = new() { 1m, 2m, 3m } };+ return c;+ }+ static readonly PopulationLocation Place = new("synthetic-place");+ static readonly Gdd03FixtureSlot[] Slots = {+ new("synthetic-industry", Gdd03FixtureWork.Industry, 1, "winery"),+ new("synthetic-builder", Gdd03FixtureWork.Builder, 1, "builder"),+ new("synthetic-farm", Gdd03FixtureWork.Farm, 1, "farm"),+ new("synthetic-workshop", Gdd03FixtureWork.Workshop, 1, "workshop") };+ static PopulationUnit Person(CivilianProfession profession = CivilianProfession.None, CivilianTier tier = CivilianTier.None) =>+ new(0, PopulationStatus.FreeCivilian, 6, Place, PopulationAssignment.Unassigned,+ profession, tier, profession == CivilianProfession.None ? 0 : 7, "", 0, PopulationEquipment.None);+ static GameState State(Content c, params PopulationUnit[] people) => PopulationLedger.CreateFixture(c,+ new PopulationBindings(new[] { Place }, Slots.Select(x => new PopulationTarget(x.Id, PopulationAssignmentKind.Work, Place.Region)),+ Array.Empty<string>(), Array.Empty<string>()), people.Select((p, i) => new PopulationInventoryRow("synthetic", i, p)));++ // R-CITY-001, R-CITY-002: destination validation must precede any release or resource mutation.+ [Fact] public void FullMissingAndIneligibleDestinationsPreserveWholeState()+ {+ var c = Content(); var s = State(c, Person(), Person());+ var f = new Gdd03AssignmentFixture(c, s, Slots);+ Assert.Equal(Gdd03FixtureAssignmentResult.Applied, f.Assign(0, Slots[0].Id));+ f.Assign(1, Slots[1].Id);+ foreach (var target in new[] { Slots[0].Id, "missing" }) {+ var before = s.Describe(); Assert.Throws<ArgumentException>(() => f.Assign(1, target)); Assert.Equal(before, s.Describe());+ }+ var same = s.Describe();+ Assert.Equal(Gdd03FixtureAssignmentResult.DeferredSameSlot, f.Assign(0, Slots[0].Id)); Assert.Equal(same, s.Describe());+ s.PopulationLedger.Replace(s.PopulationLedger.Get(1).WithReception(0));+ same = s.Describe(); Assert.Throws<ArgumentException>(() => f.Assign(1, Slots[2].Id)); Assert.Equal(same, s.Describe());+ Assert.Throws<ArgumentException>(() => new Gdd03AssignmentFixture(c, s, Slots.Concat(new[] { Slots[0] })));+ }++ // R-CITY-001: independent seeded model checks capacity, exclusivity and conservation after every step.+ [Fact] public void GeneratedReassignmentsUseOnlyTheAuthoritativeLedger()+ {+ var c = Content(); var s = State(c, Person(), Person(), Person());+ var f = new Gdd03AssignmentFixture(c, s, Slots); var model = new string?[3]; var rng = new Random(1692);+ for (var step = 0; step < 500; step++) {+ var id = rng.Next(3); var index = rng.Next(6); var target = index < 4 ? Slots[index].Id : index == 4 ? null : "missing";+ var before = s.Describe();+ var invalid = target == "missing" || (target != null && model.Where((_, i) => i != id).Contains(target));+ if (invalid) { Assert.Throws<ArgumentException>(() => f.Assign(id, target)); Assert.Equal(before, s.Describe()); }+ else if (target != null && model[id] == target) {+ Assert.Equal(Gdd03FixtureAssignmentResult.DeferredSameSlot, f.Assign(id, target)); Assert.Equal(before, s.Describe());+ } else { Assert.Equal(Gdd03FixtureAssignmentResult.Applied, f.Assign(id, target)); model[id] = target; }+ Assert.Equal(model, s.PopulationLedger.Units.Select(p => p.Assignment.Target).ToArray());+ Assert.Equal(new[] { 0, 1, 2 }, s.PopulationLedger.Units.Select(p => p.Id)); s.PopulationLedger.Reconcile();+ }+ }++ // R-CITY-002, R-CRAFT-002: tier quotation only, no production or Money settlement.+ [Theory] [InlineData("he-IL")] [InlineData("en-US")]+ public void RatesAndLabelsHaveNoSecondPaymentOrProfessionChange(string cultureName)+ {+ var previous = CultureInfo.CurrentCulture;+ try {+ CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(cultureName);+ foreach (var item in new[] {+ (CivilianProfession.None, CivilianTier.None, 1m),+ (CivilianProfession.Farmer, CivilianTier.Novice, 1m),+ (CivilianProfession.Farmer, CivilianTier.Trained, 1m),+ (CivilianProfession.Pioneer, CivilianTier.Veteran, 1m),+ (CivilianProfession.Craftsman, CivilianTier.Apprentice, 2m),+ (CivilianProfession.Craftsman, CivilianTier.Expert, 3m) }) {+ var c = Content(); var s = State(c, Person(item.Item1, item.Item2)); var before = s.PopulationLedger.Get(0);+ var f = new Gdd03AssignmentFixture(c, s, Slots); Assert.Equal(0m, f.IndustryRate(Slots[0].Id));+ f.Assign(0, Slots[0].Id); var canonical = s.Describe(); Assert.Equal(item.Item3, f.IndustryRate(Slots[0].Id));+ var relabelled = Slots.Select(x => new Gdd03FixtureSlot(x.Id, x.Work, x.Capacity, "generic"));+ Assert.Equal(item.Item3, new Gdd03AssignmentFixture(c, s, relabelled).IndustryRate(Slots[0].Id)); Assert.Equal(canonical, s.Describe());+ foreach (var slot in Slots.Skip(1)) { f.Assign(0, slot.Id); Assert.Equal(0m, f.IndustryRate(slot.Id)); Assert.Equal(0m, f.IndustryRate(Slots[0].Id)); }+ var after = s.PopulationLedger.Get(0); Assert.Equal(before.Profession, after.Profession); Assert.Equal(before.WorkXp, after.WorkXp);+ Assert.Same(before.Equipment, after.Equipment); Assert.Equal(before.Tier, after.Tier);+ }+ } finally { CultureInfo.CurrentCulture = previous; }+ }++ // R-CITY-001, R-POP-002: foundation refusal rolls back a valid-capacity destination too.+ [Fact] public void FoundationRefusalAndAggregateGuardsRemainAtomic()+ {+ var c = Content(); var s = State(c, Person());+ var invalidBinding = new Gdd03FixtureSlot("synthetic-unbound", Gdd03FixtureWork.Industry, 1, "generic");+ var f = new Gdd03AssignmentFixture(c, s, Slots.Concat(new[] { invalidBinding }));+ f.Assign(0, Slots[0].Id); var before = s.Describe();+ Assert.Throws<ArgumentException>(() => f.Assign(0, invalidBinding.Id)); Assert.Equal(before, s.Describe());+ Assert.Throws<NotSupportedException>(() => Simulation.Step(c, s, Array.Empty<Command>())); Assert.Equal(before, s.Describe());+ var cloned = s.Clone(); var cloneFixture = new Gdd03AssignmentFixture(c, cloned, Slots);+ cloneFixture.Assign(0, Slots[1].Id); Assert.Equal(before, s.Describe());+ Assert.Equal(Slots[1].Id, cloned.PopulationLedger.Get(0).Assignment.Target);+ }++ // R-CITY-002: typed reader has no missing/Deferred/malformed/old-mode fallback.+ [Fact] public void InvalidRatesAndOldIdentitiesAreRefused()+ {+ foreach (var numbers in new[] { new List<decimal>(), new() { 1m, 2m }, new() { 1m, 0m, 3m }, new() { 1m, 2m, 3m, 4m } }) {+ var c = Content(); c.Gdd03Balance!.Values["industry.income"].Values = numbers;+ var ex = Assert.Throws<ContentException>(() => Gdd03IndustryRates.Read(c)); Assert.Contains("values.industry.income", ex.Message);+ }+ foreach (var mutation in new Action<Content>[] {+ c => c.Gdd03Balance!.Values.Remove("industry.income"),+ c => c.Gdd03Balance!.Values["industry.income"].Basis = "Deferred",+ c => c.Gdd03Balance!.Values["industry.income"].Expression = "unknown",+ c => c.Gdd03Balance = null }) {+ var c = Content(); mutation(c); Assert.Throws<ContentException>(() => Gdd03IndustryRates.Read(c));+ }+ foreach (var identity in new[] { SaveGame.LegacyRuleset, SaveGame.GddRuleset }) {+ var c = Content(identity); var s = new GameState(c); var before = s.Describe();+ Assert.Throws<InvalidOperationException>(() => new Gdd03AssignmentFixture(c, s, Slots));+ Assert.Throws<InvalidOperationException>(() => Gdd03IndustryRates.Read(c)); Assert.Equal(before, s.Describe());+ }+ }++ // R-CITY-002: larger synthetic capacity tests accumulation and frozen catalogue ownership.+ [Fact] public void MixedTierSubtotalUsesFrozenRatesAfterCatalogueMutation()+ {+ var c = Content();+ var s = State(c, Person(), Person(CivilianProfession.Craftsman, CivilianTier.Apprentice),+ Person(CivilianProfession.Craftsman, CivilianTier.Expert));+ var slots = Slots.Select(x => new Gdd03FixtureSlot(x.Id, x.Work,+ x.Id == Slots[0].Id ? 3 : x.Capacity, x.Label)).ToArray();+ var f = new Gdd03AssignmentFixture(c, s, slots);+ var rates = Gdd03IndustryRates.Read(c);+ foreach (var id in new[] { 0, 1, 2 }) f.Assign(id, Slots[0].Id);+ var before = s.Describe();+ Assert.Equal(6m, f.IndustryRate(Slots[0].Id)); // independently supplied 1 + 2 + 3 subtotal+ c.Gdd03Balance!.Values["industry.income"].Values = new List<decimal> { 10m, 20m, 30m }; // synthetic mutation+ Assert.Equal(2m, rates.For(CivilianTier.Apprentice));+ Assert.Equal(20m, Gdd03IndustryRates.Read(c).For(CivilianTier.Apprentice));+ Assert.Equal(6m, f.IndustryRate(Slots[0].Id));+ Assert.Equal(before, s.Describe());+ }++ // R-CITY-001: direct ledger mutation cannot silently invalidate the frozen fixture's capacity.+ [Fact] public void DirectLedgerOverfillIsRefusedWithoutFurtherStateMutation()+ {+ var c = Content(); var s = State(c, Person(), Person());+ var f = new Gdd03AssignmentFixture(c, s, Slots); f.Assign(0, Slots[0].Id);+ var person = s.PopulationLedger.Get(1);+ s.PopulationLedger.Replace(person.WithPlacement(person.Status, person.Location,+ new PopulationAssignment(PopulationAssignmentKind.Work, Slots[0].Id), person.Captor, person.Home));+ var before = s.Describe();+ Assert.Throws<ArgumentException>(() => f.IndustryRate(Slots[0].Id)); Assert.Equal(before, s.Describe());+ Assert.Throws<ArgumentException>(() => f.Assign(1, Slots[1].Id)); Assert.Equal(before, s.Describe());+ Assert.Throws<ArgumentException>(() => new Gdd03AssignmentFixture(c, s, Slots)); Assert.Equal(before, s.Describe());+ }+}diff --git a/sim/Statecraft.Sim/Gdd03AssignmentFixture.cs b/sim/Statecraft.Sim/Gdd03AssignmentFixture.csnew file mode 100644index 000000000..a1785c2b7--- /dev/null+++ b/sim/Statecraft.Sim/Gdd03AssignmentFixture.cs@@ -0,0 +1,115 @@+using System;+using System.Collections.Generic;+using System.Linq;++namespace Statecraft.Sim+{+ // All entry points are internal. Only synthetic tests opt in; no command, loader or turn hookup.+ internal enum Gdd03FixtureWork { Industry, Builder, Farm, Workshop }+ internal enum Gdd03FixtureAssignmentResult { Applied, DeferredSameSlot }++ internal sealed class Gdd03FixtureSlot+ {+ internal string Id { get; }+ internal Gdd03FixtureWork Work { get; }+ internal int Capacity { get; }+ internal string Label { get; }+ internal Gdd03FixtureSlot(string id, Gdd03FixtureWork work, int capacity, string label)+ {+ PopulationText.ValidateText(id); PopulationText.ValidateText(label);+ if (string.IsNullOrWhiteSpace(id) || capacity < 0 || !Enum.IsDefined(typeof(Gdd03FixtureWork), work))+ throw new ArgumentException("Explicit fixture slot identity, work kind and nonnegative capacity required.");+ Id = id; Work = work; Capacity = capacity; Label = label;+ }+ }++ /// <summary>Frozen typed catalogue vector. Quotes rates; never awards Money or work receipts.</summary>+ internal sealed class Gdd03IndustryRates+ {+ readonly decimal ordinary, apprentice, expert;+ Gdd03IndustryRates(decimal ordinary, decimal apprentice, decimal expert)+ { this.ordinary = ordinary; this.apprentice = apprentice; this.expert = expert; }+ internal static Gdd03IndustryRates Read(Content content)+ {+ if (!content.UsesPopulationLedger) throw new InvalidOperationException("Industry fixture rates require gdd-0.3.");+ var catalogue = content.Gdd03Balance;+ if (catalogue == null || catalogue.Ruleset != SaveGame.Gdd03Ruleset || catalogue.Values == null+ || !catalogue.Values.TryGetValue("industry.income", out var row) || row == null+ || row.Basis != "Agreed" || row.Values == null || row.Values.Count != 3 || row.Values.Any(x => x <= 0)+ || row.Parts == null || row.Parts.Count != 0 || !string.IsNullOrEmpty(row.Expression))+ throw new ContentException(Gdd03BalanceCatalogue.FileName, "values.industry.income",+ "requires three positive Agreed decimal rates; no Deferred or legacy fallback");+ return new Gdd03IndustryRates(row.Values[0], row.Values[1], row.Values[2]);+ }+ internal decimal For(CivilianTier tier) => tier switch {+ CivilianTier.None or CivilianTier.Novice or CivilianTier.Trained or CivilianTier.Veteran => ordinary,+ CivilianTier.Apprentice => apprentice,+ CivilianTier.Expert => expert,+ _ => throw new ArgumentException("Unknown fixture tier.", nameof(tier)) };+ }++ /// <summary>R-CITY-001/002: frozen synthetic capacity overlay, never a second population ledger.+ /// Only explicit reception-complete free civilians moving among generic Work targets are supported.+ /// Feature-owned course/teaching/service transitions and same-slot policy remain outside this fixture.+ /// Single-threaded like the ledger; callers cannot reuse this overlay as a production authority.</summary>+ internal sealed class Gdd03AssignmentFixture+ {+ readonly GameState state;+ readonly SortedDictionary<string, Gdd03FixtureSlot> slots;+ readonly Gdd03IndustryRates rates;+ readonly int receptionTurns;+ internal Gdd03AssignmentFixture(Content content, GameState state, IEnumerable<Gdd03FixtureSlot> inventory)+ {+ if (!content.UsesPopulationLedger || !state.UsesPopulationLedger || !state.PopulationLedger.IsInitialized)+ throw new InvalidOperationException("Assignments require an initialized gdd-0.3 synthetic fixture.");+ this.state = state;+ rates = Gdd03IndustryRates.Read(content);+ receptionTurns = content.RequirePopulationInteger("reception.turns");+ slots = new SortedDictionary<string, Gdd03FixtureSlot>(StringComparer.Ordinal);+ foreach (var slot in inventory) slots.Add(slot.Id, slot); // duplicate aliases cannot add capacity+ ValidateInventory();+ }+ void ValidateInventory()+ {+ state.PopulationLedger.Reconcile();+ foreach (var p in state.PopulationLedger.Units.Where(p => p.Assignment.Kind == PopulationAssignmentKind.Work))+ if (p.Assignment.Target == null || !slots.ContainsKey(p.Assignment.Target))+ throw new ArgumentException("Work reservation is outside the frozen fixture inventory.");+ foreach (var slot in slots.Values)+ if (state.PopulationLedger.Units.Count(p => p.Assignment.Kind == PopulationAssignmentKind.Work && p.Assignment.Target == slot.Id) > slot.Capacity)+ throw new ArgumentException("Fixture inventory is over capacity.");+ }+ bool Eligible(PopulationUnit p) => p.Status == PopulationStatus.FreeCivilian && p.ReceptionTicks == receptionTurns;+ internal Gdd03FixtureAssignmentResult Assign(int id, string? target)+ {+ ValidateInventory();+ var ledger = state.PopulationLedger; var person = ledger.Get(id);+ if (!Eligible(person) || (person.Assignment.Kind != PopulationAssignmentKind.Unassigned && person.Assignment.Kind != PopulationAssignmentKind.Work))+ throw new ArgumentException("Only reception-complete free civilian work transitions are in this fixture.");+ if (target != null)+ {+ if (!slots.TryGetValue(target, out var slot)) throw new ArgumentException("Unknown fixture slot.");+ // This is a refusal/deferred result, not a chosen shipped idempotency policy.+ if (person.Assignment.Target == target) return Gdd03FixtureAssignmentResult.DeferredSameSlot;+ if (ledger.Units.Count(p => p.Assignment.Kind == PopulationAssignmentKind.Work && p.Assignment.Target == target) >= slot.Capacity)+ throw new ArgumentException("Fixture destination is full.");+ }+ var assignment = target == null ? PopulationAssignment.Unassigned : new PopulationAssignment(PopulationAssignmentKind.Work, target);+ // Replace validates the complete candidate before publishing it. Never release first.+ ledger.Replace(person.WithPlacement(person.Status, person.Location, assignment, person.Captor, person.Home));+ return Gdd03FixtureAssignmentResult.Applied;+ }+ /// <summary>Rate quotation for an explicitly supplied frozen fixture, not a month snapshot or payable receipt.+ /// Label is never identity. No Money/Food/XP/state mutation, time scaling or production consumer.</summary>+ internal decimal IndustryRate(string target)+ {+ ValidateInventory();+ if (!slots.TryGetValue(target, out var slot)) throw new ArgumentException("Unknown fixture slot.");+ if (slot.Work != Gdd03FixtureWork.Industry) return 0m;+ decimal total = 0m;+ foreach (var person in state.PopulationLedger.Units.Where(p => p.Assignment.Kind == PopulationAssignmentKind.Work && p.Assignment.Target == target).OrderBy(p => p.Id))+ if (Eligible(person)) total = checked(total + rates.For(person.Tier));+ return total;+ }+ }+}