35 lines
1.2 KiB
PHP
35 lines
1.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Actions;
|
||
|
|
|
||
|
|
use App\Engine\Market;
|
||
|
|
use App\Engine\Rally;
|
||
|
|
use App\Game\GameRuleViolation;
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Models\Village;
|
||
|
|
use App\Presenters\MarketPresenter;
|
||
|
|
|
||
|
|
/** What the market's send form shows first: the village at x|y, the distance and the merchants' travel time. Changes nothing. */
|
||
|
|
final class PlanTrade
|
||
|
|
{
|
||
|
|
public function __invoke(User $user, int $villageId, int $x, int $y): array
|
||
|
|
{
|
||
|
|
$village = Village::with('player')->find($villageId);
|
||
|
|
if (! $village || $village->player?->user_id !== $user->id) {
|
||
|
|
throw new GameRuleViolation('not_found', 'Village not found.');
|
||
|
|
}
|
||
|
|
$target = Village::with('player')->where('world_id', $village->world_id)->where('x', $x)->where('y', $y)->first()
|
||
|
|
?? throw new GameRuleViolation('not_found', 'No village there.');
|
||
|
|
$market = Market::of($village);
|
||
|
|
$market->checkTarget($target);
|
||
|
|
$plan = $market->plan($target, now()->toImmutable());
|
||
|
|
|
||
|
|
return [
|
||
|
|
'target' => Rally::summary($target),
|
||
|
|
'distance' => round($plan['distance'], 2),
|
||
|
|
'seconds' => $plan['seconds'],
|
||
|
|
'arrives_at' => MarketPresenter::iso($plan['arrives_at']),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|