game-village-politics-backend/app/Game/ConfigLint.php

141 lines
5.1 KiB
PHP

<?php
namespace App\Game;
use App\Game\Buildings\BuildingRules;
use App\Game\Buildings\VillageGrid;
/**
* Proves a world config has no locks (architecture.md, "No locks").
* Every check returns problems as sentences; an empty list means the config is safe.
*/
final class ConfigLint
{
/** @return list<string> */
public static function check(WorldConfig $config): array
{
$rules = new BuildingRules($config);
return [
...self::startVillageFits($config, $rules),
...self::farmsFeedTheirWorkers($config, $rules),
...self::costsFitStorage($config, $rules),
...self::requirementsReachable($config, $rules),
];
}
/** The start buildings must fit the default grid, and be allowed at all. */
private static function startVillageFits(WorldConfig $config, BuildingRules $rules): array
{
[$w, $h] = $config->get('world.village.grid');
$grid = new VillageGrid($w, $h);
$problems = [];
foreach ($config->get('world.village.start_buildings', []) as $type) {
if (! $config->hasBuilding($type)) {
$problems[] = "Start building [$type] is not a building type.";
continue;
}
[$fw, $fh] = $rules->footprint($type);
$at = $grid->freeSlot($fw, $fh);
if (! $at) {
$problems[] = "Start building [$type] does not fit the {$w}x{$h} grid.";
continue;
}
$grid->take($at['x'], $at['y'], $fw, $fh);
}
return $problems;
}
/** Capacity is never 0, and each farm level's workers eat less than it makes. */
private static function farmsFeedTheirWorkers(WorldConfig $config, BuildingRules $rules): array
{
$problems = [];
$perVillager = $config->get('economy.upkeep.villager', 1);
foreach ($config->buildingTypes() as $type) {
if (($rules->output($type, 0)['resource'] ?? null) !== 'food') {
continue;
}
for ($level = 0; $level <= $rules->maxLevel($type); $level++) {
$output = $rules->output($type, $level)['per_hour'];
if ($output <= 0 || $rules->workers($type, $level) * $perVillager >= $output) {
$problems[] = "[$type] level $level feeds fewer people than it needs as workers.";
}
}
}
return $problems;
}
/**
* Every level's cost fits one storage at max level, and storage's next level
* always fits what its current level holds.
*/
private static function costsFitStorage(WorldConfig $config, BuildingRules $rules): array
{
$storages = array_filter($config->buildingTypes(), fn (string $t) => $rules->capacity($t, 0) > 0);
if (! $storages) {
return ['No building type provides storage.'];
}
$problems = [];
$maxCap = max(array_map(fn (string $t) => $rules->capacity($t, $rules->maxLevel($t)), $storages));
foreach ($config->buildingTypes() as $type) {
for ($level = 0; $level <= $rules->maxLevel($type); $level++) {
if (max($rules->cost($type, $level)) > $maxCap) {
$problems[] = "[$type] level $level costs more than the largest storage holds.";
}
}
}
foreach ($storages as $type) {
for ($level = 0; $level < $rules->maxLevel($type); $level++) {
if (max($rules->cost($type, $level + 1)) > $rules->capacity($type, $level)) {
$problems[] = "[$type] level ".($level + 1)." costs more than level $level holds.";
}
}
}
return $problems;
}
/** Requirements name real types and levels, and have no cycles. */
private static function requirementsReachable(WorldConfig $config, BuildingRules $rules): array
{
$problems = [];
foreach ($config->buildingTypes() as $type) {
foreach ($rules->requires($type) as $needed => $level) {
if (! $config->hasBuilding($needed)) {
$problems[] = "[$type] requires unknown type [$needed].";
} elseif ($level > $rules->maxLevel($needed)) {
$problems[] = "[$type] requires [$needed] level $level, above its max level.";
}
}
}
$visiting = [];
$done = [];
$visit = function (string $type) use (&$visit, &$visiting, &$done, &$problems, $config, $rules): void {
if (isset($done[$type]) || ! $config->hasBuilding($type)) {
return;
}
if (isset($visiting[$type])) {
$problems[] = "Requirement cycle through [$type].";
return;
}
$visiting[$type] = true;
foreach (array_keys($rules->requires($type)) as $needed) {
$visit($needed);
}
unset($visiting[$type]);
$done[$type] = true;
};
foreach ($config->buildingTypes() as $type) {
$visit($type);
}
return $problems;
}
}