Refactor station modules into typed runtime models
This commit is contained in:
@@ -16,7 +16,7 @@ internal sealed class DataCatalogLoader(string dataRoot)
|
||||
var authoredSystems = Read<List<SolarSystemDefinition>>("systems.json");
|
||||
var scenario = Read<ScenarioDefinition>("scenario.json");
|
||||
var modules = NormalizeModules(Read<List<ModuleDefinition>>("modules.json"));
|
||||
var ships = Read<List<ShipDefinition>>("ships.json");
|
||||
var ships = NormalizeShips(Read<List<ShipDefinition>>("ships.json"));
|
||||
var items = NormalizeItems(Read<List<ItemDefinition>>("items.json"));
|
||||
var balance = Read<BalanceDefinition>("balance.json");
|
||||
var recipes = BuildRecipes(items, ships, modules);
|
||||
@@ -263,15 +263,57 @@ internal sealed class DataCatalogLoader(string dataRoot)
|
||||
{
|
||||
item.Type = string.IsNullOrWhiteSpace(item.Group) ? "material" : item.Group;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(item.CargoKind))
|
||||
{
|
||||
item.CargoStorageKind = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
item.CargoStorageKind = item.CargoKind.ToStorageKind();
|
||||
item.CargoKind = item.CargoStorageKind.Value.ToDataValue();
|
||||
}
|
||||
catch (ArgumentOutOfRangeException exception)
|
||||
{
|
||||
throw new InvalidOperationException($"Item '{item.Id}' has unsupported cargo kind '{item.CargoKind}'.", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
private static List<ShipDefinition> NormalizeShips(List<ShipDefinition> ships)
|
||||
{
|
||||
foreach (var ship in ships)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(ship.CargoKind))
|
||||
{
|
||||
ship.CargoStorageKind = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
ship.CargoStorageKind = ship.CargoKind.ToStorageKind();
|
||||
ship.CargoKind = ship.CargoStorageKind.Value.ToDataValue();
|
||||
}
|
||||
catch (ArgumentOutOfRangeException exception)
|
||||
{
|
||||
throw new InvalidOperationException($"Ship '{ship.Id}' has unsupported cargo kind '{ship.CargoKind}'.", exception);
|
||||
}
|
||||
}
|
||||
|
||||
return ships;
|
||||
}
|
||||
|
||||
private static List<ModuleDefinition> NormalizeModules(List<ModuleDefinition> modules)
|
||||
{
|
||||
foreach (var module in modules)
|
||||
for (var index = 0; index < modules.Count; index += 1)
|
||||
{
|
||||
var module = modules[index];
|
||||
try
|
||||
{
|
||||
module.ModuleType = module.Type.ToModuleType();
|
||||
@@ -299,10 +341,39 @@ internal sealed class DataCatalogLoader(string dataRoot)
|
||||
{
|
||||
module.WorkforceNeeded = module.WorkForce?.Max ?? 0f;
|
||||
}
|
||||
|
||||
modules[index] = CreateSpecializedModuleDefinition(module);
|
||||
}
|
||||
|
||||
return modules;
|
||||
}
|
||||
|
||||
private static ModuleDefinition CreateSpecializedModuleDefinition(ModuleDefinition module)
|
||||
{
|
||||
if (module.ModuleType == ModuleType.Storage)
|
||||
{
|
||||
if (module.Cargo is null)
|
||||
{
|
||||
throw new InvalidOperationException($"Storage module '{module.Id}' is missing cargo metadata.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return new StorageModuleDefinition(module, module.Cargo.Type.ToStorageKind(), module.Cargo.Max);
|
||||
}
|
||||
catch (ArgumentOutOfRangeException exception)
|
||||
{
|
||||
throw new InvalidOperationException($"Storage module '{module.Id}' has unsupported cargo type '{module.Cargo.Type}'.", exception);
|
||||
}
|
||||
}
|
||||
|
||||
if (module.Products.Count > 0)
|
||||
{
|
||||
return new ProductionModuleDefinition(module);
|
||||
}
|
||||
|
||||
return module;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed record ScenarioCatalog(
|
||||
|
||||
Reference in New Issue
Block a user