Enable repl output to /dev/null

This commit is contained in:
Tim Van Baak 2024-08-28 21:10:41 +00:00
parent 7b890046b6
commit 9f52c78b40
7 changed files with 64 additions and 54 deletions

View File

@ -54,7 +54,7 @@ public class ReplOptions
// The last null is returned because an EOF means we should quit the repl. // 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); Console.Write(handler.Prompt);
foreach (string? nextInput in GetInputs()) foreach (string? nextInput in GetInputs())

View File

@ -4,7 +4,11 @@ using MultiversalDiplomacy.Model;
namespace MultiversalDiplomacy.Script; namespace MultiversalDiplomacy.Script;
public class AdjudicationQueryScriptHandler(World world, bool strict = false) : IScriptHandler public class AdjudicationQueryScriptHandler(
Action<string> WriteLine,
World world,
bool strict = false)
: IScriptHandler
{ {
public string Prompt => "valid> "; public string Prompt => "valid> ";
@ -27,11 +31,11 @@ public class AdjudicationQueryScriptHandler(World world, bool strict = false) :
switch (command) switch (command)
{ {
case "---": case "---":
Console.WriteLine("Ready for orders"); WriteLine("Ready for orders");
return new GameScriptHandler(World, Strict); return new GameScriptHandler(WriteLine, World, Strict);
case "assert" when args.Length == 1: case "assert" when args.Length == 1:
Console.WriteLine("Usage:"); WriteLine("Usage:");
break; break;
case "assert": case "assert":
@ -42,7 +46,7 @@ public class AdjudicationQueryScriptHandler(World world, bool strict = false) :
throw new NotImplementedException(); throw new NotImplementedException();
default: default:
Console.WriteLine($"Unrecognized command: \"{command}\""); WriteLine($"Unrecognized command: \"{command}\"");
if (Strict) return null; if (Strict) return null;
break; break;
} }
@ -90,7 +94,7 @@ public class AdjudicationQueryScriptHandler(World world, bool strict = false) :
// Assert a unit's support was cut // Assert a unit's support was cut
default: default:
Console.WriteLine($"Unknown assertion \"{args[0]}\""); WriteLine($"Unknown assertion \"{args[0]}\"");
return !Strict; return !Strict;
} }
} }

View File

@ -6,7 +6,7 @@ using MultiversalDiplomacy.Orders;
namespace MultiversalDiplomacy.Script; namespace MultiversalDiplomacy.Script;
public class GameScriptHandler(World world, bool strict = false) : IScriptHandler public class GameScriptHandler(Action<string> WriteLine, World world, bool strict = false) : IScriptHandler
{ {
public string Prompt => "orders> "; 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 // "---" submits the orders and allows queries about the outcome
if (input == "---") { if (input == "---") {
Console.WriteLine("Submitting orders for adjudication"); WriteLine("Submitting orders for adjudication");
var adjudicator = MovementPhaseAdjudicator.Instance; var adjudicator = MovementPhaseAdjudicator.Instance;
var validation = adjudicator.ValidateOrders(World, Orders); var validation = adjudicator.ValidateOrders(World, Orders);
var validOrders = validation var validOrders = validation
@ -40,13 +40,13 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle
.ToList(); .ToList();
var adjudication = adjudicator.AdjudicateOrders(World, validOrders); var adjudication = adjudicator.AdjudicateOrders(World, validOrders);
var newWorld = adjudicator.UpdateWorld(World, adjudication); 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 // "===" submits the orders and moves immediately to taking the next set of orders
// i.e. it's "---" twice // i.e. it's "---" twice
if (input == "===") { if (input == "===") {
Console.WriteLine("Submitting orders for adjudication"); WriteLine("Submitting orders for adjudication");
var adjudicator = MovementPhaseAdjudicator.Instance; var adjudicator = MovementPhaseAdjudicator.Instance;
var validation = adjudicator.ValidateOrders(World, Orders); var validation = adjudicator.ValidateOrders(World, Orders);
var validOrders = validation var validOrders = validation
@ -55,7 +55,7 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle
.ToList(); .ToList();
var adjudication = adjudicator.AdjudicateOrders(World, validOrders); var adjudication = adjudicator.AdjudicateOrders(World, validOrders);
World = adjudicator.UpdateWorld(World, adjudication); World = adjudicator.UpdateWorld(World, adjudication);
Console.WriteLine("Ready for orders"); WriteLine("Ready for orders");
return this; return this;
} }
@ -77,7 +77,7 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle
Regex re = new($"^{World.Map.PowerRegex}(?:[:])? (.*)$", RegexOptions.IgnoreCase); Regex re = new($"^{World.Map.PowerRegex}(?:[:])? (.*)$", RegexOptions.IgnoreCase);
var match = re.Match(input); var match = re.Match(input);
if (!match.Success) { if (!match.Success) {
Console.WriteLine($"Could not determine ordering power in \"{input}\""); WriteLine($"Could not determine ordering power in \"{input}\"");
return Strict ? null : this; return Strict ? null : this;
} }
orderPower = match.Groups[1].Value; 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)) { if (OrderParser.TryParseOrder(World, orderPower, orderText, out Order? order)) {
Console.WriteLine($"Parsed {orderPower} order: {order}"); WriteLine($"Parsed {orderPower} order: {order}");
Orders.Add(order); Orders.Add(order);
return this; return this;
} }
Console.WriteLine($"Failed to parse \"{orderText}\""); WriteLine($"Failed to parse \"{orderText}\"");
return Strict ? null : this; return Strict ? null : this;
} }
} }

