47 lines
1.6 KiB
PHP
47 lines
1.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Actions;
|
||
|
|
|
||
|
|
use App\Engine\Engine;
|
||
|
|
use App\Engine\Push;
|
||
|
|
use App\Game\GameRuleViolation;
|
||
|
|
use App\Models\GameEvent;
|
||
|
|
use App\Models\UnitStack;
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Models\Village;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The owner calls their support back from another village. The host's troops change,
|
||
|
|
* so it runs as an event on the host's queue, under the host's lock, right away.
|
||
|
|
*/
|
||
|
|
final class WithdrawSupport
|
||
|
|
{
|
||
|
|
public function __construct(private readonly Engine $engine) {}
|
||
|
|
|
||
|
|
public function __invoke(User $user, int $villageId, int $hostId): Village
|
||
|
|
{
|
||
|
|
$home = Village::with('player')->find($villageId);
|
||
|
|
if (! $home || $home->player?->user_id !== $user->id) {
|
||
|
|
throw new GameRuleViolation('not_found', 'Village not found.');
|
||
|
|
}
|
||
|
|
$now = now()->toImmutable();
|
||
|
|
// Support that should have arrived by now has.
|
||
|
|
$this->engine->catchUpAround($villageId, $now);
|
||
|
|
$stationed = UnitStack::where('village_id', $hostId)->where('home_village_id', $villageId)->where('count', '>', 0)->exists();
|
||
|
|
if ($hostId === $villageId || ! $stationed) {
|
||
|
|
throw new GameRuleViolation('not_found', 'No support there.');
|
||
|
|
}
|
||
|
|
GameEvent::create([
|
||
|
|
'world_id' => $home->world_id,
|
||
|
|
'village_id' => $hostId,
|
||
|
|
'type' => GameEvent::SUPPORT_RECALLED,
|
||
|
|
'subject_id' => $villageId,
|
||
|
|
'fire_at' => $now,
|
||
|
|
]);
|
||
|
|
$this->engine->catchUp($hostId, $now);
|
||
|
|
Push::villages([$hostId]);
|
||
|
|
|
||
|
|
return app(ShowVillage::class)($user, $villageId);
|
||
|
|
}
|
||
|
|
}
|