From c6f10868aebb64bf09735c19b74d4b21abd76229 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 5 Sep 2024 23:11:29 +0000 Subject: [PATCH] Implement support assertions --- .../Script/AdjudicationQueryScriptHandler.cs | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs b/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs index 48cb34e..8022d55 100644 --- a/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs +++ b/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs @@ -163,7 +163,41 @@ public class AdjudicationQueryScriptHandler( case "support-given": case "support-cut": - return ScriptResult.Fail($"Unknown assertion \"{args[0]}\"", this); + re = new(World); + prov = new($"^{re.FullLocation}$", RegexOptions.IgnoreCase); + match = prov.Match(args[1]); + if (!match.Success) return ScriptResult.Fail($"Could not parse province from \"{args[1]}\"", this); + + timeline = match.Groups[1].Length > 0 + ? match.Groups[1].Value + : Season.First.Timeline; + seasonsInTimeline = World.Timelines.Seasons.Where(season => season.Timeline == timeline); + if (!seasonsInTimeline.Any()) return ScriptResult.Fail($"No seasons in timeline {timeline}", this); + + turn = match.Groups[4].Length > 0 + ? int.Parse(match.Groups[4].Value) + // If turn is unspecified, use the second-latest turn in the timeline, + // since we want to assert against the subjects of the orders just adjudicated, + // and adjudication created a new set of seasons. + : seasonsInTimeline.Max(season => season.Turn) - 1; + season = new(timeline, turn); + + province = World.Map.Provinces.Single(province => province.Is(match.Groups[2].Value)); + + var matchingSupports = Adjudications.Where(adj + => adj is GivesSupport sup + && sup.Order.Unit.Season == season + && World.Map.GetLocation(sup.Order.Unit.Location).ProvinceName == province.Name); + if (!matchingSupports.Any()) return ScriptResult.Fail("No matching support decisions"); + var supports = matchingSupports.Cast().First(); + + if (args[0] == "support-given" && supports.Outcome != true) { + return ScriptResult.Fail($"Adjudication {supports} is false"); + } + if (args[0] == "support-cut" && supports.Outcome != false) { + return ScriptResult.Fail($"Adjudication {supports} is true"); + } + return ScriptResult.Succeed(this); default: return ScriptResult.Fail($"Unknown assertion \"{args[0]}\"", this);