A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace Statecraft.Sim
{
public enum PlayerRegionFacilityKind { Industry, Workshop, Reception, Training }
/// <summary>Explicit fixture capacity contribution. Its stable id may be counted only once nationally.</summary>
public sealed class PlayerRegionFacility
{
public string Id { get; }
public string Location { get; }
public PlayerRegionFacilityKind Kind { get; }
public int Capacity { get; }
public PlayerRegionFacility(string id, string location, PlayerRegionFacilityKind kind, int capacity)
{
if (string.IsNullOrWhiteSpace(id) || string.IsNullOrWhiteSpace(location) || capacity < 0
|| !Enum.IsDefined(typeof(PlayerRegionFacilityKind), kind)) throw new ArgumentException("Explicit facility identity, kind and nonnegative capacity required.");
PopulationText.ValidateText(id); PopulationText.ValidateText(location);
Id = id; Location = location; Kind = kind; Capacity = capacity;
}
internal string Canonical() => PopulationText.Fields(Id, Location, Kind.ToString(), Capacity);
}
/// <summary>Immutable regional capacity record, with base values separate from uniquely owned contributions.
/// All quantities must be supplied; there is no start allocation or old-city fallback.</summary>
public sealed class PlayerRegionPool
{
public string Id { get; }
public int BaseOrdinaryHousing { get; }
public int BaseIndustrySlots { get; }
public int BaseWorkshopSlots { get; }
public decimal Condition { get; }
public IReadOnlyList<PlayerRegionFacility> Facilities { get; }
public int OrdinaryHousing => BaseOrdinaryHousing;
public int IndustrySlots => checked(BaseIndustrySlots + Capacity(PlayerRegionFacilityKind.Industry));
public int WorkshopSlots => checked(BaseWorkshopSlots + Capacity(PlayerRegionFacilityKind.Workshop));
public int ReceptionBeds => Capacity(PlayerRegionFacilityKind.Reception);
public int TrainingBeds => Capacity(PlayerRegionFacilityKind.Training);
public PlayerRegionPool(string id, int ordinaryHousing, decimal condition, int industrySlots, int workshopSlots,
IEnumerable<PlayerRegionFacility> facilities)
{
if (string.IsNullOrWhiteSpace(id) || ordinaryHousing < 0 || industrySlots < 0 || workshopSlots < 0 || condition < 0m || condition > 1m)
throw new ArgumentException("Explicit region id, nonnegative capacities and a condition fraction from zero to one required.");
PopulationText.ValidateText(id);
Id = id; BaseOrdinaryHousing = ordinaryHousing; Condition = condition;
BaseIndustrySlots = industrySlots; BaseWorkshopSlots = workshopSlots;
var rows = facilities.OrderBy(f => f.Id, StringComparer.Ordinal).ToArray();
if (rows.Select(f => f.Id).Distinct(StringComparer.Ordinal).Count() != rows.Length) throw new ArgumentException("Duplicate facility id.");
Facilities = Array.AsReadOnly(rows);
// Validate sums before a record can enter a state.
_ = IndustrySlots; _ = WorkshopSlots; _ = ReceptionBeds; _ = TrainingBeds;
}
int Capacity(PlayerRegionFacilityKind kind)
{
int sum = 0;
foreach (var f in Facilities) if (f.Kind == kind) sum = checked(sum + f.Capacity);
return sum;
}
internal string Canonical() => PopulationText.Fields(Id, BaseOrdinaryHousing, Condition, BaseIndustrySlots,
BaseWorkshopSlots, string.Join(";", Facilities.Select(f => f.Canonical())));
}
/// <summary>One settlement's control state. Residents are a state, inhabited (the default) or depopulated, never a count
/// or a combatant field (Q-CAP-2 (b); R-CTRL-003). Immutable so <see cref="GameState.Clone"/> can copy the containing map
/// shallowly.</summary>
public sealed partial class SettlementControl
{
public string Owner { get; }
public string State { get; }
public DateTime Since { get; }
public bool Depopulated { get; }
public int Combatants { get; }
public string Buildings { get; }
/// <summary>The occupation's turns left (R-CTRL-012): X when Occupy is given, counted down in resolver step 10; 0 when
/// the place is not occupied or its timer has ended.</summary>
public int Turns { get; }
/// <summary>The residents fled when the place was captured (R-CTRL-003, R-CTRL-005; OWNER QUESTION 2 (b) on #799). Set
/// at capture from the target row and never cleared, so it outlasts every return to enemy control.</summary>
public bool Fled { get; }
public SettlementControl(string owner = "", string state = "", DateTime since = default, bool depopulated = false, int combatants = 0, string buildings = "", int turns = 0, bool fled = false)
{
Owner = owner; State = state; Since = since; Depopulated = depopulated; Combatants = combatants; Buildings = buildings; Turns = turns; Fled = fled;
}
public string Canonical()
{
var fields = new List<string>();
if (Owner != "") fields.Add("owner=" + Owner);
if (State != "") fields.Add("state=" + State);
if (Since != default) fields.Add("since=" + Since.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
if (Depopulated) fields.Add("depopulated=1");
if (Combatants != 0) fields.Add("combatants=" + Combatants.ToString(CultureInfo.InvariantCulture));
if (Buildings != "") fields.Add("buildings=" + Buildings);
if (Turns != 0) fields.Add("turns=" + Turns.ToString(CultureInfo.InvariantCulture));
if (Fled) fields.Add("fled=1");
return string.Join(",", fields);
}
}
/// <summary>A city in the GDD v0.1 loop (docs/GDD.md "Cities and construction"; owner decision D-A2: civilians and
/// refugees are counts per city). Counts are population units, each a group (D-A1). Immutable, so GameState.Clone can copy
/// the map shallowly; a change replaces the record. Only the attributes approved decisions define are here.</summary>
public sealed class City
{
public string Id { get; }
/// <summary>How much population the city can house (GDD "Housing capacity").</summary>
public int HousingCapacity { get; }
/// <summary>The number of jobs (GDD "Employment slots").</summary>
public int JobSlots { get; }
public int Civilians { get; }
/// <summary>The people in jobs: part of <see cref="Civilians"/> and <see cref="Craftsmen"/>, not in addition to them.
/// Craftsmen take the jobs first (R-CRAFT-002).</summary>
public int Employed { get; }
public int Refugees { get; }
/// <summary>Legacy city recruit count, retained for canonical compatibility. New training completes into
/// State.Units (R-TRAIN-003), never this field; saves replay their command log rather than city snapshots.</summary>
public int Recruits { get; }
/// <summary>The craftsmen trained here (D-D4; R-CRAFT-001): a count, not movable units (D-A2), and not part of
/// <see cref="Civilians"/>.</summary>
public int Craftsmen { get; }
/// <summary>Whether the housing step fills the city's free jobs (R-CITY-009, Q-JOBS-1). On at the start; an accepted
/// AssignWorkers order turns it off for the city.</summary>
public bool JobsDefault { get; }
/// <summary>The city's housing condition, 0 to 1, an average over the city (GDD "Housing condition"; D-D2, Q-HOUSE-2;
/// R-HOUSE-001). Null only on a record made without one (a test's hand-built city); every GDD game's cities have one.</summary>
public double? Condition { get; }
/// <summary>The civilians who keep a pioneer's or craftsman's qualification (R-QUAL-001): part of <see cref="Civilians"/>,
/// not in addition to them. <see cref="Qualified.None"/> in every city until someone returns to civilian life.</summary>
public Qualified Qualified { get; }
public City(string id, int housingCapacity, int jobSlots, int civilians, int employed, int refugees, int recruits = 0,
double? condition = null, int craftsmen = 0, bool jobsDefault = true, Qualified? qualified = null)
{
Id = id; HousingCapacity = housingCapacity; JobSlots = jobSlots; Civilians = civilians; Employed = employed; Refugees = refugees;
Recruits = recruits; Condition = condition; Craftsmen = craftsmen; JobsDefault = jobsDefault; Qualified = qualified ?? Qualified.None;
}
/// <summary>The same city with new population counts.</summary>
public City WithCounts(int civilians, int employed, int refugees) => new City(Id, HousingCapacity, JobSlots, civilians, employed, refugees, Recruits, Condition, Craftsmen, JobsDefault, Qualified);
/// <summary>The same city with a new number of recruits.</summary>
public City WithRecruits(int recruits) => new City(Id, HousingCapacity, JobSlots, Civilians, Employed, Refugees, recruits, Condition, Craftsmen, JobsDefault, Qualified);
/// <summary>The same city with a new number of craftsmen.</summary>
public City WithCraftsmen(int craftsmen) => new City(Id, HousingCapacity, JobSlots, Civilians, Employed, Refugees, Recruits, Condition, craftsmen, JobsDefault, Qualified);
/// <summary>The same city with its job default on or off.</summary>
public City WithJobsDefault(bool enabled) => new City(Id, HousingCapacity, JobSlots, Civilians, Employed, Refugees, Recruits, Condition, Craftsmen, enabled, Qualified);
/// <summary>The same city with a new housing condition.</summary>
public City WithCondition(double condition) => new City(Id, HousingCapacity, JobSlots, Civilians, Employed, Refugees, Recruits, condition, Craftsmen, JobsDefault, Qualified);
/// <summary>The same city with new qualified civilians (R-QUAL-001).</summary>
public City WithQualified(Qualified qualified) => new City(Id, HousingCapacity, JobSlots, Civilians, Employed, Refugees, Recruits, Condition, Craftsmen, JobsDefault, qualified);
/// <summary>The record as the state hash writes it. The recruits are written only when there are some, the condition
/// only when it is set (",c" and its 64 bits in hex; readable, the value and then the bits), the craftsmen only when
/// there are some and the job default only when it is off (",jobs=manual"), so a city with none of them hashes as it
/// did before they existed (R-LOOP-002's rule).</summary>
public string Canonical(bool readable = false)
{
var text = string.Format(CultureInfo.InvariantCulture, "{0}={1},{2},{3},{4},{5}", Id, HousingCapacity, JobSlots, Civilians, Employed, Refugees)
+ (Recruits == 0 ? "" : "," + Recruits.ToString(CultureInfo.InvariantCulture));
if (Condition is double c)
{
if (c == 0.0) c = 0.0; // -0 and 0 hash alike, as GameState's numbers do
var bits = BitConverter.DoubleToInt64Bits(c).ToString("X16", CultureInfo.InvariantCulture);
text += readable ? ",c" + c.ToString("R", CultureInfo.InvariantCulture) + " [" + bits + "]" : ",c" + bits;
}
if (Craftsmen != 0) text += ",craftsmen=" + Craftsmen.ToString(CultureInfo.InvariantCulture);
if (!JobsDefault) text += ",jobs=manual";
return text + Qualified.Canonical();
}
}
/// <summary>A movable unit in the GDD v0.1 loop (owner decision D-A2: halutzim and soldiers are movable units), a group
/// (D-A1), on hex (Q, R). Immutable; a change replaces the record.</summary>
public sealed class Unit
{
public int Id { get; }
public string Type { get; }
public int Q { get; }
public int R { get; }
/// <summary>Turns of work done as a halutz (R-VET-001). Phase 5 Q-UNIT-2 counts soldiers' service in the same field.</summary>
public int Service { get; }
/// <summary>A military unit's side (a unit_types.yaml `side`), or "" for a halutz (R-UNIT-001).</summary>
public string Side { get; }
/// <summary>A military unit's strength in population units (Q-UNIT-1, Q-UNIT-4), or 0 (R-UNIT-001).</summary>
public int Strength { get; }
/// <summary>The unit's standing order and what it names, or "" for none (R-UNIT-001). Later Phase 5 PRs set them.</summary>
public string Order { get; }
public string OrderTarget { get; }
/// <summary>The city whose housing includes this formation (R-HOUSE-003, Q-HOUSE-5), independent of its current hex.
/// Empty for units without a known home, including pioneers.</summary>
public string HomeCity { get; }
/// <summary>The strength the formation had before its first loss, the most replacements refill it to (R-TRAIN-004), or
/// 0 while it has lost none: combat sets it (R-COMBAT-003) and a refill to full clears it.</summary>
public int Full { get; }
/// <summary>Combat experience under exact gdd-0.3 (R-UNIT-011): survived engagements, up to military.combat_xp_full.
/// Always 0 under every other ruleset.</summary>
public int CombatXp { get; }
/// <summary>#1725 PR 7 (owner, 9 November 2026, "Add the link first (Recommended)" and "List on the unit (Recommended)"):
/// on a Historical ledger game, the population ids of the pioneer records that form this halutz or veteran pioneer
/// unit, ascending, hashed only when set, like a cohort's members; null on any other game and for any other unit. A
/// promotion keeps it ("Yes, keep the link (Recommended)"), as every change of a unit does (R-UNIT-002).</summary>
public IReadOnlyList<int>? Records { get; }
public Unit(int id, string type, int q, int r, int service = 0, string side = "", int strength = 0, string order = "", string orderTarget = "", string homeCity = "", int full = 0, int combatXp = 0, IEnumerable<int>? records = null)
{
Id = id; Type = type; Q = q; R = r; Service = service; Side = side; Strength = strength; Order = order; OrderTarget = orderTarget; HomeCity = homeCity; Full = full; CombatXp = combatXp;
if (records != null)
{
var sorted = records.OrderBy(x => x).ToArray();
if (sorted.Length == 0 || sorted.Distinct().Count() != sorted.Length)
throw new ArgumentException("A unit's records are one or more distinct population ids.", nameof(records));
Records = Array.AsReadOnly(sorted);
}
}
/// <summary>The record as the state hash writes it. Each field after the hex is written only when it is not at its
/// default, so a halutz with no service hashes as it did before Phase 4 PR 6, and with service as before Phase 5
/// (R-LOOP-002's rule; R-UNIT-002).</summary>
public string Canonical() => string.Format(CultureInfo.InvariantCulture, "{0}:{1}@{2},{3}", Id, Type, Q, R)
+ (Service == 0 ? "" : "/" + Service.ToString(CultureInfo.InvariantCulture))
+ (Side == "" ? "" : "|side=" + Side)
+ (Strength == 0 ? "" : "|strength=" + Strength.ToString(CultureInfo.InvariantCulture))
+ (Order == "" ? "" : "|order=" + Order)
+ (OrderTarget == "" ? "" : "|target=" + OrderTarget)
+ (HomeCity == "" ? "" : "|home=" + HomeCity)
+ (Full == 0 ? "" : "|full=" + Full.ToString(CultureInfo.InvariantCulture))
+ (CombatXp == 0 ? "" : "|xp=" + CombatXp.ToString(CultureInfo.InvariantCulture))
+ (Records == null ? "" : "|records=" + string.Join(",", Records.Select(x => x.ToString(CultureInfo.InvariantCulture))));
}
/// <summary>A group of a city's people in training (GDD "Progression"; R-TRAIN-001): <see cref="Count"/> population units
/// becoming <see cref="Type"/> (a unit_types.yaml id with a facility) in <see cref="TurnsLeft"/> more turns, holding the
/// <see cref="Arms"/> its training took when it started (R-ARMS-003), which a cancel returns. Immutable; a change replaces the
/// record.</summary>
public sealed class Cohort
{
public int Id { get; }
public string Type { get; }
public string City { get; }
public int Count { get; }
public int TurnsLeft { get; }
public int Arms { get; }
/// <summary>The formation these replacements join when they finish (R-TRAIN-004), or null for a cohort that becomes
/// something new (R-TRAIN-003).</summary>
public int? Unit { get; }
/// <summary>#1725 PR 7, producer 12 (owner, 8 November 2026, "Cohorts stay main (Rec.)" (#1725 issuecomment-6724941867): the cohort keeps a
/// sorted list of its members' ids, hashed only when set): on a Historical ledger game, the population ids of the cohort's trainee
/// records, ascending, one per <see cref="Count"/>; null on any other game.</summary>
public IReadOnlyList<int>? Members { get; }
public Cohort(int id, string type, string city, int count, int turnsLeft, int arms = 0, int? unit = null, IEnumerable<int>? members = null)
{
Id = id; Type = type; City = city; Count = count; TurnsLeft = turnsLeft; Arms = arms; Unit = unit;
if (members != null)
{
var sorted = members.OrderBy(x => x).ToArray();
if (sorted.Length != count || sorted.Distinct().Count() != sorted.Length)
throw new ArgumentException("A cohort's members are its count of distinct population ids.", nameof(members));
Members = Array.AsReadOnly(sorted);
}
}
/// <summary>The same cohort with <paramref name="turnsLeft"/> turns to go.</summary>
public Cohort WithTurnsLeft(int turnsLeft) => new Cohort(Id, Type, City, Count, turnsLeft, Arms, Unit, Members);
/// <summary>The record as the state hash writes it. Arms only when the cohort holds some, the target unit only when
/// it has one, and the members only when set, so other cohorts hash as before.</summary>
public string Canonical() => string.Format(CultureInfo.InvariantCulture, "{0}:{1}@{2}x{3}/{4}", Id, Type, City, Count, TurnsLeft)
+ (Arms == 0 ? "" : "|arms=" + Arms.ToString(CultureInfo.InvariantCulture))
+ (Unit is int u ? "|unit=" + u.ToString(CultureInfo.InvariantCulture) : "")
+ (Members == null ? "" : "|members=" + string.Join(",", Members.Select(m => m.ToString(CultureInfo.InvariantCulture))));
}
/// <summary>Pioneer work at a designated site (sites.yaml; R-SITE-001 to R-SITE-003): <see cref="Unit"/> building it with
/// <see cref="TurnsLeft"/> more turns to go, or, once <see cref="BuiltOn"/> is set, built on that date. Only sites being
/// built or built have a record; an available site has none. A unit is busy while a record names it (D-D6: one project per
/// unit), so Unit itself holds no order. Immutable; a change replaces the record.</summary>
public sealed class SiteWork
{
public string Site { get; }
/// <summary>The halutz unit building the site; null once it is built (unit ids start at 0, so 0 is a real unit).</summary>
public int? Unit { get; }
public int TurnsLeft { get; }
/// <summary>The end date of the turn the site was built on; null while it is being built.</summary>
public DateTime? BuiltOn { get; }
/// <summary>The turn-equivalents of work accumulated toward the next whole turn (R-MIN-002: the office multiplier's
/// scaled gain accumulates here, and only whole turns leave it, so no fraction of a turn is ever a turn to go). In
/// [0, 1) while being built; 0 once built and on a site no minister system scales.</summary>
public double Progress { get; }
/// <summary>A site being built (<paramref name="builtOn"/> null) needs its unit; a built site has none.</summary>
public SiteWork(string site, int? unit, int turnsLeft, DateTime? builtOn, double progress = 0)
{
if ((builtOn == null) != (unit != null)) throw new ArgumentException("a site being built has a unit, and a built site has none", nameof(unit));
Site = site; Unit = unit; TurnsLeft = turnsLeft; BuiltOn = builtOn; Progress = progress;
}
/// <summary>The site built on <paramref name="on"/>, with no unit on it.</summary>
public static SiteWork Built(string site, DateTime on) => new SiteWork(site, null, 0, on);
/// <summary>The same work with <paramref name="turnsLeft"/> turns to go.</summary>
public SiteWork WithTurnsLeft(int turnsLeft) => new SiteWork(Site, Unit, turnsLeft, BuiltOn, Progress);
/// <summary>The same work with <paramref name="turnsLeft"/> turns to go and <paramref name="progress"/> accumulated
/// (R-MIN-002's floor with carry).</summary>
public SiteWork WithProgress(int turnsLeft, double progress) => new SiteWork(Site, Unit, turnsLeft, BuiltOn, progress);
/// <summary>The record as the state hash writes it: "id=b,UNIT,TURNSLEFT" while building, ",PROGRESS" appended when
/// it is not 0, as IEEE bits like the other hashed doubles (R-MIN-002), "id=done,yyyy-MM-dd" once built.</summary>
public string Canonical() => BuiltOn is DateTime on
? Site + "=done," + on.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)
: string.Format(CultureInfo.InvariantCulture, "{0}=b,{1},{2}", Site, Unit, TurnsLeft)
+ (Progress == 0 ? "" : "," + BitConverter.DoubleToInt64Bits(Progress).ToString("X16", CultureInfo.InvariantCulture));
}
/// <summary>A city project (projects.yaml; R-PROJ-001 to R-PROJ-003): <see cref="Kind"/> built for <see cref="City"/> on hex
/// (<see cref="Q"/>, <see cref="R"/>) with <see cref="TurnsLeft"/> more turns to go, or, once <see cref="BuiltOn"/> is set,
/// built on that date (its capacity, condition and slots added then, once). A built project keeps its record, so its hex
/// stays taken (Q-PROJ-2) and the map can draw it (Phase 3 PR 8); a gdd-0.3 housing repair, which has no hex of its own,
/// leaves no record once done (R-HOUSE-006). No workers (Q-PROJ-3); under gdd-0.3 at most one
/// builder (<see cref="Builder"/>, R-CITY-011). Immutable; a change replaces the record.</summary>
public sealed class CityProject
{
public int Id { get; }
public string Kind { get; }
public string City { get; }
public int Q { get; }
public int R { get; }
public int TurnsLeft { get; }
/// <summary>The end date of the turn the project was built on; null while it is being built.</summary>
public DateTime? BuiltOn { get; }
/// <summary>The turn-equivalents of work accumulated toward the next whole turn (R-MIN-002), as
/// <see cref="SiteWork.Progress"/>. In [0, 1) while being built; 0 once built and on a kind no minister system scales.</summary>
public double Progress { get; }
/// <summary>R-CITY-011 (#1700, gdd-0.3 only): whether one of the city's people without a job builds it. Always false
/// once built (the builder is freed) and under every other ruleset.</summary>
public bool Builder { get; }
public CityProject(int id, string kind, string city, int q, int r, int turnsLeft, DateTime? builtOn = null, double progress = 0, bool builder = false)
{
Id = id; Kind = kind; City = city; Q = q; R = r; TurnsLeft = turnsLeft; BuiltOn = builtOn; Progress = progress; Builder = builder;
}
/// <summary>The same project with or without its builder (R-CITY-011).</summary>
public CityProject WithBuilder(bool builder) => new CityProject(Id, Kind, City, Q, R, TurnsLeft, BuiltOn, Progress, builder);
/// <summary>The same project with <paramref name="turnsLeft"/> turns to go.</summary>
public CityProject WithTurnsLeft(int turnsLeft) => new CityProject(Id, Kind, City, Q, R, turnsLeft, BuiltOn, Progress, Builder);
/// <summary>The same project with <paramref name="turnsLeft"/> turns to go and <paramref name="progress"/>
/// accumulated (R-MIN-002's floor with carry).</summary>
public CityProject WithProgress(int turnsLeft, double progress) => new CityProject(Id, Kind, City, Q, R, turnsLeft, BuiltOn, progress, Builder);
/// <summary>The same project, built on <paramref name="on"/>; its builder, if any, is freed (R-CITY-011).</summary>
public CityProject Built(DateTime on) => new CityProject(Id, Kind, City, Q, R, 0, on);
/// <summary>The record as the state hash writes it: "id:KIND@CITY,Q,R/TURNSLEFT" while building, ",PROGRESS"
/// appended as IEEE bits when it is not 0 (R-MIN-002) and ";builder" when it has one (R-CITY-011, so no record without
/// a builder, and no gdd-0.1 hash, changes), "/done,yyyy-MM-dd" once built.</summary>
public string Canonical() => string.Format(CultureInfo.InvariantCulture, "{0}:{1}@{2},{3},{4}/", Id, Kind, City, Q, R)
+ (BuiltOn is DateTime on ? "done," + on.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)
: TurnsLeft.ToString(CultureInfo.InvariantCulture) + (Progress == 0 ? "" : "," + BitConverter.DoubleToInt64Bits(Progress).ToString("X16", CultureInfo.InvariantCulture))
+ (Builder ? ";builder" : ""));
}
/// <summary>R-ARMS-005, R-ARMS-006 (gdd-0.3 only): the cycle of the built arms workshop that is city project
/// <see cref="Project"/>. <see cref="Staffed"/>: whether one of its city's people without a job works it, priced at
/// <see cref="WorkerTier"/> (always Ordinary on the aggregate cities until the population ledger's tiers are live; #1701
/// OQ6). <see cref="PaidCost"/>: the Money paid for the cycle under way, null when none is; <see cref="Progress"/>: its
/// staffed months so far. Immutable; a change replaces the record.</summary>
public sealed class Workshop
{
public int Project { get; }
public bool Staffed { get; }
public SyntheticWorkshopTier WorkerTier { get; }
public decimal? PaidCost { get; }
public int Progress { get; }
public Workshop(int project, bool staffed, SyntheticWorkshopTier workerTier, decimal? paidCost = null, int progress = 0)
{
Project = project; Staffed = staffed; WorkerTier = workerTier; PaidCost = paidCost; Progress = progress;
}
/// <summary>The same workshop, staffed or not; its cycle is kept (a pause, not a loss: R-ARMS-007).</summary>
public Workshop WithStaffed(bool staffed) => new Workshop(Project, staffed, WorkerTier, PaidCost, Progress);
/// <summary>The same workshop with a cycle paid at <paramref name="cost"/> and <paramref name="progress"/> months done.</summary>
public Workshop WithCycle(decimal? cost, int progress) => new Workshop(Project, Staffed, WorkerTier, cost, progress);
/// <summary>The record as the state hash writes it: "PROJECT:on|off,TIER" and, while a cycle is under way,
/// "/COST,PROGRESS" (the cost as invariant decimal text).</summary>
public string Canonical() => string.Format(CultureInfo.InvariantCulture, "{0}:{1},{2}", Project, Staffed ? "on" : "off", WorkerTier)
+ (PaidCost is decimal cost ? string.Format(CultureInfo.InvariantCulture, "/{0},{1}", cost, Progress) : "");
}
/// <summary>Work on one corridor segment (R-COR-001 to R-COR-003): <see cref="Unit"/> building it with
/// <see cref="Progress"/> turns of work done, or, once <see cref="BuiltOn"/> is set, built on that date. Immutable.</summary>
public sealed class SegmentWork
{
/// <summary>The halutz unit building the segment; null once it is built (unit ids start at 0).</summary>
public int? Unit { get; }
/// <summary>Turns of work done (spec section 2.2: "segment progress").</summary>
public int Progress { get; }
/// <summary>Fractional work carried to the next turn (R-MIN-002), omitted from the hash while zero.</summary>
public double Carry { get; }
/// <summary>The end date of the turn the segment was built on; null while it is being built.</summary>
public DateTime? BuiltOn { get; }
/// <summary>A segment being built (<paramref name="builtOn"/> null) needs its unit; a built segment has none.</summary>
public SegmentWork(int? unit, int progress, DateTime? builtOn, double carry = 0)
{
if ((builtOn == null) != (unit != null)) throw new ArgumentException("a segment being built has a unit, and a built segment has none", nameof(unit));
Unit = unit; Progress = progress; BuiltOn = builtOn; Carry = carry;
}
/// <summary>"b,UNIT,PROGRESS" while building, with nonzero carry appended as IEEE bits; "done,yyyy-MM-dd" once built.</summary>
public string Canonical() => BuiltOn is DateTime on
? "done," + on.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)
: string.Format(CultureInfo.InvariantCulture, "b,{0},{1}", Unit, Progress)
+ (Carry == 0 ? "" : "," + BitConverter.DoubleToInt64Bits(Carry).ToString("X16", CultureInfo.InvariantCulture));
}
/// <summary>A corridor some of whose segments are being built or built (corridors.yaml; R-COR-001 to R-COR-003): one
/// entry per segment of the corridor, in index order, null for a segment not started. A corridor with no started
/// segment has no record. Immutable; a change replaces the record.</summary>
public sealed class CorridorWork
{
public string Id { get; }
public IReadOnlyList<SegmentWork?> Segments { get; }
public CorridorWork(string id, SegmentWork?[] segments) { Id = id; Segments = Array.AsReadOnly((SegmentWork?[])segments.Clone()); }
/// <summary>The same corridor with segment <paramref name="index"/> (0-based) replaced; null when no segment is left
/// with a record.</summary>
public CorridorWork? With(int index, SegmentWork? work)
{
var a = Segments.ToArray();
a[index] = work;
return a.All(x => x == null) ? null : new CorridorWork(Id, a);
}
/// <summary>"id=SEG;SEG;...", each segment "-" when not started.</summary>
public string Canonical() => Id + "=" + string.Join(";", Segments.Select(x => x?.Canonical() ?? "-"));
}
}