game-village-politics-backend/app/Actions/ListWorlds.php

28 lines
1.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Actions;
use App\Models\Player;
use App\Models\User;
use App\Models\World;
final class ListWorlds
{
/** @return list<array{id: int, name: string, slug: string, speed: float, village_id: int|null, ended_at: string|null, winner: array{id: int, name: string}|null}> */
public function __invoke(User $user): array
{
$villageIds = Player::where('user_id', $user->id)->with('villages:id,player_id')->get()
->mapWithKeys(fn (Player $p) => [$p->world_id => $p->villages->min('id')]);
return World::with('winner')->orderBy('id')->get()->map(fn (World $world) => [
'id' => $world->id,
'name' => $world->name,
'slug' => $world->slug,
'speed' => $world->gameConfig()->speed(),
'village_id' => $villageIds[$world->id] ?? null,
'ended_at' => $world->ended_at?->utc()->format('Y-m-d\TH:i:s.v\Z'),
'winner' => $world->winner ? ['id' => $world->winner->id, 'name' => $world->winner->name] : null,
])->all();
}
}