A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
sim: coalition changes inside a government's term (fixes #278) (#304)
Guy Shoham5b98f9c27e96
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 047ca8da6..58a86d3f6 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,11 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIB## [Unreleased]### Added+- Coalition changes inside a government's term (`coalition_changes` in governments.yaml, each with a date and sources).+ Seats, majority, the Cabinet's party rule and the causes use the coalition of the day. Adds Agudat Yisrael and Poalei+ Agudat Yisrael leaving the Third Government (18 September 1952, so 60 of 120 seats and no majority until 24 December),+ the General Zionists leaving the Fourth (25 May 1953) and returning (3 June 1953), and the Progressive Party joining+ the Fifth (1 February 1954). The goldens do not move (fixes #278).- A turn now ends on the next era's start date, not only on the last era's end (chapters spec 4.1). A turn that wouldleave under 14 days before the boundary is lengthened to it. Real days scale the turn's rates. With one era, or noboundary in a run, nothing changes; goldens unchanged (refs #59).diff --git a/content/governments.yaml b/content/governments.yamlindex c83dbce1f..acd0d0686 100644--- a/content/governments.yaml+++ b/content/governments.yaml@@ -40,12 +40,26 @@ governments:# Coalition of 65 seats per he.wiki. Democratic List for Israeli Arabs, Progress and Work and Agriculture and# Development were Mapai-affiliated minority lists without ministers. The Progressive Party and the General Zionists# were in opposition. Investiture vote 56-40 (he.wiki citing Haaretz, 9 October 1951). Agudat Yisrael and Poalei- # Agudat Yisrael left the coalition in September 1952 (he.wiki: 18 September in its table, 23 September in its text;- # Levin's term ends on 18 September), leaving 60 seats. A government has one coalition list here, so the change is- # not modelled.+ # Agudat Yisrael left the coalition in September 1952 (#278). The Knesset's position record ends Levin's Welfare term+ # on 18 September 1952, and he.wiki's table gives 18 September for the party's exit; he.wiki's text gives 23+ # September, but its own footnote cites a press report of 19 September 1952, so 18 September is used. The Knesset's+ # session list (KNS_PlenumSession) has no plenum sitting between 28 August and 3 November 1952, so there is no+ # plenum record: the position record and he.wiki are the only sources for the date.+ # DECISION FOR THE MAINTAINER (proposal, change it if you prefer another rule): the coalition then held 60 of 120+ # seats, which is half and not more than half. HasMajority stays "more than half", so a vote that needs a majority+ # (SetPolicy, and AllocateBudget while the legislature approves the budget) fails from 18 September to 24 December+ # 1952. Why: it is the simplest rule, it follows the seat count, and it breaks nothing: the historical AI sends no policy or budget order, so a historical run never+ # votes, and a player can still act in other ways (events, cabinet changes, security orders). Other options: a+ # tie-break in favour of the government, or a confidence rule; both need a new rule in the sim.from: 1951-10-08to: 1952-12-24coalition: [mapai, hapoel_hamizrachi, agudat_yisrael, democratic_list_israeli_arabs, mizrachi, poalei_agudat_yisrael, progress_and_work, agriculture_and_development]+ coalition_changes:+ - date: 1952-09-18+ leaves: [agudat_yisrael, poalei_agudat_yisrael]+ sources:+ - { url: "https://knesset.gov.il/Odata/ParliamentInfo.svc/KNS_PersonToPosition?$filter=GovernmentNum%20eq%203", checked: 2026-10-12 }+ - { url: "https://he.wikipedia.org/wiki/ממשלת_ישראל_השלישית", checked: 2026-10-12 }sources:- { url: "https://en.wikipedia.org/wiki/Third_government_of_Israel", checked: 2026-10-10 }- { url: "https://he.wikipedia.org/wiki/ממשלת_ישראל_השלישית", checked: 2026-10-12 }@@ -59,11 +73,29 @@ governments:# ministers were sworn in on 24 December, when Hapoel HaMizrachi's Shapira and Burg joined (plenum records). The# General Zionists' 20 seats count with the Sephardim (2) and the Yemenite Association (1): those three MKs sat in# the General Zionist faction from 10 September 1951 (Knesset faction records; he.wiki gives the faction 23 seats).- # That makes 87 of 120, as in he.wiki. The General Zionists left on 26 May 1953 and returned on 2 June 1953- # (he.wiki); not modelled.+ # That makes 87 of 120, as in he.wiki. The General Zionists left on 25 May 1953 and returned on 3 June 1953 (#278),+ # per the plenum records: on 25 May 1953 (evening sitting, 2_ptm_250584.pdf) the Prime Minister told the Knesset,+ # under section 11 of the Transition Law, that the four General Zionist ministers (Rokach, Bernstein, Sapir, Serlin)+ # had resigned; on 3 June 1953 (2_ptm_250727.pdf) the Knesset approved the government's decision to add the four+ # again, and they made their declarations of allegiance. he.wiki gives 26 May and 2 June (citing Davar of 26 May).+ # Without the General Zionists and the three MKs who sat in their faction, the coalition held 64 seats (he.wiki).+ # The position records (KNS_PersonToPosition) show the four ministers' terms without a break, so their terms in+ # people.yaml are unchanged.from: 1952-12-24to: 1954-01-26coalition: [mapai, general_zionists, sephardim, yemenite_association, hapoel_hamizrachi, mizrachi, progressive, democratic_list_israeli_arabs, progress_and_work, agriculture_and_development]+ coalition_changes:+ - date: 1953-05-25+ leaves: [general_zionists, sephardim, yemenite_association]+ sources:+ - { url: "https://fs.knesset.gov.il/2/Plenum/2_ptm_250584.pdf", checked: 2026-10-12 }+ - { url: "https://fs.knesset.gov.il/2/Plenum/2_ptm_250724.pdf", checked: 2026-10-12 }+ - { url: "https://he.wikipedia.org/wiki/ממשלת_ישראל_הרביעית", checked: 2026-10-12 }+ - date: 1953-06-03+ joins: [general_zionists, sephardim, yemenite_association]+ sources:+ - { url: "https://fs.knesset.gov.il/2/Plenum/2_ptm_250727.pdf", checked: 2026-10-12 }+ - { url: "https://he.wikipedia.org/wiki/ממשלת_ישראל_הרביעית", checked: 2026-10-12 }sources:- { url: "https://knesset.gov.il/Odata/ParliamentInfo.svc/KNS_PersonToPosition?$filter=GovernmentNum%20eq%204", checked: 2026-10-12 }- { url: "https://he.wikipedia.org/wiki/ממשלת_ישראל_הרביעית", checked: 2026-10-12 }@@ -76,11 +108,21 @@ governments:- id: fifth_governmentname: Fifth Government# Sharett's first government, approved 75 to 23 on 26 January 1954, with the same parties as the Fourth. The- # Progressives had not decided on 25 January 1954 and joined on 1 February (plenum record of 25 January; he.wiki);- # one coalition list per government, so they count from the start.+ # Progressives had not decided on 25 January 1954 and joined on 1 February (#278). On 1 February the Prime Minister+ # told the Knesset that the Progressive Party had decided to return to the coalition and that the government had+ # decided to add Pinchas Rosen as Minister of Justice; the Knesset's position record starts Rosen's Justice term+ # that day.from: 1954-01-26to: 1955-06-29- coalition: [mapai, general_zionists, sephardim, yemenite_association, hapoel_hamizrachi, mizrachi, progressive, democratic_list_israeli_arabs, progress_and_work, agriculture_and_development]+ coalition: [mapai, general_zionists, sephardim, yemenite_association, hapoel_hamizrachi, mizrachi, democratic_list_israeli_arabs, progress_and_work, agriculture_and_development]+ coalition_changes:+ - date: 1954-02-01+ joins: [progressive]+ sources:+ - { url: "https://fs.knesset.gov.il/2/Plenum/2_ptm_250778.pdf", checked: 2026-10-12 }+ - { url: "https://fs.knesset.gov.il/2/Plenum/2_ptm_250782.pdf", checked: 2026-10-12 }+ - { url: "https://knesset.gov.il/Odata/ParliamentInfo.svc/KNS_PersonToPosition?$filter=GovernmentNum%20eq%205", checked: 2026-10-12 }+ - { url: "https://he.wikipedia.org/wiki/ממשלת_ישראל_החמישית", checked: 2026-10-12 }sources:- { url: "https://knesset.gov.il/Odata/ParliamentInfo.svc/KNS_PersonToPosition?$filter=GovernmentNum%20eq%205", checked: 2026-10-12 }- { url: "https://he.wikipedia.org/wiki/ממשלת_ישראל_החמישית", checked: 2026-10-12 }diff --git a/sim/Statecraft.Sim.Tests/CoalitionChangesTests.cs b/sim/Statecraft.Sim.Tests/CoalitionChangesTests.csnew file mode 100644index 000000000..a8a5ff971--- /dev/null+++ b/sim/Statecraft.Sim.Tests/CoalitionChangesTests.cs@@ -0,0 +1,260 @@+using System;+using System.Collections.Generic;+using System.Linq;+using Statecraft.Sim;++/// <summary>Parties joining or leaving a coalition inside a government's term (#278): the loader checks, and every+/// reader of the coalition (seats, majority, the cabinet's party rule, the causes) uses the coalition of the day.</summary>+public class CoalitionChangesTests+{+ const string Src = "sources: [{ url: \"https://example.org/x\", checked: 2026-10-12 }]";+ const string Open = " coalition: [alpha, beta]\n";++ static string Changes(params string[] items) => Open + " coalition_changes:\n" + string.Concat(items.Select(i => " - " + i + "\n"));+ static string Leave(string date, string party = "beta") => $"{{ date: {date}, leaves: [{party}], {Src} }}";+ static string Join(string date, string party = "beta") => $"{{ date: {date}, joins: [{party}], {Src} }}";++ static Content Load(params string[] items) => ContentLoader.Load(Fixture.Write(new Dictionary<string, string>+ {+ ["governments.yaml"] = Fixture.Files["governments.yaml"].Replace(Open, Changes(items)),+ ["people.yaml"] = Fixture.Files["people.yaml"] + Outsider,+ }));++ const string Outsider = @"+ - id: outsider+ name: Out Sider+ living: false+ party: beta+ competence: 9+ loyalty: 3+ offices: []+";++ // Government Two: alpha and beta from 10 March 1949, in a House of 10 where alpha holds 4 and beta 6.+ static readonly Content C = Load(Leave("1949-06-01"), Join("1949-09-01"));++ [Fact]+ public void TheCoalitionOfTheDayFollowsTheChanges()+ {+ var gov = Offices.Government(C, new DateTime(1949, 4, 1))!;+ Assert.Equal(new[] { "alpha", "beta" }, gov.CoalitionOn(new DateTime(1949, 3, 10)));+ Assert.Equal(new[] { "alpha", "beta" }, gov.CoalitionOn(new DateTime(1949, 5, 31)));+ Assert.Equal(new[] { "alpha" }, gov.CoalitionOn(new DateTime(1949, 6, 1))); // a change applies from its own date+ Assert.Equal(new[] { "alpha" }, gov.CoalitionOn(new DateTime(1949, 8, 31)));+ Assert.Equal(new[] { "alpha", "beta" }, gov.CoalitionOn(new DateTime(1949, 9, 1)));+ Assert.Equal(new[] { "alpha", "beta" }, gov.Coalition); // the opening list is untouched+ }++ [Fact]+ public void SeatsAndMajorityUseTheCoalitionOfTheDay()+ {+ Assert.Equal((10, 10), PoliticsSystem.CoalitionSeats(C, new DateTime(1949, 5, 31)));+ Assert.True(PoliticsSystem.HasMajority(C, new DateTime(1949, 5, 31)));+ Assert.Equal((4, 10), PoliticsSystem.CoalitionSeats(C, new DateTime(1949, 6, 1)));+ Assert.False(PoliticsSystem.HasMajority(C, new DateTime(1949, 6, 1)));+ Assert.Equal((10, 10), PoliticsSystem.CoalitionSeats(C, new DateTime(1949, 9, 1)));+ Assert.True(PoliticsSystem.HasMajority(C, new DateTime(1949, 9, 1)));+ }++ [Fact]+ public void AVoteFailsWhileTheCoalitionHasNoMajority()+ {+ var rng = new Rng(1);+ var s = new GameState { Date = new DateTime(1949, 7, 1), CoalitionStability = 1.0 };+ Assert.False(PoliticsSystem.Vote(C, s, ref rng));+ s.Date = new DateTime(1949, 9, 1);+ Assert.True(PoliticsSystem.Vote(C, s, ref rng));+ }++ [Fact]+ public void ExactlyHalfOfTheSeatsIsNotAMajority()+ {+ // The Third Government's 60 of 120 seats (September to December 1952) rest on this rule.+ var half = ContentLoader.Load(Fixture.Write(new Dictionary<string, string>+ {+ ["legislatures.yaml"] = Fixture.Files["legislatures.yaml"].Replace("seats: { alpha: 4, beta: 6 }", "seats: { alpha: 5, beta: 5 }"),+ ["governments.yaml"] = Fixture.Files["governments.yaml"].Replace(" coalition: [alpha, beta]\n", " coalition: [alpha]\n"),+ }));+ Assert.Equal((5, 10), PoliticsSystem.CoalitionSeats(half, new DateTime(1949, 6, 1)));+ Assert.False(PoliticsSystem.HasMajority(half, new DateTime(1949, 6, 1)));+ }++ [Fact]+ public void TheCabinetsPartyRuleUsesTheCoalitionOfTheDay()+ {+ var s = Simulation.NewGame(C, 42, Mode.Historical);+ s.Date = new DateTime(1949, 5, 31);+ Assert.Null(Cabinet.Check(C, s, "finance_minister", "outsider")); // Beta is in+ s.Date = new DateTime(1949, 6, 1);+ Assert.Equal("Out Sider's party (Beta) is not in the coalition", Cabinet.Check(C, s, "finance_minister", "outsider"));+ s.Date = new DateTime(1949, 9, 1);+ Assert.Null(Cabinet.Check(C, s, "finance_minister", "outsider")); // and back in+ }++ [Fact]+ public void TheCausesReportEachChangeAndTheOpeningCoalition()+ {+ var why = new List<Cause>();+ PoliticsSystem.ReportChanges(C, new DateTime(1949, 3, 1), new DateTime(1949, 3, 15), why);+ Assert.Equal("Government Two took office (Alpha, Beta)", why.Single(w => w.Target == "gov2").Message);++ why.Clear();+ PoliticsSystem.ReportChanges(C, new DateTime(1949, 5, 28), new DateTime(1949, 6, 4), why);+ Assert.Equal("Beta left the coalition (coalition holds 4/10)", why.Single().Message);++ why.Clear();+ PoliticsSystem.ReportChanges(C, new DateTime(1949, 8, 28), new DateTime(1949, 9, 4), why);+ Assert.Equal("Beta joined the coalition (coalition holds 10/10)", why.Single().Message);++ why.Clear();+ PoliticsSystem.ReportChanges(C, new DateTime(1949, 6, 4), new DateTime(1949, 6, 11), why);+ Assert.Empty(why);+ }++ [Fact]+ public void ATookOfficeCauseNamesTheCoalitionOnTheStartDate()+ {+ var changed = Load(Leave("1949-03-12")); // two days after Government Two starts+ var why = new List<Cause>();+ PoliticsSystem.ReportChanges(changed, new DateTime(1949, 3, 1), new DateTime(1949, 3, 15), why);+ Assert.Equal(new[] { "Government Two took office (Alpha, Beta)", "Beta left the coalition (coalition holds 4/10)" }, why.Select(w => w.Message).ToArray());+ }++ [Fact]+ public void AGovernmentWithoutChangesIsAsBefore()+ {+ var plain = ContentLoader.Load(Fixture.Write());+ var gov = Offices.Government(plain, new DateTime(1949, 4, 1))!;+ Assert.Empty(gov.CoalitionChanges);+ Assert.Equal(gov.Coalition, gov.CoalitionOn(new DateTime(1970, 1, 1)));+ }++ // Loader checks.++ [Fact]+ public void AChangeOutsideTheTermIsALoadError()+ {+ foreach (var date in new[] { "1949-03-10", "1949-03-09" }) // the start day itself, and before it+ {+ var ex = ValidationTests.Bad("governments.yaml", Open, Changes(Leave(date)));+ Assert.Equal("governments.yaml", ex.File);+ Assert.Equal("governments[gov2].coalition_changes[0].date", ex.Field);+ Assert.Contains("must be inside the term, after 1949-03-10", ex.Message);+ }+ var late = ValidationTests.Bad("governments.yaml", " to: 1949-03-10\n coalition: [alpha]\n",+ " to: 1949-03-10\n coalition: [alpha]\n coalition_changes:\n - " + Join("1949-03-10") + "\n"); // the end day is Government Two's+ Assert.Equal("governments[gov1].coalition_changes[0].date", late.Field);+ Assert.Contains("and before 1949-03-10", late.Message);+ }++ [Fact]+ public void AChangeNeedsADateASourceAndAPartyList()+ {+ var noDate = ValidationTests.Bad("governments.yaml", Open, Changes($"{{ leaves: [beta], {Src} }}"));+ Assert.Equal("governments[gov2].coalition_changes[0].date", noDate.Field);+ var noSource = ValidationTests.Bad("governments.yaml", Open, Changes("{ date: 1949-06-01, leaves: [beta] }"));+ Assert.Equal("governments[gov2].coalition_changes[0].sources", noSource.Field);+ var empty = ValidationTests.Bad("governments.yaml", Open, Changes($"{{ date: 1949-06-01, {Src} }}"));+ Assert.Equal("governments[gov2].coalition_changes[0]", empty.Field);+ }++ [Fact]+ public void AChangeNamesKnownPartiesThatAreInOrOutOfTheCoalition()+ {+ var unknownLeaver = ValidationTests.Bad("governments.yaml", Open, Changes(Leave("1949-06-01", "nobody")));+ Assert.Equal("governments[gov2].coalition_changes[0].leaves.nobody", unknownLeaver.Field);+ Assert.Contains("unknown party", unknownLeaver.Message); // not "not in the coalition"+ var unknownJoiner = ValidationTests.Bad("governments.yaml", Open, Changes(Join("1949-06-01", "nobody")));+ Assert.Equal("governments[gov2].coalition_changes[0].joins.nobody", unknownJoiner.Field);+ Assert.Contains("unknown party", unknownJoiner.Message);+ Assert.Equal("governments[gov2].coalition_changes[0].joins.beta", ValidationTests.Bad("governments.yaml", Open, Changes(Join("1949-06-01"))).Field); // already in+ Assert.Equal("governments[gov2].coalition_changes[1].leaves.beta", ValidationTests.Bad("governments.yaml", Open, Changes(Leave("1949-06-01"), Leave("1949-07-01"))).Field); // already out+ var both = ValidationTests.Bad("governments.yaml", Open, Changes($"{{ date: 1949-06-01, leaves: [beta], joins: [beta], {Src} }}"));+ Assert.Equal("governments[gov2].coalition_changes[0].joins.beta", both.Field);+ }++ [Fact]+ public void ChangesMustComeInDateOrder()+ {+ var ex = ValidationTests.Bad("governments.yaml", Open, Changes(Leave("1949-09-01"), Join("1949-06-01")));+ Assert.Equal("governments[gov2].coalition_changes[1].date", ex.Field);+ Assert.Contains("must be after the previous change (1949-09-01)", ex.Message);+ var same = ValidationTests.Bad("governments.yaml", Open, Changes(Leave("1949-06-01"), Join("1949-06-01")));+ Assert.Equal("governments[gov2].coalition_changes[1].date", same.Field);+ }++ // The real content.++ static readonly Content Real = ContentLoader.Load(RealContentTests.ContentRoot());++ [Fact]+ public void TheThirdGovernmentLosesTheAgudotIn1952AndHasNoMajorityUntilTheFourth()+ {+ Assert.Equal((65, 120), PoliticsSystem.CoalitionSeats(Real, new DateTime(1952, 9, 17)));+ Assert.Equal((60, 120), PoliticsSystem.CoalitionSeats(Real, new DateTime(1952, 9, 18))); // Levin's Welfare term ends that day+ Assert.Equal((60, 120), PoliticsSystem.CoalitionSeats(Real, new DateTime(1952, 12, 23)));+ Assert.True(PoliticsSystem.HasMajority(Real, new DateTime(1952, 9, 17)));+ Assert.False(PoliticsSystem.HasMajority(Real, new DateTime(1952, 9, 18)));+ Assert.False(PoliticsSystem.HasMajority(Real, new DateTime(1952, 12, 23)));+ Assert.True(PoliticsSystem.HasMajority(Real, new DateTime(1952, 12, 24))); // the Fourth Government+ Assert.Equal(new DateTime(1952, 9, 18), Real.Governments.Single(g => g.Id == "third_government").CoalitionChanges.Single().Date);+ Assert.NotNull(Offices.Holder(Real, "welfare_minister", new DateTime(1952, 9, 17)));+ Assert.Null(Offices.Holder(Real, "welfare_minister", new DateTime(1952, 9, 18))); // no Agudat Yisrael minister left+ }++ [Fact]+ public void TheFourthGovernmentLosesTheGeneralZionistsForAWeekIn1953()+ {+ Assert.Equal((87, 120), PoliticsSystem.CoalitionSeats(Real, new DateTime(1953, 5, 24)));+ Assert.Equal((64, 120), PoliticsSystem.CoalitionSeats(Real, new DateTime(1953, 5, 25))); // plenum of 25 May: the four ministers resigned; he.wiki: 64+ Assert.Equal((64, 120), PoliticsSystem.CoalitionSeats(Real, new DateTime(1953, 6, 2)));+ Assert.Equal((87, 120), PoliticsSystem.CoalitionSeats(Real, new DateTime(1953, 6, 3))); // plenum of 3 June: the Knesset approves their return+ Assert.True(PoliticsSystem.HasMajority(Real, new DateTime(1953, 5, 25))); // 64 of 120 is still a majority+ }++ [Fact]+ public void TheFifthGovernmentGainsTheProgressivesOnTheDayRosenBecomesMinisterOfJustice()+ {+ var before = PoliticsSystem.CoalitionSeats(Real, new DateTime(1954, 1, 31)).seats;+ var after = PoliticsSystem.CoalitionSeats(Real, new DateTime(1954, 2, 1)).seats;+ Assert.Equal(PoliticsSystem.CoalitionSeats(Real, new DateTime(1954, 1, 26)).seats, before);+ Assert.Equal(87, after); // the Fourth Government's 87 with the Progressives+ Assert.Equal(Real.Legislatures.Single(l => l.Id == "second_knesset").Seats["progressive"], after - before);+ Assert.Equal("rosen", Offices.Holder(Real, "justice_minister", new DateTime(1954, 2, 1))?.Id);+ Assert.NotEqual("rosen", Offices.Holder(Real, "justice_minister", new DateTime(1954, 1, 31))?.Id);+ var fifth = Real.Governments.Single(g => g.Id == "fifth_government");+ Assert.DoesNotContain("progressive", fifth.CoalitionOn(new DateTime(1954, 1, 31)));+ Assert.Contains("progressive", fifth.CoalitionOn(new DateTime(1954, 2, 1)));+ Assert.Equal(new DateTime(1954, 2, 1), fifth.CoalitionChanges.Single().Date);+ }++ [Fact]+ public void EveryRealChangeHasASourceAndTheRealCoalitionsEndAsTheyBegan()+ {+ foreach (var g in Real.Governments)+ foreach (var ch in g.CoalitionChanges)+ {+ Assert.NotEmpty(ch.Sources);+ Assert.All(ch.Sources, s => Assert.StartsWith("https://", s.Url));+ }+ // The Fourth Government's coalition is whole again when it ends, the Third Government's is not.+ var fourth = Real.Governments.Single(g => g.Id == "fourth_government");+ Assert.Equal(fourth.Coalition.OrderBy(p => p), fourth.CoalitionOn(fourth.To!.Value.AddDays(-1)).OrderBy(p => p));+ }++ [Fact]+ public void ARealRunReportsTheThreeChangesOnTheirDays()+ {+ foreach (var (from, to, text) in new[]+ {+ (new DateTime(1952, 9, 15), new DateTime(1952, 9, 22), "Agudat Yisrael, Poalei Agudat Yisrael left the coalition (coalition holds 60/120)"),+ (new DateTime(1953, 5, 23), new DateTime(1953, 5, 27), "General Zionists, Sephardim and Oriental Communities, Yemenite Association left the coalition (coalition holds 64/120)"),+ (new DateTime(1954, 1, 30), new DateTime(1954, 2, 3), "Progressive Party joined the coalition (coalition holds 87/120)"),+ })+ {+ var why = new List<Cause>();+ PoliticsSystem.ReportChanges(Real, from, to, why);+ Assert.Equal(text, why.Single(w => w.Message.Contains("coalition holds")).Message);+ }+ }+}diff --git a/sim/Statecraft.Sim.Tests/Governments1952To1956Tests.cs b/sim/Statecraft.Sim.Tests/Governments1952To1956Tests.csindex aade7fef7..ac47f1641 100644--- a/sim/Statecraft.Sim.Tests/Governments1952To1956Tests.cs+++ b/sim/Statecraft.Sim.Tests/Governments1952To1956Tests.cs@@ -69,7 +69,8 @@ public class Governments1952To1956Testspublic void CoalitionSeatsMatchTheSources(){Assert.Equal((87, 120), PoliticsSystem.CoalitionSeats(C, Fourth)); // he.wiki: 87- Assert.Equal((87, 120), PoliticsSystem.CoalitionSeats(C, new DateTime(1954, 1, 26)));+ Assert.Equal((83, 120), PoliticsSystem.CoalitionSeats(C, new DateTime(1954, 1, 26))); // without the Progressives' 4 seats until 1 February (#278)+ Assert.Equal((87, 120), PoliticsSystem.CoalitionSeats(C, new DateTime(1954, 2, 1)));Assert.Equal((64, 120), PoliticsSystem.CoalitionSeats(C, new DateTime(1955, 6, 29))); // election lists; he.wiki's 66 counts later factions// The Third Knesset convenes on 15 August 1955 once #221's results are in content/; until then the Second runs on.var third = C.Legislatures.FirstOrDefault(l => l.Id == "third_knesset");diff --git a/sim/Statecraft.Sim/Cabinet.cs b/sim/Statecraft.Sim/Cabinet.csindex ada278954..9ec731d68 100644--- a/sim/Statecraft.Sim/Cabinet.cs+++ b/sim/Statecraft.Sim/Cabinet.cs@@ -103,7 +103,7 @@ namespace Statecraft.Simif (officeId == "chief_of_staff")return person.Offices.Any(t => t.Office == "chief_of_staff") ? null : $"{person.Name} is not an IDF career officer";var party = Offices.PartyOf(c, person, s.Date);- if (party != "" && !gov.Coalition.Contains(party))+ if (party != "" && !gov.CoalitionOn(s.Date).Contains(party))return $"{person.Name}'s party ({c.Parties.First(p => p.Id == party).Name}) is not in the coalition";return null;}diff --git a/sim/Statecraft.Sim/Content.cs b/sim/Statecraft.Sim/Content.csindex d52f3d8c0..3e70f7333 100644--- a/sim/Statecraft.Sim/Content.cs+++ b/sim/Statecraft.Sim/Content.cs@@ -113,9 +113,36 @@ namespace Statecraft.Simpublic string Name { get; set; } = "";public DateTime From { get; set; }public DateTime? To { get; set; }+ /// <summary>The parties in the coalition when the government took office. Parties that join or leave later are+ /// in <see cref="CoalitionChanges"/>; read the coalition of a given day with <see cref="CoalitionOn"/>.</summary>public List<string> Coalition { get; set; } = new List<string>();+ /// <summary>Parties joining or leaving the coalition inside the term, in date order (the loader checks it).</summary>+ public List<CoalitionChange> CoalitionChanges { get; set; } = new List<CoalitionChange>();public List<Source> Sources { get; set; } = new List<Source>();public string Status { get; set; } = "draft";++ /// <summary>The coalition on <paramref name="date"/>: the opening list with every change dated on or before that+ /// day applied. The result keeps the opening order, with parties that joined added at the end.</summary>+ public List<string> CoalitionOn(DateTime date)+ {+ var result = new List<string>(Coalition);+ foreach (var change in CoalitionChanges)+ {+ if (change.Date > date) break;+ result.RemoveAll(p => change.Leaves.Contains(p));+ foreach (var p in change.Joins) if (!result.Contains(p)) result.Add(p);+ }+ return result;+ }+ }++ /// <summary>Parties joining or leaving a government's coalition on one day, inside its term. Applies from that day.</summary>+ public sealed class CoalitionChange+ {+ public DateTime Date { get; set; }+ public List<string> Joins { get; set; } = new List<string>();+ public List<string> Leaves { get; set; } = new List<string>();+ public List<Source> Sources { get; set; } = new List<Source>();}public sealed class Indicatordiff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.csindex 759e1bbc4..96e23c797 100644--- a/sim/Statecraft.Sim/ContentLoader.cs+++ b/sim/Statecraft.Sim/ContentLoader.cs@@ -553,6 +553,32 @@ namespace Statecraft.SimPeriod("governments.yaml", $"governments[{g.Id}]", g.From, g.To);foreach (var p in g.Coalition)if (!parties.Contains(p)) throw new ContentException("governments.yaml", $"governments[{g.Id}].coalition.{p}", "unknown party");+ var inOffice = new List<string>(g.Coalition);+ var previous = g.From;+ for (int i = 0; i < g.CoalitionChanges.Count; i++)+ {+ var ch = g.CoalitionChanges[i];+ var at = $"governments[{g.Id}].coalition_changes[{i}]";+ if (ch.Date == default) throw new ContentException("governments.yaml", at + ".date", "required");+ if (ch.Date <= g.From || (g.To.HasValue && ch.Date >= g.To.Value))+ throw new ContentException("governments.yaml", at + ".date", FormattableString.Invariant($"must be inside the term, after {g.From:yyyy-MM-dd}") + (g.To.HasValue ? FormattableString.Invariant($" and before {g.To.Value:yyyy-MM-dd}") : ""));+ if (i > 0 && ch.Date <= previous) throw new ContentException("governments.yaml", at + ".date", FormattableString.Invariant($"must be after the previous change ({previous:yyyy-MM-dd})"));+ previous = ch.Date;+ if (ch.Joins.Count == 0 && ch.Leaves.Count == 0) throw new ContentException("governments.yaml", at, "needs joins or leaves");+ if (ch.Sources.Count == 0) throw new ContentException("governments.yaml", at + ".sources", "required: a coalition change needs a source");+ foreach (var p in ch.Leaves)+ {+ if (!parties.Contains(p)) throw new ContentException("governments.yaml", $"{at}.leaves.{p}", "unknown party");+ if (!inOffice.Remove(p)) throw new ContentException("governments.yaml", $"{at}.leaves.{p}", "not in the coalition on that day");+ }+ foreach (var p in ch.Joins)+ {+ if (!parties.Contains(p)) throw new ContentException("governments.yaml", $"{at}.joins.{p}", "unknown party");+ if (ch.Leaves.Contains(p)) throw new ContentException("governments.yaml", $"{at}.joins.{p}", "cannot join and leave on the same day");+ if (inOffice.Contains(p)) throw new ContentException("governments.yaml", $"{at}.joins.{p}", "already in the coalition on that day");+ inOffice.Add(p);+ }+ }}NoOverlap("legislatures.yaml", "legislatures", c.Legislatures.Select(l => (l.Id, l.From, l.To)));NoOverlap("governments.yaml", "governments", c.Governments.Select(g => (g.Id, g.From, g.To)));diff --git a/sim/Statecraft.Sim/PoliticsSystem.cs b/sim/Statecraft.Sim/PoliticsSystem.csindex e90d826c5..23fd300cf 100644--- a/sim/Statecraft.Sim/PoliticsSystem.cs+++ b/sim/Statecraft.Sim/PoliticsSystem.cs@@ -13,7 +13,7 @@ namespace Statecraft.Simvar leg = Offices.Legislature(c, date);var gov = Offices.Government(c, date);if (leg == null || gov == null) return (0, 0);- return (gov.Coalition.Sum(p => leg.Seats.TryGetValue(p, out var n) ? n : 0), leg.Size);+ return (gov.CoalitionOn(date).Sum(p => leg.Seats.TryGetValue(p, out var n) ? n : 0), leg.Size);}public static bool HasMajority(Content c, DateTime date)@@ -54,8 +54,24 @@ namespace Statecraft.Simwhy.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)- why.Add(new Cause("politics", g1.Id, 0, $"{g1.Name} took office ({string.Join(", ", g1.Coalition.Select(id => c.Parties.First(p => p.Id == id).Name))})"));+ var tookOffice = g1 != null && g1 != g0;+ var reported = false;+ void TookOffice() { reported = true; why.Add(new Cause("politics", g1!.Id, 0, $"{g1.Name} took office ({string.Join(", ", PartyNames(c, g1.CoalitionOn(g1.From)))})")); }+ // Changes inside a term, in date order; a government taking office comes before the changes dated on or after its start.+ var changes = c.Governments.SelectMany(g => g.CoalitionChanges.Select(ch => (g, ch)))+ .Where(x => x.ch.Date > from && x.ch.Date <= to).OrderBy(x => x.ch.Date);+ foreach (var (g, ch) in changes)+ {+ if (tookOffice && !reported && ch.Date >= g1!.From) TookOffice();+ var (seats, size) = CoalitionSeats(c, ch.Date);+ var what = new List<string>();+ if (ch.Leaves.Count > 0) what.Add($"{string.Join(", ", PartyNames(c, ch.Leaves))} left the coalition");+ if (ch.Joins.Count > 0) what.Add($"{string.Join(", ", PartyNames(c, ch.Joins))} joined the coalition");+ why.Add(new Cause("politics", g.Id, 0, FormattableString.Invariant($"{string.Join("; ", what)} (coalition holds {seats}/{size})")));+ }+ if (tookOffice && !reported) TookOffice();}++ static IEnumerable<string> PartyNames(Content c, IEnumerable<string> ids) => ids.Select(id => c.Parties.First(p => p.Id == id).Name);}}diff --git a/unity/Assets/Scripts/UI/CabinetPresenter.cs b/unity/Assets/Scripts/UI/CabinetPresenter.csindex 8924c60db..2590e4513 100644--- a/unity/Assets/Scripts/UI/CabinetPresenter.cs+++ b/unity/Assets/Scripts/UI/CabinetPresenter.cs@@ -81,11 +81,11 @@ namespace Statecraft.Game.UI/// <summary>The legislature's party blocks in drawing order: the coalition's parties, then the opposition's, each by/// seats (most first, then content order), in the coalition or opposition colour. Parties without a seat are left out.</summary>- public static IReadOnlyList<Hemicycle.Group> Blocks(Content c, Legislature leg, Government? gov)+ public static IReadOnlyList<Hemicycle.Group> Blocks(Content c, Legislature leg, Government? gov, System.DateTime date){var order = c.Parties.Select((p, i) => (p.Id, i)).ToDictionary(x => x.Id, x => x.i);int Order(string id) => order.TryGetValue(id, out var i) ? i : int.MaxValue;- var inCoalition = new HashSet<string>(gov?.Coalition ?? new List<string>());+ var inCoalition = new HashSet<string>(gov?.CoalitionOn(date) ?? new List<string>());var blocks = new List<Hemicycle.Group>();foreach (var coalition in new[] { true, false })foreach (var kv in leg.Seats.Where(kv => kv.Value > 0 && inCoalition.Contains(kv.Key) == coalition)@@ -223,7 +223,7 @@ namespace Statecraft.Game.UIscreen.Q<Label>("cabinet-coalition").text = text.Format("cabinet.coalition", seats, size, PlayerText.Percent(s.CoalitionStability));screen.Q<UiBar>("cabinet-stability").Value = (float)s.CoalitionStability;if (leg == null) { hemicycle.Set(new Hemicycle.Group[0], 0); chips.Clear(); chipsFor = ""; return; }- var blocks = Blocks(c, leg, gov);+ var blocks = Blocks(c, leg, gov, s.Date);hemicycle.Set(blocks, leg.Size);screen.Q<Label>("cabinet-house-size").text = leg.Size.ToString(System.Globalization.CultureInfo.InvariantCulture);screen.Q<Label>("cabinet-opposition").text = text.Format("cabinet.opposition", blocks.Where(b => !b.Coalition).Sum(b => b.Seats));diff --git a/unity/Assets/Tests/EditMode/CabinetScreenTests.cs b/unity/Assets/Tests/EditMode/CabinetScreenTests.csindex 71d11c034..2f1b94c4f 100644--- a/unity/Assets/Tests/EditMode/CabinetScreenTests.cs+++ b/unity/Assets/Tests/EditMode/CabinetScreenTests.cs@@ -279,4 +279,17 @@ public class CabinetScreenTestsAssert.AreEqual($"Consults the {leg.Body} · {leg.Size} members · its votes do not bind", ui.Q<Label>("cabinet-legislature").text);cabinet.Dispose();}++ [Test]+ public void TheHouseColoursTheCoalitionOfTheDay()+ {+ // Agudat Yisrael and Poalei Agudat Yisrael leave the Third Government on 18 September 1952 (#278).+ var leg = Offices.Legislature(C, new DateTime(1952, 9, 18))!;+ var gov = Offices.Government(C, new DateTime(1952, 9, 18))!;+ bool InCoalition(DateTime d, string party) => CabinetPresenter.Blocks(C, leg, gov, d).Single(b => b.Party == party).Coalition;+ Assert.IsTrue(InCoalition(new DateTime(1952, 9, 17), "agudat_yisrael"));+ Assert.IsFalse(InCoalition(new DateTime(1952, 9, 18), "agudat_yisrael"));+ Assert.IsFalse(InCoalition(new DateTime(1952, 9, 18), "poalei_agudat_yisrael"));+ Assert.AreEqual(60, CabinetPresenter.Blocks(C, leg, gov, new DateTime(1952, 9, 18)).Where(b => b.Coalition).Sum(b => b.Seats));+ }}diff --git a/unity/Assets/Tests/EditMode/PromoScreensTests.cs b/unity/Assets/Tests/EditMode/PromoScreensTests.csindex 1d400adf0..b173b3381 100644--- a/unity/Assets/Tests/EditMode/PromoScreensTests.cs+++ b/unity/Assets/Tests/EditMode/PromoScreensTests.cs@@ -57,7 +57,7 @@ public class PromoScreensTests{var block = house.Groups.Single(b => b.Party == kv.Key);Assert.AreEqual(kv.Value, block.Seats, where + " " + kv.Key);- Assert.AreEqual(gov.Coalition.Contains(kv.Key), block.Coalition, where + " " + kv.Key);+ Assert.AreEqual(gov.CoalitionOn(g.State.Date).Contains(kv.Key), block.Coalition, where + " " + kv.Key);}Assert.AreEqual(leg.Seats.Count(kv => kv.Value > 0), house.Groups.Count, where + ": only parties with seats");var (seats, size) = PoliticsSystem.CoalitionSeats(C, g.State.Date);@@ -70,7 +70,7 @@ public class PromoScreensTestsAssert.AreEqual(size, dots.Count);Assert.AreEqual(leg.Size.ToString(), ui.Q<Label>("cabinet-house-size").text, where);Assert.AreEqual($"Coalition: {seats} of {size} seats · stability {PlayerText.Percent(g.State.CoalitionStability)}", ui.Q<Label>("cabinet-coalition").text, where);- Assert.AreEqual($"Opposition: {leg.Seats.Where(kv => !gov.Coalition.Contains(kv.Key)).Sum(kv => kv.Value)} seats", ui.Q<Label>("cabinet-opposition").text, where);+ Assert.AreEqual($"Opposition: {leg.Seats.Where(kv => !gov.CoalitionOn(g.State.Date).Contains(kv.Key)).Sum(kv => kv.Value)} seats", ui.Q<Label>("cabinet-opposition").text, where);Assert.AreEqual($"Majority: {size / 2 + 1} seats", ui.Q<Label>("cabinet-majority").text, where);var top = leg.Seats.OrderByDescending(kv => kv.Value).First();Assert.AreEqual($"Largest party: {C.Parties.First(p => p.Id == top.Key).Name}, {top.Value} seats", ui.Q<Label>("cabinet-largest").text, where);@@ -302,9 +302,9 @@ public class PromoScreensTestsvar leg = Offices.Legislature(C, s.Date)!;var gov = Offices.Government(C, s.Date);var (seats, size) = PoliticsSystem.CoalitionSeats(C, s.Date);- var blocks = CabinetPresenter.Blocks(C, leg, gov);+ var blocks = CabinetPresenter.Blocks(C, leg, gov, s.Date);texts["cabinet-coalition"] = $"Coalition: {seats} of {size} seats · stability {PlayerText.Percent(s.CoalitionStability)}";- texts["cabinet-opposition"] = $"Opposition: {leg.Seats.Where(kv => gov == null || !gov.Coalition.Contains(kv.Key)).Sum(kv => kv.Value)} seats";+ texts["cabinet-opposition"] = $"Opposition: {leg.Seats.Where(kv => gov == null || !gov.CoalitionOn(s.Date).Contains(kv.Key)).Sum(kv => kv.Value)} seats";texts["cabinet-majority"] = $"Majority: {leg.Size / 2 + 1} seats";var top = leg.Seats.OrderByDescending(kv => kv.Value).First();texts["cabinet-largest"] = $"Largest party: {C.Parties.First(p => p.Id == top.Key).Name}, {top.Value} seats";