120 lines
5.0 KiB
PHP
120 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Presenters;
|
|
|
|
use App\Engine\Market;
|
|
use App\Engine\Rally;
|
|
use App\Engine\Simulation;
|
|
use App\Game\Trade\TradeRules;
|
|
use App\Models\Movement;
|
|
use App\Models\TradeOffer;
|
|
use App\Models\TradeRoute;
|
|
use App\Models\Village;
|
|
use Carbon\CarbonImmutable;
|
|
use DateTimeInterface;
|
|
|
|
/** The `market.show` payload, and the market part of `village.show`. */
|
|
final class MarketPresenter
|
|
{
|
|
/**
|
|
* For the village payload: the market and its merchants, or null without one. Kept
|
|
* cheap: no market costs no query, a market two sums.
|
|
*
|
|
* @return array{building_id: int, level: int, merchants: int, free: int, carry: int}|null
|
|
*/
|
|
public static function summary(Village $village, Simulation $sim): ?array
|
|
{
|
|
$market = Market::of($village, $sim);
|
|
$building = $market->building();
|
|
if (! $building) {
|
|
return null;
|
|
}
|
|
$merchants = $market->merchants();
|
|
|
|
return [
|
|
'building_id' => $building->id,
|
|
'level' => $building->level,
|
|
'merchants' => $merchants,
|
|
'free' => TradeRules::free($merchants, $market->busy()),
|
|
'carry' => $market->rules()->carry(),
|
|
];
|
|
}
|
|
|
|
public static function present(Village $village, CarbonImmutable $now): array
|
|
{
|
|
$sim = Simulation::of($village);
|
|
$market = Market::of($village, $sim);
|
|
$rules = $market->rules();
|
|
$building = $market->building();
|
|
$config = $sim->config();
|
|
$id = $village->id;
|
|
|
|
$merchants = $market->merchants();
|
|
$busy = $building ? $market->busy() : 0;
|
|
|
|
return [
|
|
'server_time' => self::iso($now),
|
|
'market' => $building ? [
|
|
'building_id' => $building->id,
|
|
'level' => $building->level,
|
|
'merchants' => $merchants,
|
|
'free' => TradeRules::free($merchants, $busy),
|
|
'busy' => $busy,
|
|
'carry' => $rules->carry(),
|
|
// At this world's speed, like the rest of the payload's durations.
|
|
'minutes_per_field' => round($rules->minutesPerField() / ($config->speed() * $config->get('movement.unit_speed', 1.0)), 4),
|
|
'resources' => $rules->resources(),
|
|
'max_ratio' => $rules->maxRatio(),
|
|
'route' => ['min_every' => $rules->minEvery(), 'max_every' => $rules->maxEvery(), 'max_routes' => $rules->maxRoutes()],
|
|
] : null,
|
|
// out: this village's merchants on the way there; back: on their way home; in:
|
|
// anyone's merchants coming here with cargo.
|
|
'trades' => Movement::with(['origin.player', 'target.player'])
|
|
->where(fn ($q) => $q
|
|
->where(fn ($q) => $q->where('origin_id', $id)->where('type', Movement::TRADE))
|
|
->orWhere(fn ($q) => $q->where('target_id', $id)->whereIn('type', Movement::TRADES)))
|
|
->orderBy('arrives_at')->orderBy('id')
|
|
->get()
|
|
->map(fn (Movement $m) => [
|
|
'id' => $m->id,
|
|
'kind' => $m->type === Movement::TRADE_RETURN ? 'back' : ($m->origin_id === $id ? 'out' : 'in'),
|
|
'from' => Rally::summary($m->origin),
|
|
'to' => Rally::summary($m->target),
|
|
'cargo' => $m->cargo,
|
|
'merchants' => $m->merchants,
|
|
'sent_at' => self::iso($m->sent_at),
|
|
'arrives_at' => self::iso($m->arrives_at),
|
|
'route_id' => $m->route_id,
|
|
'offer' => $m->offer,
|
|
])->values()->all(),
|
|
'offers' => TradeOffer::where('village_id', $id)->orderBy('id')->get()
|
|
->map(fn (TradeOffer $o) => [
|
|
'id' => $o->id,
|
|
'give' => ['resource' => $o->give_resource, 'amount' => $o->give_amount],
|
|
'want' => ['resource' => $o->want_resource, 'amount' => $o->want_amount],
|
|
'count' => $o->count,
|
|
// Per unit of count.
|
|
'merchants' => $o->merchants,
|
|
'created_at' => self::iso($o->created_at),
|
|
])->values()->all(),
|
|
'routes' => TradeRoute::with('target.player')->where('village_id', $id)->orderBy('id')->get()
|
|
->map(fn (TradeRoute $r) => [
|
|
'id' => $r->id,
|
|
'to' => Rally::summary($r->target),
|
|
'resources' => $r->amounts(),
|
|
'every' => $r->every,
|
|
'next_at' => self::iso($r->next_at),
|
|
'paused' => $r->paused,
|
|
'paused_reason' => $r->paused_reason,
|
|
'last_sent_at' => $r->last_sent_at ? self::iso($r->last_sent_at) : null,
|
|
'last_cargo' => $r->last_cargo,
|
|
])->values()->all(),
|
|
];
|
|
}
|
|
|
|
public static function iso(DateTimeInterface $at): string
|
|
{
|
|
return CarbonImmutable::instance($at)->utc()->format('Y-m-d\TH:i:s.v\Z');
|
|
}
|
|
}
|