2024-08-28 21:27:35 +00:00
|
|
|
using MultiversalDiplomacy.Adjudicate;
|
2024-08-21 14:25:25 +00:00
|
|
|
using MultiversalDiplomacy.Model;
|
|
|
|
|
|
|
|
namespace MultiversalDiplomacy.Script;
|
|
|
|
|
2024-08-28 21:10:41 +00:00
|
|
|
public class AdjudicationQueryScriptHandler(
|
|
|
|
Action<string> WriteLine,
|
|
|
|
World world,
|
2024-08-28 21:27:35 +00:00
|
|
|
IPhaseAdjudicator adjudicator,
|
2024-08-28 21:10:41 +00:00
|
|
|
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)
|
|
|
|
{
|
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('#'))
|
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 "---":
|
2024-08-28 21:10:41 +00:00
|
|
|
WriteLine("Ready for orders");
|
2024-08-28 21:27:35 +00:00
|
|
|
return new GameScriptHandler(WriteLine, World, adjudicator, Strict);
|
2024-08-21 16:11:39 +00:00
|
|
|
|
2024-08-21 14:25:25 +00:00
|
|
|
case "assert" when args.Length == 1:
|
2024-08-28 21:10:41 +00:00
|
|
|
WriteLine("Usage:");
|
2024-08-21 14:25:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "assert":
|
2024-08-28 19:14:19 +00:00
|
|
|
if (!EvaluateAssertion(args[1])) return Strict ? null : this;
|
|
|
|
break;
|
2024-08-21 14:25:25 +00:00
|
|
|
|
2024-08-21 16:11:39 +00:00
|
|
|
case "status":
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
2024-08-21 14:25:25 +00:00
|
|
|
default:
|
2024-08-28 21:10:41 +00:00
|
|
|
WriteLine($"Unrecognized command: \"{command}\"");
|
2024-08-21 14:25:25 +00:00
|
|
|
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":
|
|
|
|
// 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:
|
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
|
|
|
}
|
|
|
|
}
|
2024-08-21 14:25:25 +00:00
|
|
|
}
|