From aaf3320cf8c0b6fc4e5d91c07a67dd8191461555 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 21 Aug 2024 14:25:25 +0000 Subject: [PATCH] Add a handler for asserting against orders --- .../Script/GameScriptHandler.cs | 2 +- .../Script/ValidatedOrdersScriptHandler.cs | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 MultiversalDiplomacy/Script/ValidatedOrdersScriptHandler.cs diff --git a/MultiversalDiplomacy/Script/GameScriptHandler.cs b/MultiversalDiplomacy/Script/GameScriptHandler.cs index 79c86fc..295f669 100644 --- a/MultiversalDiplomacy/Script/GameScriptHandler.cs +++ b/MultiversalDiplomacy/Script/GameScriptHandler.cs @@ -28,7 +28,7 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle // "---" submits the orders for validation to allow for assertions about it if (input == "---") { // TODO submit orders - // TODO return a new handler that handles asserts + return new ValidatedOrdersScriptHandler(World, Strict); } // A block of orders for a single power beginning with "{name}:" diff --git a/MultiversalDiplomacy/Script/ValidatedOrdersScriptHandler.cs b/MultiversalDiplomacy/Script/ValidatedOrdersScriptHandler.cs new file mode 100644 index 0000000..5a48105 --- /dev/null +++ b/MultiversalDiplomacy/Script/ValidatedOrdersScriptHandler.cs @@ -0,0 +1,56 @@ +using System.Text.RegularExpressions; + +using MultiversalDiplomacy.Model; + +namespace MultiversalDiplomacy.Script; + +public class ValidatedOrdersScriptHandler(World world, bool strict = false) : IScriptHandler +{ + public string Prompt => "valid> "; + + public World World { get; private set; } = world; + + /// + /// Whether unsuccessful commands should terminate the script. + /// + public bool Strict { get; } = strict; + + public IScriptHandler? HandleInput(string input) + { + var args = input.Split(' ', StringSplitOptions.RemoveEmptyEntries); + if (args.Length == 0) + { + return this; + } + + var command = args[0]; + switch (command) + { + 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) { + Console.WriteLine($"Could not parse province from {assertion}"); + return Strict ? null : this; + } + // TODO look up order once orders are validated and adjudicated + Console.WriteLine("Order lookup not implemented yet"); + return null; + + default: + // noop on comments that begin with # + if (command.StartsWith('#')) break; + Console.WriteLine($"Unrecognized command: {command}"); + if (Strict) return null; + break; + } + + return this; + } +}