2024-08-25 04:26:10 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2024-08-20 14:39:49 +00:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
2024-08-25 04:26:10 +00:00
|
|
|
using MultiversalDiplomacy.Orders;
|
|
|
|
|
2024-08-20 14:39:49 +00:00
|
|
|
namespace MultiversalDiplomacy.Model;
|
|
|
|
|
|
|
|
public class OrderRegex(World world)
|
|
|
|
{
|
|
|
|
public const string Type = "(A|F|Army|Fleet)";
|
|
|
|
|
|
|
|
public const string Timeline = "([A-Za-z]+)";
|
|
|
|
|
|
|
|
public const string Turn = "([0-9]+)";
|
|
|
|
|
|
|
|
public const string SlashLocation = "(?:/([A-Za-z]+))";
|
|
|
|
|
|
|
|
public const string ParenLocation = "(?:\\(([A-Za-z ]+)\\))";
|
|
|
|
|
2024-08-25 03:50:08 +00:00
|
|
|
public string FullLocation => $"(?:{Timeline}-)?{world.Map.ProvinceRegex}(?:{SlashLocation}|{ParenLocation})?(?:@{Turn})?";
|
2024-08-20 14:39:49 +00:00
|
|
|
|
2024-08-26 04:56:09 +00:00
|
|
|
public string UnitSpec => $"(?:{Type} )?{FullLocation}";
|
2024-08-20 14:39:49 +00:00
|
|
|
|
|
|
|
public const string HoldVerb = "(h|hold|holds)";
|
|
|
|
|
|
|
|
public const string MoveVerb = "(-|(?:->)|(?:=>)|(?:attack(?:s)?)|(?:move(?:s)?(?: to)?))";
|
|
|
|
|
2024-08-21 13:35:28 +00:00
|
|
|
public const string ViaConvoy = "(convoy|via convoy|by convoy)";
|
|
|
|
|
2024-08-26 04:56:09 +00:00
|
|
|
public Regex Hold => new($"^{UnitSpec} {HoldVerb}$", RegexOptions.IgnoreCase);
|
2024-08-20 14:39:49 +00:00
|
|
|
|
|
|
|
public static (
|
|
|
|
string type,
|
|
|
|
string timeline,
|
|
|
|
string province,
|
|
|
|
string location,
|
|
|
|
string turn,
|
2024-08-26 04:56:09 +00:00
|
|
|
string holdVerb)
|
|
|
|
ParseHold(Match match) => (
|
|
|
|
match.Groups[1].Value,
|
|
|
|
match.Groups[2].Value,
|
|
|
|
match.Groups[3].Value,
|
|
|
|
match.Groups[4].Length > 0
|
|
|
|
? match.Groups[4].Value
|
|
|
|
: match.Groups[5].Value,
|
|
|
|
match.Groups[6].Value,
|
|
|
|
match.Groups[7].Value);
|
|
|
|
|
|
|
|
public Regex Move => new($"^{UnitSpec} {MoveVerb} {FullLocation}(?: {ViaConvoy})?$");
|
2024-08-20 14:39:49 +00:00
|
|
|
|
|
|
|
public static (
|
|
|
|
string power,
|
|
|
|
string type,
|
|
|
|
string timeline,
|
|
|
|
string province,
|
|
|
|
string location,
|
|
|
|
string turn,
|
2024-08-26 04:56:09 +00:00
|
|
|
string moveVerb,
|
|
|
|
string destTimeline,
|
|
|
|
string destProvince,
|
|
|
|
string destLocation,
|
|
|
|
string destTurn,
|
2024-08-21 13:35:28 +00:00
|
|
|
string viaConvoy)
|
2024-08-26 04:56:09 +00:00
|
|
|
ParseMove(Match match) => (
|
|
|
|
match.Groups[1].Value,
|
|
|
|
match.Groups[2].Value,
|
|
|
|
match.Groups[3].Value,
|
|
|
|
match.Groups[4].Value,
|
|
|
|
match.Groups[5].Length > 0
|
|
|
|
? match.Groups[5].Value
|
|
|
|
: match.Groups[6].Value,
|
|
|
|
match.Groups[7].Value,
|
|
|
|
match.Groups[8].Value,
|
|
|
|
match.Groups[9].Value,
|
|
|
|
match.Groups[10].Value,
|
|
|
|
match.Groups[11].Length > 1
|
|
|
|
? match.Groups[11].Value
|
|
|
|
: match.Groups[12].Value,
|
|
|
|
match.Groups[13].Value,
|
|
|
|
match.Groups[14].Value);
|
2024-08-25 04:26:10 +00:00
|
|
|
|
|
|
|
public static bool TryParseUnit(World world, string unitSpec, [NotNullWhen(true)] out Unit? newUnit)
|
|
|
|
{
|
|
|
|
newUnit = null;
|
|
|
|
|
|
|
|
Regex reUnit = new(
|
|
|
|
$"^{world.Map.PowerRegex} {Type} {world.Map.ProvinceRegex}(?:{SlashLocation}|{ParenLocation})?$");
|
|
|
|
Match match = reUnit.Match(unitSpec);
|
|
|
|
if (!match.Success) {
|
|
|
|
Console.WriteLine($"Could not match unit spec \"{unitSpec}\"");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
string power = world.Map.Powers.First(p => p.EqualsAnyCase(match.Groups[1].Value));
|
|
|
|
|
|
|
|
string typeName = Enum.GetNames<UnitType>().First(name => name.StartsWithAnyCase(match.Groups[2].Value));
|
|
|
|
UnitType type = Enum.Parse<UnitType>(typeName);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool TryParseOrder(World world, string power, string command, out Order? order) {
|
|
|
|
order = null;
|
|
|
|
OrderRegex re = new(world);
|
|
|
|
Match match;
|
|
|
|
if ((match = re.Hold.Match(command)).Success) {
|
|
|
|
var hold = ParseHold(match);
|
|
|
|
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(command)).Success) {
|
|
|
|
var move = 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(power, unit, Season.First, "l");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2024-08-20 14:39:49 +00:00
|
|
|
}
|