5dplomacy/MultiversalDiplomacy/Script/ReplScriptHandler.cs

65 lines
2.2 KiB
C#
Raw Normal View History

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