Replace Map lookup methods with Unit methods

This makes the flow of data easier to read and is slightly shorter in some cases. This pattern will be better for replacing more reference members with keys.
This commit is contained in:
Tim Van Baak 2024-09-09 11:58:59 -07:00
parent ddf951c17e
commit 4ce5faaac2
11 changed files with 32 additions and 31 deletions

View File

@ -94,7 +94,7 @@ public class MovementDecisions
.ToList(); .ToList();
(string province, string season) UnitPoint(Unit unit) (string province, string season) UnitPoint(Unit unit)
=> (world.Map.GetLocation(unit.Location).Province.Name, unit.Season.Key); => (unit.GetProvince(world).Name, unit.Season.Key);
(string province, string season) MovePoint(MoveOrder move) (string province, string season) MovePoint(MoveOrder move)
=> (SplitKey(move.Location).province, move.Season.Key); => (SplitKey(move.Location).province, move.Season.Key);
@ -102,7 +102,7 @@ public class MovementDecisions
foreach (UnitOrder order in relevantOrders) foreach (UnitOrder order in relevantOrders)
{ {
HoldStrength[UnitPoint(order.Unit)] = new( HoldStrength[UnitPoint(order.Unit)] = new(
world.Map.GetLocation(order.Unit.Location).Province, order.Unit.GetProvince(world),
order.Unit.Season, order.Unit.Season,
order); order);
} }
@ -110,7 +110,7 @@ public class MovementDecisions
bool IsIncoming(UnitOrder me, MoveOrder other) bool IsIncoming(UnitOrder me, MoveOrder other)
=> me != other => me != other
&& other.Season == me.Unit.Season && other.Season == me.Unit.Season
&& SplitKey(other.Location).province == world.Map.GetLocation(me.Unit).Province.Name; && SplitKey(other.Location).province == me.Unit.GetProvince(world).Name;
bool IsSupportFor(SupportMoveOrder me, MoveOrder move) bool IsSupportFor(SupportMoveOrder me, MoveOrder move)
=> me.Target.Key == move.Unit.Key => me.Target.Key == move.Unit.Key
@ -120,8 +120,8 @@ public class MovementDecisions
bool AreOpposing(MoveOrder one, MoveOrder two) bool AreOpposing(MoveOrder one, MoveOrder two)
=> one.Season == two.Unit.Season => one.Season == two.Unit.Season
&& two.Season == one.Unit.Season && two.Season == one.Unit.Season
&& SplitKey(one.Location).province == world.Map.GetLocation(two.Unit).Province.Name && SplitKey(one.Location).province == two.Unit.GetProvince(world).Name
&& SplitKey(two.Location).province == world.Map.GetLocation(one.Unit).Province.Name; && SplitKey(two.Location).province == one.Unit.GetProvince(world).Name;
bool AreCompeting(MoveOrder one, MoveOrder two) bool AreCompeting(MoveOrder one, MoveOrder two)
=> one != two => one != two
@ -174,7 +174,7 @@ public class MovementDecisions
// Ensure a hold strength decision exists for the target's province. // Ensure a hold strength decision exists for the target's province.
HoldStrength.Ensure(UnitPoint(support.Target), () => new( HoldStrength.Ensure(UnitPoint(support.Target), () => new(
world.Map.GetLocation(support.Target.Location).Province, support.Target.GetProvince(world),
support.Target.Season)); support.Target.Season));
if (support is SupportHoldOrder supportHold) if (support is SupportHoldOrder supportHold)

View File

