Compare commits
No commits in common. "ebeb17898429ee90983f1843b49e4b2fe52c9e98" and "55dfe0ca99e9bdb0b7e8c6a7cb0433e3ce1ddea8" have entirely different histories.
ebeb178984
...
55dfe0ca99
|
@ -37,16 +37,6 @@ 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>
|
||||||
|
|
|
@ -31,11 +31,6 @@ 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;
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
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]+)";
|
||||||
|
@ -17,9 +20,9 @@ public class OrderRegex(World world)
|
||||||
|
|
||||||
public const string ParenLocation = "(?:\\(([A-Za-z ]+)\\))";
|
public const string ParenLocation = "(?:\\(([A-Za-z ]+)\\))";
|
||||||
|
|
||||||
public string FullLocation => $"(?:{Timeline}-)?{world.Map.ProvinceRegex}(?:{SlashLocation}|{ParenLocation})?(?:@{Turn})?";
|
public string FullLocation => $"(?:{Timeline}-)?{Province}(?:{SlashLocation}|{ParenLocation})?(?:@{Turn})?";
|
||||||
|
|
||||||
public string UnitSpec => $"(?:(?:{world.Map.PowerRegex} )?{Type} )?{FullLocation}";
|
public string Unit => $"(?:(?:{Power} )?{Type} )?{FullLocation}";
|
||||||
|
|
||||||
public const string HoldVerb = "(h|hold|holds)";
|
public const string HoldVerb = "(h|hold|holds)";
|
||||||
|
|
||||||
|
@ -48,7 +51,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($"^{UnitSpec} {MoveVerb} {FullLocation}$(?: {ViaConvoy})?");
|
public Regex Move => new($"^{Unit} {MoveVerb} {FullLocation}$(?: {ViaConvoy})?");
|
||||||
|
|
||||||
public static (
|
public static (
|
||||||
string power,
|
string power,
|
||||||
|
@ -80,59 +83,4 @@ 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -66,17 +66,11 @@ 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. Every season has an entry, so
|
/// Map of season designations to their parent seasons.
|
||||||
/// the set of keys is the set of existing seasons.
|
/// The set of keys here is the set of all seasons in the multiverse.
|
||||||
/// </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>
|
||||||
|
|
|
@ -50,31 +50,39 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it's not a comment, submit, or order block, assume it's an order.
|
if (TryParseOrder(input, out Order? order)) {
|
||||||
string orderPower;
|
Console.WriteLine($"Parsed order \"{input}\" but doing anything with it isn't implemented yet");
|
||||||
string orderText;
|
} else if (Strict) {
|
||||||
if (CurrentPower is not null) {
|
return 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine($"Failed to parse \"{orderText}\"");
|
private bool TryParseOrder(string input, out Order? order) {
|
||||||
return Strict ? null : this;
|
order = null;
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
|
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>
|
||||||
|
@ -74,13 +79,12 @@ public class SetupScriptHandler(World world, bool strict = false) : IScriptHandl
|
||||||
|
|
||||||
case "unit":
|
case "unit":
|
||||||
string unitSpec = input["unit ".Length..];
|
string unitSpec = input["unit ".Length..];
|
||||||
if (OrderRegex.TryParseUnit(World, unitSpec, out Unit? newUnit)) {
|
if (TryParseUnit(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}");
|
||||||
return this;
|
} else if (Strict) {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
Console.WriteLine($"Could not match unit spec \"{unitSpec}\"");
|
|
||||||
if (Strict) return null;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -91,4 +95,34 @@ 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.Seasons.Where(s => s.Timeline == s0.Timeline).Count(),
|
world.Timelines.Pasts.Keys.Select(key => new Season(key)).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.Seasons.Where(s => s.Timeline != s0.Timeline).Count(),
|
world.Timelines.Pasts.Keys.Select(key => new Season(key)).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");
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@ 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);
|
||||||
|
|
Loading…
Reference in New Issue