109 lines
3.9 KiB
PHP
109 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Websocket\Controllers;
|
|
|
|
use App\Actions\ListOffers;
|
|
use App\Actions\MarketDeals;
|
|
use App\Actions\MarketRoutes;
|
|
use App\Actions\PlanTrade;
|
|
use App\Actions\ShowMarket;
|
|
use App\Presenters\MarketPresenter;
|
|
use App\Websocket\GameController;
|
|
use Closure;
|
|
|
|
/**
|
|
* market.show {village_id}; market.preview {village_id, x, y}: target, distance and travel time;
|
|
* market.offers {village_id, give?, want?}: the world's open offers, nearest first.
|
|
* market.send {village_id, x, y, wood, clay, iron}, market.offer {village_id, give, want, count},
|
|
* market.cancelOffer {village_id, offer_id}, market.accept {village_id, offer_id, count},
|
|
* market.routeCreate {village_id, x, y, wood, clay, iron, every},
|
|
* market.routeUpdate {village_id, route_id, wood?, clay?, iron?, every?, paused?},
|
|
* market.routeDelete {village_id, route_id}: each answers with the market.show payload.
|
|
*/
|
|
class MarketController extends GameController
|
|
{
|
|
public function show()
|
|
{
|
|
return $this->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()));
|
|
}
|
|
}
|