diff --git a/public/css/dk.css b/public/css/dk.css index ecdee74..b9a434c 100644 --- a/public/css/dk.css +++ b/public/css/dk.css @@ -2,6 +2,10 @@ --font-size: 12px; } +* { + box-sizing: border-box; +} + html { font-size: var(--font-size); font-family: sans-serif; @@ -200,3 +204,26 @@ div.stat-bar > div { position: absolute; bottom: 0; } + +#babblebox > .messages { + max-height: 200px; + overflow-y: auto; +} + +#babblebox > .messages .message { + padding: 0.25rem; + background-color: #eee; + + &:nth-child(even) { + background-color: white; + } +} + +#babblebox > form { + margin-top: 0 !important; + + & > input[type="text"] { + width: 100%; + margin-bottom: 0.5rem; + } +} diff --git a/public/index.php b/public/index.php index 0ba8140..6fe5905 100644 --- a/public/index.php +++ b/public/index.php @@ -4,6 +4,12 @@ require_once '../src/bootstrap.php'; +// Do an early return with babblebox data if that's what's being requested +if ($uri[0] === 'babblebox' && (isset($uri[1]) && $uri[1] === 'messages')) { + echo babblebox_messages(); + exit; +} + $r = new Router; $r->get('/', function() { @@ -39,14 +45,14 @@ $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'); // [code, handler, params, middleware] $l = $r->lookup($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); if (is_int($l)) exit("Error: $l"); -if (!empty($l['middleware'])) foreach ($l['middleware'] as $middleware) $middleware(); $content = $l['handler'](...$l['params'] ?? []); -if (is_htmx()) { +if (is_htmx() && $uri[0] !== 'babblebox') { $content .= Render\debug_db_info(); if ($GLOBALS['state']['user-state-changed'] ?? false) { $content .= Render\right_nav(); @@ -113,22 +119,36 @@ function showmap() } /** - * Either render the latest 40 chats to the babblebox, or add a chat to it and redirect. This is used - * within an iframe. + * ... */ function babblebox() { if ($_SERVER['REQUEST_METHOD'] === 'POST') { - $safecontent = make_safe($_POST["babble"]); - if (!empty($safecontent)) { + $content = trim($_POST["babble"]); + if (!empty($content)) { db()->query('INSERT INTO babble (posttime, author, babble) VALUES (CURRENT_TIMESTAMP, ?, ?);', - [user()->username, $safecontent]); + [user()->username, $content]); } - redirect('/babblebox'); + return babblebox_messages(); } +} - $query = db()->query('SELECT * FROM babble ORDER BY id DESC LIMIT 40;'); - echo render('babblebox', ['messages' => $query]); +/** + * Is the handler for the HTMX get request for 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 .= '
'; + } + if (!$has_chats) $messages = 'There are no messages. :('; + return $messages; } /** diff --git a/src/bootstrap.php b/src/bootstrap.php index 88ac6bb..0c52d5a 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -27,7 +27,7 @@ if (!file_exists('../.installed') && $uri[0] !== 'install') { redirect('/install'); } elseif (file_exists(('../.installed')) && $uri[0] === 'install') { redirect('/'); -} elseif (file_exists(('../.installed')) && $uri[0] !== 'install') { +} else { $controlrow = get_control_row(); if (!$controlrow["gameopen"]) { diff --git a/src/render.php b/src/render.php index c967839..09a675d 100644 --- a/src/render.php +++ b/src/render.php @@ -38,3 +38,8 @@ function right_nav(): string if (is_htmx()) $template = '