5dplomacy/MultiversalDiplomacy/Script/ReplScriptHandler.cs

65 lines
2.2 KiB
C#

using MultiversalDiplomacy.Model;
namespace MultiversalDiplomacy.Script;
/// <summary>
/// A script handler for the interactive repl.
/// </summary>
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(" map <variant>: start a new game of the given variant");
Console.WriteLine(" stab: stab");
break;
case "stab":
Console.WriteLine("stab");
break;
case "map" when args.Length == 1:
Console.WriteLine("Usage:");
Console.WriteLine(" map <variant>");
Console.WriteLine("Available variants:");
Console.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)) {
Console.WriteLine($"Unknown variant \"{mapType}\"");
Console.WriteLine("Available variants:");
Console.WriteLine($" {string.Join(", ", Enum.GetNames<MapType>().Select(s => s.ToLowerInvariant()))}");
break;
}
World world = World.WithMap(Map.FromType(map));
Console.WriteLine($"Created a new {map} game");
return new SetupScriptHandler(world);
default:
// noop on comments that begin with #
if (!command.StartsWith('#')) {
Console.WriteLine($"Unrecognized command: \"{command}\"");
}
break;
}
return this;
}
}