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

24 lines
773 B
PHP
Raw Permalink Normal View History

<?php
namespace App\Actions;
use App\Engine\Military;
use App\Engine\Simulation;
use App\Game\GameRuleViolation;
use App\Models\User;
use App\Models\Village;
use Carbon\CarbonImmutable;
/** Stop a recruiting order: units already trained stay, the rest are refunded. */
final class CancelRecruit extends VillageAction
{
public function __invoke(User $user, int $villageId, int $orderId): Village
{
return $this->mutate($user, $villageId, function (Simulation $sim, Village $village, CarbonImmutable $now) use ($orderId) {
$order = $village->recruits->firstWhere('id', $orderId)
?? throw new GameRuleViolation('not_found', 'Order not found.');
Military::of($village, $sim)->cancel($order, $now);
});
}
}