5dplomacy/MultiversalDiplomacy/Script/ReplScriptHandler.cs

61 lines
1.8 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(" 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<MapType>().Select(s => s.ToLowerInvariant())));
break;
case "new" when args.Length > 1:
if (!Enum.TryParse<MapType>(args[1], out MapType map)) {
Console.WriteLine($"Unknown map {args[1]}");
Console.WriteLine("Available maps:");
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 this;
default:
Console.WriteLine($"Unrecognized command: {command}");
break;
}
return this;
}
}