Reduce Power to string

This commit is contained in:
Tim Van Baak 2024-08-15 07:54:38 -07:00
parent bfdf2d5636
commit 4b2712e4bc
6 changed files with 49 additions and 62 deletions

View File

@ -22,11 +22,11 @@ public class Map
/// <summary> /// <summary>
/// The game powers. /// The game powers.
/// </summary> /// </summary>
public IReadOnlyCollection<Power> Powers => _Powers.AsReadOnly(); public IReadOnlyCollection<string> Powers => _Powers.AsReadOnly();
private List<Power> _Powers { get; } private List<string> _Powers { get; }
private Map(MapType type, IEnumerable<Province> provinces, IEnumerable<Power> powers) private Map(MapType type, IEnumerable<Province> provinces, IEnumerable<string> powers)
{ {
Type = type; Type = type;
_Provinces = provinces.ToList(); _Provinces = provinces.ToList();
@ -84,10 +84,10 @@ public class Map
: GetLocation(provinceName, l => l.Name == coastName || l.Abbreviation == coastName); : GetLocation(provinceName, l => l.Name == coastName || l.Abbreviation == coastName);
/// <summary> /// <summary>
/// Get a power by name. Throws if there is not exactly one such power. /// Get a power by full or partial name. Throws if there is not exactly one such power.
/// </summary> /// </summary>
public Power GetPower(string powerName) public string GetPower(string powerName)
=> Powers.SingleOrDefault(p => p!.Name == powerName || p.Name.StartsWith(powerName), null) => Powers.SingleOrDefault(p => p!.EqualsAnyCase(powerName) || p!.StartsWithAnyCase(powerName), null)
?? throw new KeyNotFoundException($"Power {powerName} not found (powers: {string.Join(", ", Powers)})"); ?? throw new KeyNotFoundException($"Power {powerName} not found (powers: {string.Join(", ", Powers)})");
public static Map FromType(MapType type) public static Map FromType(MapType type)
@ -110,10 +110,7 @@ public class Map
center.AddBorder(lef.Locations.First()); center.AddBorder(lef.Locations.First());
center.AddBorder(rig.Locations.First()); center.AddBorder(rig.Locations.First());
Power a = new("Alpha"); return new(MapType.Test, [lef, cen, rig], ["Alpha", "Beta"]);
Power b = new("Beta");
return new(MapType.Test, [lef, cen, rig], [a, b]);
}); });
public static Map Classical => _Classical.Value; public static Map Classical => _Classical.Value;
@ -557,15 +554,15 @@ public class Map
Water("WES").AddBorder(Coast("SPA", "sc")); Water("WES").AddBorder(Coast("SPA", "sc"));
#endregion #endregion
List<Power> powers = List<string> powers =
[ [
new("Austria"), "Austria",
new("England"), "England",
new("France"), "France",
new("Germany"), "Germany",
new("Italy"), "Italy",
new("Russia"), "Russia",
new("Turkey"), "Turkey",
]; ];
return new(MapType.Classical, provinces, powers); return new(MapType.Classical, provinces, powers);

View File

@ -1,22 +0,0 @@
namespace MultiversalDiplomacy.Model;
/// <summary>
/// One of the rival nations vying for control of the map.
/// </summary>
public class Power
{
/// <summary>
/// The power's name.
/// </summary>
public string Name { get; }
public Power(string name)
{
this.Name = name;
}
public override string ToString()
{
return this.Name;
}
}

View File

