5dplomacy/MultiversalDiplomacy/Script/AdjudicationQueryScriptHand...

138 lines
4.4 KiB
C#
Raw Normal View History

2024-09-01 04:54:28 +00:00
using System.Text.RegularExpressions;
using MultiversalDiplomacy.Adjudicate;
using MultiversalDiplomacy.Model;
2024-09-01 04:54:28 +00:00
using MultiversalDiplomacy.Orders;
namespace MultiversalDiplomacy.Script;
2024-08-28 21:10:41 +00:00
public class AdjudicationQueryScriptHandler(
Action<string> WriteLine,
2024-09-01 04:54:28 +00:00
List<OrderValidation> validations,
2024-08-28 21:10:41 +00:00
World world,
IPhaseAdjudicator adjudicator,
2024-08-28 21:10:41 +00:00
bool strict = false)
: IScriptHandler
{
public string Prompt => "valid> ";
2024-09-01 04:54:28 +00:00
public List<OrderValidation> Validations { get; } = validations;
public World World { get; private set; } = world;
/// <summary>
/// Whether unsuccessful commands should terminate the script.
/// </summary>
public bool Strict { get; } = strict;
public IScriptHandler? HandleInput(string input)
{
2024-08-28 15:01:27 +00:00
var args = input.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
2024-08-21 16:47:13 +00:00
if (args.Length == 0 || input.StartsWith('#'))
{
return this;
}
var command = args[0];
switch (command)
{
case "---":
2024-08-28 21:10:41 +00:00
WriteLine("Ready for orders");
return new GameScriptHandler(WriteLine, World, adjudicator, Strict);
case "assert" when args.Length == 1:
2024-08-28 21:10:41 +00:00
WriteLine("Usage:");
break;
case "assert":
2024-08-28 19:14:19 +00:00
if (!EvaluateAssertion(args[1])) return Strict ? null : this;
break;
case "status":
throw new NotImplementedException();
default:
2024-08-28 21:10:41 +00:00
WriteLine($"Unrecognized command: \"{command}\"");
if (Strict) return null;
break;
}
return this;
}
2024-08-28 15:01:27 +00:00
2024-08-28 19:14:19 +00:00
private bool EvaluateAssertion(string assertion)
2024-08-28 15:01:27 +00:00
{
2024-08-28 19:14:19 +00:00
var args = assertion.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
switch (args[0])
2024-08-28 15:01:27 +00:00
{
case "true":
2024-08-28 19:14:19 +00:00
return true;
2024-08-28 15:01:27 +00:00
case "false":
2024-08-28 19:14:19 +00:00
return false;
case "order-valid":
case "order-invalid":
2024-09-01 04:54:28 +00:00
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;
2024-08-28 19:14:19 +00:00
case "has-past":
// Assert a timeline's past
case "holds":
// Assert a unit successfully held
case "dislodged":
// Assert a unit was dislodged
case "moves":
// Assert a unit successfully moved
case "no-move":
// Assert a unit did not move
case "supports":
// Assert a unit's support was given
case "cut":
// Assert a unit's support was cut
default:
2024-08-28 21:10:41 +00:00
WriteLine($"Unknown assertion \"{args[0]}\"");
2024-08-28 19:14:19 +00:00
return !Strict;
2024-08-28 15:01:27 +00:00
}
}
}