game-village-politics-backend/app/Engine/Hospital.php

103 lines
3.6 KiB
PHP

<?php
namespace App\Engine;
use App\Game\Military\Wounds;
use App\Models\Building;
use App\Models\GameEvent;
use App\Models\Village;
use App\Models\Wounded;
use Carbon\CarbonImmutable;
/**
* The Lazarett of one village (architecture.md, "Hospital"). After a battle here, part
* of the own fallen come in as wounded; each battle's wounded are one row that a
* wounded.healed event turns back into troops at home.
*/
final class Hospital
{
private function __construct(private readonly Village $village, private readonly Simulation $sim) {}
public static function of(Village $village, ?Simulation $sim = null): self
{
return new self($village, $sim ?? Simulation::of($village));
}
public function rules(): Wounds
{
return new Wounds($this->sim->config());
}
/** Beds of all hospitals here, each as far as it is staffed, a copy at its instance share. */
public function beds(): int
{
$rules = $this->sim->rules();
$workforce = $this->sim->workforce();
return (int) floor($this->village->buildings->sum(function (Building $b) use ($rules, $workforce) {
$beds = $rules->beds($b->type, $b->level);
return $beds > 0 ? $beds * $this->sim->staffingFactor($workforce->ratio($b->id)) * $this->sim->share($b) : 0;
}));
}
/** Beds the wounded lie in now. */
public function occupied(): int
{
return $this->village->wounded->sum(fn (Wounded $w) => array_sum($w->units));
}
/**
* Take in the wounded among the own units that fell defending here at $at. Barbarian
* villages keep no hospital.
*
* @param array<string, int> $dead
* @return Wounded|null the wounded of this battle, if any
*/
public function admit(array $dead, CarbonImmutable $at): ?Wounded
{
if ($this->village->player_id === null) {
return null;
}
$wounded = $this->rules()->admit($dead, max(0, $this->beds() - $this->occupied()));
if (! $wounded) {
return null;
}
$batch = $this->village->wounded()->create(['units' => $wounded, 'heals_at' => $at->addSeconds($this->rules()->healSeconds())]);
$this->village->wounded->push($batch);
GameEvent::create([
'world_id' => $this->village->world_id,
'village_id' => $this->village->id,
'type' => GameEvent::WOUNDED_HEALED,
'subject_id' => $batch->id,
'fire_at' => $batch->heals_at,
]);
return $batch;
}
/** The wounded healed by $at stand at home again. They ate all along, so upkeep stays. */
public function heal(CarbonImmutable $at): void
{
$military = Military::of($this->village, $this->sim);
$healed = $this->village->wounded->filter(fn (Wounded $w) => ! $w->heals_at->greaterThan($at));
foreach ($healed as $batch) {
foreach ($batch->units as $unit => $count) {
$military->addTroops($unit, $count);
}
$batch->delete();
}
$this->village->setRelation('wounded', $this->village->wounded->diff($healed)->values());
}
/** The wounded don't outlive the owner's rule: they are gone when the village changes hands. */
public static function clear(Village $village): void
{
foreach ($village->wounded as $batch) {
GameEvent::where('type', GameEvent::WOUNDED_HEALED)->where('subject_id', $batch->id)->whereNull('processed_at')->delete();
$batch->delete();
}
$village->setRelation('wounded', collect());
}
}