@ -37,7 +37,7 @@ public class World
/// The game powers. /// The game powers.
/// </summary> /// </summary>
[JsonIgnore] [JsonIgnore]
public IReadOnlyCollection<Power> Powers => this.Map.Powers; public IReadOnlyCollection<string> Powers => this.Map.Powers;
/// <summary> /// <summary>
/// Lookup for seasons by designation. /// Lookup for seasons by designation.
@ -190,7 +190,7 @@ public class World
IEnumerable<Unit> units = unitSpecs.Select(spec => IEnumerable<Unit> units = unitSpecs.Select(spec =>
{ {
string[] splits = spec.Split(' ', 4); string[] splits = spec.Split(' ', 4);
Power power = Map.GetPower(splits[0]); string power = Map.GetPower(splits[0]);
UnitType type = splits[1] switch UnitType type = splits[1] switch
{ {
"A" => UnitType.Army, "A" => UnitType.Army,
@ -202,7 +202,7 @@ public class World
: splits.Length == 3 : splits.Length == 3
? Map.GetWater(splits[2]) ? Map.GetWater(splits[2])
: Map.GetWater(splits[2], splits[3]); : Map.GetWater(splits[2], splits[3]);
Unit unit = Unit.Build(location.Key, this.RootSeason, power.Name, type); Unit unit = Unit.Build(location.Key, this.RootSeason, power, type);
return unit; return unit;
}); });
return this.Update(units: units); return this.Update(units: units);

View File

@ -0,0 +1,10 @@
namespace System;
public static class StringExtensions
{
public static bool EqualsAnyCase(this string str, string? other)
=> str.Equals(other, StringComparison.InvariantCultureIgnoreCase);
public static bool StartsWithAnyCase(this string str, string other)
=> str.StartsWith(other, StringComparison.InvariantCultureIgnoreCase);
}

View File

@ -3,6 +3,7 @@ using System.Collections.ObjectModel;
using MultiversalDiplomacy.Adjudicate; using MultiversalDiplomacy.Adjudicate;
using MultiversalDiplomacy.Model; using MultiversalDiplomacy.Model;
using MultiversalDiplomacy.Orders; using MultiversalDiplomacy.Orders;
using NUnit.Framework;
namespace MultiversalDiplomacyTests; namespace MultiversalDiplomacyTests;
@ -254,14 +255,14 @@ public class TestCaseBuilder
/// of this type. /// of this type.
/// </param> /// </param>
private Unit GetOrBuildUnit( private Unit GetOrBuildUnit(
Power power, string power,
Location location, Location location,
Season season, Season season,
UnitType type) UnitType type)
{ {
foreach (Unit unit in this.World.Units) foreach (Unit unit in this.World.Units)
{ {
if (unit.Power == power.Name if (unit.Power == power
&& World.Map.GetLocation(unit).Province == location.Province && World.Map.GetLocation(unit).Province == location.Province
&& unit.Season == season) && unit.Season == season)
{ {
@ -270,7 +271,7 @@ public class TestCaseBuilder
} }
// Not found // Not found
Unit newUnit = Unit.Build(location.Key, season, power.Name, type); Unit newUnit = Unit.Build(location.Key, season, power, type);
this.World = this.World.Update(units: this.World.Units.Append(newUnit)); this.World = this.World.Update(units: this.World.Units.Append(newUnit));
return newUnit; return newUnit;
} }
@ -344,13 +345,15 @@ public class TestCaseBuilder
{ {
public TestCaseBuilder Builder; public TestCaseBuilder Builder;
public SeasonContext SeasonContext; public SeasonContext SeasonContext;
public Power Power; public string Power;
public PowerContext(SeasonContext seasonContext, Power Power) public PowerContext(SeasonContext seasonContext, string power)
{ {
Assert.That(power, Is.AnyOf([.. seasonContext.Builder.World.Map.Powers]), "Invalid power");
this.Builder = seasonContext.Builder; this.Builder = seasonContext.Builder;
this.SeasonContext = seasonContext; this.SeasonContext = seasonContext;
this.Power = Power; this.Power = power;
} }
public ISeasonContext this[(string timeline, int turn) seasonCoord] public ISeasonContext this[(string timeline, int turn) seasonCoord]
@ -361,7 +364,7 @@ public class TestCaseBuilder
public IUnitContext Army(string provinceName, string? powerName = null) public IUnitContext Army(string provinceName, string? powerName = null)
{ {
Power power = powerName == null string power = powerName == null
? this.Power ? this.Power
: this.Builder.World.Map.GetPower(powerName); : this.Builder.World.Map.GetPower(powerName);
Location location = this.Builder.World.Map.GetLand(provinceName); Location location = this.Builder.World.Map.GetLand(provinceName);
@ -372,7 +375,7 @@ public class TestCaseBuilder
public IUnitContext Fleet(string provinceName, string? coast = null, string? powerName = null) public IUnitContext Fleet(string provinceName, string? coast = null, string? powerName = null)
{ {
Power power = powerName == null string power = powerName == null
? this.Power ? this.Power
: this.Builder.World.Map.GetPower(powerName); : this.Builder.World.Map.GetPower(powerName);
Location location = this.Builder.World.Map.GetWater(provinceName, coast); Location location = this.Builder.World.Map.GetWater(provinceName, coast);
@ -402,7 +405,7 @@ public class TestCaseBuilder
public IOrderDefinedContext<HoldOrder> Holds() public IOrderDefinedContext<HoldOrder> Holds()
{ {
HoldOrder order = new HoldOrder(this.PowerContext.Power.Name, this.Unit); HoldOrder order = new HoldOrder(this.PowerContext.Power, this.Unit);
this.Builder.OrderList.Add(order); this.Builder.OrderList.Add(order);
return new OrderDefinedContext<HoldOrder>(this, order); return new OrderDefinedContext<HoldOrder>(this, order);
} }
@ -417,7 +420,7 @@ public class TestCaseBuilder
: this.Builder.World.Map.GetWater(provinceName, coast); : this.Builder.World.Map.GetWater(provinceName, coast);
Season destSeason = season ?? this.SeasonContext.Season; Season destSeason = season ?? this.SeasonContext.Season;
MoveOrder moveOrder = new MoveOrder( MoveOrder moveOrder = new MoveOrder(
this.PowerContext.Power.Name, this.PowerContext.Power,
this.Unit, this.Unit,
destSeason.Key, destSeason.Key,
destination); destination);
@ -449,7 +452,7 @@ public class TestCaseBuilder
public IConvoyDestinationContext Army(string provinceName, string? powerName = null) public IConvoyDestinationContext Army(string provinceName, string? powerName = null)
{ {
Power power = powerName == null string power = powerName == null
? this.PowerContext.Power ? this.PowerContext.Power
: this.Builder.World.Map.GetPower(powerName); : this.Builder.World.Map.GetPower(powerName);
Location location = this.Builder.World.Map.GetLand(provinceName); Location location = this.Builder.World.Map.GetLand(provinceName);
@ -463,7 +466,7 @@ public class TestCaseBuilder
string? coast = null, string? coast = null,
string? powerName = null) string? powerName = null)
{ {
Power power = powerName == null string power = powerName == null
? this.PowerContext.Power ? this.PowerContext.Power
: this.Builder.World.Map.GetPower(powerName); : this.Builder.World.Map.GetPower(powerName);
Location location = this.Builder.World.Map.GetWater(provinceName, coast); Location location = this.Builder.World.Map.GetWater(provinceName, coast);
@ -494,7 +497,7 @@ public class TestCaseBuilder
{ {
Location location = this.Builder.World.Map.GetLand(provinceName); Location location = this.Builder.World.Map.GetLand(provinceName);
ConvoyOrder order = new ConvoyOrder( ConvoyOrder order = new ConvoyOrder(
this.PowerContext.Power.Name, this.PowerContext.Power,
this.UnitContext.Unit, this.UnitContext.Unit,
this.Target, this.Target,
this.SeasonContext.Season, this.SeasonContext.Season,
@ -524,7 +527,7 @@ public class TestCaseBuilder
Season? season = null, Season? season = null,
string? powerName = null) string? powerName = null)
{ {
Power power = powerName == null string power = powerName == null
? this.PowerContext.Power ? this.PowerContext.Power
: this.Builder.World.Map.GetPower(powerName); : this.Builder.World.Map.GetPower(powerName);
Location location = this.Builder.World.Map.GetLand(provinceName); Location location = this.Builder.World.Map.GetLand(provinceName);
@ -539,7 +542,7 @@ public class TestCaseBuilder
string? coast = null, string? coast = null,
string? powerName = null) string? powerName = null)
{ {
Power power = powerName == null string power = powerName == null
? this.PowerContext.Power ? this.PowerContext.Power
: this.Builder.World.Map.GetPower(powerName); : this.Builder.World.Map.GetPower(powerName);
Location location = this.Builder.World.Map.GetWater(provinceName, coast); Location location = this.Builder.World.Map.GetWater(provinceName, coast);
@ -569,7 +572,7 @@ public class TestCaseBuilder
public IOrderDefinedContext<SupportHoldOrder> Hold() public IOrderDefinedContext<SupportHoldOrder> Hold()
{ {
SupportHoldOrder order = new SupportHoldOrder( SupportHoldOrder order = new SupportHoldOrder(
this.PowerContext.Power.Name, this.PowerContext.Power,
this.UnitContext.Unit, this.UnitContext.Unit,
this.Target); this.Target);
this.Builder.OrderList.Add(order); this.Builder.OrderList.Add(order);
@ -586,7 +589,7 @@ public class TestCaseBuilder
: this.Builder.World.Map.GetWater(provinceName, coast); : this.Builder.World.Map.GetWater(provinceName, coast);
Season targetDestSeason = season ?? this.Target.Season; Season targetDestSeason = season ?? this.Target.Season;
SupportMoveOrder order = new SupportMoveOrder( SupportMoveOrder order = new SupportMoveOrder(
this.PowerContext.Power.Name, this.PowerContext.Power,
this.UnitContext.Unit, this.UnitContext.Unit,
this.Target, this.Target,
targetDestSeason, targetDestSeason,

View File

@ -13,9 +13,8 @@ public class UnitTests
Location Mun = world.Map.GetLand("Mun"), Location Mun = world.Map.GetLand("Mun"),
Boh = world.Map.GetLand("Boh"), Boh = world.Map.GetLand("Boh"),
Tyr = world.Map.GetLand("Tyr"); Tyr = world.Map.GetLand("Tyr");
Power pw1 = world.Map.GetPower("Austria");
Season a0 = world.RootSeason; Season a0 = world.RootSeason;
Unit u1 = Unit.Build(Mun.Key, a0, pw1.Name, UnitType.Army); Unit u1 = Unit.Build(Mun.Key, a0, "Austria", UnitType.Army);
world = world.ContinueOrFork(a0, out Season a1); world = world.ContinueOrFork(a0, out Season a1);
Unit u2 = u1.Next(Boh.Key, a1); Unit u2 = u1.Next(Boh.Key, a1);