Compare commits

..

2 Commits

3 changed files with 72 additions and 81 deletions

View File

@ -28,6 +28,8 @@ public class OrderRegex(World world)
public const string MoveVerb = "(-|(?:->)|(?:=>)|(?:attack(?:s)?)|(?:move(?:s)?(?: to)?))";
public const string ViaConvoy = "(convoy|via convoy|by convoy)";
public Regex Hold => new($"^{Unit} {HoldVerb}$");
public static (
@ -49,7 +51,7 @@ public class OrderRegex(World world)
match.Groups[7].Value,
match.Groups[8].Value);
public Regex Move => new($"^{Unit} {MoveVerb} {FullLocation}$");
public Regex Move => new($"^{Unit} {MoveVerb} {FullLocation}$(?: {ViaConvoy})?");
public static (
string power,
@ -62,7 +64,8 @@ public class OrderRegex(World world)
string timeline2,
string province2,
string location2,
string turn2)
string turn2,
string viaConvoy)
ParseMove(Match match)
=> (match.Groups[1].Value,
match.Groups[2].Value,
@ -78,5 +81,6 @@ public class OrderRegex(World world)
match.Groups[11].Length > 1
? match.Groups[11].Value
: match.Groups[12].Value,
match.Groups[13].Value);
match.Groups[13].Value,
match.Groups[14].Value);
}

View File

@ -1,10 +1,13 @@
using System.Text.RegularExpressions;
using MultiversalDiplomacy.Model;
using MultiversalDiplomacy.Orders;
namespace MultiversalDiplomacy.Script;
public class GameScriptHandler(World world, bool strict = false) : IScriptHandler
{
public string Prompt => "game> ";
public string Prompt => "orders> ";
public World World { get; private set; } = world;
@ -22,7 +25,7 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle
return this;
}
// --- submits the orders for validation to allow for assertions about it
// "---" submits the orders for validation to allow for assertions about it
if (input == "---") {
// TODO submit orders
// TODO return a new handler that handles asserts
@ -34,10 +37,41 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle
return this;
}
// TODO parse order, including "{power} {order}" for one-offs
if (TryParseOrder(input, out Order? order)) {
Console.WriteLine($"Parsed order \"{input}\" but doing anything with it isn't implemented yet");
} else if (Strict) {
return null;
}
Console.WriteLine($"{CurrentPower}: {input}");
return this;
}
private bool TryParseOrder(string input, out Order? order) {
order = null;
OrderRegex re = new(World);
Match match;
if ((match = re.Hold.Match(input)).Success) {
var hold = OrderRegex.ParseHold(match);
string power = hold.power.Length > 0 ? hold.power : CurrentPower ?? hold.power;
Unit unit = World.Units.First(unit
=> World.Map.GetLocation(unit.Location).ProvinceName == hold.province
&& unit.Season.Timeline == (hold.timeline.Length > 0 ? hold.timeline : "a")
&& unit.Season.Turn.ToString() == (hold.turn.Length > 0 ? hold.turn : "0"));
order = new HoldOrder(power, unit);
return true;
} else if ((match = re.Move.Match(input)).Success) {
var move = OrderRegex.ParseMove(match);
Unit unit = World.Units.First(unit
=> World.Map.GetLocation(unit.Location).ProvinceName == move.province
&& unit.Season.Timeline == (move.timeline.Length > 0 ? move.timeline : "a")
&& unit.Season.Turn.ToString() == (move.turn.Length > 0 ? move.turn : "0"));
order = new MoveOrder(move.power, unit, Season.First, "l");
return true;
}
Console.WriteLine($"Failed to parse \"{input}\"");
return false;
}
}

View File

@ -1,7 +1,10 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using MultiversalDiplomacy.Model;
using static MultiversalDiplomacy.Model.OrderRegex;
namespace MultiversalDiplomacy.Script;
/// <summary>
@ -9,7 +12,7 @@ namespace MultiversalDiplomacy.Script;
/// </summary>
public class SetupScriptHandler(World world, bool strict = false) : IScriptHandler
{
public string Prompt => "5dp> ";
public string Prompt => "setup> ";
public World World { get; private set; } = world;
@ -66,18 +69,15 @@ public class SetupScriptHandler(World world, bool strict = false) : IScriptHandl
break;
case "option" when args.Length < 3:
throw new NotImplementedException();
throw new NotImplementedException("There are no supported options yet");
case "unit" when args.Length < 4:
Console.WriteLine("usage: unit [power] [type] [province] <location>");
case "unit" when args.Length < 2:
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)) {
string unitSpec = input["unit ".Length..];
if (TryParseUnit(unitSpec, out Unit? newUnit)) {
World = World.Update(units: World.Units.Append(newUnit));
Console.WriteLine($"Created {newUnit}");
} else if (Strict) {
@ -96,80 +96,33 @@ public class SetupScriptHandler(World world, bool strict = false) : IScriptHandl
return this;
}
private bool ParseUnit(
string powerArg,
string typeArg,
string provinceArg,
string? locationArg,
[NotNullWhen(true)] out Unit? newUnit)
private bool TryParseUnit(string unitSpec, [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}\"");
OrderRegex re = new(World);
Regex reUnit = new($"^{re.Power} {OrderRegex.Type} {re.Province}(?:{SlashLocation}|{ParenLocation})?$");
Match match = reUnit.Match(unitSpec);
if (!match.Success) {
Console.WriteLine($"Could not match unit spec \"{unitSpec}\"");
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());
string power = World.Powers.First(p => p.EqualsAnyCase(match.Groups[1].Value));
// 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();
string typeName = Enum.GetNames<UnitType>().First(name => name.StartsWithAnyCase(match.Groups[2].Value));
UnitType type = Enum.Parse<UnitType>(typeName);
newUnit = Unit.Build(location.Key, Season.First, powerArg, type);
Province province = World.Map.Provinces.First(prov
=> prov.Name.EqualsAnyCase(match.Groups[3].Value)
|| prov.Abbreviations.Any(abv => abv.EqualsAnyCase(match.Groups[3].Value)));
string locationName = match.Groups[4].Length > 0 ? match.Groups[4].Value : match.Groups[5].Value;
Location location = province.Locations.First(loc
=> loc.Name.StartsWithAnyCase(locationName)
|| loc.Abbreviation.StartsWithAnyCase(locationName));
newUnit = Unit.Build(location.Key, Season.First, power, type);
return true;
}
}