102 lines
2.5 KiB
C#
102 lines
2.5 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
using MultiversalDiplomacy.Model;
|
|
|
|
namespace MultiversalDiplomacy.Script;
|
|
|
|
public class AdjudicationQueryScriptHandler(
|
|
Action<string> WriteLine,
|
|
World world,
|
|
bool strict = false)
|
|
: IScriptHandler
|
|
{
|
|
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(' ', 2, StringSplitOptions.RemoveEmptyEntries);
|
|
if (args.Length == 0 || input.StartsWith('#'))
|
|
{
|
|
return this;
|
|
}
|
|
|
|
var command = args[0];
|
|
switch (command)
|
|
{
|
|
case "---":
|
|
WriteLine("Ready for orders");
|
|
return new GameScriptHandler(WriteLine, World, Strict);
|
|
|
|
case "assert" when args.Length == 1:
|
|
WriteLine("Usage:");
|
|
break;
|
|
|
|
case "assert":
|
|
if (!EvaluateAssertion(args[1])) return Strict ? null : this;
|
|
break;
|
|
|
|
case "status":
|
|
throw new NotImplementedException();
|
|
|
|
default:
|
|
WriteLine($"Unrecognized command: \"{command}\"");
|
|
if (Strict) return null;
|
|
break;
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
private bool EvaluateAssertion(string assertion)
|
|
{
|
|
var args = assertion.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
switch (args[0])
|
|
{
|
|
case "true":
|
|
return true;
|
|
|
|
case "false":
|
|
return false;
|
|
|
|
case "order-valid":
|
|
// Assert order was valid
|
|
|
|
case "order-invalid":
|
|
// Assert order was invalid
|
|
|
|
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:
|
|
WriteLine($"Unknown assertion \"{args[0]}\"");
|
|
return !Strict;
|
|
}
|
|
}
|
|
}
|