A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Add synthetic craftsman course and work arithmetic, test-only (Refs #1698) (#1830)
Eyal Golan1fa15af401ff
diff --git a/changelog.d/1698-craft-arithmetic.md b/changelog.d/1698-craft-arithmetic.mdnew file mode 100644index 000000000..8327c198d--- /dev/null+++ b/changelog.d/1698-craft-arithmetic.md@@ -0,0 +1 @@+- Add isolated GDD 0.3 craftsman catalogue and course/work tick arithmetic for synthetic fixtures.diff --git a/sim/Statecraft.Sim.Tests/Gdd03CraftArithmeticTests.cs b/sim/Statecraft.Sim.Tests/Gdd03CraftArithmeticTests.csnew file mode 100644index 000000000..5bb9eb656--- /dev/null+++ b/sim/Statecraft.Sim.Tests/Gdd03CraftArithmeticTests.cs@@ -0,0 +1,169 @@+using System.Globalization;+using Statecraft.Sim;++// All identities, eligibility flags, progress and work receipts below are test-only synthetic inputs.+public class Gdd03CraftArithmeticTests+{+ static Gdd03BalanceCatalogue Catalogue() => ContentLoader.Load(RealContentTests.ContentRoot()).Gdd03Balance!;+ static Gdd03CraftArithmetic Arithmetic() => Gdd03CraftArithmetic.Read(Catalogue());++ // R-CRAFT-001/002: expected values independently transcribed from gdd03-balance.yaml:138-185,190-193.+ [Theory]+ [InlineData("he-IL")]+ [InlineData("en-US")]+ public void CatalogueAndSixCourseTicksUseOnlyCourseIncome(string culture)+ {+ var old = CultureInfo.CurrentCulture; var oldUi = CultureInfo.CurrentUICulture;+ try+ {+ CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture);+ CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(culture);+ var a = Arithmetic();+ Assert.Equal(5m, a.CourseCost); Assert.Equal(6, a.CourseTurns);+ Assert.Equal(1m, a.CourseIncome); Assert.Equal(36, a.ExpertWorkTurns);+ var progress = 0; decimal income = 0;+ for (var turn = 1; turn <= 6; turn++)+ {+ var r = a.CourseTick(17, progress, 4, eligibleTurn: true);+ Assert.Equal(17, r.UnitId); Assert.Equal(4, r.WorkXp);+ Assert.Equal(turn, r.ProgressTicks); Assert.Equal(turn == 6, r.Completes);+ Assert.Equal(1m, r.CourseIncome); Assert.Equal(0m, r.IndustryIncome);+ progress = r.ProgressTicks; income += r.CourseIncome;+ }+ Assert.Equal(6m, income);+ var idle = a.CourseTick(17, 5, 4, eligibleTurn: false);+ Assert.Equal(5, idle.ProgressTicks); Assert.False(idle.Completes);+ Assert.Equal(0m, idle.CourseIncome); Assert.Equal(0m, idle.IndustryIncome);+ // Complete-course input is invalid: caller must decide follow-on policy elsewhere.+ Assert.Throws<ArgumentOutOfRangeException>(() => a.CourseTick(17, 6, 4, true));+ }+ finally { CultureInfo.CurrentCulture = old; CultureInfo.CurrentUICulture = oldUi; }+ }++ // R-CRAFT-002: one confirmed work receipt, regardless of output/rate; old tier supplies this turn's quotes.+ [Theory]+ [InlineData("Industry")]+ [InlineData("Workshop")]+ [InlineData("Construction")]+ public void ActualWorkCrossesThresholdAfterOldTierOutput(string activityName)+ {+ var activity = Enum.Parse<CraftTickActivity>(activityName);+ var a = Arithmetic();+ var before = a.WorkTick(17, CivilianTier.Apprentice, 34, activity, true);+ Assert.Equal(35, before.WorkXp); Assert.Equal(CivilianTier.Apprentice, before.TierAfter);+ var crossing = a.WorkTick(17, before.TierAfter, before.WorkXp, activity, true);+ Assert.Equal(17, crossing.UnitId); Assert.Equal(36, crossing.WorkXp);+ Assert.Equal(CivilianTier.Apprentice, crossing.OutputTier); Assert.Equal(CivilianTier.Expert, crossing.TierAfter);+ Assert.Equal(activity == CraftTickActivity.Industry ? 2m : 0m, crossing.IndustryIncome);+ Assert.Equal(activity == CraftTickActivity.Construction ? 1.5m : 0m, crossing.BuilderWork);+ var next = a.WorkTick(17, crossing.TierAfter, crossing.WorkXp, activity, true);+ Assert.Equal(37, next.WorkXp); Assert.Equal(CivilianTier.Expert, next.OutputTier);+ Assert.Equal(activity == CraftTickActivity.Industry ? 3m : 0m, next.IndustryIncome);+ Assert.Equal(activity == CraftTickActivity.Construction ? 3m : 0m, next.BuilderWork);+ // Workshop work has no realized output field: confirmed intermediate/zero-yield turns still count.+ Assert.Equal(0m, a.WorkTick(17, CivilianTier.Apprentice, 0, CraftTickActivity.Workshop, true).IndustryIncome);+ }++ // R-CRAFT-002: synthetic 12+12+12 participation sequence, no second persisted XP store.+ [Fact]+ public void MixedWorkSourcesCountOneTickPerExplicitReceipt()+ {+ var a = Arithmetic(); var xp = 0; var tier = CivilianTier.Apprentice;+ foreach (var activity in new[] { CraftTickActivity.Industry, CraftTickActivity.Workshop, CraftTickActivity.Construction })+ for (var i = 0; i < 12; i++)+ {+ var r = a.WorkTick(17, tier, xp, activity, true); xp = r.WorkXp; tier = r.TierAfter;+ }+ Assert.Equal(36, xp); Assert.Equal(CivilianTier.Expert, tier);+ }++ // R-CRAFT-001/002: paused work and all nonwork activities award no output or XP.+ [Theory]+ [InlineData("Idle")] [InlineData("Travel")]+ [InlineData("Course")] [InlineData("Industry")]+ [InlineData("Workshop")] [InlineData("Construction")]+ public void NoActualWorkMeansNoXpOrOutput(string activityName)+ {+ var activity = Enum.Parse<CraftTickActivity>(activityName);+ var r = Arithmetic().WorkTick(17, CivilianTier.Apprentice, 35, activity, false);+ Assert.Equal(35, r.WorkXp); Assert.Equal(CivilianTier.Apprentice, r.TierAfter);+ Assert.Equal(0m, r.IndustryIncome); Assert.Equal(0m, r.BuilderWork);+ if (activity == CraftTickActivity.Idle || activity == CraftTickActivity.Travel || activity == CraftTickActivity.Course)+ Assert.Throws<ArgumentException>(() => Arithmetic().WorkTick(17, CivilianTier.Apprentice, 35, activity, true));+ }++ // R-CRAFT-001/002: malformed catalogue terms remain unavailable; no fallback/default or formula evaluation.+ [Theory]+ [InlineData("craft.course_cost")] [InlineData("craft.course_turns")]+ [InlineData("craft.course_income")] [InlineData("craft.expert_work_turns")]+ [InlineData("industry.income")] [InlineData("builder.work_rate")]+ public void ReaderRejectsMissingDeferredAndWrongShape(string key)+ {+ foreach (var fault in new[] { "missing", "deferred", "default", "expression", "arity", "parts", "negative" })+ {+ var c = Catalogue(); var value = c.Values[key];+ switch (fault)+ {+ case "missing": c.Values.Remove(key); break;+ case "deferred": value.Basis = "Deferred"; value.Values.Clear(); value.Expression = "unresolved"; break;+ case "default": value.Basis = "Default"; break;+ case "expression": value.Expression = "unresolved"; break;+ case "arity": value.Values.Add(9m); break; // test-only malformed synthetic value+ case "parts": value.Parts["synthetic"] = new Gdd03DesignValue(); break;+ case "negative": value.Values[0] = -1m; break; // test-only invalid value+ }+ Assert.Throws<ContentException>(() => Gdd03CraftArithmetic.Read(c));+ }+ }++ [Theory]+ [InlineData("craft.course_turns")] [InlineData("craft.expert_work_turns")]+ public void ReaderRejectsZeroFractionalAndOutOfRangeTicks(string key)+ {+ foreach (var invalid in new[] { 0m, 1.5m, (decimal)int.MaxValue + 1m })+ {+ var c = Catalogue(); c.Values[key].Values[0] = invalid;+ Assert.Throws<ContentException>(() => Gdd03CraftArithmetic.Read(c));+ }+ }++ [Fact]+ public void ReaderSnapshotsValuesAndRequiresExactCatalogueIdentity()+ {+ var c = Catalogue(); var a = Gdd03CraftArithmetic.Read(c);+ c.Values["craft.course_income"].Values[0] = 99m; // test-only mutation after read+ Assert.Equal(1m, a.CourseTick(17, 0, 0, true).CourseIncome);+ foreach (var identity in new[] { "legacy", "gdd-0.1", "future" })+ {+ c.Ruleset = identity;+ Assert.Throws<ContentException>(() => Gdd03CraftArithmetic.Read(c));+ }+ }++ [Fact]+ public void InvalidExplicitInputsAndXpOverflowRefuse()+ {+ var a = Arithmetic();+ Assert.Throws<ArgumentOutOfRangeException>(() => a.CourseTick(-1, 0, 0, true));+ Assert.Throws<ArgumentOutOfRangeException>(() => a.CourseTick(17, -1, 0, true));+ Assert.Throws<ArgumentOutOfRangeException>(() => a.CourseTick(17, 0, -1, true));+ Assert.Throws<ArgumentOutOfRangeException>(() => a.WorkTick(-1, CivilianTier.Apprentice, 0, CraftTickActivity.Industry, true));+ Assert.Throws<ArgumentOutOfRangeException>(() => a.WorkTick(17, CivilianTier.Apprentice, -1, CraftTickActivity.Industry, true));+ Assert.Throws<ArgumentOutOfRangeException>(() => a.WorkTick(17, CivilianTier.Trained, 0, CraftTickActivity.Industry, true));+ Assert.Throws<ArgumentOutOfRangeException>(() => a.WorkTick(17, CivilianTier.Apprentice, 0, (CraftTickActivity)99, true));+ Assert.Throws<OverflowException>(() => a.WorkTick(17, CivilianTier.Expert, int.MaxValue, CraftTickActivity.Industry, true));+ }++ // R-CRAFT-002: a shared builder snapshot needs only its key and cannot expose a mutable backing array.+ [Fact] public void BuilderRateAccessorIsNarrowFrozenAndReadOnly()+ {+ var catalogue = Catalogue(); var row = catalogue.Values["builder.work_rate"];+ catalogue.Values.Clear(); catalogue.Values.Add("builder.work_rate", row);+ var rates = Gdd03CraftArithmetic.ReadBuilderRates(catalogue);+ Assert.Equal(new[] { 1m, 1.5m, 3m }, rates);+ row.Values[0] = 99m; // synthetic source mutation after snapshot+ Assert.Equal(1m, rates[0]);+ Assert.Throws<NotSupportedException>(() => ((IList<decimal>)rates)[0] = 9m);+ Assert.Throws<ContentException>(() => Gdd03CraftArithmetic.Read(catalogue));+ }+}diff --git a/sim/Statecraft.Sim/Gdd03AssignmentFixture.cs b/sim/Statecraft.Sim/Gdd03AssignmentFixture.csindex a1785c2b7..996e51a99 100644--- a/sim/Statecraft.Sim/Gdd03AssignmentFixture.cs+++ b/sim/Statecraft.Sim/Gdd03AssignmentFixture.cs@@ -32,7 +32,10 @@ namespace Statecraft.Siminternal static Gdd03IndustryRates Read(Content content){if (!content.UsesPopulationLedger) throw new InvalidOperationException("Industry fixture rates require gdd-0.3.");- var catalogue = content.Gdd03Balance;+ return Read(content.Gdd03Balance);+ }+ internal static Gdd03IndustryRates Read(Gdd03BalanceCatalogue? catalogue)+ {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)diff --git a/sim/Statecraft.Sim/Gdd03CraftArithmetic.cs b/sim/Statecraft.Sim/Gdd03CraftArithmetic.csnew file mode 100644index 000000000..eb9c1e07a--- /dev/null+++ b/sim/Statecraft.Sim/Gdd03CraftArithmetic.cs@@ -0,0 +1,132 @@+using System;+using System.Collections.Generic;++namespace Statecraft.Sim+{+ // Fixture-only input labels; not assignments, persisted receipts or an eligibility decision.+ internal enum CraftTickActivity { Idle, Travel, Course, Industry, Workshop, Construction }++ /// <summary>Detached #1698 arithmetic on explicit synthetic inputs. No GameState/ledger mutation,+ /// reservation, payment, receipt deduplication or live promotion. Future owners supply those contracts.</summary>+ internal sealed class Gdd03CraftArithmetic+ {+ internal decimal CourseCost { get; }+ internal int CourseTurns { get; }+ internal decimal CourseIncome { get; }+ internal int ExpertWorkTurns { get; }+ readonly decimal apprenticeIncome, expertIncome, apprenticeWork, expertWork;++ Gdd03CraftArithmetic(decimal cost, int turns, decimal income, int expertTurns,+ decimal apprenticeIncome, decimal expertIncome, decimal apprenticeWork, decimal expertWork)+ {+ CourseCost = cost; CourseTurns = turns; CourseIncome = income; ExpertWorkTurns = expertTurns;+ this.apprenticeIncome = apprenticeIncome; this.expertIncome = expertIncome;+ this.apprenticeWork = apprenticeWork; this.expertWork = expertWork;+ }++ // R-CRAFT-001/002: exact agreed catalogue quantities only. This does not activate any ruleset.+ internal static Gdd03CraftArithmetic Read(Gdd03BalanceCatalogue catalogue)+ {+ RequireCatalogue(catalogue);+ var cost = Terms(catalogue, "craft.course_cost", 1)[0];+ var turns = Whole(Terms(catalogue, "craft.course_turns", 1)[0], "craft.course_turns");+ var income = Terms(catalogue, "craft.course_income", 1)[0];+ var expertTurns = Whole(Terms(catalogue, "craft.expert_work_turns", 1)[0], "craft.expert_work_turns");+ var industry = Gdd03IndustryRates.Read(catalogue);+ var builder = ReadBuilderRates(catalogue);+ return new Gdd03CraftArithmetic(cost, turns, income, expertTurns,+ industry.For(CivilianTier.Apprentice), industry.For(CivilianTier.Expert), builder[1], builder[2]);+ }++ // Frozen ordinary/apprentice/expert vector for catalogue-only consumers; no course terms required.+ // Consumers may layer stricter project rules on these validated nonnegative rates.+ internal static IReadOnlyList<decimal> ReadBuilderRates(Gdd03BalanceCatalogue catalogue)+ {+ RequireCatalogue(catalogue);+ return Array.AsReadOnly(Terms(catalogue, "builder.work_rate", 3));+ }++ static void RequireCatalogue(Gdd03BalanceCatalogue catalogue)+ {+ if (catalogue == null) throw new ArgumentNullException(nameof(catalogue));+ if (catalogue.Ruleset != SaveGame.Gdd03Ruleset)+ throw new ContentException(Gdd03BalanceCatalogue.FileName, "ruleset", "craft arithmetic requires the gdd-0.3 catalogue");+ }++ static decimal[] Terms(Gdd03BalanceCatalogue catalogue, string key, int count)+ {+ if (catalogue.Values == null || !catalogue.Values.TryGetValue(key, out var v) || v == null+ || v.Basis != "Agreed" || v.Values == null || v.Values.Count != count+ || !string.IsNullOrEmpty(v.Expression) || v.Parts == null || v.Parts.Count != 0)+ throw new ContentException(Gdd03BalanceCatalogue.FileName, "values." + key, "requires exact agreed scalar or tier values; no Deferred/default substitution");+ var result = v.Values.ToArray();+ foreach (var value in result)+ if (value < 0) throw new ContentException(Gdd03BalanceCatalogue.FileName, "values." + key, "requires nonnegative quantities");+ return result;+ }++ static int Whole(decimal value, string key)+ {+ if (value <= 0 || value > int.MaxValue || decimal.Truncate(value) != value)+ throw new ContentException(Gdd03BalanceCatalogue.FileName, "values." + key, "requires a positive whole Int32 tick count");+ return (int)value;+ }++ // Caller confirms one eligible course turn. Completion is a quote, not a profession/assignment mutation.+ // R-CRAFT-001: final course turn still has only course income, never Industry income or work XP.+ internal CraftCourseTick CourseTick(int unitId, int progressTicks, int workXp, bool eligibleTurn)+ {+ Nonnegative(unitId, nameof(unitId)); Nonnegative(workXp, nameof(workXp));+ if (progressTicks < 0 || progressTicks >= CourseTurns) throw new ArgumentOutOfRangeException(nameof(progressTicks));+ var progress = eligibleTurn ? checked(progressTicks + 1) : progressTicks;+ return new CraftCourseTick(unitId, progress, workXp, eligibleTurn ? CourseIncome : 0m, progress == CourseTurns);+ }++ // Exactly ONE caller-confirmed participation receipt. Common owner must deduplicate by unit/turn+ // before invoking and apply a result at most once. Realized output is deliberately not an XP input.+ // R-CRAFT-002: tier before this tick quotes output; threshold crossing affects only TierAfter.+ internal CraftWorkTick WorkTick(int unitId, CivilianTier tier, int workXp, CraftTickActivity activity, bool actualWork)+ {+ Nonnegative(unitId, nameof(unitId)); Nonnegative(workXp, nameof(workXp));+ if (tier != CivilianTier.Apprentice && tier != CivilianTier.Expert) throw new ArgumentOutOfRangeException(nameof(tier));+ if (!Enum.IsDefined(typeof(CraftTickActivity), activity)) throw new ArgumentOutOfRangeException(nameof(activity));+ var working = activity == CraftTickActivity.Industry || activity == CraftTickActivity.Workshop || activity == CraftTickActivity.Construction;+ if (actualWork && !working) throw new ArgumentException("A nonwork activity cannot carry an actual-work receipt.", nameof(actualWork));+ var xp = actualWork ? checked(workXp + 1) : workXp;+ var after = actualWork && tier == CivilianTier.Apprentice && xp >= ExpertWorkTurns ? CivilianTier.Expert : tier;+ var income = actualWork && activity == CraftTickActivity.Industry ? (tier == CivilianTier.Apprentice ? apprenticeIncome : expertIncome) : 0m;+ var work = actualWork && activity == CraftTickActivity.Construction ? (tier == CivilianTier.Apprentice ? apprenticeWork : expertWork) : 0m;+ return new CraftWorkTick(unitId, tier, after, xp, income, work);+ }++ static void Nonnegative(int value, string name)+ {+ if (value < 0) throw new ArgumentOutOfRangeException(name);+ }+ }++ // Ephemeral arithmetic results, never a second population/course/XP ledger or save format.+ internal sealed class CraftCourseTick+ {+ internal int UnitId { get; }+ internal int ProgressTicks { get; }+ internal int WorkXp { get; }+ internal decimal CourseIncome { get; }+ internal decimal IndustryIncome => 0m;+ internal bool Completes { get; }+ internal CraftCourseTick(int unitId, int progress, int xp, decimal income, bool completes)+ { UnitId = unitId; ProgressTicks = progress; WorkXp = xp; CourseIncome = income; Completes = completes; }+ }++ internal sealed class CraftWorkTick+ {+ internal int UnitId { get; }+ internal CivilianTier OutputTier { get; }+ internal CivilianTier TierAfter { get; }+ internal int WorkXp { get; }+ internal decimal IndustryIncome { get; }+ internal decimal BuilderWork { get; }+ internal CraftWorkTick(int unitId, CivilianTier before, CivilianTier after, int xp, decimal income, decimal work)+ { UnitId = unitId; OutputTier = before; TierAfter = after; WorkXp = xp; IndustryIncome = income; BuilderWork = work; }+ }+}