null, 'incoming' => [], 'shot_results' => []]; if ($user) { $view = Store::open()->viewFor($user['id']); } jsonOut([ 'ok' => true, 'configured' => $configured, 'local' => $local, 'csrf' => csrfToken(), 'me' => $user, 'lobby' => $view['lobby'], 'incoming' => $view['incoming'], 'shot_results' => $view['shot_results'], 'miss_chance_default' => METEORS_DEFAULT_MISS, 'flight_duration' => METEORS_FLIGHT_DURATION, 'login_url' => 'auth.php', 'logout_url' => 'logout.php', ]); } if ($action === 'test_login') { if (!localDev()) { jsonFail('Niet beschikbaar.', 403); } requireCsrf(); $who = (string) ($body['user'] ?? $_GET['user'] ?? ''); if (!in_array($who, ['test_user_1', 'test_user_2'], true)) { jsonFail('Onbekende testgebruiker.'); } loginUser('local:' . $who, $who); jsonOut(['ok' => true, 'me' => currentUser(), 'csrf' => csrfToken()]); } if (!$user && $action !== 'me') { jsonFail('Log in om verder te gaan.', 401); } requireCsrf(); $store = Store::open(); $userId = $user['id']; $name = $user['name']; switch ($action) { case 'lobby_create': $miss = isset($body['miss_chance']) ? (float) $body['miss_chance'] : METEORS_DEFAULT_MISS; $planet = (string) ($body['planet_id'] ?? ''); $lobby = $store->createLobby($userId, $name, $miss, $planet); jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]); case 'lobby_join': $code = (string) ($body['code'] ?? ''); $planet = (string) ($body['planet_id'] ?? ''); $lobby = $store->joinLobby($userId, $name, $code, $planet); jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]); case 'lobby_set_planet': $planet = (string) ($body['planet_id'] ?? ''); $lobby = $store->setPlanet($userId, $planet); jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]); case 'lobby_leave': $store->leaveLobby($userId); jsonOut(['ok' => true, 'lobby' => null, 'csrf' => csrfToken()]); case 'lobby_ready': $ready = (bool) ($body['ready'] ?? true); $lobby = $store->setReady($userId, $ready); jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]); case 'lobby_start': $lobby = $store->startGame($userId); jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]); case 'poll': $snapshot = isset($body['snapshot']) && is_array($body['snapshot']) ? $body['snapshot'] : null; $score = isset($body['score']) ? (int) $body['score'] : null; $eliminated = !empty($body['eliminated']); $outgoing = isset($body['outgoing_shots']) && is_array($body['outgoing_shots']) ? $body['outgoing_shots'] : []; $view = $store->viewFor($userId, $snapshot, $score, $eliminated, $outgoing); jsonOut([ 'ok' => true, 'csrf' => csrfToken(), 'me' => $user, 'lobby' => $view['lobby'], 'incoming' => $view['incoming'], 'shot_results' => $view['shot_results'], ]); default: jsonFail('Onbekende actie.', 404); } } catch (RuntimeException $e) { jsonFail($e->getMessage(), 400); } catch (Throwable $e) { jsonFail('Interne fout.', 500); }