Refactor away Unit.Province

This commit is contained in:
Tim Van Baak 2024-08-14 08:15:10 -07:00
parent 4df5ef84dc
commit ce25329d27
8 changed files with 23 additions and 41 deletions

View File

@ -91,19 +91,33 @@ public class MovementDecisions
.Distinct() .Distinct()
.ToList(); .ToList();
(Province province, Season season) Point(Unit unit)
=> (world.Map.GetLocation(unit.LocationId).Province, unit.Season);
// Create a hold strength decision with an associated order for every province with a unit. // Create a hold strength decision with an associated order for every province with a unit.
foreach (UnitOrder order in relevantOrders) foreach (UnitOrder order in relevantOrders)
{ {
HoldStrength[order.Unit.Point] = new(order.Unit.Point, order); HoldStrength[Point(order.Unit)] = new(Point(order.Unit), order);
} }
bool IsIncoming(UnitOrder me, MoveOrder other)
=> me != other
&& other.Season == me.Unit.Season
&& other.Province == world.Map.GetLocation(me.Unit).Province;
bool AreOpposing(MoveOrder one, MoveOrder two)
=> one.Season == two.Unit.Season
&& two.Season == one.Unit.Season
&& one.Province == world.Map.GetLocation(two.Unit).Province
&& two.Province == world.Map.GetLocation(one.Unit).Province;
// Create all other relevant decisions for each order in the affected timelines. // Create all other relevant decisions for each order in the affected timelines.
foreach (UnitOrder order in relevantOrders) foreach (UnitOrder order in relevantOrders)
{ {
// Create a dislodge decision for this unit. // Create a dislodge decision for this unit.
List<MoveOrder> incoming = relevantOrders List<MoveOrder> incoming = relevantOrders
.OfType<MoveOrder>() .OfType<MoveOrder>()
.Where(order.IsIncoming) .Where(other => IsIncoming(order, other))
.ToList(); .ToList();
IsDislodged[order.Unit] = new(order, incoming); IsDislodged[order.Unit] = new(order, incoming);
@ -118,7 +132,7 @@ public class MovementDecisions
// Determine if this move is a head-to-head battle. // Determine if this move is a head-to-head battle.
MoveOrder? opposingMove = relevantOrders MoveOrder? opposingMove = relevantOrders
.OfType<MoveOrder>() .OfType<MoveOrder>()
.FirstOrDefault(other => other!.IsOpposing(move), null); .FirstOrDefault(other => AreOpposing(move, other!), null);
// Find competing moves. // Find competing moves.
List<MoveOrder> competing = relevantOrders List<MoveOrder> competing = relevantOrders
@ -142,11 +156,11 @@ public class MovementDecisions
GivesSupport[support] = new(support, incoming); GivesSupport[support] = new(support, incoming);
// Ensure a hold strength decision exists for the target's province. // Ensure a hold strength decision exists for the target's province.
HoldStrength.Ensure(support.Target.Point, () => new(support.Target.Point)); HoldStrength.Ensure(Point(support.Target), () => new(Point(support.Target)));
if (support is SupportHoldOrder supportHold) if (support is SupportHoldOrder supportHold)
{ {
HoldStrength[support.Target.Point].Supports.Add(supportHold); HoldStrength[Point(support.Target)].Supports.Add(supportHold);
} }
else if (support is SupportMoveOrder supportMove) else if (support is SupportMoveOrder supportMove)
{ {

View File

@ -19,12 +19,6 @@ public class Unit
public string LocationId => Location.Designation; public string LocationId => Location.Designation;
/// <summary>
/// The province where the unit is.
/// </summary>
[JsonIgnore]
public Province Province => this.Location.Province;
/// <summary> /// <summary>
/// The season in time when the unit is. /// The season in time when the unit is.
/// </summary> /// </summary>
@ -40,11 +34,6 @@ public class Unit
/// </summary> /// </summary>
public UnitType Type { get; } public UnitType Type { get; }
/// <summary>
/// The unit's spatiotemporal location as a province-season tuple.
/// </summary>
public (Province province, Season season) Point => (this.Province, this.Season);
private Unit(Unit? past, Location location, Season season, Power power, UnitType type) private Unit(Unit? past, Location location, Season season, Power power, UnitType type)
{ {
this.Past = past; this.Past = past;
@ -55,9 +44,7 @@ public class Unit
} }
public override string ToString() public override string ToString()
{ => $"{Power.Name[0]} {Type.ToShort()} {Season.Timeline}-{LocationId}@{Season.Turn}";
return $"{this.Power.Name[0]} {this.Type.ToShort()} {(this.Province, this.Season).ToShort()}";
}
/// <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

View File

@ -1,4 +1,3 @@
using System.Collections.ObjectModel;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
namespace MultiversalDiplomacy.Model; namespace MultiversalDiplomacy.Model;

View File

@ -39,15 +39,6 @@ public class MoveOrder : UnitOrder
return $"{this.Unit} -> {(this.Province, this.Season).ToShort()}"; return $"{this.Unit} -> {(this.Province, this.Season).ToShort()}";
} }
/// <summary>
/// Returns whether another move order is in a head-to-head battle with this order.
/// </summary>
public bool IsOpposing(MoveOrder other)
=> this.Season == other.Unit.Season
&& other.Season == this.Unit.Season
&& this.Province == other.Unit.Province
&& other.Province == this.Unit.Province;
/// <summary> /// <summary>
/// Returns whether another move order has the same destination as this order. /// Returns whether another move order has the same destination as this order.
/// </summary> /// </summary>

View File

@ -16,12 +16,4 @@ public abstract class UnitOrder : Order
{ {
this.Unit = unit; this.Unit = unit;
} }
/// <summary>
/// Returns whether a move order is moving into this order's unit's province.
/// </summary>
public bool IsIncoming(MoveOrder other)
=> this != other
&& other.Season == this.Unit.Season
&& other.Province == this.Unit.Province;
} }

View File

@ -108,8 +108,7 @@ public abstract class OrderReference
DefendStrength defend => defend.Order == this.Order, DefendStrength defend => defend.Order == this.Order,
PreventStrength prevent => prevent.Order == this.Order, PreventStrength prevent => prevent.Order == this.Order,
HoldStrength hold => this.Order is UnitOrder unitOrder HoldStrength hold => this.Order is UnitOrder unitOrder
? hold.Province == unitOrder.Unit.Province && hold.Province == Builder.World.Map.GetLocation(unitOrder.Unit).Province,
: false,
_ => false, _ => false,
}).ToList(); }).ToList();
return adjudications; return adjudications;

View File

@ -262,7 +262,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
&& unit.Province == location.Province && World.Map.GetLocation(unit).Province == location.Province
&& unit.Season == season) && unit.Season == season)
{ {
return unit; return unit;

View File

@ -68,7 +68,7 @@ class TestCaseBuilderTest
List<UnitOrder> orders = setup.Orders.OfType<UnitOrder>().ToList(); List<UnitOrder> orders = setup.Orders.OfType<UnitOrder>().ToList();
Func<UnitOrder, bool> OrderForProvince(string name) Func<UnitOrder, bool> OrderForProvince(string name)
=> order => order.Unit.Province.Name == name; => order => setup.World.Map.GetLocation(order.Unit).Province.Name == name;
UnitOrder orderBer = orders.Single(OrderForProvince("Berlin")); UnitOrder orderBer = orders.Single(OrderForProvince("Berlin"));
Assert.That(orderBer, Is.InstanceOf<MoveOrder>(), "Unexpected order type"); Assert.That(orderBer, Is.InstanceOf<MoveOrder>(), "Unexpected order type");