get('/', 'index'); $r->post('/move', 'Explore\move'); $r->get('/spell/:id', 'healspells'); $r->get('/character', 'show_character_info'); $r->get('/character/:id', 'show_character_info'); $r->get('/showmap', 'showmap'); $r->form('/babblebox', 'babblebox'); $r->get('/babblebox/messages', 'babblebox_messages'); Towns\register_routes($r); Fights\register_routes($r); Users\register_routes($r); Help\register_routes($r); Forum\register_routes($r); Install\register_routes($r); Admin\register_routes($r); /* NINJA! 🥷 */ $r->get('/ninja', function() { exit('NINJA! 🥷'); }); // [code, handler, params, middleware] $l = $r->lookup($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); if (is_int($l)) exit("Error: $l"); $content = $l['handler'](...$l['params'] ?? []); if (is_htmx() && $uri[0] !== 'babblebox') { header('HX-Push-Url: '.$_SERVER['REQUEST_URI']); $content .= ''.page_title().''; $content .= Render\debug_db_info(); if (env('debug', false)) $content .= Render\debug_query_log(); if ($GLOBALS['state']['user-state-changed'] ?? false) { $content .= Render\right_nav(); $content .= Render\left_nav(); } } echo $content; exit; /** * Return a page for a couple generic actions. */ function index(): string { if (user()->currentaction === "In Town") { $page = Towns\town(); } elseif (user()->currentaction === "Exploring") { $page = Explore\explore(); } elseif (user()->currentaction === "Fighting") { redirect('/fight'); } return Render\content($page); } /** * Show a character's info. Defaults to the currently logged in user. */ function show_character_info(int $id = 0): string { global $controlrow; $user = $id !== 0 ? User::find($id) : user(); if ($user === false) exit('Failed to show info for user ID '.$id); $level = db()->query("SELECT `{$user->charclass}_exp` FROM levels WHERE id=? LIMIT 1;", [$user->level + 1])->fetchArray(SQLITE3_ASSOC); $spells = $user->spells(); $magic_list = 'None'; if (!empty($spells)) { $magic_list = ''; foreach ($spells as $spell) $magic_list .= $spell['name'].'
'; } $showchar = render('showchar', [ 'char' => $user, 'level' => $level, 'magic_list' => $magic_list, 'controlrow' => $controlrow ]); return render('layouts/minimal', ['content' => $showchar, 'title' => $user->username.' Information']); } function showmap() { $pos = sprintf( '
', round(258 + user()->longitude * (500 / 500) - 3), round(258 - user()->latitude * (500 / 500) - 3) ); echo render('layouts/minimal', [ 'content' => 'Map'.$pos, 'title' => 'Map' ]); } /** * ... */ function babblebox() { if (is_post()) { $content = trim($_POST["babble"]); if (!empty($content)) { db()->query('INSERT INTO babble (posttime, author, babble) VALUES (CURRENT_TIMESTAMP, ?, ?);', [user()->username, $content]); } return babblebox_messages(); } } /** * The handler that is polled by HTMX for new babblebox messages. */ function babblebox_messages(): string { if (user() === false) return ''; $query = db()->query('SELECT * FROM babble ORDER BY id ASC LIMIT 40;'); $has_chats = false; $messages = ''; while ($row = $query->fetchArray(SQLITE3_ASSOC)) { $has_chats = true; $messages .= '
[' . $row['author'] . '] ' . make_safe($row['babble']) . '
'; } if (!$has_chats) $messages = 'There are no messages. :('; return $messages; }