Skip to content

Commit

Permalink
Add DATC F
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliveriver committed Sep 2, 2024
1 parent 0c9ed77 commit 0203437
Show file tree
Hide file tree
Showing 5 changed files with 1,231 additions and 13 deletions.
40 changes: 30 additions & 10 deletions server/Adjudication/Validation/ConvoyPathValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class ConvoyPathValidator(List<Convoy> convoys, List<Region> regions, Adj
private readonly List<Region> regions = regions;
private readonly AdjacencyValidator adjacencyValidator = adjacencyValidator;

public bool HasPath(Unit unit, Location location, Location destination)
public List<Convoy> GetPossibleConvoys(Unit unit, Location location, Location destination)
{
if (unit.Type == UnitType.Fleet || location == destination)
{
return false;
return [];
}

var startRegion = regions.First(r => r.Id == location.RegionId);
Expand All @@ -27,7 +27,7 @@ public bool HasPath(Unit unit, Location location, Location destination)

if (!startsOnCoast || !endsOnCoast)
{
return false;
return [];
}

var convoysInPath = convoys.Where(c =>
Expand All @@ -37,38 +37,58 @@ public bool HasPath(Unit unit, Location location, Location destination)

if (convoysInPath.Count == 0)
{
return false;
return [];
}

var depthFirstSearch = new DepthFirstSearch(convoysInPath, adjacencyValidator);
return depthFirstSearch.HasPath(unit, location, destination);
var depthFirstSearch = new DepthFirstSearch(convoysInPath, adjacencyValidator, regions);
return depthFirstSearch.GetPossibleConvoys(unit, location, destination);
}

private class DepthFirstSearch(List<Convoy> convoys, AdjacencyValidator adjacencyValidator)
private class DepthFirstSearch(List<Convoy> convoys, AdjacencyValidator adjacencyValidator, List<Region> regions)
{
private readonly List<Convoy> convoys = convoys;
private readonly AdjacencyValidator adjacencyValidator = adjacencyValidator;
private readonly List<Region> regions = regions;

private readonly List<Convoy> visitedConvoys = [];

public bool HasPath(Unit unit, Location location, Location destination)
public List<Convoy> GetPossibleConvoys(Unit unit, Location location, Location destination)
{
if (adjacencyValidator.IsValidDirectMove(unit, location, destination))
{
return true;
if (location == convoys.First().Midpoint)
{
var directConvoys = convoys.Where(c =>
adjacencyValidator.IsValidDirectMove(c.Unit!, c.Location, destination, allowDestinationChild: true));

if (directConvoys.Any())
{
return directConvoys.ToList();
}
}

return convoys.Where(c => c.Unit == unit).ToList();
}

var convoy = convoys.FirstOrDefault(c => c.Location == location);
if (convoy != null)
{
visitedConvoys.Add(convoy);

var region = regions.First(r => r.Id == convoy.Location.RegionId);
if (region.Type != RegionType.Sea)
{
return [];
}
}

var adjacentConvoys = convoys.Where(c =>
!visitedConvoys.Contains(c)
&& adjacencyValidator.IsValidDirectMove(c.Unit!, c.Location, location, allowDestinationChild: true));

return adjacentConvoys.Any(c => HasPath(c.Unit!, c.Location, destination));
var onwardConvoys = adjacentConvoys.SelectMany(c => GetPossibleConvoys(c.Unit!, c.Location, destination));

return convoy == null || !onwardConvoys.Any() ? [.. onwardConvoys] : [.. onwardConvoys, convoy];
}
}
}
6 changes: 4 additions & 2 deletions server/Adjudication/Validation/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ private void ValidateMoves()
foreach (var move in moves)
{
var canDirectMove = adjacencyValidator.IsValidDirectMove(move.Unit!, move.Location, move.Destination);
var canConvoyMove = convoyPathValidator.HasPath(move.Unit!, move.Location, move.Destination);

move.ConvoyPath = convoyPathValidator.GetPossibleConvoys(move.Unit!, move.Location, move.Destination);
var canConvoyMove = move.ConvoyPath.Count > 0;

move.Status = canDirectMove || canConvoyMove ? OrderStatus.New : OrderStatus.Invalid;
}
Expand Down Expand Up @@ -106,7 +108,7 @@ private void ValidateConvoys()
continue;
}

var hasMatchingMove = moves.Any(m => m.Location == convoy.Midpoint && m.Destination == convoy.Destination && m.Status != OrderStatus.Invalid);
var hasMatchingMove = moves.Any(m => m.ConvoyPath.Contains(convoy) && m.Status != OrderStatus.Invalid);

convoy.Status = hasMatchingMove ? OrderStatus.New : OrderStatus.Invalid;
}
Expand Down
3 changes: 3 additions & 0 deletions server/Entities/Orders/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public class Move : Order
[NotMapped]
public override List<Location> TouchedLocations => [Location, Destination];

[NotMapped]
public List<Convoy> ConvoyPath { get; set; } = [];

[NotMapped]
public Move? OpposingMove { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion server/Tests/DATC_A.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void DATC_A_5()
// Assert
englishMove.Status.Should().Be(OrderStatus.Invalid);
englishConvoy.Status.Should().Be(OrderStatus.Invalid);
englishSupport.Status.Should().Be(OrderStatus.Invalid);
englishSupport.Status.Should().Be(OrderStatus.Success);
germanMove.Status.Should().Be(OrderStatus.Success);
germanSupport.Status.Should().Be(OrderStatus.Success);

Expand Down
Loading

0 comments on commit 0203437

Please sign in to comment.