From 20b7269e4ba484fb064823e129128f6301f86893 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 14 Aug 2024 08:44:22 -0700 Subject: [PATCH] tmp commit of cli --- .../CommandLine/AdjudicateOptions.cs | 11 ++++ MultiversalDiplomacy/Script/IScriptHandler.cs | 21 +++++++ .../Script/ReplScriptHandler.cs | 60 +++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 MultiversalDiplomacy/Script/IScriptHandler.cs create mode 100644 MultiversalDiplomacy/Script/ReplScriptHandler.cs diff --git a/MultiversalDiplomacy/CommandLine/AdjudicateOptions.cs b/MultiversalDiplomacy/CommandLine/AdjudicateOptions.cs index 67fca84..0bf1398 100644 --- a/MultiversalDiplomacy/CommandLine/AdjudicateOptions.cs +++ b/MultiversalDiplomacy/CommandLine/AdjudicateOptions.cs @@ -1,5 +1,9 @@ +using System.Text.Json; + using CommandLine; +using MultiversalDiplomacy.Model; + namespace MultiversalDiplomacy.CommandLine; [Verb("adjudicate", HelpText = "Adjudicate a Multiversal Diplomacy game state.")] @@ -10,6 +14,13 @@ public class AdjudicateOptions public static void Execute(AdjudicateOptions args) { + Stream input = args.InputFile switch { + null => Console.OpenStandardInput(), + "-" => Console.OpenStandardInput(), + _ => new FileStream(args.InputFile!, FileMode.Open, FileAccess.Read), + }; + var state = JsonSerializer.Deserialize(input); + throw new NotImplementedException(); } } diff --git a/MultiversalDiplomacy/Script/IScriptHandler.cs b/MultiversalDiplomacy/Script/IScriptHandler.cs new file mode 100644 index 0000000..20ef562 --- /dev/null +++ b/MultiversalDiplomacy/Script/IScriptHandler.cs @@ -0,0 +1,21 @@ +namespace MultiversalDiplomacy.Script; + +/// +/// A handler that interprets and executes 5dp script commands. Script handlers may create additional script handlers +/// and delegate handling to them, allowing a sort of recursive parsing of script commands. +/// +public interface IScriptHandler +{ + /// + /// When used interactively, the prompt that should be displayed. + /// + public string Prompt { get; } + + /// + /// Process a line of input. + /// + /// + /// The handler that should handle the next line of input, or null if script handling should end. + /// + public IScriptHandler? HandleInput(string input); +} diff --git a/MultiversalDiplomacy/Script/ReplScriptHandler.cs b/MultiversalDiplomacy/Script/ReplScriptHandler.cs new file mode 100644 index 0000000..475ef83 --- /dev/null +++ b/MultiversalDiplomacy/Script/ReplScriptHandler.cs @@ -0,0 +1,60 @@ +using MultiversalDiplomacy.Model; + +namespace MultiversalDiplomacy.Script; + +/// +/// A script handler for the interactive repl. +/// +public class ReplScriptHandler : IScriptHandler +{ + public string Prompt => "5dp> "; + + public IScriptHandler? HandleInput(string input) + { + var args = input.Split(' ', StringSplitOptions.RemoveEmptyEntries); + if (args.Length == 0) + { + return this; + } + + var command = args[0]; + switch (command) + { + case "help": + case "?": + Console.WriteLine("Commands:"); + Console.WriteLine(" help, ?: print this message"); + Console.WriteLine(" stab: stab"); + Console.WriteLine(" new: start a new game"); + break; + + case "stab": + Console.WriteLine("stab"); + break; + + case "new" when args.Length == 1: + Console.WriteLine("Usage:"); + Console.WriteLine(" new [map]"); + Console.WriteLine("Available maps:"); + Console.WriteLine(string.Join(", ", Enum.GetNames().Select(s => s.ToLowerInvariant()))); + break; + + case "new" when args.Length > 1: + if (!Enum.TryParse(args[1], out MapType map)) { + Console.WriteLine($"Unknown map {args[1]}"); + Console.WriteLine("Available maps:"); + Console.WriteLine(string.Join(", ", Enum.GetNames().Select(s => s.ToLowerInvariant()))); + break; + } + World world = World.WithMap(Map.FromType(map)); + Console.WriteLine($"Created a new {map} game"); + return this; + + default: + Console.WriteLine($"Unrecognized command: {command}"); + break; + } + + return this; + } +}