Compare commits
No commits in common. "2745d12d29fb263013ae9bb33037dbc0f38260c3" and "bfafb666033d10f007ddc4b10319170ebf0214a7" have entirely different histories.
2745d12d29
...
bfafb66603
|
@ -28,8 +28,6 @@ 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 (
|
||||
|
@ -51,7 +49,7 @@ public class OrderRegex(World world)
|
|||
match.Groups[7].Value,
|
||||
match.Groups[8].Value);
|
||||
|
||||
public Regex Move => new($"^{Unit} {MoveVerb} {FullLocation}$(?: {ViaConvoy})?");
|
||||
public Regex Move => new($"^{Unit} {MoveVerb} {FullLocation}$");
|
||||
|
||||
public static (
|
||||
string power,
|
||||
|
@ -64,8 +62,7 @@ public class OrderRegex(World world)
|
|||
string timeline2,
|
||||
string province2,
|
||||
string location2,
|
||||
string turn2,
|
||||
string viaConvoy)
|
||||
string turn2)
|
||||
ParseMove(Match match)
|
||||
=> (match.Groups[1].Value,
|
||||
match.Groups[2].Value,
|
||||
|
@ -81,6 +78,5 @@ public class OrderRegex(World world)
|
|||
match.Groups[11].Length > 1
|
||||
? match.Groups[11].Value
|
||||
: match.Groups[12].Value,
|
||||
match.Groups[13].Value,
|
||||
match.Groups[14].Value);
|
||||
match.Groups[13].Value);
|
||||
}
|
|
@ -1,13 +1,10 @@
|
|||
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 => "orders> ";
|
||||
public string Prompt => "game> ";
|
||||
|
||||
public World World { get; private set; } = world;
|
||||
|
||||
|
@ -25,7 +22,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
|
||||
|
@ -37,41 +34,10 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle
|
|||
return this;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
// TODO parse order, including "{power} {order}" for one-offs
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
using MultiversalDiplomacy.Model;
|
||||
|
||||
using static MultiversalDiplomacy.Model.OrderRegex;
|
||||
|
||||
namespace MultiversalDiplomacy.Script;
|
||||
|
||||
/// <summary>
|
||||
|
@ -12,7 +9,7 @@ namespace MultiversalDiplomacy.Script;
|
|||
/// </summary>
|
||||
public class SetupScriptHandler(World world, bool strict = false) : IScriptHandler
|
||||
{
|
||||
public string Prompt => "setup> ";
|
||||
public string Prompt => "5dp> ";
|
||||
|
||||
public World World { get; private set; } = world;
|
||||
|
||||
|
@ -69,15 +66,18 @@ public class SetupScriptHandler(World world, bool strict = false) : IScriptHandl
|
|||
break;
|
||||
|
||||
case "option" when args.Length < 3:
|
||||
throw new NotImplementedException("There are no supported options yet");
|
||||
throw new NotImplementedException();
|
||||
|
||||
case "unit" when args.Length < 2:
|
||||
Console.WriteLine("usage: unit [power] [type] [province]</location>");
|
||||
case "unit" when args.Length < 4:
|
||||
Console.WriteLine("usage: unit [power] [type] [province] <location>");
|
||||
break;
|
||||
|
||||
case "unit":
|
||||
string unitSpec = input["unit ".Length..];
|
||||
if (TryParseUnit(unitSpec, out Unit? newUnit)) {
|
||||
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}");
|
||||
} else if (Strict) {
|
||||
|
@ -96,33 +96,80 @@ public class SetupScriptHandler(World world, bool strict = false) : IScriptHandl
|
|||
return this;
|
||||
}
|
||||
|
||||
private bool TryParseUnit(string unitSpec, [NotNullWhen(true)] out Unit? newUnit)
|
||||
private bool ParseUnit(
|
||||
string powerArg,
|
||||
string typeArg,
|
||||
string provinceArg,
|
||||
string? locationArg,
|
||||
[NotNullWhen(true)] out Unit? newUnit)
|
||||
{
|
||||
newUnit = null;
|
||||
|
||||
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}\"");
|
||||
// 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();
|
||||
|
||||
string power = World.Powers.First(p => p.EqualsAnyCase(match.Groups[1].Value));
|
||||
// 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 typeName = Enum.GetNames<UnitType>().First(name => name.StartsWithAnyCase(match.Groups[2].Value));
|
||||
UnitType type = Enum.Parse<UnitType>(typeName);
|
||||
// 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();
|
||||
|
||||
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);
|
||||
newUnit = Unit.Build(location.Key, Season.First, powerArg, type);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue