game-village-politics-backend/app/Game/Economy/Workforce.php

46 lines
1.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Game\Economy;
/**
* Who works where in a village, as computed by Population::workforce(). The budget and
* the upkeep are heads (food per hour at world speed 1), not the food rate.
*/
final class Workforce
{
/**
* @param array<int, int> $assigned building id => villagers working there
* @param array<int, int> $required building id => villagers it needs
*/
public function __construct(
/** Heads the farms feed, nominally. */
public readonly float $foodBudget,
public readonly int $villagers,
/** Heads that eat: villagers, soldiers, stationed support, units in training, the wounded. */
public readonly float $upkeep,
public readonly array $assigned,
public readonly array $required,
/** Villagers drafted by a levy, at most all of them. */
public readonly int $militia = 0,
) {}
/** Villagers not drafted by a levy: the ones recruiting can train. */
public function undrafted(): int
{
return $this->villagers - $this->militia;
}
public function workers(): int
{
return array_sum($this->assigned);
}
/** Staffing ratio 0..1; a building that needs nobody counts as fully staffed. */
public function ratio(int $buildingId): float
{
$required = $this->required[$buildingId] ?? 0;
return $required === 0 ? 1.0 : ($this->assigned[$buildingId] ?? 0) / $required;
}
}