Move province and power regexes to Map

This commit is contained in:
Tim Van Baak 2024-08-25 03:50:08 +00:00
parent 973f8ea0d7
commit 93b106da1e
4 changed files with 18 additions and 10 deletions

View File

@ -37,6 +37,16 @@ public class Map
.ToDictionary(location => location.Key);
}
/// <summary>
/// A regex that matches any of the power names for this variant.
/// </summary>
public string PowerRegex => $"({string.Join("|", Powers)})";
/// <summary>
/// A regex that matches any of the province names or abbreviations for this variant.
/// </summary>
public string ProvinceRegex => $"({string.Join("|", Provinces.SelectMany(p => p.AllNames))})";
/// <summary>
/// Get a province by name. Throws if the province is not found.
/// </summary>

View File

@ -31,6 +31,11 @@ public class Province
public IEnumerable<Location> Locations => LocationList;
private List<Location> LocationList { get; set; }
/// <summary>
/// The province's name and abbreviations as a single enumeration.
/// </summary>
public IEnumerable<string> AllNames => Abbreviations.Append(Name);
public Province(string name, string[] abbreviations, bool isSupply, bool isTime)
{
this.Name = name;

View File

@ -4,12 +4,6 @@ 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]+)";
@ -20,9 +14,9 @@ public class OrderRegex(World world)
public const string ParenLocation = "(?:\\(([A-Za-z ]+)\\))";
public string FullLocation => $"(?:{Timeline}-)?{Province}(?:{SlashLocation}|{ParenLocation})?(?:@{Turn})?";
public string FullLocation => $"(?:{Timeline}-)?{world.Map.ProvinceRegex}(?:{SlashLocation}|{ParenLocation})?(?:@{Turn})?";
public string Unit => $"(?:(?:{Power} )?{Type} )?{FullLocation}";
public string Unit => $"(?:(?:{world.Map.PowerRegex} )?{Type} )?{FullLocation}";
public const string HoldVerb = "(h|hold|holds)";

View File

@ -100,8 +100,7 @@ public class SetupScriptHandler(World world, bool strict = false) : IScriptHandl
{
newUnit = null;
OrderRegex re = new(World);
Regex reUnit = new($"^{re.Power} {OrderRegex.Type} {re.Province}(?:{SlashLocation}|{ParenLocation})?$");
Regex reUnit = new($"^{World.Map.PowerRegex} {OrderRegex.Type} {World.Map.ProvinceRegex}(?:{SlashLocation}|{ParenLocation})?$");
Match match = reUnit.Match(unitSpec);
if (!match.Success) {
Console.WriteLine($"Could not match unit spec \"{unitSpec}\"");