5dplomacy/MultiversalDiplomacy/Model/Regex.cs

86 lines
2.7 KiB
C#

using System.Text.RegularExpressions;
namespace MultiversalDiplomacy.Model;
public class OrderRegex(World world)
{
static IEnumerable<string> AllProvinceNames(Province prov) => prov.Abbreviations.Append(prov.Name);
public string Power = $"({string.Join("|", world.Powers)})";
public string Province = $"({string.Join("|", world.Provinces.SelectMany(AllProvinceNames))})";
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 ]+)\\))";
public string FullLocation => $"(?:{Timeline}-)?{Province}(?:{SlashLocation}|{ParenLocation})?(?:@{Turn})?";
public string Unit => $"(?:(?:{Power} )?{Type} )?{FullLocation}";
public const string HoldVerb = "(h|hold|holds)";
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 (
string power,
string type,
string timeline,
string province,
string location,
string turn,
string verb)
ParseHold(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);
public Regex Move => new($"^{Unit} {MoveVerb} {FullLocation}$(?: {ViaConvoy})?");
public static (
string power,
string type,
string timeline,
string province,
string location,
string turn,
string verb,
string timeline2,
string province2,
string location2,
string turn2,
string viaConvoy)
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);
}