@ -92,7 +92,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
ILookup<bool, MoveOrder> moveOrdersByAdjacency = moveOrders ILookup<bool, MoveOrder> moveOrdersByAdjacency = moveOrders
.ToLookup(order => .ToLookup(order =>
// Map adjacency // Map adjacency
world.Map.GetLocation(order.Unit).Adjacents.Select(loc => loc.Key).Contains(order.Location) order.Unit.GetLocation(world).Adjacents.Select(loc => loc.Key).Contains(order.Location)
// Turn adjacency // Turn adjacency
&& Math.Abs(order.Unit.Season.Turn - order.Season.Turn) <= 1 && Math.Abs(order.Unit.Season.Turn - order.Season.Turn) <= 1
// Timeline adjacency // Timeline adjacency
@ -177,8 +177,8 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
AdjudicatorHelpers.InvalidateIfNotMatching( AdjudicatorHelpers.InvalidateIfNotMatching(
order => order =>
// Map adjacency with respect to province // Map adjacency with respect to province
world.Map.GetLocation(order.Unit).Adjacents.Any( order.Unit.GetLocation(world).Adjacents.Any(
adjLocation => adjLocation.Province == world.Map.GetLocation(order.Target).Province) adjLocation => adjLocation.Province == order.Target.GetLocation(world).Province)
// Turn adjacency // Turn adjacency
&& Math.Abs(order.Unit.Season.Turn - order.Target.Season.Turn) <= 1 && Math.Abs(order.Unit.Season.Turn - order.Target.Season.Turn) <= 1
// Timeline adjacency // Timeline adjacency
@ -197,7 +197,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
// Support-move orders are invalid if the unit supports a move to any location in its own // Support-move orders are invalid if the unit supports a move to any location in its own
// province. // province.
AdjudicatorHelpers.InvalidateIfNotMatching( AdjudicatorHelpers.InvalidateIfNotMatching(
order => world.Map.GetLocation(order.Unit).Province != order.Province, order => order.Unit.GetProvince(world) != order.Province,
ValidationReason.NoSupportMoveAgainstSelf, ValidationReason.NoSupportMoveAgainstSelf,
ref supportMoveOrders, ref supportMoveOrders,
ref validationResults); ref validationResults);
@ -209,7 +209,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
AdjudicatorHelpers.InvalidateIfNotMatching( AdjudicatorHelpers.InvalidateIfNotMatching(
order => order =>
// Map adjacency with respect to province // Map adjacency with respect to province
world.Map.GetLocation(order.Unit).Adjacents.Any( order.Unit.GetLocation(world).Adjacents.Any(
adjLocation => adjLocation.Province == order.Province) adjLocation => adjLocation.Province == order.Province)
// Turn adjacency // Turn adjacency
&& Math.Abs(order.Unit.Season.Turn - order.Season.Turn) <= 1 && Math.Abs(order.Unit.Season.Turn - order.Season.Turn) <= 1
@ -370,7 +370,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
if (isDislodged.Outcome == false) if (isDislodged.Outcome == false)
{ {
// Non-dislodged units continue into the future. // Non-dislodged units continue into the future.
Unit next = order.Unit.Next(world.Map.GetLocation(order.Unit).Key, future); Unit next = order.Unit.Next(order.Unit.GetLocation(world).Key, future);
logger.Log(3, "Advancing unit to {0}", next); logger.Log(3, "Advancing unit to {0}", next);
createdUnits.Add(next); createdUnits.Add(next);
} }
@ -379,7 +379,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
// Create a retreat for each dislodged unit. // Create a retreat for each dislodged unit.
// TODO check valid retreats and disbands // TODO check valid retreats and disbands
logger.Log(3, "Creating retreat for {0}", order.Unit); logger.Log(3, "Creating retreat for {0}", order.Unit);
var validRetreats = world.Map.GetLocation(order.Unit).Adjacents var validRetreats = order.Unit.GetLocation(world).Adjacents
.Select(loc => (future, loc)) .Select(loc => (future, loc))
.ToList(); .ToList();
RetreatingUnit retreat = new(order.Unit, validRetreats); RetreatingUnit retreat = new(order.Unit, validRetreats);
@ -640,7 +640,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
// If the origin and destination are adjacent, then there is a path. // If the origin and destination are adjacent, then there is a path.
if (// Map adjacency if (// Map adjacency
world.Map.GetLocation(decision.Order.Unit).Adjacents.Select(loc => loc.Key).Contains(decision.Order.Location) decision.Order.Unit.GetLocation(world).Adjacents.Select(loc => loc.Key).Contains(decision.Order.Location)
// Turn adjacency // Turn adjacency
&& Math.Abs(decision.Order.Unit.Season.Turn - decision.Order.Season.Turn) <= 1 && Math.Abs(decision.Order.Unit.Season.Turn - decision.Order.Season.Turn) <= 1
// Timeline adjacency // Timeline adjacency

View File

@ -12,7 +12,7 @@ public static class PathFinder
/// Determines if a convoy path exists for a move in a convoy order. /// Determines if a convoy path exists for a move in a convoy order.
/// </summary> /// </summary>
public static bool ConvoyPathExists(World world, ConvoyOrder order) public static bool ConvoyPathExists(World world, ConvoyOrder order)
=> ConvoyPathExists(world, world.Map.GetLocation(order.Target), order.Location, order.Season); => ConvoyPathExists(world, order.Target.GetLocation(world), order.Location, order.Season);
/// <summary> /// <summary>
/// Determines if a convoy path exists for a move order. /// Determines if a convoy path exists for a move order.
@ -20,7 +20,7 @@ public static class PathFinder
public static bool ConvoyPathExists(World world, MoveOrder order) public static bool ConvoyPathExists(World world, MoveOrder order)
=> ConvoyPathExists( => ConvoyPathExists(
world, world,
world.Map.GetLocation(order.Unit), order.Unit.GetLocation(world),
world.Map.GetLocation(order.Location), world.Map.GetLocation(order.Location),
order.Season); order.Season);

View File

@ -72,9 +72,6 @@ public class Map
public Location GetLocation(string designation) public Location GetLocation(string designation)
=> LocationLookup[designation]; => LocationLookup[designation];
public Location GetLocation(Unit unit)
=> GetLocation(unit.Location);
/// <summary> /// <summary>
/// Get the sole land location of a province. /// Get the sole land location of a province.
/// </summary> /// </summary>

View File

@ -259,7 +259,7 @@ public class OrderParser(World world)
// and the location is ignored. This also satisfies DATC 4.B.5, which requires that a wrong coast for the // and the location is ignored. This also satisfies DATC 4.B.5, which requires that a wrong coast for the
// subject be ignored. // subject be ignored.
subject = world.Units.FirstOrDefault(unit subject = world.Units.FirstOrDefault(unit
=> world.Map.GetLocation(unit!.Location).ProvinceName == province.Name => unit!.GetProvince(world).Name == province.Name
&& unit!.Season.Timeline == timeline && unit!.Season.Timeline == timeline
&& unit!.Season.Turn == turn, && unit!.Season.Turn == turn,
null); null);
@ -338,7 +338,7 @@ public class OrderParser(World world)
// If the order location didn't disambiguate the coasts, either because it's missing or it's nonsense, the // If the order location didn't disambiguate the coasts, either because it's missing or it's nonsense, the
// order can be disambiguated by there being one accessible coast from the order source. // order can be disambiguated by there being one accessible coast from the order source.
if (destLocationKey is null) { if (destLocationKey is null) {
Location source = world.Map.GetLocation(subject.Location); Location source = subject.GetLocation(world);
var accessibleLocations = destProvince.Locations.Where(loc => loc.Adjacents.Contains(source)); var accessibleLocations = destProvince.Locations.Where(loc => loc.Adjacents.Contains(source));
if (accessibleLocations.Count() == 1) destLocationKey ??= accessibleLocations.Single().Key; if (accessibleLocations.Count() == 1) destLocationKey ??= accessibleLocations.Single().Key;
} }
@ -434,7 +434,7 @@ public class OrderParser(World world)
// If the order location didn't disambiguate the coasts, either because it's missing or it's nonsense, the // If the order location didn't disambiguate the coasts, either because it's missing or it's nonsense, the
// order can be disambiguated by there being one accessible coast from the order source. // order can be disambiguated by there being one accessible coast from the order source.
if (destLocationKey is null) { if (destLocationKey is null) {
Location source = world.Map.GetLocation(target.Location); Location source = target.GetLocation(world);
var accessibleLocations = destProvince.Locations.Where(loc => loc.Adjacents.Contains(source)); var accessibleLocations = destProvince.Locations.Where(loc => loc.Adjacents.Contains(source));
if (accessibleLocations.Count() == 1) destLocationKey ??= accessibleLocations.Single().Key; if (accessibleLocations.Count() == 1) destLocationKey ??= accessibleLocations.Single().Key;
} }

View File

@ -62,4 +62,8 @@ public class Unit
/// </summary> /// </summary>
public Unit Next(string location, Season season) public Unit Next(string location, Season season)
=> new(past: this.Key, location, season, this.Power, this.Type); => new(past: this.Key, location, season, this.Power, this.Type);
public Location GetLocation(World world) => world.Map.GetLocation(Location);
public Province GetProvince(World world) => GetLocation(world).Province;
} }

View File

@ -231,7 +231,7 @@ public class World
Province province = Map.GetProvince(provinceName); Province province = Map.GetProvince(provinceName);
season ??= Season.First; season ??= Season.First;
Unit? foundUnit = this.Units.SingleOrDefault( Unit? foundUnit = this.Units.SingleOrDefault(
u => Map.GetLocation(u!).Province == province && u!.Season == season, u => u!.GetProvince(this) == province && u!.Season == season,
null) null)
?? throw new KeyNotFoundException($"Unit at {province} at {season} not found"); ?? throw new KeyNotFoundException($"Unit at {province} at {season} not found");
return foundUnit; return foundUnit;

View File

@ -100,7 +100,7 @@ public class AdjudicationQueryScriptHandler(
=> val.Valid => val.Valid
&& val.Order is HoldOrder hold && val.Order is HoldOrder hold
&& hold.Unit.Season == season && hold.Unit.Season == season
&& World.Map.GetLocation(hold.Unit.Location).ProvinceName == province.Name); && hold.Unit.GetProvince(World).Name == province.Name);
if (!matchingHolds.Any()) return ScriptResult.Fail("No matching holds"); if (!matchingHolds.Any()) return ScriptResult.Fail("No matching holds");
return ScriptResult.Succeed(this); return ScriptResult.Succeed(this);
@ -129,7 +129,7 @@ public class AdjudicationQueryScriptHandler(
var matching = Validations.Where(val var matching = Validations.Where(val
=> val.Order is UnitOrder order => val.Order is UnitOrder order
&& order.Unit.Season == season && order.Unit.Season == season
&& World.Map.GetLocation(order.Unit.Location).ProvinceName == province.Name); && order.Unit.GetProvince(World).Name == province.Name);
if (!matching.Any()) return ScriptResult.Fail("No matching validations"); if (!matching.Any()) return ScriptResult.Fail("No matching validations");
if (args[0] == "order-valid" && !matching.First().Valid) { if (args[0] == "order-valid" && !matching.First().Valid) {
@ -181,7 +181,7 @@ public class AdjudicationQueryScriptHandler(
var matchingDislodges = Adjudications.Where(adj var matchingDislodges = Adjudications.Where(adj
=> adj is IsDislodged dislodge => adj is IsDislodged dislodge
&& dislodge.Order.Unit.Season == season && dislodge.Order.Unit.Season == season
&& World.Map.GetLocation(dislodge.Order.Unit.Location).ProvinceName == province.Name); && dislodge.Order.Unit.GetProvince(World).Name == province.Name);
if (!matchingDislodges.Any()) return ScriptResult.Fail("No matching dislodge decisions"); if (!matchingDislodges.Any()) return ScriptResult.Fail("No matching dislodge decisions");
var isDislodged = matchingDislodges.Cast<IsDislodged>().First(); var isDislodged = matchingDislodges.Cast<IsDislodged>().First();
@ -219,7 +219,7 @@ public class AdjudicationQueryScriptHandler(
var matchingMoves = Adjudications.Where(adj var matchingMoves = Adjudications.Where(adj
=> adj is DoesMove moves => adj is DoesMove moves
&& moves.Order.Unit.Season == season && moves.Order.Unit.Season == season
&& World.Map.GetLocation(moves.Order.Unit.Location).ProvinceName == province.Name); && moves.Order.Unit.GetProvince(World).Name == province.Name);
if (!matchingMoves.Any()) return ScriptResult.Fail("No matching movement decisions"); if (!matchingMoves.Any()) return ScriptResult.Fail("No matching movement decisions");
var doesMove = matchingMoves.Cast<DoesMove>().First(); var doesMove = matchingMoves.Cast<DoesMove>().First();
@ -257,7 +257,7 @@ public class AdjudicationQueryScriptHandler(
var matchingSupports = Adjudications.Where(adj var matchingSupports = Adjudications.Where(adj
=> adj is GivesSupport sup => adj is GivesSupport sup
&& sup.Order.Unit.Season == season && sup.Order.Unit.Season == season
&& World.Map.GetLocation(sup.Order.Unit.Location).ProvinceName == province.Name); && sup.Order.Unit.GetProvince(World).Name == province.Name);
if (!matchingSupports.Any()) return ScriptResult.Fail("No matching support decisions"); if (!matchingSupports.Any()) return ScriptResult.Fail("No matching support decisions");
var supports = matchingSupports.Cast<GivesSupport>().First(); var supports = matchingSupports.Cast<GivesSupport>().First();

View File

@ -108,7 +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 == Builder.World.Map.GetLocation(unitOrder.Unit).Province, && hold.Province == unitOrder.Unit.GetProvince(Builder.World),
_ => false, _ => false,
}).ToList(); }).ToList();
return adjudications; return adjudications;

View File

@ -263,7 +263,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
&& World.Map.GetLocation(unit).Province == location.Province && unit.GetProvince(World) == 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 => setup.World.Map.GetLocation(order.Unit).Province.Name == name; => order => order.Unit.GetProvince(setup.World).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");