95 lines
3.2 KiB
C#
95 lines
3.2 KiB
C#
using MultiversalDiplomacy.Model;
|
|
|
|
namespace MultiversalDiplomacy.Script;
|
|
|
|
/// <summary>
|
|
/// A script handler for modifying a game before it begins.
|
|
/// </summary>
|
|
public class SetupScriptHandler(Action<string> WriteLine, World world, bool strict = false) : IScriptHandler
|
|
{
|
|
public string Prompt => "setup> ";
|
|
|
|
public World World { get; private set; } = world;
|
|
|
|
/// <summary>
|
|
/// Whether unsuccessful commands should terminate the script.
|
|
/// </summary>
|
|
public bool Strict { get; } = strict;
|
|
|
|
public IScriptHandler? HandleInput(string input)
|
|
{
|
|
var args = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
if (args.Length == 0 || input.StartsWith('#'))
|
|
{
|
|
return this;
|
|
}
|
|
|
|
var command = args[0];
|
|
switch (command)
|
|
{
|
|
case "help":
|
|
case "?":
|
|
WriteLine("commands:");
|
|
WriteLine(" begin: complete setup and start the game (alias: ---)");
|
|
WriteLine(" list <type>: list things in a game category");
|
|
WriteLine(" option <name> <value>: set a game option");
|
|
WriteLine(" unit <power> <type> <province> [location]: add a unit to the game");
|
|
WriteLine(" <province> may be \"province/location\"");
|
|
break;
|
|
|
|
case "begin":
|
|
case "---":
|
|
WriteLine("Starting game");
|
|
WriteLine("Ready for orders");
|
|
return new GameScriptHandler(WriteLine, World, Strict);
|
|
|
|
case "list" when args.Length == 1:
|
|
WriteLine("usage:");
|
|
WriteLine(" list powers: the powers in the game");
|
|
WriteLine(" list units: units created so far");
|
|
break;
|
|
|
|
case "list" when args[1] == "powers":
|
|
WriteLine("Powers:");
|
|
foreach (string powerName in World.Powers)
|
|
{
|
|
WriteLine($" {powerName}");
|
|
}
|
|
break;
|
|
|
|
case "list" when args[1] == "units":
|
|
WriteLine("Units:");
|
|
foreach (Unit unit in World.Units)
|
|
{
|
|
WriteLine($" {unit}");
|
|
}
|
|
break;
|
|
|
|
case "option" when args.Length < 3:
|
|
throw new NotImplementedException("There are no supported options yet");
|
|
|
|
case "unit" when args.Length < 2:
|
|
WriteLine("usage: unit [power] [type] [province]</location>");
|
|
break;
|
|
|
|
case "unit":
|
|
string unitSpec = input["unit ".Length..];
|
|
if (OrderParser.TryParseUnit(World, unitSpec, out Unit? newUnit)) {
|
|
World = World.Update(units: World.Units.Append(newUnit));
|
|
WriteLine($"Created {newUnit}");
|
|
return this;
|
|
}
|
|
WriteLine($"Could not match unit spec \"{unitSpec}\"");
|
|
if (Strict) return null;
|
|
break;
|
|
|
|
default:
|
|
WriteLine($"Unrecognized command: \"{command}\"");
|
|
if (Strict) return null;
|
|
break;
|
|
}
|
|
|
|
return this;
|
|
}
|
|
}
|