View File

@ -5,7 +5,7 @@ namespace MultiversalDiplomacy.Script;
/// <summary> /// <summary>
/// A script handler for the interactive repl. /// A script handler for the interactive repl.
/// </summary> /// </summary>
public class ReplScriptHandler : IScriptHandler public class ReplScriptHandler(Action<string> WriteLine) : IScriptHandler
{ {
public string Prompt => "5dp> "; public string Prompt => "5dp> ";
@ -22,37 +22,37 @@ public class ReplScriptHandler : IScriptHandler
{ {
case "help": case "help":
case "?": case "?":
Console.WriteLine("Commands:"); WriteLine("Commands:");
Console.WriteLine(" help, ?: print this message"); WriteLine(" help, ?: print this message");
Console.WriteLine(" map <variant>: start a new game of the given variant"); WriteLine(" map <variant>: start a new game of the given variant");
Console.WriteLine(" stab: stab"); WriteLine(" stab: stab");
break; break;
case "stab": case "stab":
Console.WriteLine("stab"); WriteLine("stab");
break; break;
case "map" when args.Length == 1: case "map" when args.Length == 1:
Console.WriteLine("Usage:"); WriteLine("Usage:");
Console.WriteLine(" map <variant>"); WriteLine(" map <variant>");
Console.WriteLine("Available variants:"); WriteLine("Available variants:");
Console.WriteLine($" {string.Join(", ", Enum.GetNames<MapType>().Select(s => s.ToLowerInvariant()))}"); WriteLine($" {string.Join(", ", Enum.GetNames<MapType>().Select(s => s.ToLowerInvariant()))}");
break; break;
case "map" when args.Length > 1: case "map" when args.Length > 1:
string mapType = args[1].Trim(); string mapType = args[1].Trim();
if (!Enum.TryParse(mapType, ignoreCase: true, out MapType map)) { if (!Enum.TryParse(mapType, ignoreCase: true, out MapType map)) {
Console.WriteLine($"Unknown variant \"{mapType}\""); WriteLine($"Unknown variant \"{mapType}\"");
Console.WriteLine("Available variants:"); WriteLine("Available variants:");
Console.WriteLine($" {string.Join(", ", Enum.GetNames<MapType>().Select(s => s.ToLowerInvariant()))}"); WriteLine($" {string.Join(", ", Enum.GetNames<MapType>().Select(s => s.ToLowerInvariant()))}");
break; break;
} }
World world = World.WithMap(Map.FromType(map)); World world = World.WithMap(Map.FromType(map));
Console.WriteLine($"Created a new {map} game"); WriteLine($"Created a new {map} game");
return new SetupScriptHandler(world); return new SetupScriptHandler(WriteLine, world);
default: default:
Console.WriteLine($"Unrecognized command: \"{command}\""); WriteLine($"Unrecognized command: \"{command}\"");
break; break;
} }

View File

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

View File

@ -9,7 +9,10 @@ namespace MultiversalDiplomacyTests;
public class ReplTest public class ReplTest
{ {
private static ReplDriver StandardRepl() => new( private static ReplDriver StandardRepl() => new(
new SetupScriptHandler(World.WithStandardMap(), strict: true)); new SetupScriptHandler(
(msg) => {/* discard*/},
World.WithStandardMap(),
strict: true));
[Test] [Test]
public void SetupHandler() public void SetupHandler()

View File

@ -22,7 +22,10 @@ public class ScriptTests
Assert.Ignore("Script tests postponed until parsing tests are done"); Assert.Ignore("Script tests postponed until parsing tests are done");
string filename = Path.GetFileName(testScriptPath); string filename = Path.GetFileName(testScriptPath);
int line = 0; 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)) { foreach (string input in File.ReadAllLines(testScriptPath)) {
line++; line++;
handler = handler?.HandleInput(input); handler = handler?.HandleInput(input);