5dplomacy/MultiversalDiplomacy/Script/GameScriptHandler.cs

114 lines
3.8 KiB
C#
Raw Normal View History

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(" adjudicate: adjudicate the current orders");
2024-04-02 15:30:18 +00:00
Console.WriteLine(" assert: assert about the state of the game");
Console.WriteLine(" list: list things in a game category");
Console.WriteLine(" orders: submit order sets");
2023-04-05 03:54:53 +00:00
Console.WriteLine(" status: overview of the state of the game");
break;
case "adjudicate":
World = GameController.AdjudicateOrders(World);
break;
2024-04-02 15:30:18 +00:00
case "assert" when args.Length == 1:
Console.WriteLine("usage:");
Console.WriteLine(" assert timeline [timeline]@[turn]: timeline exists");
Console.WriteLine(" assert unit [unit spec]: unit exists");
break;
case "assert" when args[1] == "timeline":
// TODO: raise an error if the timeline doesn't exist
Console.WriteLine("WIP");
break;
case "assert" when args[1] == "unit":
// TODO: raise an error if the unit doesn't exist
Console.WriteLine("WIP");
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":
2022-12-31 21:52:30 +00:00
if (World.Powers.Any(p => p.Name == args[1]))
{
var handler = new OrderSetScriptHandler(this, World, input);
return handler;
}
Console.WriteLine("Unrecognized power");
break;
2023-04-05 03:54:53 +00:00
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;
}
}