Implement assert order-valid

This commit is contained in:
Tim Van Baak 2024-09-01 04:54:28 +00:00
parent f18147f666
commit 3984b814ca
3 changed files with 41 additions and 5 deletions

View File

@ -227,7 +227,7 @@ public class OrderParser(World world)
} else if (re.SupportMove.Match(command) is Match smoveMatch && smoveMatch.Success) { } else if (re.SupportMove.Match(command) is Match smoveMatch && smoveMatch.Success) {
return TryParseSupportMoveOrder(world, power, smoveMatch, out order); return TryParseSupportMoveOrder(world, power, smoveMatch, out order);
} else { } else {
throw new NotImplementedException(); return false;
} }
} }

View File

@ -1,10 +1,14 @@
using System.Text.RegularExpressions;
using MultiversalDiplomacy.Adjudicate; using MultiversalDiplomacy.Adjudicate;
using MultiversalDiplomacy.Model; using MultiversalDiplomacy.Model;
using MultiversalDiplomacy.Orders;
namespace MultiversalDiplomacy.Script; namespace MultiversalDiplomacy.Script;
public class AdjudicationQueryScriptHandler( public class AdjudicationQueryScriptHandler(
Action<string> WriteLine, Action<string> WriteLine,
List<OrderValidation> validations,
World world, World world,
IPhaseAdjudicator adjudicator, IPhaseAdjudicator adjudicator,
bool strict = false) bool strict = false)
@ -12,6 +16,8 @@ public class AdjudicationQueryScriptHandler(
{ {
public string Prompt => "valid> "; public string Prompt => "valid> ";
public List<OrderValidation> Validations { get; } = validations;
public World World { get; private set; } = world; public World World { get; private set; } = world;
/// <summary> /// <summary>
@ -67,10 +73,40 @@ public class AdjudicationQueryScriptHandler(
return false; return false;
case "order-valid": case "order-valid":
// Assert order was valid
case "order-invalid": case "order-invalid":
// Assert order was invalid OrderParser re = new(World);
Regex prov = new($"^{re.FullLocation}$", RegexOptions.IgnoreCase);
Match match = prov.Match(args[1]);
if (!match.Success) {
WriteLine($"Could not parse province from \"{args[1]}\"");
return !Strict;
}
string timeline = match.Groups[1].Length > 0
? match.Groups[1].Value
: Season.First.Timeline;
var seasonsInTimeline = World.Timelines.Seasons.Where(season => season.Timeline == timeline);
if (!seasonsInTimeline.Any()) return false;
int 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 season = new(timeline, turn);
Province province = World.Map.Provinces.Single(province => province.Is(match.Groups[2].Value));
var matching = Validations.Where(val
=> val.Order is UnitOrder order
&& order.Unit.Season == season
&& World.Map.GetLocation(order.Unit.Location).ProvinceName == province.Name);
if (!matching.Any()) return false;
return args[0] == "order-valid"
? matching.First().Valid
: !matching.First().Valid;
case "has-past": case "has-past":
// Assert a timeline's past // Assert a timeline's past

View File

@ -44,7 +44,7 @@ public class GameScriptHandler(
.ToList(); .ToList();
var adjudication = adjudicator.AdjudicateOrders(World, validOrders); var adjudication = adjudicator.AdjudicateOrders(World, validOrders);
var newWorld = adjudicator.UpdateWorld(World, adjudication); var newWorld = adjudicator.UpdateWorld(World, adjudication);
return new AdjudicationQueryScriptHandler(WriteLine, newWorld, adjudicator, Strict); return new AdjudicationQueryScriptHandler(WriteLine, validation, newWorld, adjudicator, Strict);
} }
// "===" submits the orders and moves immediately to taking the next set of orders // "===" submits the orders and moves immediately to taking the next set of orders