5dplomacy/MultiversalDiplomacy/Script/ReplScriptHandler.cs

63 lines
2.1 KiB
C#

using MultiversalDiplomacy.Adjudicate;
using MultiversalDiplomacy.Model;
namespace MultiversalDiplomacy.Script;
/// <summary>
/// A script handler for the interactive repl.
/// </summary>
public class ReplScriptHandler(Action<string> WriteLine) : IScriptHandler
{
public string Prompt => "5dp> ";
public IScriptHandler? HandleInput(string input)
{
var args = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (args.Length == 0 || input.StartsWith('#'))
{
return this;
}
var command = args[0];
switch (command)
{
case "help":
case "?":
WriteLine("Commands:");
WriteLine(" help, ?: print this message");
WriteLine(" map <variant>: start a new game of the given variant");
WriteLine(" stab: stab");
break;
case "stab":
WriteLine("stab");
break;
case "map" when args.Length == 1:
WriteLine("Usage:");
WriteLine(" map <variant>");
WriteLine("Available variants:");
WriteLine($" {string.Join(", ", Enum.GetNames<MapType>().Select(s => s.ToLowerInvariant()))}");
break;
case "map" when args.Length > 1:
string mapType = args[1].Trim();
if (!Enum.TryParse(mapType, ignoreCase: true, out MapType map)) {
WriteLine($"Unknown variant \"{mapType}\"");
WriteLine("Available variants:");
WriteLine($" {string.Join(", ", Enum.GetNames<MapType>().Select(s => s.ToLowerInvariant()))}");
break;
}
World world = World.WithMap(Map.FromType(map));
WriteLine($"Created a new {map} game");
return new SetupScriptHandler(WriteLine, world, MovementPhaseAdjudicator.Instance);
default:
WriteLine($"Unrecognized command: \"{command}\"");
break;
}
return this;
}
}