5dplomacy/MultiversalDiplomacy/Script/AdjudicationQueryScriptHand...

102 lines
2.6 KiB
C#
Raw Normal View History

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