game-village-politics-backend/app/Actions/QueueDemolish.php

36 lines
1.4 KiB
PHP

<?php
namespace App\Actions;
use App\Engine\Simulation;
use App\Game\GameRuleViolation;
use App\Models\BuildOrder;
use App\Models\User;
use App\Models\Village;
use Carbon\CarbonImmutable;
/** Queue taking an instance down one level; below 0 it is removed. Refunds on finish. */
final class QueueDemolish extends VillageAction
{
public function __invoke(User $user, int $villageId, int $buildingId): Village
{
return $this->mutate($user, $villageId, function (Simulation $sim, Village $village, CarbonImmutable $now) use ($buildingId) {
$building = $this->building($village, $buildingId);
$offer = $sim->demolishOffer($building)
?? throw new GameRuleViolation('core_building', 'Core buildings cannot go below level 1.');
// A smaller headquarters means less building space; what stands must still fit.
if ($building->type === 'headquarters'
&& $sim->spaceUsed() > $sim->rules()->spaceMax($village->space, $offer['target_level'])) {
throw new GameRuleViolation('no_space', 'The buildings would no longer fit.');
}
$sim->enqueue([
'building_id' => $building->id,
'type' => $building->type,
'action' => BuildOrder::DEMOLISH,
'target_level' => $offer['target_level'],
'cost' => ['wood' => 0, 'clay' => 0, 'iron' => 0],
], $now);
});
}
}