58 lines
1.6 KiB
PHP
58 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Game;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
/**
|
|
* Game values for one world: the config/game/* defaults with the world's overrides
|
|
* merged on top. The only reader of game config, so formulas never touch config().
|
|
*/
|
|
final class WorldConfig
|
|
{
|
|
/** @param array<string, mixed> $values */
|
|
private function __construct(private readonly array $values) {}
|
|
|
|
/**
|
|
* @param array<string, mixed> $defaults keyed by file: world, economy, buildings
|
|
* @param array<string, mixed> $overrides the same shape, e.g. ['world' => ['speed' => 100]]
|
|
*/
|
|
public static function make(array $defaults, array $overrides = []): self
|
|
{
|
|
return new self(array_replace_recursive($defaults, $overrides));
|
|
}
|
|
|
|
public function get(string $key, mixed $default = null): mixed
|
|
{
|
|
return Arr::get($this->values, $key, $default);
|
|
}
|
|
|
|
public function speed(): float
|
|
{
|
|
return (float) $this->get('world.speed', 1.0);
|
|
}
|
|
|
|
/** @return list<string> */
|
|
public function buildingTypes(): array
|
|
{
|
|
return array_keys($this->get('buildings', []));
|
|
}
|
|
|
|
public function hasBuilding(string $type): bool
|
|
{
|
|
return array_key_exists($type, $this->get('buildings', []));
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
public function building(string $type): array
|
|
{
|
|
return $this->get("buildings.$type") ?? throw new \InvalidArgumentException("Unknown building type [$type].");
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
public function toArray(): array
|
|
{
|
|
return $this->values;
|
|
}
|
|
}
|