import { json } from '@sveltejs/kit'; import { listServers, heldByBeta } from '$lib/server/fleet/servers.js'; import { classifyHealth } from '$lib/server/fleet/health.js'; import { createServer } from '$lib/server/fleet/create.js'; import { config, komodoConfigured, rconConfigured } from '$lib/server/config.js'; import type { RconPlayer } from '$lib/server/integrations/rcon.js'; import type { GameMode } from '$lib/server/fleet/map-aliases.js'; function safeJson(raw: string | null): T | null { if (!raw) return null; try { return JSON.parse(raw) as T; } catch { return null; } } export async function GET() { const rconHost = config.rcon.host; const servers = listServers().map((s) => ({ ...s, roster: safeJson(s.last_roster), health: classifyHealth(s.last_seen_at, s.last_error), replySnippet: s.last_reply_snippet, // Derived endpoint — same logic as endpointFor() but without asking // Komodo. If we don't have a port yet, endpoint is null. endpoint: s.game_port ? { host: s.rcon_host ?? rconHost, port: s.game_port } : null, held: [...heldByBeta(s.name).entries()].map(([target, betaId]) => ({ target, betaId })) })); return json({ servers, integrations: { rcon: rconConfigured(), komodo: komodoConfigured(), rconTimeoutMs: config.rcon.timeoutMs, defaultRconHost: rconHost } }); } export async function POST({ request }: { request: Request }) { const body = (await request.json().catch(() => ({}))) as { gameMode?: string; mapSlug?: string; plutoKey?: string; lobby?: number; }; if (!body || typeof body.gameMode !== 'string' || typeof body.mapSlug !== 'string' || (body.plutoKey !== undefined && typeof body.plutoKey !== 'string') || (body.lobby !== undefined && typeof body.lobby !== 'number')) { return json( { ok: false, error: 'gameMode and mapSlug are required strings; plutoKey and lobby are optional', kind: 'bad-request' }, { status: 400 } ); } const result = await createServer({ gameMode: body.gameMode as GameMode, mapSlug: body.mapSlug, plutoKey: body.plutoKey, lobby: body.lobby, createdBy: 'ui' }); if (!result.ok) { const status = result.kind === 'bad-request' ? 400 : result.kind === 'unconfigured' ? 503 : result.kind === 'unknown-map' || result.kind === 'no-slot' || result.kind === 'no-port' ? 409 : 500; return json({ ok: false, error: result.message, kind: result.kind }, { status }); } return json({ ok: true, name: result.name, port: result.port }); }