26 lines
898 B
PHP
26 lines
898 B
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Actions;
|
||
|
|
|
||
|
|
use App\Engine\Rally;
|
||
|
|
use App\Engine\Simulation;
|
||
|
|
use App\Game\GameRuleViolation;
|
||
|
|
use App\Models\UnitStack;
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Models\Village;
|
||
|
|
use Carbon\CarbonImmutable;
|
||
|
|
|
||
|
|
/** The host sends the support of one village home; always allowed. */
|
||
|
|
final class SendSupportHome extends VillageAction
|
||
|
|
{
|
||
|
|
public function __invoke(User $user, int $villageId, int $homeVillageId): Village
|
||
|
|
{
|
||
|
|
return $this->mutate($user, $villageId, function (Simulation $sim, Village $village, CarbonImmutable $now) use ($homeVillageId) {
|
||
|
|
if ($homeVillageId === $village->id || ! $village->troops->contains(fn (UnitStack $s) => $s->home_village_id === $homeVillageId)) {
|
||
|
|
throw new GameRuleViolation('not_found', 'No support from there.');
|
||
|
|
}
|
||
|
|
Rally::of($village, $sim)->sendHome($homeVillageId, $now);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|