52 lines
1.5 KiB
PHP
52 lines
1.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Game\Combat;
|
||
|
|
|
||
|
|
use App\Game\Military\UnitRules;
|
||
|
|
use App\Game\WorldConfig;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Opponents defeated, for the ranking: every fallen enemy counts with the food it ate,
|
||
|
|
* so a nobleman weighs ten spears and a levied villager one. Defenders share what the
|
||
|
|
* attackers lost by what each of them had standing in the village.
|
||
|
|
*/
|
||
|
|
final class Defeated
|
||
|
|
{
|
||
|
|
private UnitRules $units;
|
||
|
|
|
||
|
|
public function __construct(private readonly WorldConfig $config)
|
||
|
|
{
|
||
|
|
$this->units = new UnitRules($config);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @param array<string, int> $units by unit; `militia` for levied villagers */
|
||
|
|
public function weight(array $units): float
|
||
|
|
{
|
||
|
|
$total = 0.0;
|
||
|
|
foreach ($units as $unit => $count) {
|
||
|
|
$total += $count * ($unit === 'militia' ? (float) $this->config->get('economy.upkeep.villager', 1) : $this->units->upkeep($unit));
|
||
|
|
}
|
||
|
|
|
||
|
|
return $total;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Points for what fell. */
|
||
|
|
public function points(array $lost): int
|
||
|
|
{
|
||
|
|
return (int) round($this->weight($lost));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Each defender's part of the points, by the weight they had standing.
|
||
|
|
*
|
||
|
|
* @param array<int|string, float> $standing defender => weight
|
||
|
|
* @return array<int|string, int>
|
||
|
|
*/
|
||
|
|
public static function share(int $points, array $standing): array
|
||
|
|
{
|
||
|
|
$total = array_sum($standing);
|
||
|
|
|
||
|
|
return $total > 0 ? array_map(fn (float $w) => (int) round($points * $w / $total), $standing) : [];
|
||
|
|
}
|
||
|
|
}
|