game-village-politics-backend/app/Game/Buildings/Specialisation.php

110 lines
3.4 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Game\Buildings;
use App\Game\WorldConfig;
/**
* A village's role from what fills its space (world.village.specialisation): when one
* role's buildings take at least `share` of the space both roles take together, and at
* least `min_space`, the village has that role. A resource village produces more in its
* camps; a military village trains faster and its defenders fight harder.
*/
final class Specialisation
{
private BuildingRules $rules;
public function __construct(private readonly WorldConfig $config)
{
$this->rules = new BuildingRules($config);
}
/** @return list<string> */
public function roles(): array
{
return array_keys($this->config->get('world.village.specialisation.roles', []));
}
/**
* Space each role's buildings take, from one entry per standing instance.
*
* @param iterable<string> $types
* @return array<string, int>
*/
public function space(iterable $types): array
{
$space = array_fill_keys($this->roles(), 0);
foreach ($types as $type) {
if ($role = $this->roleOf($type)) {
$space[$role] += $this->rules->space($type);
}
}
return $space;
}
/**
* The village's role, or null while no role's buildings dominate.
*
* @param iterable<string> $types one entry per standing instance
*/
public function role(iterable $types): ?string
{
$space = $this->space($types);
$total = array_sum($space);
$share = (float) $this->config->get('world.village.specialisation.share', 1);
$min = (int) $this->config->get('world.village.specialisation.min_space', 0);
foreach ($space as $role => $taken) {
if ($total > 0 && $taken >= $min && $taken / $total >= $share) {
return $role;
}
}
return null;
}
/** The role a building type counts for, if any. */
public function roleOf(string $type): ?string
{
foreach ($this->config->get('world.village.specialisation.roles', []) as $role => $values) {
if (in_array($type, $values['buildings'] ?? [], true)) {
return $role;
}
}
return null;
}
/** Extra output share of this building in a village with that role (0.2 = +20%). */
public function productionBonus(?string $role, string $type): float
{
return $role !== null && $this->roleOf($type) === $role ? $this->bonus($role, 'production') : 0.0;
}
/** Extra training speed share in a village with that role. */
public function recruitBonus(?string $role): float
{
return $role !== null ? $this->bonus($role, 'recruit_speed') : 0.0;
}
/** Extra defence share for everyone defending a village with that role. */
public function defenseBonus(?string $role): float
{
return $role !== null ? $this->bonus($role, 'defense') : 0.0;
}
/** @return array<string, float> the role's bonuses by key, for the client */
public function bonuses(string $role): array
{
$values = $this->config->get("world.village.specialisation.roles.$role", []);
unset($values['buildings']);
return array_map('floatval', $values);
}
private function bonus(string $role, string $key): float
{
return (float) $this->config->get("world.village.specialisation.roles.$role.$key", 0);
}
}