5dplomacy/MultiversalDiplomacy/Script/SetupScriptHandler.cs

92 lines
3.1 KiB
C#
Raw Permalink Normal View History

using MultiversalDiplomacy.Adjudicate;
2024-08-16 22:52:13 +00:00
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,
IPhaseAdjudicator adjudicator)
: IScriptHandler
2024-08-16 22:52:13 +00:00
{
public string Prompt => "setup> ";
2024-08-16 22:52:13 +00:00
public World World { get; private set; } = world;
public ScriptResult HandleInput(string input)
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(" 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\"");
2024-08-16 22:52:13 +00:00
break;
case "begin":
2024-08-18 04:24:59 +00:00
case "---":
2024-08-28 21:10:41 +00:00
WriteLine("Starting game");
WriteLine("Ready for orders");
return ScriptResult.Succeed(new GameScriptHandler(WriteLine, World, adjudicator));
2024-08-16 22:52:13 +00:00
case "list" when args.Length == 1:
2024-08-28 21:10:41 +00:00
WriteLine("usage:");
WriteLine(" list powers: the powers in the game");
WriteLine(" list units: units created so far");
2024-08-16 22:52:13 +00:00
break;
case "list" when args[1] == "powers":
2024-08-28 21:10:41 +00:00
WriteLine("Powers:");
2024-08-16 22:52:13 +00:00
foreach (string powerName in World.Powers)
{
2024-08-28 21:10:41 +00:00
WriteLine($" {powerName}");
2024-08-16 22:52:13 +00:00
}
break;
case "list" when args[1] == "units":
2024-08-28 21:10:41 +00:00
WriteLine("Units:");
2024-08-16 22:52:13 +00:00
foreach (Unit unit in World.Units)
{
2024-08-28 21:10:41 +00:00
WriteLine($" {unit}");
2024-08-16 22:52:13 +00:00
}
break;
case "option" when args.Length < 3:
throw new NotImplementedException("There are no supported options yet");
2024-08-16 22:52:13 +00:00
case "unit" when args.Length < 2:
2024-08-28 21:10:41 +00:00
WriteLine("usage: unit [power] [type] [province]</location>");
2024-08-16 22:52:13 +00:00
break;
case "unit":
string unitSpec = input["unit ".Length..];
2024-08-27 03:23:28 +00:00
if (OrderParser.TryParseUnit(World, unitSpec, out Unit? newUnit)) {
2024-08-16 22:52:13 +00:00
World = World.Update(units: World.Units.Append(newUnit));
2024-08-28 21:10:41 +00:00
WriteLine($"Created {newUnit}");
return ScriptResult.Succeed(this);
2024-08-16 22:52:13 +00:00
}
return ScriptResult.Fail($"Could not match unit spec \"{unitSpec}\"", this);
2024-08-16 22:52:13 +00:00
default:
ScriptResult.Fail($"Unrecognized command: \"{command}\"", this);
2024-08-16 22:52:13 +00:00
break;
}
return ScriptResult.Succeed(this);
2024-08-16 22:52:13 +00:00
}
}