Reduce Unit.Power to string

This commit is contained in:
Tim Van Baak 2024-08-15 07:37:05 -07:00
parent 2e6e6c55b8
commit bfdf2d5636
7 changed files with 21 additions and 21 deletions

View File

@ -77,13 +77,13 @@ internal static class AdjudicatorHelpers
{ {
InvalidateIfNotMatching( InvalidateIfNotMatching(
order => order switch { order => order switch {
ConvoyOrder convoy => convoy.Power == convoy.Unit.Power.Name, ConvoyOrder convoy => convoy.Power == convoy.Unit.Power,
DisbandOrder disband => disband.Power == disband.Unit.Power.Name, DisbandOrder disband => disband.Power == disband.Unit.Power,
HoldOrder hold => hold.Power == hold.Unit.Power.Name, HoldOrder hold => hold.Power == hold.Unit.Power,
MoveOrder move => move.Power == move.Unit.Power.Name, MoveOrder move => move.Power == move.Unit.Power,
RetreatOrder retreat => retreat.Power == retreat.Unit.Power.Name, RetreatOrder retreat => retreat.Power == retreat.Unit.Power,
SupportHoldOrder support => support.Power == support.Unit.Power.Name, SupportHoldOrder support => support.Power == support.Unit.Power,
SupportMoveOrder support => support.Power == support.Unit.Power.Name, SupportMoveOrder support => support.Power == support.Unit.Power,
// Any order not given to a unit, by definition, cannot be given to a unit of the // Any order not given to a unit, by definition, cannot be given to a unit of the
// wrong power // wrong power
_ => true, _ => true,

View File

@ -262,7 +262,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
.Where(unit => !orderedUnits.Contains(unit)) .Where(unit => !orderedUnits.Contains(unit))
.ToList(); .ToList();
List<HoldOrder> implicitHolds = unorderedUnits List<HoldOrder> implicitHolds = unorderedUnits
.Select(unit => new HoldOrder(unit.Power.Name, unit)) .Select(unit => new HoldOrder(unit.Power, unit))
.ToList(); .ToList();
validationResults = validationResults validationResults = validationResults
.Concat(implicitHolds.Select(o => o.Validate(ValidationReason.Valid))) .Concat(implicitHolds.Select(o => o.Validate(ValidationReason.Valid)))
@ -793,7 +793,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
// Is failing to move away // Is failing to move away
|| destMoveAway.Outcome == false)) || destMoveAway.Outcome == false))
{ {
Power destPower = destOrder.Unit.Power; string destPower = destOrder.Unit.Power;
if (decision.Order.Unit.Power == destPower) if (decision.Order.Unit.Power == destPower)
{ {
// Cannot dislodge own unit. // Cannot dislodge own unit.
@ -823,7 +823,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
// the case where it doesn't move and the attack strength is mitigated by supports not // the case where it doesn't move and the attack strength is mitigated by supports not
// helping to dislodge units of the same power as the support. The maximum tracks the // helping to dislodge units of the same power as the support. The maximum tracks the
// case where it does move and the attack strength is unmitigated. // case where it does move and the attack strength is unmitigated.
Power destPower = destMoveAway.Order.Unit.Power; string destPower = destMoveAway.Order.Unit.Power;
int min = 1; int min = 1;
int max = 1; int max = 1;
foreach (SupportMoveOrder support in decision.Supports) foreach (SupportMoveOrder support in decision.Supports)

View File

@ -25,7 +25,7 @@ public class Unit
/// <summary> /// <summary>
/// The allegiance of the unit. /// The allegiance of the unit.
/// </summary> /// </summary>
public Power Power { get; } public string Power { get; }
/// <summary> /// <summary>
/// The type of unit. /// The type of unit.
@ -38,7 +38,7 @@ public class Unit
[JsonIgnore] [JsonIgnore]
public string Key => $"{Type.ToShort()} {Season.Timeline}-{Location}@{Season.Turn}"; public string Key => $"{Type.ToShort()} {Season.Timeline}-{Location}@{Season.Turn}";
public Unit(string? past, string location, Season season, Power power, UnitType type) public Unit(string? past, string location, Season season, string power, UnitType type)
{ {
this.Past = past; this.Past = past;
this.Location = location; this.Location = location;
@ -48,13 +48,13 @@ public class Unit
} }
public override string ToString() public override string ToString()
=> $"{Power.Name[0]} {Type.ToShort()} {Season.Timeline}-{Location}@{Season.Turn}"; => $"{Power[0]} {Type.ToShort()} {Season.Timeline}-{Location}@{Season.Turn}";
/// <summary> /// <summary>
/// Create a new unit. No validation is performed; the adjudicator should only call this /// Create a new unit. No validation is performed; the adjudicator should only call this
/// method after accepting a build order. /// method after accepting a build order.
/// </summary> /// </summary>
public static Unit Build(string location, Season season, Power power, UnitType type) public static Unit Build(string location, Season season, string power, UnitType type)
=> new(past: null, location, season, power, type); => new(past: null, location, season, power, type);
/// <summary> /// <summary>

View File

@ -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, type); Unit unit = Unit.Build(location.Key, this.RootSeason, power.Name, type);
return unit; return unit;
}); });
return this.Update(units: units); return this.Update(units: units);

View File

@ -261,7 +261,7 @@ public class TestCaseBuilder
{ {
foreach (Unit unit in this.World.Units) foreach (Unit unit in this.World.Units)
{ {
if (unit.Power == power if (unit.Power == power.Name
&& World.Map.GetLocation(unit).Province == location.Province && World.Map.GetLocation(unit).Province == location.Province
&& unit.Season == season) && unit.Season == season)
{ {
@ -270,7 +270,7 @@ public class TestCaseBuilder
} }
// Not found // Not found
Unit newUnit = Unit.Build(location.Key, season, power, type); Unit newUnit = Unit.Build(location.Key, season, power.Name, 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;
} }

View File

@ -28,15 +28,15 @@ class TestCaseBuilderTest
Assert.That(setup.World.Units, Is.Not.Empty, "Expected units to be created"); Assert.That(setup.World.Units, Is.Not.Empty, "Expected units to be created");
Unit armyLON = setup.World.GetUnitAt("London"); Unit armyLON = setup.World.GetUnitAt("London");
Assert.That(armyLON.Power.Name, Is.EqualTo("England"), "Unit created with wrong power"); Assert.That(armyLON.Power, Is.EqualTo("England"), "Unit created with wrong power");
Assert.That(armyLON.Type, Is.EqualTo(UnitType.Army), "Unit created with wrong type"); Assert.That(armyLON.Type, Is.EqualTo(UnitType.Army), "Unit created with wrong type");
Unit fleetIRI = setup.World.GetUnitAt("Irish Sea"); Unit fleetIRI = setup.World.GetUnitAt("Irish Sea");
Assert.That(fleetIRI.Power.Name, Is.EqualTo("England"), "Unit created with wrong power"); Assert.That(fleetIRI.Power, Is.EqualTo("England"), "Unit created with wrong power");
Assert.That(fleetIRI.Type, Is.EqualTo(UnitType.Fleet), "Unit created with wrong type"); Assert.That(fleetIRI.Type, Is.EqualTo(UnitType.Fleet), "Unit created with wrong type");
Unit fleetSTP = setup.World.GetUnitAt("Saint Petersburg"); Unit fleetSTP = setup.World.GetUnitAt("Saint Petersburg");
Assert.That(fleetSTP.Power.Name, Is.EqualTo("Russia"), "Unit created with wrong power"); Assert.That(fleetSTP.Power, Is.EqualTo("Russia"), "Unit created with wrong power");
Assert.That(fleetSTP.Type, Is.EqualTo(UnitType.Fleet), "Unit created with wrong type"); Assert.That(fleetSTP.Type, Is.EqualTo(UnitType.Fleet), "Unit created with wrong type");
Assert.That( Assert.That(
fleetSTP.Location, fleetSTP.Location,

View File

@ -15,7 +15,7 @@ public class UnitTests
Tyr = world.Map.GetLand("Tyr"); Tyr = world.Map.GetLand("Tyr");
Power pw1 = world.Map.GetPower("Austria"); Power pw1 = world.Map.GetPower("Austria");
Season a0 = world.RootSeason; Season a0 = world.RootSeason;
Unit u1 = Unit.Build(Mun.Key, a0, pw1, UnitType.Army); Unit u1 = Unit.Build(Mun.Key, a0, pw1.Name, 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);