Clean up some redundancy

This commit is contained in:
Sky Johnson 2024-12-15 22:02:49 -06:00
parent ae0f0802cb
commit f7f6e7fb97
6 changed files with 83 additions and 59 deletions

View File

@ -179,3 +179,19 @@ div.town-content {
div.town-content div.options, div.town-content div.news { div.town-content div.options, div.town-content div.news {
grid-column: span 2; grid-column: span 2;
} }
div.stat-table div.stat-row {
display: flex;
justify-content: space-around;
gap: 0.5rem;
}
div.stat-bar {
position: relative;
}
div.stat-bar > div {
width: 100%;
position: absolute;
bottom: 0;
}

View File

@ -20,7 +20,7 @@ $r->get('/', function() {
} }
display($page, $title); display($page, $title);
})->middleware('auth_only'); });
$r->get('/ninja', function() { $r->get('/ninja', function() {
exit('NINJA! 🥷'); exit('NINJA! 🥷');
@ -89,7 +89,12 @@ function dotown()
// Who's Online. Currently just members. Guests maybe later. // Who's Online. Currently just members. Guests maybe later.
if ($controlrow["showonline"] == 1) { if ($controlrow["showonline"] == 1) {
$onlinequery = db()->query("SELECT id, username FROM users WHERE strftime('%s', onlinetime) >= strftime('%s', 'now') - 600 ORDER BY username;"); $onlinequery = db()->query(<<<SQL
SELECT id, username
FROM users
WHERE onlinetime >= datetime('now', '-600 seconds')
ORDER BY username;
SQL);
$online_count = 0; $online_count = 0;
$online_rows = []; $online_rows = [];

View File

@ -20,8 +20,6 @@ function register_routes(Router $r): Router
*/ */
function login() function login()
{ {
if (checkcookies() !== false) redirect('/');
if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$form = validate($_POST, [ $form = validate($_POST, [
'username' => ['length:3-18', 'alpha-spaces'], 'username' => ['length:3-18', 'alpha-spaces'],

View File

@ -16,6 +16,7 @@ require_once 'actions/admin.php';
env_load('../.env'); env_load('../.env');
$uri = uri(); $uri = uri();
$GLOBALS['cache'] = [];
if (!file_exists('../.installed') && $uri[0] !== 'install') { if (!file_exists('../.installed') && $uri[0] !== 'install') {
redirect('/install'); redirect('/install');
@ -40,7 +41,7 @@ if (!file_exists('../.installed') && $uri[0] !== 'install') {
} }
// Force verify if the user isn't verified yet. // Force verify if the user isn't verified yet.
if ($controlrow['verifyemail'] && $userrow['verify'] !== 'g2g') { if ($controlrow['verifyemail'] && $userrow['verify'] !== 'g2g' && $uri[0] !== 'verify') {
redirect('/verify'); redirect('/verify');
} }

View File

@ -126,15 +126,14 @@ function display($content, $title, bool $topnav = true, bool $leftnav = true, bo
$userrow = $userquery->fetchArray(SQLITE3_ASSOC); $userrow = $userquery->fetchArray(SQLITE3_ASSOC);
// Current town name. // Current town name.
if ($userrow["currentaction"] == "In Town") { if ($userrow['currentaction'] == 'In Town') {
$townquery = db()->query('SELECT * FROM towns WHERE latitude = ? AND longitude = ? LIMIT 1;', [$userrow["latitude"], $userrow["longitude"]]); $townrow = get_town_by_xy($userrow['latitude'], $userrow['longitude']);
$townrow = $townquery->fetchArray(SQLITE3_ASSOC); $userrow['currenttown'] = "Welcome to <b>{$townrow['name']}</b>.<br><br>";
$userrow["currenttown"] = "Welcome to <b>".$townrow["name"]."</b>.<br><br>";
} else { } else {
$userrow["currenttown"] = ""; $userrow['currenttown'] = '';
} }
$userrow["forumslink"] = '<a href="/forum">Forum</a><br>'; $userrow['forumslink'] = '<a href="/forum">Forum</a><br>';
// Format various userrow stuffs... // Format various userrow stuffs...
if ($userrow["latitude"] < 0) { $userrow["latitude"] = $userrow["latitude"] * -1 . "S"; } else { $userrow["latitude"] .= "N"; } if ($userrow["latitude"] < 0) { $userrow["latitude"] = $userrow["latitude"] * -1 . "S"; } else { $userrow["latitude"] .= "N"; }
@ -144,42 +143,22 @@ function display($content, $title, bool $topnav = true, bool $leftnav = true, bo
if ($userrow["authlevel"] == 1) { $userrow["adminlink"] = "<a href=\"/admin\">Admin</a><br>"; } else { $userrow["adminlink"] = ""; } if ($userrow["authlevel"] == 1) { $userrow["adminlink"] = "<a href=\"/admin\">Admin</a><br>"; } else { $userrow["adminlink"] = ""; }
// HP/MP/TP bars. // HP/MP/TP bars.
$stathp = ceil($userrow["currenthp"] / $userrow["maxhp"] * 100); $userrow['statbars'] = create_stat_table($userrow);
if ($userrow["maxmp"] != 0) { $statmp = ceil($userrow["currentmp"] / $userrow["maxmp"] * 100); } else { $statmp = 0; }
$stattp = ceil($userrow["currenttp"] / $userrow["maxtp"] * 100);
$stattable = "<table width=\"100\"><tr><td width=\"33%\">\n";
$stattable .= "<table cellspacing=\"0\" cellpadding=\"0\"><tr><td style=\"padding:0px; width:15px; height:100px; border:solid 1px black; vertical-align:bottom;\">\n";
if ($stathp >= 66) { $stattable .= "<div style=\"padding:0px; height:".$stathp."px; border-top:solid 1px black; background-image:url(/img/bars_green.gif);\"><img src=\"/img/bars_green.gif\" alt=\"\" /></div>"; }
if ($stathp < 66 && $stathp >= 33) { $stattable .= "<div style=\"padding:0px; height:".$stathp."px; border-top:solid 1px black; background-image:url(/img/bars_yellow.gif);\"><img src=\"/img/bars_yellow.gif\" alt=\"\" /></div>"; }
if ($stathp < 33) { $stattable .= "<div style=\"padding:0px; height:".$stathp."px; border-top:solid 1px black; background-image:url(/img/bars_red.gif);\"><img src=\"/img/bars_red.gif\" alt=\"\" /></div>"; }
$stattable .= "</td></tr></table></td><td width=\"33%\">\n";
$stattable .= "<table cellspacing=\"0\" cellpadding=\"0\"><tr><td style=\"padding:0px; width:15px; height:100px; border:solid 1px black; vertical-align:bottom;\">\n";
if ($statmp >= 66) { $stattable .= "<div style=\"padding:0px; height:".$statmp."px; border-top:solid 1px black; background-image:url(/img/bars_green.gif);\"><img src=\"/img/bars_green.gif\" alt=\"\" /></div>"; }
if ($statmp < 66 && $statmp >= 33) { $stattable .= "<div style=\"padding:0px; height:".$statmp."px; border-top:solid 1px black; background-image:url(/img/bars_yellow.gif);\"><img src=\"/img/bars_yellow.gif\" alt=\"\" /></div>"; }
if ($statmp < 33) { $stattable .= "<div style=\"padding:0px; height:".$statmp."px; border-top:solid 1px black; background-image:url(/img/bars_red.gif);\"><img src=\"/img/bars_red.gif\" alt=\"\" /></div>"; }
$stattable .= "</td></tr></table></td><td width=\"33%\">\n";
$stattable .= "<table cellspacing=\"0\" cellpadding=\"0\"><tr><td style=\"padding:0px; width:15px; height:100px; border:solid 1px black; vertical-align:bottom;\">\n";
if ($stattp >= 66) { $stattable .= "<div style=\"padding:0px; height:".$stattp."px; border-top:solid 1px black; background-image:url(/img/bars_green.gif);\"><img src=\"/img/bars_green.gif\" alt=\"\" /></div>"; }
if ($stattp < 66 && $stattp >= 33) { $stattable .= "<div style=\"padding:0px; height:".$stattp."px; border-top:solid 1px black; background-image:url(/img/bars_yellow.gif);\"><img src=\"/img/bars_yellow.gif\" alt=\"\" /></div>"; }
if ($stattp < 33) { $stattable .= "<div style=\"padding:0px; height:".$stattp."px; border-top:solid 1px black; background-image:url(/img/bars_red.gif);\"><img src=\"/img/bars_red.gif\" alt=\"\" /></div>"; }
$stattable .= "</td></tr></table></td>\n";
$stattable .= "</tr><tr><td>HP</td><td>MP</td><td>TP</td></tr></table>\n";
$userrow["statbars"] = $stattable;
// Now make numbers stand out if they're low. // Now make numbers stand out if they're low.
if ($userrow["currenthp"] <= ($userrow["maxhp"]/5)) { $userrow["currenthp"] = "<blink><span class=\"highlight\"><b>*".$userrow["currenthp"]."*</b></span></blink>"; } if ($userrow["currenthp"] <= ($userrow["maxhp"]/5)) { $userrow["currenthp"] = "<blink><span class=\"highlight\"><b>*".$userrow["currenthp"]."*</b></span></blink>"; }
if ($userrow["currentmp"] <= ($userrow["maxmp"]/5)) { $userrow["currentmp"] = "<blink><span class=\"highlight\"><b>*".$userrow["currentmp"]."*</b></span></blink>"; } if ($userrow["currentmp"] <= ($userrow["maxmp"]/5)) { $userrow["currentmp"] = "<blink><span class=\"highlight\"><b>*".$userrow["currentmp"]."*</b></span></blink>"; }
$spellquery = db()->query('SELECT id, name, type FROM spells;'); $user_spells = explode(',', $userrow['spells']);
$userspells = explode(",",$userrow["spells"]); $spellquery = get_spells_from_list($user_spells);
$userrow["magiclist"] = ""; $userrow['magiclist'] = '';
while ($spellrow = $spellquery->fetchArray(SQLITE3_ASSOC)) { while ($spell = $spellquery->fetchArray(SQLITE3_ASSOC)) {
$spell = false; $spell = false;
foreach($userspells as $a => $b) { foreach($user_spells as $id) {
if ($b == $spellrow["id"] && $spellrow["type"] == 1) { $spell = true; } if ($id === $spell['id'] && $spell['type'] == 1) $spell = true;
} }
if ($spell == true) { if ($spell == true) {
$userrow["magiclist"] .= "<a href=\"/spell/{$spellrow["id"]}\">".$spellrow["name"]."</a><br>"; $userrow['magiclist'] .= "<a href=\"/spell/{$spell['id']}\">".$spell['name']."</a><br>";
} }
} }
if ($userrow["magiclist"] == "") { $userrow["magiclist"] = "None"; } if ($userrow["magiclist"] == "") { $userrow["magiclist"] = "None"; }
@ -269,9 +248,15 @@ function get_control_row(): array|false
*/ */
function get_town_by_xy(int $x, int $y): array|false function get_town_by_xy(int $x, int $y): array|false
{ {
$cache_tag = "town-$x-$y";
if (!isset($GLOBALS['cache'][$cache_tag])) {
$query = db()->query('SELECT * FROM towns WHERE longitude = ? AND latitude = ? LIMIT 1;', [$x, $y]); $query = db()->query('SELECT * FROM towns WHERE longitude = ? AND latitude = ? LIMIT 1;', [$x, $y]);
if ($query === false) return false; if ($query === false) return false;
return $query->fetchArray(SQLITE3_ASSOC); $GLOBALS['cache'][$cache_tag] = $query->fetchArray(SQLITE3_ASSOC);
}
return $GLOBALS['cache'][$cache_tag];
} }
/** /**
@ -552,22 +537,6 @@ function uri(): array
return explode('/', trim($_SERVER['REQUEST_URI'], '/')); return explode('/', trim($_SERVER['REQUEST_URI'], '/'));
} }
/**
* Redirect to login if not authenticated.
*/
function auth_only(): void
{
if (!checkcookies()) redirect('/login');
}
/**
* Redirect to home if authenticated.
*/
function guest_only(): void
{
if (checkcookies()) redirect('/login');
}
/** /**
* Load the environment variables from the .env file. * Load the environment variables from the .env file.
*/ */
@ -613,3 +582,38 @@ function env(string $key, mixed $default = null): mixed
default => $v default => $v
}; };
} }
/**
* Get the data on spells from a given list of IDs.
*/
function get_spells_from_list(array|string $spell_ids): SQLite3Result|false
{
if (is_string($spell_ids)) $spell_ids = explode(',', $spell_ids);
$placeholders = implode(',', array_fill(0, count($spell_ids), '?'));
$query = db()->query("SELECT id, name, type FROM spells WHERE id IN($placeholders)", $spell_ids);
if ($query === false) return false;
return $query;
}
function generate_stat_bar($current, $max)
{
$percent = ($max === 0) ? 0 : ceil($current / $max * 100);
$color = $percent >= 66 ? 'green' : ($percent >= 33 ? 'yellow' : 'red');
return '<div class="stat-bar" style="width: 15px; height: 100px; border: solid 1px black;">' .
'<div style="height: ' . $percent . 'px; background-image: url(/img/bars_' . $color . '.gif);"></div>' .
'</div>';
}
function create_stat_table($userrow)
{
$stat_table = '<div class="stat-table">' .
'<div class="stat-row">' .
'<div class="stat-col">' . generate_stat_bar($userrow['currenthp'], $userrow['maxhp']) . '<div>HP</div></div>' .
'<div class="stat-col">' . generate_stat_bar($userrow['currentmp'], $userrow['maxmp']) . '<div>MP</div></div>' .
'<div class="stat-col">' . generate_stat_bar($userrow['currenttp'], $userrow['maxtp']) . '<div>TP</div></div>' .
'</div>' .
'</div>';
return $stat_table;
}

View File

@ -6,7 +6,7 @@
Gold: <?= $user['gold'] ?><br> Gold: <?= $user['gold'] ?><br>
HP: <?= $user['currenthp'] ?><br> HP: <?= $user['currenthp'] ?><br>
MP: <?= $user['currentmp'] ?><br> MP: <?= $user['currentmp'] ?><br>
TP: <?= $user['currenttp'] ?><br> TP: <?= $user['currenttp'] ?><br><br>
<?= $user['statbars'] ?><br> <?= $user['statbars'] ?><br>
<a href="javascript:opencharpopup()">Extended Stats</a> <a href="javascript:opencharpopup()">Extended Stats</a>
</section> </section>