Compare commits

...

4 Commits

8 changed files with 112 additions and 80 deletions

View File

@ -37,6 +37,16 @@ public class Map
.ToDictionary(location => location.Key); .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> /// <summary>
/// Get a province by name. Throws if the province is not found. /// Get a province by name. Throws if the province is not found.
/// </summary> /// </summary>

View File

@ -31,6 +31,11 @@ public class Province
public IEnumerable<Location> Locations => LocationList; public IEnumerable<Location> Locations => LocationList;
private List<Location> LocationList { get; set; } 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) public Province(string name, string[] abbreviations, bool isSupply, bool isTime)
{ {
this.Name = name; this.Name = name;

View File

@ -1,15 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using MultiversalDiplomacy.Orders;
namespace MultiversalDiplomacy.Model; namespace MultiversalDiplomacy.Model;
public class OrderRegex(World world) 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 Type = "(A|F|Army|Fleet)";
public const string Timeline = "([A-Za-z]+)"; public const string Timeline = "([A-Za-z]+)";
@ -20,9 +17,9 @@ public class OrderRegex(World world)
public const string ParenLocation = "(?:\\(([A-Za-z ]+)\\))"; 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 UnitSpec => $"(?:(?:{world.Map.PowerRegex} )?{Type} )?{FullLocation}";
public const string HoldVerb = "(h|hold|holds)"; public const string HoldVerb = "(h|hold|holds)";
@ -51,7 +48,7 @@ public class OrderRegex(World world)
match.Groups[7].Value, match.Groups[7].Value,
match.Groups[8].Value); match.Groups[8].Value);
public Regex Move => new($"^{Unit} {MoveVerb} {FullLocation}$(?: {ViaConvoy})?"); public Regex Move => new($"^{UnitSpec} {MoveVerb} {FullLocation}$(?: {ViaConvoy})?");
public static ( public static (
string power, string power,
@ -83,4 +80,59 @@ public class OrderRegex(World world)
: match.Groups[12].Value, : match.Groups[12].Value,
match.Groups[13].Value, match.Groups[13].Value,
match.Groups[14].Value); match.Groups[14].Value);
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;
}
} }

View File

@ -66,11 +66,17 @@ public class Timelines(int next, Dictionary<string, Season?> pasts)
public int Next { get; private set; } = next; public int Next { get; private set; } = next;
/// <summary> /// <summary>
/// Map of season designations to their parent seasons. /// Map of season designations to their parent seasons. Every season has an entry, so
/// The set of keys here is the set of all seasons in the multiverse. /// the set of keys is the set of existing seasons.
/// </summary> /// </summary>
public Dictionary<string, Season?> Pasts { get; } = pasts; public Dictionary<string, Season?> Pasts { get; } = pasts;
/// <summary>
/// All seasons in the multiverse.
/// </summary>
[JsonIgnore]
public IEnumerable<Season> Seasons => Pasts.Keys.Select(key => new Season(key));
/// <summary> /// <summary>
/// Create a new multiverse with an initial season. /// Create a new multiverse with an initial season.
/// </summary> /// </summary>

View File

@ -50,39 +50,31 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle
return this; return this;
} }
if (TryParseOrder(input, out Order? order)) { // If it's not a comment, submit, or order block, assume it's an order.
Console.WriteLine($"Parsed order \"{input}\" but doing anything with it isn't implemented yet"); string orderPower;
} else if (Strict) { string orderText;
return null; if (CurrentPower is not null) {
// In a block of orders from a power, the power was specified at the top and each line is an order.
orderPower = CurrentPower;
orderText = input;
} else {
// Outside a power block, the power is prefixed to each order.
Regex re = new($"^{World.Map.PowerRegex}(?:[:])? (.*)$", RegexOptions.IgnoreCase);
var match = re.Match(input);
if (!match.Success) {
Console.WriteLine($"Could not determine ordering power in \"{input}\"");
return Strict ? null : this;
}
orderPower = match.Groups[1].Value;
orderText = match.Groups[2].Value;
} }
if (OrderRegex.TryParseOrder(World, orderPower, orderText, out Order? order)) {
Console.WriteLine($"Parsed {orderPower} order \"{orderText}\" but doing anything with it isn't implemented yet");
return this; return this;
} }
private bool TryParseOrder(string input, out Order? order) { Console.WriteLine($"Failed to parse \"{orderText}\"");
order = null; return Strict ? null : this;
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;
} }
} }

View File

@ -1,10 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using MultiversalDiplomacy.Model; using MultiversalDiplomacy.Model;
using static MultiversalDiplomacy.Model.OrderRegex;
namespace MultiversalDiplomacy.Script; namespace MultiversalDiplomacy.Script;
/// <summary> /// <summary>
@ -79,12 +74,13 @@ public class SetupScriptHandler(World world, bool strict = false) : IScriptHandl
case "unit": case "unit":
string unitSpec = input["unit ".Length..]; string unitSpec = input["unit ".Length..];
if (TryParseUnit(unitSpec, out Unit? newUnit)) { if (OrderRegex.TryParseUnit(World, unitSpec, out Unit? newUnit)) {
World = World.Update(units: World.Units.Append(newUnit)); World = World.Update(units: World.Units.Append(newUnit));
Console.WriteLine($"Created {newUnit}"); Console.WriteLine($"Created {newUnit}");
} else if (Strict) { return this;
return null;
} }
Console.WriteLine($"Could not match unit spec \"{unitSpec}\"");
if (Strict) return null;
break; break;
default: default:
@ -95,34 +91,4 @@ public class SetupScriptHandler(World world, bool strict = false) : IScriptHandl
return this; return this;
} }
private bool TryParseUnit(string unitSpec, [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}\"");
return false;
}
string power = World.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;
}
} }

View File

@ -33,11 +33,11 @@ public class TimeTravelTest
// Confirm that there are now four seasons: three in the main timeline and one in a fork. // Confirm that there are now four seasons: three in the main timeline and one in a fork.
Assert.That( Assert.That(
world.Timelines.Pasts.Keys.Select(key => new Season(key)).Where(s => s.Timeline == s0.Timeline).Count(), world.Timelines.Seasons.Where(s => s.Timeline == s0.Timeline).Count(),
Is.EqualTo(3), Is.EqualTo(3),
"Failed to advance main timeline after last unit left"); "Failed to advance main timeline after last unit left");
Assert.That( Assert.That(
world.Timelines.Pasts.Keys.Select(key => new Season(key)).Where(s => s.Timeline != s0.Timeline).Count(), world.Timelines.Seasons.Where(s => s.Timeline != s0.Timeline).Count(),
Is.EqualTo(1), Is.EqualTo(1),
"Failed to fork timeline when unit moved in"); "Failed to fork timeline when unit moved in");

View File

@ -19,6 +19,7 @@ public class ScriptTests
[TestCaseSource(nameof(DatcTestCases))] [TestCaseSource(nameof(DatcTestCases))]
public void Test_DATC(string testScriptPath) public void Test_DATC(string testScriptPath)
{ {
Assert.Ignore("Script tests postponed until parsing tests are done");
string filename = Path.GetFileName(testScriptPath); string filename = Path.GetFileName(testScriptPath);
int line = 0; int line = 0;
IScriptHandler? handler = new SetupScriptHandler(World.WithStandardMap(), strict: true); IScriptHandler? handler = new SetupScriptHandler(World.WithStandardMap(), strict: true);