diff --git a/MultiversalDiplomacy/CommandLine/ReplOptions.cs b/MultiversalDiplomacy/CommandLine/ReplOptions.cs index 2ed1668..4d29e8a 100644 --- a/MultiversalDiplomacy/CommandLine/ReplOptions.cs +++ b/MultiversalDiplomacy/CommandLine/ReplOptions.cs @@ -54,7 +54,7 @@ public class ReplOptions // The last null is returned because an EOF means we should quit the repl. } - IScriptHandler? handler = new ReplScriptHandler(); + IScriptHandler? handler = new ReplScriptHandler(Console.WriteLine); Console.Write(handler.Prompt); foreach (string? nextInput in GetInputs()) diff --git a/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs b/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs index 32f63aa..654d621 100644 --- a/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs +++ b/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs @@ -4,7 +4,11 @@ using MultiversalDiplomacy.Model; namespace MultiversalDiplomacy.Script; -public class AdjudicationQueryScriptHandler(World world, bool strict = false) : IScriptHandler +public class AdjudicationQueryScriptHandler( + Action WriteLine, + World world, + bool strict = false) + : IScriptHandler { public string Prompt => "valid> "; @@ -27,11 +31,11 @@ public class AdjudicationQueryScriptHandler(World world, bool strict = false) : switch (command) { case "---": - Console.WriteLine("Ready for orders"); - return new GameScriptHandler(World, Strict); + WriteLine("Ready for orders"); + return new GameScriptHandler(WriteLine, World, Strict); case "assert" when args.Length == 1: - Console.WriteLine("Usage:"); + WriteLine("Usage:"); break; case "assert": @@ -42,7 +46,7 @@ public class AdjudicationQueryScriptHandler(World world, bool strict = false) : throw new NotImplementedException(); default: - Console.WriteLine($"Unrecognized command: \"{command}\""); + WriteLine($"Unrecognized command: \"{command}\""); if (Strict) return null; break; } @@ -90,7 +94,7 @@ public class AdjudicationQueryScriptHandler(World world, bool strict = false) : // Assert a unit's support was cut default: - Console.WriteLine($"Unknown assertion \"{args[0]}\""); + WriteLine($"Unknown assertion \"{args[0]}\""); return !Strict; } } diff --git a/MultiversalDiplomacy/Script/GameScriptHandler.cs b/MultiversalDiplomacy/Script/GameScriptHandler.cs index dda1d80..7c4ea5d 100644 --- a/MultiversalDiplomacy/Script/GameScriptHandler.cs +++ b/MultiversalDiplomacy/Script/GameScriptHandler.cs @@ -6,7 +6,7 @@ using MultiversalDiplomacy.Orders; namespace MultiversalDiplomacy.Script; -public class GameScriptHandler(World world, bool strict = false) : IScriptHandler +public class GameScriptHandler(Action WriteLine, World world, bool strict = false) : IScriptHandler { public string Prompt => "orders> "; @@ -31,7 +31,7 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle // "---" submits the orders and allows queries about the outcome if (input == "---") { - Console.WriteLine("Submitting orders for adjudication"); + WriteLine("Submitting orders for adjudication"); var adjudicator = MovementPhaseAdjudicator.Instance; var validation = adjudicator.ValidateOrders(World, Orders); var validOrders = validation @@ -40,13 +40,13 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle .ToList(); var adjudication = adjudicator.AdjudicateOrders(World, validOrders); var newWorld = adjudicator.UpdateWorld(World, adjudication); - return new AdjudicationQueryScriptHandler(newWorld, Strict); + return new AdjudicationQueryScriptHandler(WriteLine, newWorld, Strict); } // "===" submits the orders and moves immediately to taking the next set of orders // i.e. it's "---" twice if (input == "===") { - Console.WriteLine("Submitting orders for adjudication"); + WriteLine("Submitting orders for adjudication"); var adjudicator = MovementPhaseAdjudicator.Instance; var validation = adjudicator.ValidateOrders(World, Orders); var validOrders = validation @@ -55,7 +55,7 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle .ToList(); var adjudication = adjudicator.AdjudicateOrders(World, validOrders); World = adjudicator.UpdateWorld(World, adjudication); - Console.WriteLine("Ready for orders"); + WriteLine("Ready for orders"); return this; } @@ -77,7 +77,7 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle Regex re = new($"^{World.Map.PowerRegex}(?:[:])? (.*)$", RegexOptions.IgnoreCase); var match = re.Match(input); if (!match.Success) { - Console.WriteLine($"Could not determine ordering power in \"{input}\""); + WriteLine($"Could not determine ordering power in \"{input}\""); return Strict ? null : this; } orderPower = match.Groups[1].Value; @@ -85,12 +85,12 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle } if (OrderParser.TryParseOrder(World, orderPower, orderText, out Order? order)) { - Console.WriteLine($"Parsed {orderPower} order: {order}"); + WriteLine($"Parsed {orderPower} order: {order}"); Orders.Add(order); return this; } - Console.WriteLine($"Failed to parse \"{orderText}\""); + WriteLine($"Failed to parse \"{orderText}\""); return Strict ? null : this; } } diff --git a/MultiversalDiplomacy/Script/ReplScriptHandler.cs b/MultiversalDiplomacy/Script/ReplScriptHandler.cs index ba17760..f76d8af 100644 --- a/MultiversalDiplomacy/Script/ReplScriptHandler.cs +++ b/MultiversalDiplomacy/Script/ReplScriptHandler.cs @@ -5,7 +5,7 @@ namespace MultiversalDiplomacy.Script; /// /// A script handler for the interactive repl. /// -public class ReplScriptHandler : IScriptHandler +public class ReplScriptHandler(Action WriteLine) : IScriptHandler { public string Prompt => "5dp> "; @@ -22,37 +22,37 @@ public class ReplScriptHandler : IScriptHandler { case "help": case "?": - Console.WriteLine("Commands:"); - Console.WriteLine(" help, ?: print this message"); - Console.WriteLine(" map : start a new game of the given variant"); - Console.WriteLine(" stab: stab"); + WriteLine("Commands:"); + WriteLine(" help, ?: print this message"); + WriteLine(" map : start a new game of the given variant"); + WriteLine(" stab: stab"); break; case "stab": - Console.WriteLine("stab"); + WriteLine("stab"); break; case "map" when args.Length == 1: - Console.WriteLine("Usage:"); - Console.WriteLine(" map "); - Console.WriteLine("Available variants:"); - Console.WriteLine($" {string.Join(", ", Enum.GetNames().Select(s => s.ToLowerInvariant()))}"); + WriteLine("Usage:"); + WriteLine(" map "); + WriteLine("Available variants:"); + WriteLine($" {string.Join(", ", Enum.GetNames().Select(s => s.ToLowerInvariant()))}"); break; case "map" when args.Length > 1: string mapType = args[1].Trim(); if (!Enum.TryParse(mapType, ignoreCase: true, out MapType map)) { - Console.WriteLine($"Unknown variant \"{mapType}\""); - Console.WriteLine("Available variants:"); - Console.WriteLine($" {string.Join(", ", Enum.GetNames().Select(s => s.ToLowerInvariant()))}"); + WriteLine($"Unknown variant \"{mapType}\""); + WriteLine("Available variants:"); + WriteLine($" {string.Join(", ", Enum.GetNames().Select(s => s.ToLowerInvariant()))}"); break; } World world = World.WithMap(Map.FromType(map)); - Console.WriteLine($"Created a new {map} game"); - return new SetupScriptHandler(world); + WriteLine($"Created a new {map} game"); + return new SetupScriptHandler(WriteLine, world); default: - Console.WriteLine($"Unrecognized command: \"{command}\""); + WriteLine($"Unrecognized command: \"{command}\""); break; } diff --git a/MultiversalDiplomacy/Script/SetupScriptHandler.cs b/MultiversalDiplomacy/Script/SetupScriptHandler.cs index 80e8bc5..88d42e4 100644 --- a/MultiversalDiplomacy/Script/SetupScriptHandler.cs +++ b/MultiversalDiplomacy/Script/SetupScriptHandler.cs @@ -5,7 +5,7 @@ namespace MultiversalDiplomacy.Script; /// /// A script handler for modifying a game before it begins. /// -public class SetupScriptHandler(World world, bool strict = false) : IScriptHandler +public class SetupScriptHandler(Action WriteLine, World world, bool strict = false) : IScriptHandler { public string Prompt => "setup> "; @@ -29,39 +29,39 @@ public class SetupScriptHandler(World world, bool strict = false) : IScriptHandl { 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\""); + WriteLine("commands:"); + WriteLine(" begin: complete setup and start the game (alias: ---)"); + WriteLine(" list : list things in a game category"); + WriteLine(" option : set a game option"); + WriteLine(" unit [location]: add a unit to the game"); + WriteLine(" may be \"province/location\""); break; case "begin": case "---": - Console.WriteLine("Starting game"); - Console.WriteLine("Ready for orders"); - return new GameScriptHandler(World, Strict); + WriteLine("Starting game"); + WriteLine("Ready for orders"); + return new GameScriptHandler(WriteLine, 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"); + WriteLine("usage:"); + WriteLine(" list powers: the powers in the game"); + WriteLine(" list units: units created so far"); break; case "list" when args[1] == "powers": - Console.WriteLine("Powers:"); + WriteLine("Powers:"); foreach (string powerName in World.Powers) { - Console.WriteLine($" {powerName}"); + WriteLine($" {powerName}"); } break; case "list" when args[1] == "units": - Console.WriteLine("Units:"); + WriteLine("Units:"); foreach (Unit unit in World.Units) { - Console.WriteLine($" {unit}"); + WriteLine($" {unit}"); } break; @@ -69,22 +69,22 @@ public class SetupScriptHandler(World world, bool strict = false) : IScriptHandl throw new NotImplementedException("There are no supported options yet"); case "unit" when args.Length < 2: - Console.WriteLine("usage: unit [power] [type] [province]"); + WriteLine("usage: unit [power] [type] [province]"); break; case "unit": string unitSpec = input["unit ".Length..]; if (OrderParser.TryParseUnit(World, unitSpec, out Unit? newUnit)) { World = World.Update(units: World.Units.Append(newUnit)); - Console.WriteLine($"Created {newUnit}"); + WriteLine($"Created {newUnit}"); return this; } - Console.WriteLine($"Could not match unit spec \"{unitSpec}\""); + WriteLine($"Could not match unit spec \"{unitSpec}\""); if (Strict) return null; break; default: - Console.WriteLine($"Unrecognized command: \"{command}\""); + WriteLine($"Unrecognized command: \"{command}\""); if (Strict) return null; break; } diff --git a/MultiversalDiplomacyTests/ReplTest.cs b/MultiversalDiplomacyTests/ReplTest.cs index 2460201..e70ffb1 100644 --- a/MultiversalDiplomacyTests/ReplTest.cs +++ b/MultiversalDiplomacyTests/ReplTest.cs @@ -9,7 +9,10 @@ namespace MultiversalDiplomacyTests; public class ReplTest { private static ReplDriver StandardRepl() => new( - new SetupScriptHandler(World.WithStandardMap(), strict: true)); + new SetupScriptHandler( + (msg) => {/* discard*/}, + World.WithStandardMap(), + strict: true)); [Test] public void SetupHandler() diff --git a/MultiversalDiplomacyTests/ScriptTests.cs b/MultiversalDiplomacyTests/ScriptTests.cs index c89f374..82fd2d6 100644 --- a/MultiversalDiplomacyTests/ScriptTests.cs +++ b/MultiversalDiplomacyTests/ScriptTests.cs @@ -22,7 +22,10 @@ public class ScriptTests Assert.Ignore("Script tests postponed until parsing tests are done"); string filename = Path.GetFileName(testScriptPath); int line = 0; - IScriptHandler? handler = new SetupScriptHandler(World.WithStandardMap(), strict: true); + IScriptHandler? handler = new SetupScriptHandler( + (msg) => {/* discard*/}, + World.WithStandardMap(), + strict: true); foreach (string input in File.ReadAllLines(testScriptPath)) { line++; handler = handler?.HandleInput(input);