2024-08-21 13:46:02 +00:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
2024-08-18 04:24:59 +00:00
|
|
|
using MultiversalDiplomacy.Model;
|
2024-08-21 13:46:02 +00:00
|
|
|
using MultiversalDiplomacy.Orders;
|
2024-08-18 04:24:59 +00:00
|
|
|
|
|
|
|
namespace MultiversalDiplomacy.Script;
|
|
|
|
|
|
|
|
public class GameScriptHandler(World world, bool strict = false) : IScriptHandler
|
|
|
|
{
|
2024-08-21 13:46:02 +00:00
|
|
|
public string Prompt => "orders> ";
|
2024-08-18 04:24:59 +00:00
|
|
|
|
|
|
|
public World World { get; private set; } = world;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether unsuccessful commands should terminate the script.
|
|
|
|
/// </summary>
|
|
|
|
public bool Strict { get; } = strict;
|
|
|
|
|
|
|
|
private string? CurrentPower = null;
|
|
|
|
|
|
|
|
public IScriptHandler? HandleInput(string input)
|
|
|
|
{
|
|
|
|
if (input == "") {
|
|
|
|
CurrentPower = null;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-08-21 13:46:02 +00:00
|
|
|
// "---" submits the orders for validation to allow for assertions about it
|
2024-08-18 04:24:59 +00:00
|
|
|
if (input == "---") {
|
|
|
|
// TODO submit orders
|
2024-08-21 14:25:25 +00:00
|
|
|
return new ValidatedOrdersScriptHandler(World, Strict);
|
2024-08-18 04:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A block of orders for a single power beginning with "{name}:"
|
|
|
|
if (World.Powers.FirstOrDefault(p => input.EqualsAnyCase($"{p}:"), null) is string power) {
|
|
|
|
CurrentPower = power;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-08-21 13:46:02 +00:00
|
|
|
if (TryParseOrder(input, out Order? order)) {
|
|
|
|
Console.WriteLine($"Parsed order \"{input}\" but doing anything with it isn't implemented yet");
|
|
|
|
} else if (Strict) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-08-18 04:24:59 +00:00
|
|
|
|
|
|
|
Console.WriteLine($"{CurrentPower}: {input}");
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2024-08-21 13:46:02 +00:00
|
|
|
|
|
|
|
private bool TryParseOrder(string input, out Order? order) {
|
|
|
|
order = null;
|
|
|
|
OrderRegex re = new(World);
|
|
|
|
Match match;
|
|
|
|
if ((match = re.Hold.Match(input)).Success) {
|
|
|
|
var hold = OrderRegex.ParseHold(match);
|
|
|
|
string power = hold.power.Length > 0 ? hold.power : CurrentPower ?? hold.power;
|
|
|
|
Unit unit = World.Units.First(unit
|
|
|
|
=> World.Map.GetLocation(unit.Location).ProvinceName == hold.province
|
|
|
|
&& unit.Season.Timeline == (hold.timeline.Length > 0 ? hold.timeline : "a")
|
|
|
|
&& unit.Season.Turn.ToString() == (hold.turn.Length > 0 ? hold.turn : "0"));
|
|
|
|
order = new HoldOrder(power, unit);
|
|
|
|
return true;
|
|
|
|
} else if ((match = re.Move.Match(input)).Success) {
|
|
|
|
var move = OrderRegex.ParseMove(match);
|
|
|
|
Unit unit = World.Units.First(unit
|
|
|
|
=> World.Map.GetLocation(unit.Location).ProvinceName == move.province
|
|
|
|
&& unit.Season.Timeline == (move.timeline.Length > 0 ? move.timeline : "a")
|
|
|
|
&& unit.Season.Turn.ToString() == (move.turn.Length > 0 ? move.turn : "0"));
|
|
|
|
order = new MoveOrder(move.power, unit, Season.First, "l");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Console.WriteLine($"Failed to parse \"{input}\"");
|
|
|
|
return false;
|
|
|
|
}
|
2024-08-18 04:24:59 +00:00
|
|
|
}
|