2026-10-28 18:17:34 +00:00
|
|
|
<?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;
|
|
|
|
|
|
2026-11-03 08:38:17 +00:00
|
|
|
/**
|
|
|
|
|
* Queue the next level after whatever is already queued for this instance, if idle
|
|
|
|
|
* villagers stand ready for the workers it adds (farms need none).
|
|
|
|
|
*/
|
2026-10-28 18:17:34 +00:00
|
|
|
final class QueueUpgrade 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->upgradeOffer($building) ?? throw new GameRuleViolation('max_level', 'Already at max level.');
|
2026-11-03 08:38:17 +00:00
|
|
|
if ($offer['blocked'] === 'villagers') {
|
|
|
|
|
throw new GameRuleViolation('villagers', 'Not enough free villagers: build up the farm.');
|
|
|
|
|
}
|
2026-10-28 18:17:34 +00:00
|
|
|
$sim->pay($offer['cost']);
|
|
|
|
|
$sim->enqueue([
|
|
|
|
|
'building_id' => $building->id,
|
|
|
|
|
'type' => $building->type,
|
|
|
|
|
'action' => BuildOrder::UPGRADE,
|
|
|
|
|
'target_level' => $offer['target_level'],
|
|
|
|
|
'cost' => $offer['cost'],
|
|
|
|
|
], $now);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|