2024-08-21 13:46:02 +00:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
2024-08-28 14:41:23 +00:00
|
|
|
using MultiversalDiplomacy.Adjudicate;
|
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;
|
|
|
|
|
2024-08-28 21:27:35 +00:00
|
|
|
public class GameScriptHandler(
|
|
|
|
Action<string> WriteLine,
|
|
|
|
World world,
|
2024-09-03 03:20:59 +00:00
|
|
|
IPhaseAdjudicator adjudicator)
|
2024-08-28 21:27:35 +00:00
|
|
|
: IScriptHandler
|
2024-08-18 04:24:59 +00:00
|
|
|
{
|
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;
|
|
|
|
|
2024-08-28 00:46:32 +00:00
|
|
|
private string? CurrentPower { get; set; } = null;
|
|
|
|
|
|
|
|
public List<Order> Orders { get; } = [];
|
2024-08-18 04:24:59 +00:00
|
|
|
|
2024-09-03 03:20:59 +00:00
|
|
|
public ScriptResult HandleInput(string input)
|
2024-08-18 04:24:59 +00:00
|
|
|
{
|
|
|
|
if (input == "") {
|
|
|
|
CurrentPower = null;
|
2024-09-03 03:20:59 +00:00
|
|
|
return ScriptResult.Succeed(this);
|
2024-08-18 04:24:59 +00:00
|
|
|
}
|
2024-09-03 03:20:59 +00:00
|
|
|
if (input.StartsWith('#')) return ScriptResult.Succeed(this);
|
2024-08-18 04:24:59 +00:00
|
|
|
|
2024-08-21 16:11:39 +00:00
|
|
|
// "---" submits the orders and allows queries about the outcome
|
2024-08-18 04:24:59 +00:00
|
|
|
if (input == "---") {
|
2024-08-28 21:10:41 +00:00
|
|
|
WriteLine("Submitting orders for adjudication");
|
2024-08-28 14:41:23 +00:00
|
|
|
var validation = adjudicator.ValidateOrders(World, Orders);
|
|
|
|
var validOrders = validation
|
|
|
|
.Where(v => v.Valid)
|
|
|
|
.Select(v => v.Order)
|
|
|
|
.ToList();
|
|
|
|
var adjudication = adjudicator.AdjudicateOrders(World, validOrders);
|
|
|
|
var newWorld = adjudicator.UpdateWorld(World, adjudication);
|
2024-09-03 03:20:59 +00:00
|
|
|
return ScriptResult.Succeed(new AdjudicationQueryScriptHandler(
|
2024-09-03 04:25:37 +00:00
|
|
|
WriteLine, validation, adjudication, newWorld, adjudicator));
|
2024-08-21 16:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// "===" submits the orders and moves immediately to taking the next set of orders
|
|
|
|
// i.e. it's "---" twice
|
|
|
|
if (input == "===") {
|
2024-08-28 21:10:41 +00:00
|
|
|
WriteLine("Submitting orders for adjudication");
|
2024-08-28 14:41:23 +00:00
|
|
|
var validation = adjudicator.ValidateOrders(World, Orders);
|
|
|
|
var validOrders = validation
|
|
|
|
.Where(v => v.Valid)
|
|
|
|
.Select(v => v.Order)
|
|
|
|
.ToList();
|
|
|
|
var adjudication = adjudicator.AdjudicateOrders(World, validOrders);
|
|
|
|
World = adjudicator.UpdateWorld(World, adjudication);
|
2024-08-28 21:10:41 +00:00
|
|
|
WriteLine("Ready for orders");
|
2024-09-03 03:20:59 +00:00
|
|
|
return ScriptResult.Succeed(this);
|
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;
|
2024-09-03 03:20:59 +00:00
|
|
|
return ScriptResult.Succeed(this);
|
2024-08-18 04:24:59 +00:00
|
|
|
}
|
|
|
|
|
2024-08-25 04:26:10 +00:00
|
|
|
// If it's not a comment, submit, or order block, assume it's an order.
|
|
|
|
string orderPower;
|
|
|
|
string orderText;
|
|
|
|
if (CurrentPower is not null) {
|
|
|
|
// In a block of orders from a power, the power was specified at the top and each line is an order.
|
|
|
|
orderPower = CurrentPower;
|
|
|
|
orderText = input;
|
|
|
|
} else {
|
|
|
|
// Outside a power block, the power is prefixed to each order.
|
|
|
|
Regex re = new($"^{World.Map.PowerRegex}(?:[:])? (.*)$", RegexOptions.IgnoreCase);
|
|
|
|
var match = re.Match(input);
|
2024-09-03 03:20:59 +00:00
|
|
|
if (!match.Success) return ScriptResult.Fail($"Could not determine ordering power in \"{input}\"", this);
|
2024-08-25 04:26:10 +00:00
|
|
|
orderPower = match.Groups[1].Value;
|
|
|
|
orderText = match.Groups[2].Value;
|
2024-08-21 13:46:02 +00:00
|
|
|
}
|
2024-08-18 04:24:59 +00:00
|
|
|
|
2024-08-27 03:23:28 +00:00
|
|
|
if (OrderParser.TryParseOrder(World, orderPower, orderText, out Order? order)) {
|
2024-08-28 21:10:41 +00:00
|
|
|
WriteLine($"Parsed {orderPower} order: {order}");
|
2024-08-28 00:46:32 +00:00
|
|
|
Orders.Add(order);
|
2024-09-03 03:20:59 +00:00
|
|
|
return ScriptResult.Succeed(this);
|
2024-08-26 15:39:42 +00:00
|
|
|
}
|
2024-08-21 13:46:02 +00:00
|
|
|
|
2024-09-03 03:20:59 +00:00
|
|
|
return ScriptResult.Fail($"Failed to parse \"{orderText}\"", this);
|
2024-08-21 13:46:02 +00:00
|
|
|
}
|
2024-08-18 04:24:59 +00:00
|
|
|
}
|