2024-12-11 22:05:45 -06:00
|
|
|
<?php
|
2017-02-05 10:49:37 -06:00
|
|
|
|
2024-12-11 22:05:45 -06:00
|
|
|
// index.php :: Primary program script, evil alien overlord, you decide.
|
|
|
|
|
2024-12-13 15:52:37 -06:00
|
|
|
require_once '../src/bootstrap.php';
|
2024-12-11 22:05:45 -06:00
|
|
|
|
2024-12-18 10:51:29 -06:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2024-12-13 11:42:22 -06:00
|
|
|
$r = new Router;
|
|
|
|
|
2024-12-19 10:04:03 -06:00
|
|
|
$r->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');
|
2024-12-19 18:33:17 -06:00
|
|
|
$r->get('/showmap', 'show_map');
|
2024-12-19 10:04:03 -06:00
|
|
|
$r->form('/babblebox', 'babblebox');
|
|
|
|
$r->get('/babblebox/messages', 'babblebox_messages');
|
2024-12-13 11:42:22 -06:00
|
|
|
|
2024-12-13 14:57:48 -06:00
|
|
|
Towns\register_routes($r);
|
|
|
|
Fights\register_routes($r);
|
|
|
|
Users\register_routes($r);
|
|
|
|
Help\register_routes($r);
|
2024-12-13 15:35:06 -06:00
|
|
|
Forum\register_routes($r);
|
2024-12-13 15:52:37 -06:00
|
|
|
Install\register_routes($r);
|
2024-12-13 16:09:57 -06:00
|
|
|
Admin\register_routes($r);
|
2024-12-13 11:42:22 -06:00
|
|
|
|
2024-12-19 10:04:03 -06:00
|
|
|
/*
|
|
|
|
NINJA! 🥷
|
|
|
|
*/
|
|
|
|
$r->get('/ninja', function() {
|
|
|
|
exit('NINJA! 🥷');
|
|
|
|
});
|
2024-12-13 11:42:22 -06:00
|
|
|
|
|
|
|
// [code, handler, params, middleware]
|
|
|
|
$l = $r->lookup($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
|
|
|
|
|
2024-12-14 18:44:33 -06:00
|
|
|
if (is_int($l)) exit("Error: $l");
|
2024-12-19 18:33:17 -06:00
|
|
|
echo render_response($uri, $l['handler'](...$l['params'] ?? []));
|
2024-12-18 10:00:41 -06:00
|
|
|
exit;
|
2017-02-05 10:49:37 -06:00
|
|
|
|
2024-12-19 10:04:03 -06:00
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
}
|
|
|
|
|
2024-12-20 14:20:34 -06:00
|
|
|
return $page;
|
2024-12-19 10:04:03 -06:00
|
|
|
}
|
|
|
|
|
2024-12-13 22:14:44 -06:00
|
|
|
/**
|
|
|
|
* Show a character's info. Defaults to the currently logged in user.
|
|
|
|
*/
|
2024-12-18 23:09:04 -06:00
|
|
|
function show_character_info(int $id = 0): string
|
2024-12-11 22:05:45 -06:00
|
|
|
{
|
2024-12-18 23:09:04 -06:00
|
|
|
$user = $id !== 0 ? User::find($id) : user();
|
|
|
|
if ($user === false) exit('Failed to show info for user ID '.$id);
|
2024-12-12 09:33:17 -06:00
|
|
|
|
2024-12-18 23:09:04 -06:00
|
|
|
$level = db()->query("SELECT `{$user->charclass}_exp` FROM levels WHERE id=? LIMIT 1;", [$user->level + 1])->fetchArray(SQLITE3_ASSOC);
|
2024-12-12 09:33:17 -06:00
|
|
|
|
2024-12-18 23:09:04 -06:00
|
|
|
$spells = $user->spells();
|
|
|
|
$magic_list = 'None';
|
|
|
|
if (!empty($spells)) {
|
|
|
|
$magic_list = '';
|
|
|
|
foreach ($spells as $spell) $magic_list .= $spell['name'].'<br>';
|
|
|
|
}
|
2024-12-13 22:14:44 -06:00
|
|
|
|
2024-12-19 13:16:55 -06:00
|
|
|
$showchar = render('show_char', [
|
2024-12-18 23:09:04 -06:00
|
|
|
'char' => $user,
|
|
|
|
'level' => $level,
|
2024-12-19 13:16:55 -06:00
|
|
|
'magic_list' => $magic_list
|
2024-12-13 22:14:44 -06:00
|
|
|
]);
|
2024-12-18 23:09:04 -06:00
|
|
|
return render('layouts/minimal', ['content' => $showchar, 'title' => $user->username.' Information']);
|
2017-02-05 10:49:37 -06:00
|
|
|
}
|
|
|
|
|
2024-12-19 18:33:17 -06:00
|
|
|
/**
|
|
|
|
* Show the user their position on the current world map. Only works with a game size of 250 and the default towns 😅
|
|
|
|
*/
|
|
|
|
function show_map()
|
2024-12-12 09:33:17 -06:00
|
|
|
{
|
2024-12-13 17:28:51 -06:00
|
|
|
$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>',
|
2024-12-17 22:10:49 -06:00
|
|
|
round(258 + user()->longitude * (500 / 500) - 3),
|
|
|
|
round(258 - user()->latitude * (500 / 500) - 3)
|
2024-12-13 17:28:51 -06:00
|
|
|
);
|
|
|
|
|
2024-12-18 10:00:41 -06:00
|
|
|
echo render('layouts/minimal', [
|
2024-12-13 17:28:51 -06:00
|
|
|
'content' => '<img src="/img/map.gif" alt="Map">'.$pos,
|
|
|
|
'title' => 'Map'
|
|
|
|
]);
|
2024-12-12 09:33:17 -06:00
|
|
|
}
|
2024-12-11 22:05:45 -06:00
|
|
|
|
2024-12-13 17:28:51 -06:00
|
|
|
/**
|
2024-12-19 18:33:17 -06:00
|
|
|
* Handle a POST request to send a new babblebox message.
|
2024-12-13 17:28:51 -06:00
|
|
|
*/
|
2024-12-12 09:33:17 -06:00
|
|
|
function babblebox()
|
|
|
|
{
|
2024-12-18 23:09:04 -06:00
|
|
|
if (is_post()) {
|
2024-12-18 10:51:29 -06:00
|
|
|
$content = trim($_POST["babble"]);
|
|
|
|
if (!empty($content)) {
|
2024-12-13 17:28:51 -06:00
|
|
|
db()->query('INSERT INTO babble (posttime, author, babble) VALUES (CURRENT_TIMESTAMP, ?, ?);',
|
2024-12-18 10:51:29 -06:00
|
|
|
[user()->username, $content]);
|
2024-12-12 09:33:17 -06:00
|
|
|
}
|
2024-12-18 10:51:29 -06:00
|
|
|
return babblebox_messages();
|
2017-02-05 10:49:37 -06:00
|
|
|
}
|
2024-12-18 10:51:29 -06:00
|
|
|
}
|
2024-12-11 22:05:45 -06:00
|
|
|
|
2024-12-18 10:51:29 -06:00
|
|
|
/**
|
2024-12-18 23:09:04 -06:00
|
|
|
* The handler that is polled by HTMX for new babblebox messages.
|
2024-12-18 10:51:29 -06:00
|
|
|
*/
|
|
|
|
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;
|
2017-02-05 10:49:37 -06:00
|
|
|
}
|