Dragon-Knight/public/index.php

216 lines
6.4 KiB
PHP

<?php
// index.php :: Primary program script, evil alien overlord, you decide.
require_once '../src/bootstrap.php';
$r = new Router;
$r->get('/', function() {
global $userrow;
if ($userrow["currentaction"] == "In Town") {
$page = dotown();
$title = "In Town";
} elseif ($userrow["currentaction"] == "Exploring") {
$page = doexplore();
$title = "Exploring";
} elseif ($userrow["currentaction"] == "Fighting") {
redirect('/fight');
}
display($page, $title);
})->middleware('auth_only');
$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');
// [code, handler, params, middleware]
$l = $r->lookup($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
if ($l['code'] !== 200) exit("Error: {$l['code']}");
if (!empty($l['middleware'])) foreach ($l['middleware'] as $middleware) $middleware();
$l['handler'](...$l['params'] ?? []);
function donothing()
{
global $userrow;
if ($userrow["currentaction"] == "In Town") {
$page = dotown();
$title = "In Town";
} elseif ($userrow["currentaction"] == "Exploring") {
$page = doexplore();
$title = "Exploring";
} elseif ($userrow["currentaction"] == "Fighting") {
redirect('/fight');
}
display($page, $title);
}
/**
* Spit out the main town page.
*/
function dotown()
{
global $userrow, $controlrow;
$townrow = get_town_by_xy($userrow['longitude'], $userrow['latitude']);
if ($townrow === false) display("There is an error with your user account, or with the town data. Please try again.","Error");
$townrow["news"] = "";
$townrow["whosonline"] = "";
$townrow["babblebox"] = "";
// News box. Grab latest news entry and display it. Something a little more graceful coming soon maybe.
if ($controlrow["shownews"] == 1) {
$newsrow = db()->query('SELECT * FROM news ORDER BY id DESC LIMIT 1;')->fetchArray(SQLITE3_ASSOC);
$townrow["news"] = '<div class="title">Latest News</div>';
$townrow["news"] .= "<span class=\"light\">[".prettydate($newsrow["postdate"])."]</span><br>".nl2br($newsrow["content"]);
}
// Who's Online. Currently just members. Guests maybe later.
if ($controlrow["showonline"] == 1) {
$onlinequery = db()->query("SELECT id, username FROM users WHERE strftime('%s', onlinetime) >= strftime('%s', 'now') - 600 ORDER BY username");
$online_count = 0;
$online_rows = [];
while ($onlinerow = $onlinequery->fetchArray(SQLITE3_ASSOC)) {
$online_count++;
$online_rows[] = "<a href=\"javascript:opencharpopup({$onlinerow['id']})\">".$onlinerow["username"]."</a>";
}
$townrow["whosonline"] = '<div class="title">Who\'s Online</div>';
$townrow["whosonline"] .= "There are <b>$online_count</b> user(s) online within the last 10 minutes: ";
$townrow["whosonline"] .= rtrim(implode(', ', $online_rows), ', ');
}
if ($controlrow["showbabble"] == 1) {
$townrow["babblebox"] = <<<HTML
<div class="title">Babble Box</div>
<iframe src="/babblebox" name="sbox" width="100%" height="250" frameborder="0" id="bbox">
Your browser does not support inline frames! The Babble Box will not be available until you upgrade to
a newer <a href="http://www.mozilla.org" target="_new">browser</a>.
</iframe>
HTML;
}
return parsetemplate(gettemplate("towns"), $townrow);
}
/**
* 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
<table width="100%">
<tr><td class="title"><img src="/img/title_exploring.gif" alt="Exploring" /></td></tr>
<tr><td>
You are exploring the map, and nothing has happened. Continue exploring using the direction buttons or the Travel To menus.
</td></tr>
</table>
HTML;
}
/**
* Show a character's info. Defaults to the currently logged in user.
*/
function show_character_info(int $id = 0): void
{
global $controlrow;
if ($id === 0) {
global $userrow;
} else {
$userrow = get_user_by_id($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('minimal', ['content' => $showchar, 'title' => $userrow['username'].' Information']);
}
function showmap()
{
global $userrow;
$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 + $userrow['longitude'] * (500 / 500) - 3),
round(258 - $userrow['latitude'] * (500 / 500) - 3)
);
echo render('minimal', [
'content' => '<img src="/img/map.gif" alt="Map">'.$pos,
'title' => 'Map'
]);
}
/**
* 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()
{
global $userrow;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$safecontent = make_safe($_POST["babble"]);
if (!empty($safecontent)) {
db()->query('INSERT INTO babble (posttime, author, babble) VALUES (CURRENT_TIMESTAMP, ?, ?);',
[$userrow['username'], $safecontent]);
}
redirect('/babblebox');
}
$query = db()->query('SELECT * FROM babble ORDER BY id DESC LIMIT 40;');
echo render('babblebox', ['messages' => $query]);
}
/**
* NINJA! 🥷
*/
function ninja(): void
{
exit('NINJA! 🥷');
}