2024-08-21 14:25:25 +00:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
using MultiversalDiplomacy.Model;
|
|
|
|
|
|
|
|
namespace MultiversalDiplomacy.Script;
|
|
|
|
|
2024-08-21 16:11:39 +00:00
|
|
|
public class AdjudicationQueryScriptHandler(World world, bool strict = false) : IScriptHandler
|
2024-08-21 14:25:25 +00:00
|
|
|
{
|
|
|
|
public string Prompt => "valid> ";
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
var args = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
2024-08-21 16:47:13 +00:00
|
|
|
if (args.Length == 0 || input.StartsWith('#'))
|
2024-08-21 14:25:25 +00:00
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
var command = args[0];
|
|
|
|
switch (command)
|
|
|
|
{
|
2024-08-21 16:11:39 +00:00
|
|
|
case "---":
|
|
|
|
Console.WriteLine("Ready for orders");
|
|
|
|
return new GameScriptHandler(World, Strict);
|
|
|
|
|
2024-08-21 14:25:25 +00:00
|
|
|
case "assert" when args.Length == 1:
|
|
|
|
Console.WriteLine("Usage:");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "assert":
|
|
|
|
string assertion = input["assert ".Length..];
|
|
|
|
OrderRegex re = new(World);
|
|
|
|
Regex prov = new($"{re.Province} (.*)");
|
|
|
|
Match match = prov.Match(assertion);
|
|
|
|
if (!match.Success) {
|
2024-08-21 16:11:39 +00:00
|
|
|
Console.WriteLine($"Could not parse province from \"{assertion}\"");
|
2024-08-21 14:25:25 +00:00
|
|
|
return Strict ? null : this;
|
|
|
|
}
|
|
|
|
// TODO look up order once orders are validated and adjudicated
|
|
|
|
Console.WriteLine("Order lookup not implemented yet");
|
|
|
|
return null;
|
|
|
|
|
2024-08-21 16:11:39 +00:00
|
|
|
case "status":
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
2024-08-21 14:25:25 +00:00
|
|
|
default:
|
2024-08-21 16:11:39 +00:00
|
|
|
Console.WriteLine($"Unrecognized command: \"{command}\"");
|
2024-08-21 14:25:25 +00:00
|
|
|
if (Strict) return null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|