Dragon-Knight/public/index.php

161 lines
4.4 KiB
PHP

<?php
// index.php :: Primary program script, evil alien overlord, you decide.
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() {
if (user()->currentaction === "In Town") {
$page = Towns\town();
$title = "In Town";
} elseif (user()->currentaction === "Exploring") {
$page = doexplore();
$title = "Exploring";
} elseif (user()->currentaction === "Fighting") {
redirect('/fight');
}
return is_htmx() ? $page : display($page, $title);
});
$r->get('/ninja', function() {
exit('NINJA! 🥷');
});
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);
$r->post('/move', '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');
// [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') {
$content .= Render\debug_db_info();
if ($GLOBALS['state']['user-state-changed'] ?? false) {
$content .= Render\right_nav();
}
}
echo $content;
exit;
/**
* Just spit out a blank exploring page. Exploring without a GET string is normally when they first log in, or when
* they've just finished fighting.
*/
function doexplore()
{
return <<<HTML
<div class="title"><img src="/img/title_exploring.gif" alt="Exploring"></div>
You are exploring the map, and nothing has happened. Continue exploring using the direction buttons or the Travel To menus.
HTML;
}
/**
* Show a character's info. Defaults to the currently logged in user.
*/
function show_character_info(int $id = 0): void
{
global $controlrow, $userrow;
$userrow = ($id === 0) ? $userrow : get_user($id);
if ($userrow === false) exit('Failed to show info for user ID '.$id);
$levelrow = db()->query("SELECT `{$userrow["charclass"]}_exp` FROM levels WHERE id=? LIMIT 1;", [$userrow['level'] + 1])->fetchArray(SQLITE3_ASSOC);
$spells = db()->query('SELECT id, name FROM spells;');
$userspells = explode(',', $userrow['spells']);
$magic_list = '';
while ($spellrow = $spells->fetchArray(SQLITE3_ASSOC)) {
$spell = false;
foreach($userspells as $b) if ($b == $spellrow["id"]) $spell = true;
if ($spell == true) $magic_list .= $spellrow["name"]."<br>";
}
if ($magic_list == "") $magic_list = "None";
$showchar = render('showchar', [
'char' => $userrow,
'level' => $levelrow,
'magic_list' => $magic_list,
'controlrow' => $controlrow
]);
echo render('layouts/minimal', ['content' => $showchar, 'title' => $userrow['username'].' Information']);
}
function showmap()
{
$pos = sprintf(
'<div style="position: absolute; width: 5px; height: 5px; border-radius: 1000px; border: solid 1px black; background-color: red; left: %dpx; top: %dpx;"></div>',
round(258 + user()->longitude * (500 / 500) - 3),
round(258 - user()->latitude * (500 / 500) - 3)
);
echo render('layouts/minimal', [
'content' => '<img src="/img/map.gif" alt="Map">'.$pos,
'title' => 'Map'
]);
}
/**
* ...
*/
function babblebox()
{
if ($_SERVER['REQUEST_METHOD'] === '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();
}
}
/**
* 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 .= '<div class="message">[<b>' . $row['author'] . '</b>] ' . make_safe($row['babble']) . '</div>';
}
if (!$has_chats) $messages = 'There are no messages. :(';
return $messages;
}
/**
* NINJA! 🥷
*/
function ninja(): void
{
exit('NINJA! 🥷');
}