5dplomacy/MultiversalDiplomacy/Script/GameScriptHandler.cs

92 lines
2.8 KiB
C#

using MultiversalDiplomacy.Model;
namespace MultiversalDiplomacy.Script;
/// <summary>
/// A script handler for interacting with a loaded game.
/// </summary>
public class GameScriptHandler : IScriptHandler
{
public GameScriptHandler(World world)
{
World = world;
}
public string Prompt => "5dp> ";
public World World { get; set; }
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(" list: list things in a game category");
Console.WriteLine(" orders: submit order sets");
Console.WriteLine(" status: overview of the state of the game");
break;
case "list" when args.Length == 1:
Console.WriteLine("usage:");
Console.WriteLine(" list ordersets: unadjudicated order sets");
Console.WriteLine(" list powers: the powers in the game");
break;
case "list" when args[1] == "ordersets":
foreach (OrderSet orderSet in World.OrderSets.Where(os => !os.Adjudicated))
{
var lines = orderSet.Text.Split('\n');
var firstLine = lines[0].Trim();
Console.WriteLine($" {firstLine} ({lines.Length - 1} orders)");
}
break;
case "list" when args[1] == "powers":
Console.WriteLine("Powers:");
foreach (Power power in World.Powers)
{
Console.WriteLine($" {power.Name}");
}
break;
case "orders" when args.Length == 1:
Console.WriteLine("usage: orders [power]");
break;
case "orders":
if (World.Powers.Any(p => p.Name == args[1]))
{
var handler = new OrderSetScriptHandler(this, World, input);
return handler;
}
Console.WriteLine("Unrecognized power");
break;
case "status":
foreach (Season season in World.Seasons)
{
Console.WriteLine($"{season}");
foreach (Unit unit in World.Units.Where(u => u.Season == season))
{
Console.WriteLine($" {unit}");
}
}
break;
default:
Console.WriteLine("Unrecognized command");
break;
}
return this;
}
}