Compare commits
4 Commits
eaafdeb5a9
...
92506ac6ed
Author | SHA1 | Date |
---|---|---|
Tim Van Baak | 92506ac6ed | |
Tim Van Baak | 114379de59 | |
Tim Van Baak | 7ad6f3a3d3 | |
Tim Van Baak | 6bb6c0695f |
3
Makefile
3
Makefile
|
@ -8,3 +8,6 @@ tests: ## run all tests
|
||||||
|
|
||||||
test: ## name=[test name]: run a single test with logging
|
test: ## name=[test name]: run a single test with logging
|
||||||
dotnet test MultiversalDiplomacyTests -l "console;verbosity=normal" --filter $(name)
|
dotnet test MultiversalDiplomacyTests -l "console;verbosity=normal" --filter $(name)
|
||||||
|
|
||||||
|
repl: ## execute the repl
|
||||||
|
dotnet run --project MultiversalDiplomacy repl
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
using CommandLine;
|
||||||
|
|
||||||
|
namespace MultiversalDiplomacy.CommandLine;
|
||||||
|
|
||||||
|
[Verb("adjudicate", HelpText = "Adjudicate a Multiversal Diplomacy game state.")]
|
||||||
|
public class AdjudicateOptions
|
||||||
|
{
|
||||||
|
[Value(0, HelpText = "Input file describing the game state to adjudicate, or - to read from stdin.")]
|
||||||
|
public string? InputFile { get; set; }
|
||||||
|
|
||||||
|
public static void Execute(AdjudicateOptions args)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
using CommandLine;
|
||||||
|
|
||||||
|
namespace MultiversalDiplomacy.CommandLine;
|
||||||
|
|
||||||
|
[Verb("image", HelpText = "Generate an image of a game state.")]
|
||||||
|
public class ImageOptions
|
||||||
|
{
|
||||||
|
[Value(0, HelpText = "Input file describing the game state to visualize, or - to read from stdin.")]
|
||||||
|
public string? InputFile { get; set; }
|
||||||
|
|
||||||
|
public static void Execute(ImageOptions args)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,84 @@
|
||||||
|
using CommandLine;
|
||||||
|
|
||||||
|
using MultiversalDiplomacy.Script;
|
||||||
|
|
||||||
|
namespace MultiversalDiplomacy.CommandLine;
|
||||||
|
|
||||||
|
[Verb("repl", HelpText = "Begin an interactive 5dplomacy session.")]
|
||||||
|
public class ReplOptions
|
||||||
|
{
|
||||||
|
[Option('i', "input", HelpText = "Begin the repl session by executing the commands in this file.")]
|
||||||
|
public string? InputFile { get; set; }
|
||||||
|
|
||||||
|
[Option('o', "output", HelpText = "Echo the repl session to this file. Specify a directory to autogenerate a filename.")]
|
||||||
|
public string? OutputFile { get; set; }
|
||||||
|
|
||||||
|
public static void Execute(ReplOptions args)
|
||||||
|
{
|
||||||
|
IEnumerable<string>? inputFileLines = null;
|
||||||
|
if (args.InputFile is not null) {
|
||||||
|
var fullPath = Path.GetFullPath(args.InputFile);
|
||||||
|
inputFileLines = File.ReadAllLines(fullPath);
|
||||||
|
Console.WriteLine($"Reading from {fullPath}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a writer to the output file, if specified.
|
||||||
|
StreamWriter? outputWriter = null;
|
||||||
|
if (args.OutputFile is not null)
|
||||||
|
{
|
||||||
|
string fullPath = Path.GetFullPath(args.OutputFile);
|
||||||
|
string outputPath = Directory.Exists(fullPath)
|
||||||
|
? Path.Combine(fullPath, $"{DateTime.UtcNow:yyyyMMddHHmmss}.log")
|
||||||
|
: fullPath;
|
||||||
|
Console.WriteLine($"Echoing to {outputPath}");
|
||||||
|
outputWriter = File.CreateText(outputPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerable<string?> GetInputs()
|
||||||
|
{
|
||||||
|
foreach (string line in inputFileLines ?? [])
|
||||||
|
{
|
||||||
|
var trimmed = line.Trim();
|
||||||
|
// File inputs weren't echoed to the terminal so they need to be echoed here
|
||||||
|
Console.WriteLine($"{trimmed}");
|
||||||
|
yield return trimmed;
|
||||||
|
}
|
||||||
|
|
||||||
|
string? input;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
input = Console.ReadLine();
|
||||||
|
yield return input;
|
||||||
|
}
|
||||||
|
while (input is not null);
|
||||||
|
// The last null is returned because an EOF means we should quit the repl.
|
||||||
|
}
|
||||||
|
|
||||||
|
IScriptHandler? handler = new ReplScriptHandler();
|
||||||
|
|
||||||
|
Console.Write(handler.Prompt);
|
||||||
|
foreach (string? nextInput in GetInputs())
|
||||||
|
{
|
||||||
|
// Handle quitting directly.
|
||||||
|
if (nextInput is null || nextInput == "quit" || nextInput == "exit")
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
string input = nextInput.Trim();
|
||||||
|
outputWriter?.WriteLine(input);
|
||||||
|
outputWriter?.Flush();
|
||||||
|
|
||||||
|
// Delegate all other command parsing to the handler.
|
||||||
|
handler = handler.HandleInput(input);
|
||||||
|
|
||||||
|
// Quit if the handler ends processing, otherwise prompt for the next command.
|
||||||
|
if (handler is null)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Console.Write(handler.Prompt);
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("exiting");
|
||||||
|
}
|
||||||
|
}
|
|
@ -194,7 +194,7 @@ public class World
|
||||||
{
|
{
|
||||||
return this.AddUnits(
|
return this.AddUnits(
|
||||||
"Austria A Bud",
|
"Austria A Bud",
|
||||||
"Austria A Vir",
|
"Austria A Vie",
|
||||||
"Austria F Tri",
|
"Austria F Tri",
|
||||||
"England A Lvp",
|
"England A Lvp",
|
||||||
"England F Edi",
|
"England F Edi",
|
||||||
|
|
|
@ -13,4 +13,8 @@
|
||||||
</AssemblyAttribute>
|
</AssemblyAttribute>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="CommandLineParser" Version="2.9.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -1,12 +1,32 @@
|
||||||
using System;
|
using CommandLine;
|
||||||
|
|
||||||
namespace MultiversalDiplomacy
|
using MultiversalDiplomacy.CommandLine;
|
||||||
|
|
||||||
|
namespace MultiversalDiplomacy;
|
||||||
|
|
||||||
|
internal class Program
|
||||||
{
|
{
|
||||||
internal class Program
|
[Verb("stab", HelpText = "stab")]
|
||||||
|
private class StabOptions
|
||||||
{
|
{
|
||||||
static void Main(string[] args)
|
public static void Execute(StabOptions _)
|
||||||
{
|
=> Console.WriteLine("stab");
|
||||||
Console.WriteLine("stab");
|
}
|
||||||
}
|
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
var parser = Parser.Default;
|
||||||
|
var parseResult = parser.ParseArguments(
|
||||||
|
args,
|
||||||
|
typeof(AdjudicateOptions),
|
||||||
|
typeof(ImageOptions),
|
||||||
|
typeof(ReplOptions),
|
||||||
|
typeof(StabOptions));
|
||||||
|
|
||||||
|
parseResult
|
||||||
|
.WithParsed<AdjudicateOptions>(AdjudicateOptions.Execute)
|
||||||
|
.WithParsed<ImageOptions>(ImageOptions.Execute)
|
||||||
|
.WithParsed<ReplOptions>(ReplOptions.Execute)
|
||||||
|
.WithParsed<StabOptions>(StabOptions.Execute);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
namespace MultiversalDiplomacy.Script;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A handler that interprets and executes 5dp script commands. Script handlers may create additional script handlers
|
||||||
|
/// and delegate handling to them, allowing a sort of recursive parsing of script commands.
|
||||||
|
/// </summary>
|
||||||
|
public interface IScriptHandler
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// When used interactively, the prompt that should be displayed.
|
||||||
|
/// </summary>
|
||||||
|
public string Prompt { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Process a line of input.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// The handler that should handle the next line of input, or null if script handling should end.
|
||||||
|
/// </returns>
|
||||||
|
public IScriptHandler? HandleInput(string input);
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
using MultiversalDiplomacy.Model;
|
||||||
|
|
||||||
|
namespace MultiversalDiplomacy.Script;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A script handler for the interactive repl.
|
||||||
|
/// </summary>
|
||||||
|
public class ReplScriptHandler : IScriptHandler
|
||||||
|
{
|
||||||
|
public string Prompt => "5dp> ";
|
||||||
|
|
||||||
|
public IScriptHandler? HandleInput(string input)
|
||||||
|
{
|
||||||
|
var args = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
if (args.Length == 0)
|
||||||
|
{
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
var command = args[0];
|
||||||
|
switch (command)
|
||||||
|
{
|
||||||
|
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");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "stab":
|
||||||
|
Console.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()))}");
|
||||||
|
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()))}");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
World world = World.WithMap(Map.FromType(map));
|
||||||
|
Console.WriteLine($"Created a new {map} game");
|
||||||
|
return new SetupScriptHandler(world);
|
||||||
|
|
||||||
|
default:
|
||||||
|
// noop on comments that begin with #
|
||||||
|
if (!command.StartsWith('#')) {
|
||||||
|
Console.WriteLine($"Unrecognized command: {command}");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,167 @@
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
using MultiversalDiplomacy.Model;
|
||||||
|
|
||||||
|
namespace MultiversalDiplomacy.Script;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A script handler for modifying a game before it begins.
|
||||||
|
/// </summary>
|
||||||
|
public class SetupScriptHandler(World world) : IScriptHandler
|
||||||
|
{
|
||||||
|
public string Prompt => "5dp> ";
|
||||||
|
|
||||||
|
public World World { get; private set; } = world;
|
||||||
|
|
||||||
|
public IScriptHandler? HandleInput(string input)
|
||||||
|
{
|
||||||
|
var args = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
if (args.Length == 0)
|
||||||
|
{
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
var command = args[0];
|
||||||
|
switch (command)
|
||||||
|
{
|
||||||
|
case "help":
|
||||||
|
case "?":
|
||||||
|
Console.WriteLine("commands:");
|
||||||
|
Console.WriteLine(" begin: complete setup and start the game");
|
||||||
|
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\"");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "begin":
|
||||||
|
return null; // TODO
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
case "unit" when args.Length < 4:
|
||||||
|
Console.WriteLine("usage: unit [power] [type] [province] <location>");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "unit":
|
||||||
|
string power = args[1];
|
||||||
|
string type = args[2];
|
||||||
|
string province = args[3];
|
||||||
|
string? location = args.Length >= 5 ? args[4] : null;
|
||||||
|
if (ParseUnit(power, type, province, location, out Unit? newUnit)) {
|
||||||
|
World = World.Update(units: World.Units.Append(newUnit));
|
||||||
|
Console.WriteLine($"Created {newUnit}");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
// noop on comments that begin with #
|
||||||
|
if (!command.StartsWith('#')) {
|
||||||
|
Console.WriteLine($"Unrecognized command: {command}");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool ParseUnit(
|
||||||
|
string powerArg,
|
||||||
|
string typeArg,
|
||||||
|
string provinceArg,
|
||||||
|
string? locationArg,
|
||||||
|
[NotNullWhen(true)] out Unit? newUnit)
|
||||||
|
{
|
||||||
|
newUnit = null;
|
||||||
|
|
||||||
|
// Parse the power name by substring matching.
|
||||||
|
var matchingPowers = World.Powers.Where(p => p.StartsWithAnyCase(powerArg));
|
||||||
|
if (!matchingPowers.Any())
|
||||||
|
{
|
||||||
|
Console.WriteLine($"No power named \"{powerArg}\"");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (matchingPowers.Count() > 1)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Ambiguous power \"{powerArg}\" between {string.Join(", ", matchingPowers)}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
string power = matchingPowers.First();
|
||||||
|
|
||||||
|
// Parse the unit type by substring matching.
|
||||||
|
var matchingTypes = Enum.GetNames<UnitType>().Where(t => t.StartsWithAnyCase(typeArg));
|
||||||
|
if (!matchingTypes.Any())
|
||||||
|
{
|
||||||
|
Console.WriteLine($"No unit type \"{typeArg}\"");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (matchingTypes.Count() > 1)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Ambiguous unit type \"{typeArg}\" between {string.Join(", ", matchingTypes)}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
UnitType type = Enum.Parse<UnitType>(matchingTypes.First());
|
||||||
|
|
||||||
|
// Parse the province by matching the province name or one of its abbreviations,
|
||||||
|
// allowing location specifications separated by a forward slash, e.g. spa/nc.
|
||||||
|
if (provinceArg.Contains('/')) {
|
||||||
|
var split = provinceArg.Split('/', 2);
|
||||||
|
locationArg ??= split[1];
|
||||||
|
provinceArg = split[0];
|
||||||
|
}
|
||||||
|
var matchingProvs = World.Provinces.Where(pr
|
||||||
|
=> pr.Abbreviations.Any(abv => abv.EqualsAnyCase(provinceArg))
|
||||||
|
|| pr.Name.EqualsAnyCase(provinceArg));
|
||||||
|
if (!matchingProvs.Any())
|
||||||
|
{
|
||||||
|
Console.WriteLine($"No province matches \"{provinceArg}\"");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (matchingProvs.Count() > 1)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Ambiguous province \"{provinceArg}\" between {string.Join(", ", matchingProvs)}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Province province = matchingProvs.First();
|
||||||
|
Location location;
|
||||||
|
locationArg ??= type == UnitType.Army ? "land" : "water";
|
||||||
|
var matchingLocs = locationArg is null
|
||||||
|
? province.Locations.Where(loc
|
||||||
|
=> type == UnitType.Army && loc.Type == LocationType.Land
|
||||||
|
|| type == UnitType.Fleet && loc.Type == LocationType.Water)
|
||||||
|
: province.Locations.Where(loc
|
||||||
|
=> loc.Name.EqualsAnyCase(locationArg)
|
||||||
|
|| loc.Abbreviation.EqualsAnyCase(locationArg));
|
||||||
|
if (!matchingLocs.Any()) {
|
||||||
|
Console.WriteLine($"Province {province.Name} has no {locationArg} location");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
location = matchingLocs.First();
|
||||||
|
|
||||||
|
newUnit = Unit.Build(location.Key, Season.First, powerArg, type);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue