Compare commits
4 Commits
55dfe0ca99
...
ebeb178984
Author | SHA1 | Date |
---|---|---|
Tim Van Baak | ebeb178984 | |
Tim Van Baak | 93b106da1e | |
Tim Van Baak | 973f8ea0d7 | |
Tim Van Baak | 868138b988 |
|
@ -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>
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -1,15 +1,12 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
using MultiversalDiplomacy.Orders;
|
||||
|
||||
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 +17,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 UnitSpec => $"(?:(?:{world.Map.PowerRegex} )?{Type} )?{FullLocation}";
|
||||
|
||||
public const string HoldVerb = "(h|hold|holds)";
|
||||
|
||||
|
@ -51,7 +48,7 @@ public class OrderRegex(World world)
|
|||
match.Groups[7].Value,
|
||||
match.Groups[8].Value);
|
||||
|
||||
public Regex Move => new($"^{Unit} {MoveVerb} {FullLocation}$(?: {ViaConvoy})?");
|
||||
public Regex Move => new($"^{UnitSpec} {MoveVerb} {FullLocation}$(?: {ViaConvoy})?");
|
||||
|
||||
public static (
|
||||
string power,
|
||||
|
@ -83,4 +80,59 @@ public class OrderRegex(World world)
|
|||
: match.Groups[12].Value,
|
||||
match.Groups[13].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,11 +66,17 @@ public class Timelines(int next, Dictionary<string, Season?> pasts)
|
|||
public int Next { get; private set; } = next;
|
||||
|
||||
/// <summary>
|
||||
/// Map of season designations to their parent seasons.
|
||||
/// The set of keys here is the set of all seasons in the multiverse.
|
||||
/// Map of season designations to their parent seasons. Every season has an entry, so
|
||||
/// the set of keys is the set of existing seasons.
|
||||
/// </summary>
|
||||
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>
|
||||
/// Create a new multiverse with an initial season.
|
||||
/// </summary>
|
||||
|
|
|
@ -50,39 +50,31 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle
|
|||
return this;
|
||||
}
|
||||
|
||||
if (TryParseOrder(input, out Order? order)) {
|
||||
Console.WriteLine($"Parsed order \"{input}\" but doing anything with it isn't implemented yet");
|
||||
} else if (Strict) {
|
||||
return null;
|
||||
// If it's not a comment, submit, or order block, assume it's an order.
|
||||
string orderPower;
|
||||
string orderText;
|
||||
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;
|
||||
}
|
||||
|
||||
private bool TryParseOrder(string input, out Order? order) {
|
||||
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;
|
||||
Console.WriteLine($"Failed to parse \"{orderText}\"");
|
||||
return Strict ? null : this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
using MultiversalDiplomacy.Model;
|
||||
|
||||
using static MultiversalDiplomacy.Model.OrderRegex;
|
||||
|
||||
namespace MultiversalDiplomacy.Script;
|
||||
|
||||
/// <summary>
|
||||
|
@ -79,12 +74,13 @@ public class SetupScriptHandler(World world, bool strict = false) : IScriptHandl
|
|||
|
||||
case "unit":
|
||||
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));
|
||||
Console.WriteLine($"Created {newUnit}");
|
||||
} else if (Strict) {
|
||||
return null;
|
||||
return this;
|
||||
}
|
||||
Console.WriteLine($"Could not match unit spec \"{unitSpec}\"");
|
||||
if (Strict) return null;
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -95,34 +91,4 @@ public class SetupScriptHandler(World world, bool strict = false) : IScriptHandl
|
|||
|
||||
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.
|
||||
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),
|
||||
"Failed to advance main timeline after last unit left");
|
||||
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),
|
||||
"Failed to fork timeline when unit moved in");
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ public class ScriptTests
|
|||
[TestCaseSource(nameof(DatcTestCases))]
|
||||
public void Test_DATC(string testScriptPath)
|
||||
{
|
||||
Assert.Ignore("Script tests postponed until parsing tests are done");
|
||||
string filename = Path.GetFileName(testScriptPath);
|
||||
int line = 0;
|
||||
IScriptHandler? handler = new SetupScriptHandler(World.WithStandardMap(), strict: true);
|
||||
|
|
Loading…
Reference in New Issue