52 lines
1.8 KiB
PHP
52 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Presenters;
|
|
|
|
use App\Game\Map\Season;
|
|
use App\Models\Player;
|
|
use App\Models\Village;
|
|
use App\Models\World;
|
|
use Carbon\CarbonImmutable;
|
|
|
|
/** The season part of `village.show`: when the crowns come, who holds them, and the end. */
|
|
final class SeasonPresenter
|
|
{
|
|
public static function present(World $world): array
|
|
{
|
|
$season = new Season($world->gameConfig());
|
|
$crowns = $world->crowns_placed_at
|
|
? Village::where('world_id', $world->id)->where('crown', true)->with('player:id,name')->orderBy('id')->get()
|
|
: collect();
|
|
|
|
return [
|
|
// When they appear, or appeared.
|
|
'crowns_at' => self::iso($world->crowns_placed_at ?? $season->crownsAt($world->started_at)),
|
|
'count' => $season->count(),
|
|
'needed' => $season->needed(),
|
|
'hold_seconds' => $season->holdSeconds(),
|
|
'crowns' => $crowns->map(fn (Village $v) => [
|
|
'id' => $v->id,
|
|
'name' => $v->name,
|
|
'x' => $v->x,
|
|
'y' => $v->y,
|
|
'player' => self::player($v->player),
|
|
])->values()->all(),
|
|
'holder' => self::player($world->holder),
|
|
// When the holder wins if they keep the crowns.
|
|
'ends_at' => $world->ends_at ? self::iso($world->ends_at) : null,
|
|
'ended_at' => $world->ended_at ? self::iso($world->ended_at) : null,
|
|
'winner' => self::player($world->winner),
|
|
];
|
|
}
|
|
|
|
private static function player(?Player $player): ?array
|
|
{
|
|
return $player ? ['id' => $player->id, 'name' => $player->name] : null;
|
|
}
|
|
|
|
private static function iso(CarbonImmutable $at): string
|
|
{
|
|
return $at->utc()->format('Y-m-d\TH:i:s.v\Z');
|
|
}
|
|
}
|