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.
}
IScriptHandler? handler = new ReplScriptHandler();
IScriptHandler? handler = new ReplScriptHandler(Console.WriteLine);
Console.Write(handler.Prompt);
foreach (string? nextInput in GetInputs())

View File

@ -4,7 +4,11 @@ using MultiversalDiplomacy.Model;
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> ";
@ -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;
}
}

View File

@ -6,7 +6,7 @@ using MultiversalDiplomacy.Orders;
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> ";
@ -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;
}
}

View File

@ -5,7 +5,7 @@ namespace MultiversalDiplomacy.Script;
/// <summary>
/// A script handler for the interactive repl.
/// </summary>
public class ReplScriptHandler : IScriptHandler
public class ReplScriptHandler(Action<string> 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 <variant>: start a new game of the given variant");
Console.WriteLine(" stab: stab");
WriteLine("Commands:");
WriteLine(" help, ?: print this message");
WriteLine(" map <variant>: 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 <variant>");
Console.WriteLine("Available variants:");
Console.WriteLine($" {string.Join(", ", Enum.GetNames<MapType>().Select(s => s.ToLowerInvariant()))}");
WriteLine("Usage:");
WriteLine(" map <variant>");
WriteLine("Available variants:");
WriteLine($" {string.Join(", ", Enum.GetNames<MapType>().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<MapType>().Select(s => s.ToLowerInvariant()))}");
WriteLine($"Unknown variant \"{mapType}\"");
WriteLine("Available variants:");
WriteLine($" {string.Join(", ", Enum.GetNames<MapType>().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;
}

View File

@ -5,7 +5,7 @@ namespace MultiversalDiplomacy.Script;
/// <summary>
/// A script handler for modifying a game before it begins.
/// </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> ";
@ -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 <type>: list things in a game category");
Console.WriteLine(" option <name> <value>: set a game option");
Console.WriteLine(" unit <power> <type> <province> [location]: add a unit to the game");
Console.WriteLine(" <province> may be \"province/location\"");
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 "---":
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]</location>");
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));
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;
}

View File

@ -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()

View File

@ -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);