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