Skip to content

Commit

Permalink
Add rest of MDATC
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliveriver committed Sep 7, 2024
1 parent 1c1d429 commit 9aa009c
Show file tree
Hide file tree
Showing 9 changed files with 829 additions and 15 deletions.
8 changes: 7 additions & 1 deletion server/Adjudication/Evaluation/RetreatEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public class RetreatEvaluator(World world, List<Order> activeOrders, AdjacencyVa

public void EvaluateRetreats()
{
var boardsWithRetreats = world.Boards.Where(b => b.Units.Any(u => u.MustRetreat));
foreach (var board in boardsWithRetreats)
{
board.MightAdvance = true;
}

EvaluateDisbands();
EvaluateRetreatMoves();
AddMissingDisbands();
Expand Down Expand Up @@ -103,7 +109,7 @@ private void EvaluateRetreatMoves()

private void AddMissingDisbands()
{
var retreatingUnits = world.ActiveBoards
var retreatingUnits = world.Boards
.SelectMany(b => b.Units)
.Where(u => u.MustRetreat);

Expand Down
2 changes: 1 addition & 1 deletion server/Adjudication/Execution/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Adjudication;
public class Executor(World world, List<Region> regions)
{
private readonly World world = world;
private readonly List<Unit> originalRetreatingUnits = world.ActiveBoards.SelectMany(b => b.Units).Where(u => u!.MustRetreat).ToList();
private readonly List<Unit> originalRetreatingUnits = world.Boards.SelectMany(b => b.Units).Where(u => u!.MustRetreat).ToList();

private readonly List<Region> regions = regions;
private readonly MapComparer mapComparer = new();
Expand Down
2 changes: 1 addition & 1 deletion server/Adjudication/Validation/ConvoyPathValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ConvoyPathValidator(List<Convoy> convoys, List<Region> regions, Adj

public List<Convoy> GetPossibleConvoys(Unit unit, Location location, Location destination)
{
if (unit.Type == UnitType.Fleet || location == destination)
if (unit.Type == UnitType.Fleet || location == destination || unit.MustRetreat)
{
return [];
}
Expand Down
28 changes: 19 additions & 9 deletions server/Adjudication/Validation/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Adjudication;
public class Validator
{
private readonly World world;
private readonly List<Hold> holds;
private readonly List<Move> moves;
private readonly List<Support> supports;
private readonly List<Convoy> convoys;
Expand All @@ -32,7 +31,6 @@ public Validator(World world, List<Region> regions, List<Centre> centres, Adjace
nonRetreats = world.Orders.Where(o => o.NeedsValidation && !o.Unit!.MustRetreat).ToList();
retreats = world.Orders.Where(o => o.NeedsValidation && o.Unit!.MustRetreat).ToList();

holds = nonRetreats.OfType<Hold>().ToList();
moves = nonRetreats.OfType<Move>().ToList();
supports = nonRetreats.OfType<Support>().ToList();
convoys = nonRetreats.OfType<Convoy>().ToList();
Expand Down Expand Up @@ -74,15 +72,25 @@ private void ValidateMoves()

move.Status = canDirectMove || canConvoyMove ? OrderStatus.New : OrderStatus.Invalid;
}

var existingMoves = world.Orders.OfType<Move>().Except(moves);
foreach (var move in existingMoves)
{
var newConvoyPath = convoyPathValidator.GetPossibleConvoys(move.Unit!, move.Location, move.Destination);
if (newConvoyPath.Count > 0)
{
move.ConvoyPath = newConvoyPath;
move.Status = OrderStatus.New;
}
}
}

private void ValidateSupports()
{
var stationaryOrders = new List<Order>();
stationaryOrders.AddRange(holds);
stationaryOrders.AddRange(supports);
stationaryOrders.AddRange(convoys);
stationaryOrders.AddRange(moves.Where(m => m.Status == OrderStatus.Invalid));
var stationaryOrders = world.Orders.Where(o =>
o is Hold or Support or Convoy
|| o is Move && o.Status == OrderStatus.Invalid);
var allMoves = world.Orders.OfType<Move>();

foreach (var support in supports)
{
Expand All @@ -91,7 +99,7 @@ private void ValidateSupports()
var hasMatchingHold = support.Midpoint == support.Destination
&& stationaryOrders.Any(o => adjacencyValidator.EqualsOrIsRelated(o.Location, support.Destination));

var hasMatchingMove = moves.Any(m =>
var hasMatchingMove = allMoves.Any(m =>
m.Location == support.Midpoint
&& adjacencyValidator.EqualsOrIsRelated(m.Destination, support.Destination)
&& m.Status != OrderStatus.Invalid);
Expand Down Expand Up @@ -121,7 +129,9 @@ private void ValidateConvoys()
continue;
}

var hasMatchingMove = moves.Any(m => m.ConvoyPath.Contains(convoy) && m.Status != OrderStatus.Invalid);
var hasMatchingMove = world.Orders
.OfType<Move>()
.Any(m => m.ConvoyPath.Contains(convoy) && m.Status != OrderStatus.Invalid);

convoy.Status = hasMatchingMove ? OrderStatus.New : OrderStatus.Invalid;
}
Expand Down
2 changes: 1 addition & 1 deletion server/Entities/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public List<Nation> LivingPlayers
=> Constants.Nations.Where(n => ActiveBoards.Any(b => b.Centres.Any(c => c.Owner == n))).ToList();

[NotMapped]
public bool HasRetreats => ActiveBoards.SelectMany(b => b.Units).Any(u => u.MustRetreat);
public bool HasRetreats => Boards.SelectMany(b => b.Units).Any(u => u.MustRetreat);
}
4 changes: 2 additions & 2 deletions server/Tests/Extensions/BoardExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests;

internal static class BoardExtensions
{
public static Build Build(this Board board, Nation owner, UnitType type, string regionId)
public static Build Build(this Board board, Nation owner, UnitType type, string regionId, OrderStatus status = OrderStatus.New)
{
var world = board.World;

Expand All @@ -21,7 +21,7 @@ public static Build Build(this Board board, Nation owner, UnitType type, string
var build = new Build
{
World = world,
Status = OrderStatus.New,
Status = status,
Location = location,
Unit = new()
{
Expand Down
Loading

0 comments on commit 9aa009c

Please sign in to comment.