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'); } }