-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#14]: move into helper, include bank
- Loading branch information
Showing
2 changed files
with
146 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
using CatanLib.Enums; | ||
using CatanLib.Interfaces.Components; | ||
using HexagonLib; | ||
using System.Diagnostics; | ||
|
||
namespace CatanLib.Helpers | ||
{ | ||
public static class ProductionPhase | ||
{ | ||
public static IEnumerable<ITerrainTile> GetProducingTiles(Dictionary<TileCoordinate, IHexTile> tileStore, int roll) | ||
{ | ||
return tileStore.Select(entry => entry.Value) | ||
.OfType<ITerrainTile>() | ||
.Where(tile => tile.Production.Roll == roll); | ||
} | ||
|
||
public static IEnumerable<ISettlement> GetSettlements(Dictionary<VertexCoordinate, ISettlement> vertexStore, ITerrainTile tile) | ||
{ | ||
return tile.Coordinate.Vertices() | ||
.Select(vertexCoordinate => vertexStore[vertexCoordinate]) | ||
.Where(settlement => settlement.IsSettlement || settlement.IsCity); | ||
} | ||
|
||
public static IEnumerable<ResourceType> GetTotalYield(Dictionary<VertexCoordinate, ISettlement> vertexStore, IEnumerable<ITerrainTile> tiles) | ||
{ | ||
IEnumerable<ResourceType> totalYield = Enumerable.Empty<ResourceType>(); | ||
|
||
foreach (ITerrainTile tile in tiles) | ||
{ | ||
ResourceType resource = TerrainResources.Resources[tile.Terrain]; | ||
IEnumerable<ISettlement> settlements = GetSettlements(vertexStore, tile); | ||
IEnumerable<ResourceType> tileProduction = settlements | ||
.SelectMany(settlement => settlement.IsSettlement ? new[] { resource } : new[] { resource, resource }); | ||
|
||
totalYield = totalYield.Concat(tileProduction); | ||
} | ||
|
||
return totalYield; | ||
} | ||
|
||
public static void DistributeResources(Dictionary<VertexCoordinate, ISettlement> vertexStore, IEnumerable<ITerrainTile> tiles, IBank bank) | ||
{ | ||
foreach (ITerrainTile tile in tiles) | ||
{ | ||
ResourceType resource = TerrainResources.Resources[tile.Terrain]; | ||
IEnumerable<ISettlement> settlements = GetSettlements(vertexStore, tile); | ||
|
||
foreach (ISettlement settlement in settlements) | ||
{ | ||
if (settlement.IsSettlement) | ||
{ | ||
GainResource(bank, settlement, resource); | ||
continue; | ||
} | ||
|
||
if (settlement.IsCity) | ||
{ | ||
GainResources(bank, settlement, new[] { resource, resource }); | ||
continue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static void GainResource(IBank bank, ISettlement settlement, ResourceType resource) | ||
{ | ||
bank.UseResource(resource); | ||
settlement.Belongs?.GainResource(resource); | ||
} | ||
|
||
public static void GainResources(IBank bank, ISettlement settlement, IEnumerable<ResourceType> resources) | ||
{ | ||
bank.UseResources(resources); | ||
settlement.Belongs?.GainResources(resources); | ||
} | ||
|
||
public static int GetPlayerCount(Dictionary<VertexCoordinate, ISettlement> vertexStore, IEnumerable<ITerrainTile> tiles) | ||
{ | ||
return tiles.SelectMany(tile => tile.Coordinate.Vertices()) | ||
.Select(vertexCoordinate => vertexStore[vertexCoordinate]) | ||
.Where(settlement => settlement.IsSettlement || settlement.IsCity) | ||
.Select(settlement => | ||
{ | ||
Debug.Assert(settlement.Belongs != null); | ||
return settlement.Belongs; | ||
}) | ||
.Distinct() | ||
.Count(); | ||
} | ||
|
||
public static void TryDistributeResources(Dictionary<VertexCoordinate, ISettlement> vertexStore, IEnumerable<ITerrainTile> tiles, IBank bank) | ||
{ | ||
foreach (ITerrainTile tile in tiles) | ||
{ | ||
ResourceType resource = TerrainResources.Resources[tile.Terrain]; | ||
IEnumerable<ISettlement> settlements = GetSettlements(vertexStore, tile); | ||
|
||
foreach (ISettlement settlement in settlements) | ||
{ | ||
if (settlement.IsSettlement) | ||
{ | ||
TryGainResource(bank, settlement, resource); | ||
continue; | ||
} | ||
|
||
if (settlement.IsCity) | ||
{ | ||
TryGainResources(bank, settlement, new[] { resource, resource }); | ||
continue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static void TryGainResource(IBank bank, ISettlement settlement, ResourceType resource) | ||
{ | ||
if (bank.HasResource(resource)) | ||
{ | ||
bank.UseResource(resource); | ||
settlement.Belongs?.GainResource(resource); | ||
} | ||
} | ||
|
||
public static void TryGainResources(IBank bank, ISettlement settlement, IEnumerable<ResourceType> resources) | ||
{ | ||
foreach (ResourceType resource in resources) | ||
{ | ||
TryGainResource(bank, settlement, resource); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters