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

26 lines
1.0 KiB
PHP

<?php
namespace App\Actions;
use App\Engine\Rally;
use App\Engine\Simulation;
use App\Game\GameRuleViolation;
use App\Models\User;
use App\Models\Village;
use Carbon\CarbonImmutable;
/** Send an attack or support from the rally point to the village at x|y. */
final class SendTroops extends VillageAction
{
/** @param array<string, mixed> $units */
public function __invoke(User $user, int $villageId, string $type, int $x, int $y, array $units, ?string $mode, ?string $catapultTarget): Village
{
return $this->mutate($user, $villageId, function (Simulation $sim, Village $village, CarbonImmutable $now) use ($type, $x, $y, $units, $mode, $catapultTarget) {
$target = Village::where('world_id', $village->world_id)->where('x', $x)->where('y', $y)->first()
?? throw new GameRuleViolation('not_found', 'No village there.');
$rally = Rally::of($village, $sim);
$rally->send($type, $target, $units, $mode ?: $rally->travel()->defaultMode(), $catapultTarget ?: null, $now);
});
}
}