Skip to content

Commit

Permalink
Remove region data from database and use JSON directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliveriver committed Aug 27, 2024
1 parent ac3072e commit 26f0171
Show file tree
Hide file tree
Showing 11 changed files with 1,462 additions and 3,996 deletions.
10 changes: 6 additions & 4 deletions server/Adjudication/Adjudicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ public class Adjudicator
{
private readonly World world;

private readonly AdjacencyValidator adjacencyValidator;
private readonly Validator validator;
private readonly Evaluator evaluator;
private readonly Executor executor;

public Adjudicator(World world, bool hasStrictAdjacencies, List<Region> regions, DefaultWorldFactory defaultWorldFactory)
public Adjudicator(World world, bool hasStrictAdjacencies, MapFactory mapFactory, DefaultWorldFactory defaultWorldFactory)
{
this.world = world;

adjacencyValidator = new(regions, hasStrictAdjacencies);
validator = new(world, regions, adjacencyValidator, defaultWorldFactory);
var regions = mapFactory.CreateRegions();
var centres = defaultWorldFactory.CreateCentres();
var adjacencyValidator = new AdjacencyValidator(regions, hasStrictAdjacencies);

validator = new(world, regions, centres, adjacencyValidator);
evaluator = new(world, adjacencyValidator);
executor = new(world, regions);
}
Expand Down
12 changes: 5 additions & 7 deletions server/Adjudication/Validation/Validator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Entities;
using Enums;
using Factories;

namespace Adjudication;

Expand All @@ -15,17 +14,18 @@ public class Validator
private readonly List<Disband> disbands;
private readonly List<Order> retreats;

private readonly DefaultWorldFactory defaultWorldFactory;
private readonly List<Region> regions;
private readonly List<Centre> centres;

private readonly AdjacencyValidator adjacencyValidator;
private readonly ConvoyPathValidator convoyPathValidator;

public Validator(World world, List<Region> regions, AdjacencyValidator adjacencyValidator, DefaultWorldFactory defaultWorldFactory)
public Validator(World world, List<Region> regions, List<Centre> centres, AdjacencyValidator adjacencyValidator)
{
this.world = world;
this.regions = regions;
this.centres = centres;
this.adjacencyValidator = adjacencyValidator;
this.defaultWorldFactory = defaultWorldFactory;

var nonRetreats = world.Orders.Where(o => o.NeedsValidation && !o.Unit!.MustRetreat).ToList();
retreats = world.Orders.Where(o => o.NeedsValidation && o.Unit!.MustRetreat).ToList();
Expand Down Expand Up @@ -107,8 +107,6 @@ private void ValidateConvoys()

private void ValidateBuilds()
{
var homeCentres = defaultWorldFactory.CreateCentres();

foreach (var build in builds)
{
if (build.Location.Phase != Phase.Winter)
Expand All @@ -119,7 +117,7 @@ private void ValidateBuilds()

var board = world.Boards.FirstOrDefault(b => b.Contains(build.Location));
var region = regions.First(r => r.Id == build.Location.RegionId);
var centre = homeCentres.FirstOrDefault(c => c.Location.RegionId == build.Location.RegionId);
var centre = centres.FirstOrDefault(c => c.Location.RegionId == build.Location.RegionId);
var unit = build.Unit!;

if (board == null || centre == null)
Expand Down
43 changes: 1 addition & 42 deletions server/Context/GameContext.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
using Entities;
using Factories;
using Microsoft.EntityFrameworkCore;

namespace Context;

public class GameContext(DbContextOptions<GameContext> options, MapFactory mapFactory) : DbContext(options)
public class GameContext(DbContextOptions<GameContext> options) : DbContext(options)
{
private readonly MapFactory mapFactory = mapFactory;

public DbSet<Region> Regions { get; set; } = null!;
public DbSet<Connection> Connections { get; set; } = null!;

public DbSet<Game> Games { get; set; } = null!;
public DbSet<World> Worlds { get; set; } = null!;
public DbSet<Board> Boards { get; set; } = null!;
Expand All @@ -24,39 +18,4 @@ public class GameContext(DbContextOptions<GameContext> options, MapFactory mapFa
public DbSet<Convoy> Convoys { get; set; } = null!;
public DbSet<Build> Builds { get; set; } = null!;
public DbSet<Disband> Disbands { get; set; } = null!;

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

var (regions, connections) = mapFactory.CreateMap();

modelBuilder.Entity<Region>()
.HasData(regions);

modelBuilder.Entity<Connection>()
.HasData(connections);

modelBuilder.Entity<Region>()
.HasMany(r => r.Connections)
.WithMany(c => c.Regions)
.UsingEntity<ConnectionMapping>("ConnectionMappings")
.HasData(connections.SelectMany(connection =>
{
var regionIds = connection.Id.Split("-");
return new List<ConnectionMapping>
{
new()
{
ConnectionsId = connection.Id,
RegionsId = regionIds[0],
},
new()
{
ConnectionsId = connection.Id,
RegionsId = regionIds[1],
},
};
}));
}
};
4 changes: 2 additions & 2 deletions server/Data/regions.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
},
{
"id": "Bur",
"name": "Burgandy",
"name": "Burgundy",
"type": "Land"
},
{
Expand Down Expand Up @@ -372,7 +372,7 @@
},
{
"id": "TYS",
"name": "Tyrhennian Sea",
"name": "Tyrrhenian Sea",
"type": "Sea"
},
{
Expand Down
4 changes: 2 additions & 2 deletions server/Factories/MapFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class MapFactory
private const string RegionsFilePath = "Data/regions.json";
private const string ConnectionsFilePath = "Data/connections.json";

public (List<Region> regions, List<Connection> connections) CreateMap()
public List<Region> CreateRegions()
{
var regionsFile = File.ReadAllText(RegionsFilePath);
var regions = JsonSerializer.Deserialize<List<Region>>(regionsFile, Constants.JsonOptions)
Expand All @@ -22,7 +22,7 @@ public class MapFactory

Check(regions, connections);
PopulateConnections(regions, connections);
return (regions, connections);
return regions;
}

private void PopulateConnections(List<Region> regions, List<Connection> connections)
Expand Down
Loading

0 comments on commit 26f0171

Please sign in to comment.