101 lines
4.6 KiB
PHP
101 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Presenters;
|
|
|
|
use App\Engine\Engine;
|
|
use App\Engine\Rally;
|
|
use App\Models\Movement;
|
|
use App\Models\UnitStack;
|
|
use App\Models\Village;
|
|
use Carbon\CarbonImmutable;
|
|
|
|
/** The rally point part of `village.show`: groups under way, support, and the send form's settings. */
|
|
final class RallyPresenter
|
|
{
|
|
/**
|
|
* Own groups leaving from or heading home to this village, and groups coming here
|
|
* that the village has spotted. Strangers' groups show no units.
|
|
*/
|
|
public static function movements(Village $village, CarbonImmutable $now): array
|
|
{
|
|
$id = $village->id;
|
|
$window = Rally::of($village)->cancelWindow();
|
|
|
|
return Movement::with(['origin.player', 'target.player'])
|
|
->whereNotIn('type', Movement::TRADES)
|
|
->where(fn ($q) => $q
|
|
->where(fn ($q) => $q->where('origin_id', $id)->where('type', '!=', Movement::RETURN))
|
|
->orWhere(fn ($q) => $q->where('target_id', $id)->where('type', Movement::RETURN))
|
|
->orWhere(fn ($q) => $q->where('target_id', $id)->where('type', '!=', Movement::RETURN)->where('visible_at', '<=', Engine::stamp($now))))
|
|
->orderBy('arrives_at')->orderBy('id')
|
|
->get()
|
|
->map(function (Movement $m) use ($village, $now, $window) {
|
|
$incoming = $m->target_id === $village->id && $m->type !== Movement::RETURN;
|
|
$mine = $m->player_id === $village->player_id;
|
|
$cancelUntil = $m->sent_at->addSeconds($window);
|
|
|
|
return [
|
|
'id' => $m->id,
|
|
'type' => $m->type,
|
|
'direction' => $incoming ? 'in' : ($m->type === Movement::RETURN ? 'home' : 'out'),
|
|
'mode' => $mine ? $m->mode : null,
|
|
'from' => Rally::summary($m->origin),
|
|
'to' => Rally::summary($m->target),
|
|
'units' => $mine ? $m->units : null,
|
|
'cargo' => $mine ? $m->cargo : null,
|
|
'sent_at' => $mine ? self::iso($m->sent_at) : null,
|
|
'arrives_at' => self::iso($m->arrives_at),
|
|
// Raiders from a barbarian camp, gathering there until marches_at.
|
|
'raid' => $m->player_id === null,
|
|
'marches_at' => $m->player_id === null && $m->sent_at->greaterThan($now) ? self::iso($m->sent_at) : null,
|
|
'cancel_until' => $mine && ! $incoming && $m->type !== Movement::RETURN && $cancelUntil->greaterThan($now) ? self::iso($cancelUntil) : null,
|
|
];
|
|
})->values()->all();
|
|
}
|
|
|
|
/** Support stationed here, by home village, and this village's troops stationed elsewhere. */
|
|
public static function support(Village $village): array
|
|
{
|
|
$here = $village->troops->filter(fn (UnitStack $s) => $s->home_village_id !== $village->id && $s->count > 0)->load('home.player');
|
|
$away = UnitStack::with('village.player')->where('home_village_id', $village->id)->where('village_id', '!=', $village->id)->where('count', '>', 0)->get();
|
|
|
|
return [
|
|
'here' => self::grouped($here, 'home_village_id', 'home'),
|
|
'away' => self::grouped($away, 'village_id', 'village'),
|
|
];
|
|
}
|
|
|
|
public static function rally(Village $village, CarbonImmutable $now): array
|
|
{
|
|
$rally = Rally::of($village);
|
|
$travel = $rally->travel();
|
|
$protected = $village->player ? $rally->protectedUntil($village->player) : null;
|
|
|
|
return [
|
|
'has_rally_point' => $rally->hasRallyPoint(),
|
|
'modes' => array_map(fn (string $mode) => ['mode' => $mode, ...$travel->mode($mode)], $travel->modes()),
|
|
'default_mode' => $travel->defaultMode(),
|
|
'detection' => round($rally->detection(), 1),
|
|
'cancel_window' => $rally->cancelWindow(),
|
|
'protected_until' => $protected && $protected->greaterThan($now) ? self::iso($protected) : null,
|
|
];
|
|
}
|
|
|
|
/** @param iterable<UnitStack> $stacks */
|
|
private static function grouped(iterable $stacks, string $key, string $relation): array
|
|
{
|
|
$groups = [];
|
|
foreach ($stacks as $stack) {
|
|
$groups[$stack->{$key}] ??= ['village' => Rally::summary($stack->{$relation}), 'units' => []];
|
|
$groups[$stack->{$key}]['units'][$stack->unit] = $stack->count;
|
|
}
|
|
|
|
return array_values(array_map(fn (array $g) => ['village' => $g['village'], 'units' => (object) $g['units']], $groups));
|
|
}
|
|
|
|
private static function iso(CarbonImmutable $at): string
|
|
{
|
|
return $at->utc()->format('Y-m-d\TH:i:s.v\Z');
|
|
}
|
|
}
|