5dplomacy/MultiversalDiplomacy/Model/Regex.cs

80 lines
2.5 KiB
C#
Raw Normal View History

2024-08-20 14:39:49 +00:00
using System.Text.RegularExpressions;
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-25 03:50:08 +00:00
public string Unit => $"(?:(?:{world.Map.PowerRegex} )?{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)?))";
public const string ViaConvoy = "(convoy|via convoy|by convoy)";
2024-08-20 14:39:49 +00:00
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})?");
2024-08-20 14:39:49 +00:00
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)
2024-08-20 14:39:49 +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-20 14:39:49 +00:00
}