market(fn ($user, $id) => app(ShowMarket::class)($user, $id)); } public function preview() { return $this->play(fn () => app(PlanTrade::class)(auth()->user(), (int) request('village_id'), (int) request('x'), (int) request('y'))); } public function offers() { return $this->play(fn () => app(ListOffers::class)( auth()->user(), (int) request('village_id'), request('give') ? (string) request('give') : null, request('want') ? (string) request('want') : null, )); } public function send() { return $this->market(fn ($user, $id) => app(MarketDeals::class)->send($user, $id, (int) request('x'), (int) request('y'), $this->amounts())); } public function offer() { $give = (array) request('give', []); $want = (array) request('want', []); return $this->market(fn ($user, $id) => app(MarketDeals::class)->offer( $user, $id, (string) ($give['resource'] ?? ''), (int) ($give['amount'] ?? 0), (string) ($want['resource'] ?? ''), (int) ($want['amount'] ?? 0), (int) request('count', 1), )); } public function cancelOffer() { return $this->market(fn ($user, $id) => app(MarketDeals::class)->cancelOffer($user, $id, (int) request('offer_id'))); } public function accept() { return $this->market(fn ($user, $id) => app(MarketDeals::class)->accept($user, $id, (int) request('offer_id'), (int) request('count', 1))); } public function routeCreate() { return $this->market(fn ($user, $id) => app(MarketRoutes::class)->create($user, $id, (int) request('x'), (int) request('y'), $this->amounts(), (int) request('every'))); } public function routeUpdate() { $changes = []; foreach (['wood', 'clay', 'iron', 'every'] as $key) { $changes[$key] = request($key) === null ? null : (int) request($key); } $changes['paused'] = request('paused') === null ? null : filter_var(request('paused'), FILTER_VALIDATE_BOOLEAN); return $this->market(fn ($user, $id) => app(MarketRoutes::class)->update($user, $id, (int) request('route_id'), $changes)); } public function routeDelete() { return $this->market(fn ($user, $id) => app(MarketRoutes::class)->delete($user, $id, (int) request('route_id'))); } /** @return array{wood: int, clay: int, iron: int} */ private function amounts(): array { return ['wood' => (int) request('wood', 0), 'clay' => (int) request('clay', 0), 'iron' => (int) request('iron', 0)]; } /** Runs a market intent and answers with the market as it is afterwards. */ private function market(Closure $action) { return $this->play(fn () => MarketPresenter::present($action(auth()->user(), (int) request('village_id')), now()->toImmutable())); } }