Compare commits
2 Commits
bfafb66603
...
2745d12d29
Author | SHA1 | Date |
---|---|---|
Tim Van Baak | 2745d12d29 | |
Tim Van Baak | 8e976433c8 |
|
@ -28,6 +28,8 @@ public class OrderRegex(World world)
|
||||||
|
|
||||||
public const string MoveVerb = "(-|(?:->)|(?:=>)|(?:attack(?:s)?)|(?:move(?:s)?(?: to)?))";
|
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 Regex Hold => new($"^{Unit} {HoldVerb}$");
|
||||||
|
|
||||||
public static (
|
public static (
|
||||||
|
@ -49,7 +51,7 @@ public class OrderRegex(World world)
|
||||||
match.Groups[7].Value,
|
match.Groups[7].Value,
|
||||||
match.Groups[8].Value);
|
match.Groups[8].Value);
|
||||||
|
|
||||||
public Regex Move => new($"^{Unit} {MoveVerb} {FullLocation}$");
|
public Regex Move => new($"^{Unit} {MoveVerb} {FullLocation}$(?: {ViaConvoy})?");
|
||||||
|
|
||||||
public static (
|
public static (
|
||||||
string power,
|
string power,
|
||||||
|
@ -62,7 +64,8 @@ public class OrderRegex(World world)
|
||||||
string timeline2,
|
string timeline2,
|
||||||
string province2,
|
string province2,
|
||||||
string location2,
|
string location2,
|
||||||
string turn2)
|
string turn2,
|
||||||
|
string viaConvoy)
|
||||||
ParseMove(Match match)
|
ParseMove(Match match)
|
||||||
=> (match.Groups[1].Value,
|
=> (match.Groups[1].Value,
|
||||||
match.Groups[2].Value,
|
match.Groups[2].Value,
|
||||||
|
@ -78,5 +81,6 @@ public class OrderRegex(World world)
|
||||||
match.Groups[11].Length > 1
|
match.Groups[11].Length > 1
|
||||||
? match.Groups[11].Value
|
? match.Groups[11].Value
|
||||||
: match.Groups[12].Value,
|
: match.Groups[12].Value,
|
||||||
match.Groups[13].Value);
|
match.Groups[13].Value,
|
||||||
|
match.Groups[14].Value);
|
||||||
}
|
}
|
|
@ -1,10 +1,13 @@
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
using MultiversalDiplomacy.Model;
|
using MultiversalDiplomacy.Model;
|
||||||
|
using MultiversalDiplomacy.Orders;
|
||||||
|
|
||||||
namespace MultiversalDiplomacy.Script;
|
namespace MultiversalDiplomacy.Script;
|
||||||
|
|
||||||
public class GameScriptHandler(World world, bool strict = false) : IScriptHandler
|
public class GameScriptHandler(World world, bool strict = false) : IScriptHandler
|
||||||
{
|
{
|
||||||
public string Prompt => "game> ";
|
public string Prompt => "orders> ";
|
||||||
|
|
||||||
public World World { get; private set; } = world;
|
public World World { get; private set; } = world;
|
||||||
|
|
||||||
|
@ -22,7 +25,7 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle
|
||||||
return this;
|
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 == "---") {
|
if (input == "---") {
|
||||||
// TODO submit orders
|
// TODO submit orders
|
||||||
// TODO return a new handler that handles asserts
|
// TODO return a new handler that handles asserts
|
||||||
|
@ -34,10 +37,41 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle
|
||||||
return this;
|
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}");
|
Console.WriteLine($"{CurrentPower}: {input}");
|
||||||
|
|
||||||
return this;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
using MultiversalDiplomacy.Model;
|
using MultiversalDiplomacy.Model;
|
||||||
|
|
||||||
|
using static MultiversalDiplomacy.Model.OrderRegex;
|
||||||
|
|
||||||
namespace MultiversalDiplomacy.Script;
|
namespace MultiversalDiplomacy.Script;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -9,7 +12,7 @@ namespace MultiversalDiplomacy.Script;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SetupScriptHandler(World world, bool strict = false) : IScriptHandler
|
public class SetupScriptHandler(World world, bool strict = false) : IScriptHandler
|
||||||
{
|
{
|
||||||
public string Prompt => "5dp> ";
|
public string Prompt => "setup> ";
|
||||||
|
|
||||||
public World World { get; private set; } = world;
|
public World World { get; private set; } = world;
|
||||||
|
|
||||||
|
@ -66,18 +69,15 @@ public class SetupScriptHandler(World world, bool strict = false) : IScriptHandl
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "option" when args.Length < 3:
|
case "option" when args.Length < 3:
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException("There are no supported options yet");
|
||||||
|
|
||||||
case "unit" when args.Length < 4:
|
case "unit" when args.Length < 2:
|
||||||
Console.WriteLine("usage: unit [power] [type] [province] <location>");
|
Console.WriteLine("usage: unit [power] [type] [province]</location>");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "unit":
|
case "unit":
|
||||||
string power = args[1];
|
string unitSpec = input["unit ".Length..];
|
||||||
string type = args[2];
|
if (TryParseUnit(unitSpec, out Unit? newUnit)) {
|
||||||
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));
|
World = World.Update(units: World.Units.Append(newUnit));
|
||||||
Console.WriteLine($"Created {newUnit}");
|
Console.WriteLine($"Created {newUnit}");
|
||||||
} else if (Strict) {
|
} else if (Strict) {
|
||||||
|
@ -96,80 +96,33 @@ public class SetupScriptHandler(World world, bool strict = false) : IScriptHandl
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool ParseUnit(
|
private bool TryParseUnit(string unitSpec, [NotNullWhen(true)] out Unit? newUnit)
|
||||||
string powerArg,
|
|
||||||
string typeArg,
|
|
||||||
string provinceArg,
|
|
||||||
string? locationArg,
|
|
||||||
[NotNullWhen(true)] out Unit? newUnit)
|
|
||||||
{
|
{
|
||||||
newUnit = null;
|
newUnit = null;
|
||||||
|
|
||||||
// Parse the power name by substring matching.
|
OrderRegex re = new(World);
|
||||||
var matchingPowers = World.Powers.Where(p => p.StartsWithAnyCase(powerArg));
|
Regex reUnit = new($"^{re.Power} {OrderRegex.Type} {re.Province}(?:{SlashLocation}|{ParenLocation})?$");
|
||||||
if (!matchingPowers.Any())
|
Match match = reUnit.Match(unitSpec);
|
||||||
{
|
if (!match.Success) {
|
||||||
Console.WriteLine($"No power named \"{powerArg}\"");
|
Console.WriteLine($"Could not match unit spec \"{unitSpec}\"");
|
||||||
return false;
|
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.
|
string power = World.Powers.First(p => p.EqualsAnyCase(match.Groups[1].Value));
|
||||||
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,
|
string typeName = Enum.GetNames<UnitType>().First(name => name.StartsWithAnyCase(match.Groups[2].Value));
|
||||||
// allowing location specifications separated by a forward slash, e.g. spa/nc.
|
UnitType type = Enum.Parse<UnitType>(typeName);
|
||||||
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);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue