2026-10-28 18:17:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Actions;
|
|
|
|
|
|
2026-10-30 10:27:14 +00:00
|
|
|
use App\Game\GameRuleViolation;
|
|
|
|
|
use App\Game\Map\Season;
|
2026-10-28 18:17:34 +00:00
|
|
|
use App\Models\Player;
|
|
|
|
|
use App\Models\User;
|
2026-10-30 10:27:14 +00:00
|
|
|
use App\Models\Village;
|
2026-10-28 18:17:34 +00:00
|
|
|
use App\Models\World;
|
2026-10-30 10:27:14 +00:00
|
|
|
use App\Presenters\WorldPresenter;
|
|
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2026-10-28 18:17:34 +00:00
|
|
|
|
2026-10-30 10:27:14 +00:00
|
|
|
/** The worlds overview: every world with its state and size, and one world's settings. */
|
2026-10-28 18:17:34 +00:00
|
|
|
final class ListWorlds
|
|
|
|
|
{
|
|
|
|
|
public function __invoke(User $user): array
|
|
|
|
|
{
|
2026-10-30 10:27:14 +00:00
|
|
|
$players = Player::where('user_id', $user->id)->with('villages:id,player_id')->get()->keyBy('world_id');
|
|
|
|
|
$sizes = Village::whereNotNull('player_id')->groupBy('world_id')
|
|
|
|
|
->selectRaw('world_id, COUNT(*) as villages, COUNT(DISTINCT player_id) as players')->get()->keyBy('world_id');
|
|
|
|
|
$tribes = DB::table('tribes')->groupBy('world_id')->selectRaw('world_id, COUNT(*) as n')->pluck('n', 'world_id');
|
|
|
|
|
|
|
|
|
|
return World::with(['winner', 'holder'])->orderBy('id')->get()->map(function (World $world) use ($players, $sizes, $tribes) {
|
|
|
|
|
$season = new Season($world->gameConfig());
|
|
|
|
|
$mine = $players[$world->id] ?? null;
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'id' => $world->id,
|
|
|
|
|
'name' => $world->name,
|
|
|
|
|
'slug' => $world->slug,
|
|
|
|
|
'speed' => $world->gameConfig()->speed(),
|
|
|
|
|
'village_id' => $mine?->villages->min('id'),
|
|
|
|
|
// Joined once and lost every village: the world offers a fresh start.
|
|
|
|
|
'fallen' => $mine !== null && $mine->villages->isEmpty(),
|
|
|
|
|
'started_at' => self::iso($world->started_at),
|
|
|
|
|
'players' => (int) ($sizes[$world->id]->players ?? 0),
|
|
|
|
|
'villages' => (int) ($sizes[$world->id]->villages ?? 0),
|
|
|
|
|
'tribes' => (int) ($tribes[$world->id] ?? 0),
|
|
|
|
|
'crowns_at' => self::iso($world->crowns_placed_at ?? $season->crownsAt($world->started_at)),
|
|
|
|
|
'holder' => $world->holder ? ['id' => $world->holder->id, 'name' => $world->holder->name] : null,
|
|
|
|
|
'ends_at' => $world->ends_at ? self::iso($world->ends_at) : null,
|
|
|
|
|
'ended_at' => $world->ended_at ? self::iso($world->ended_at) : null,
|
|
|
|
|
'winner' => $world->winner ? ['id' => $world->winner->id, 'name' => $world->winner->name] : null,
|
|
|
|
|
];
|
|
|
|
|
})->all();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** One world's settings, in plain units. */
|
|
|
|
|
public function settings(int $worldId): array
|
|
|
|
|
{
|
|
|
|
|
$world = World::find($worldId) ?? throw new GameRuleViolation('not_found', 'World not found.');
|
|
|
|
|
|
|
|
|
|
return ['id' => $world->id, 'name' => $world->name, 'settings' => WorldPresenter::settings($world->gameConfig())];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function iso(CarbonImmutable $at): string
|
|
|
|
|
{
|
|
|
|
|
return $at->utc()->format('Y-m-d\TH:i:s.v\Z');
|
2026-10-28 18:17:34 +00:00
|
|
|
}
|
|
|
|
|
}
|