From f7f6e7fb97489a1487e6ed635f9df543e5e3c3f5 Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Sun, 15 Dec 2024 22:02:49 -0600 Subject: [PATCH] Clean up some redundancy --- public/css/dk.css | 16 ++++++ public/index.php | 9 +++- src/actions/users.php | 2 - src/bootstrap.php | 3 +- src/lib.php | 110 +++++++++++++++++++++-------------------- templates/rightnav.php | 2 +- 6 files changed, 83 insertions(+), 59 deletions(-) diff --git a/public/css/dk.css b/public/css/dk.css index b699a30..fc47fec 100644 --- a/public/css/dk.css +++ b/public/css/dk.css @@ -179,3 +179,19 @@ div.town-content { div.town-content div.options, div.town-content div.news { 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; +} diff --git a/public/index.php b/public/index.php index a0de9cc..4bc5c77 100644 --- a/public/index.php +++ b/public/index.php @@ -20,7 +20,7 @@ $r->get('/', function() { } display($page, $title); -})->middleware('auth_only'); +}); $r->get('/ninja', function() { exit('NINJA! 🥷'); @@ -89,7 +89,12 @@ function dotown() // 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;"); + $onlinequery = db()->query(<<= datetime('now', '-600 seconds') + ORDER BY username; + SQL); $online_count = 0; $online_rows = []; diff --git a/src/actions/users.php b/src/actions/users.php index 576d245..a386cb7 100644 --- a/src/actions/users.php +++ b/src/actions/users.php @@ -20,8 +20,6 @@ function register_routes(Router $r): Router */ function login() { - if (checkcookies() !== false) redirect('/'); - if ($_SERVER['REQUEST_METHOD'] === 'POST') { $form = validate($_POST, [ 'username' => ['length:3-18', 'alpha-spaces'], diff --git a/src/bootstrap.php b/src/bootstrap.php index 1218534..bd171cd 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -16,6 +16,7 @@ require_once 'actions/admin.php'; env_load('../.env'); $uri = uri(); +$GLOBALS['cache'] = []; if (!file_exists('../.installed') && $uri[0] !== 'install') { redirect('/install'); @@ -40,7 +41,7 @@ if (!file_exists('../.installed') && $uri[0] !== 'install') { } // 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'); } diff --git a/src/lib.php b/src/lib.php index fadca9b..6f68c41 100644 --- a/src/lib.php +++ b/src/lib.php @@ -126,15 +126,14 @@ function display($content, $title, bool $topnav = true, bool $leftnav = true, bo $userrow = $userquery->fetchArray(SQLITE3_ASSOC); // Current town name. - if ($userrow["currentaction"] == "In Town") { - $townquery = db()->query('SELECT * FROM towns WHERE latitude = ? AND longitude = ? LIMIT 1;', [$userrow["latitude"], $userrow["longitude"]]); - $townrow = $townquery->fetchArray(SQLITE3_ASSOC); - $userrow["currenttown"] = "Welcome to ".$townrow["name"].".

"; + if ($userrow['currentaction'] == 'In Town') { + $townrow = get_town_by_xy($userrow['latitude'], $userrow['longitude']); + $userrow['currenttown'] = "Welcome to {$townrow['name']}.

"; } else { - $userrow["currenttown"] = ""; + $userrow['currenttown'] = ''; } - $userrow["forumslink"] = 'Forum
'; + $userrow['forumslink'] = 'Forum
'; // Format various userrow stuffs... 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"] = "Admin
"; } else { $userrow["adminlink"] = ""; } // HP/MP/TP bars. - $stathp = ceil($userrow["currenthp"] / $userrow["maxhp"] * 100); - if ($userrow["maxmp"] != 0) { $statmp = ceil($userrow["currentmp"] / $userrow["maxmp"] * 100); } else { $statmp = 0; } - $stattp = ceil($userrow["currenttp"] / $userrow["maxtp"] * 100); - $stattable = "\n"; - $stattable .= "
\n"; - $stattable .= "
\n"; - if ($stathp >= 66) { $stattable .= "
\"\"
"; } - if ($stathp < 66 && $stathp >= 33) { $stattable .= "
\"\"
"; } - if ($stathp < 33) { $stattable .= "
\"\"
"; } - $stattable .= "
\n"; - $stattable .= "
\n"; - if ($statmp >= 66) { $stattable .= "
\"\"
"; } - if ($statmp < 66 && $statmp >= 33) { $stattable .= "
\"\"
"; } - if ($statmp < 33) { $stattable .= "
\"\"
"; } - $stattable .= "
\n"; - $stattable .= "
\n"; - if ($stattp >= 66) { $stattable .= "
\"\"
"; } - if ($stattp < 66 && $stattp >= 33) { $stattable .= "
\"\"
"; } - if ($stattp < 33) { $stattable .= "
\"\"
"; } - $stattable .= "
HPMPTP
\n"; - $userrow["statbars"] = $stattable; + $userrow['statbars'] = create_stat_table($userrow); // Now make numbers stand out if they're low. if ($userrow["currenthp"] <= ($userrow["maxhp"]/5)) { $userrow["currenthp"] = "*".$userrow["currenthp"]."*"; } if ($userrow["currentmp"] <= ($userrow["maxmp"]/5)) { $userrow["currentmp"] = "*".$userrow["currentmp"]."*"; } - $spellquery = db()->query('SELECT id, name, type FROM spells;'); - $userspells = explode(",",$userrow["spells"]); - $userrow["magiclist"] = ""; - while ($spellrow = $spellquery->fetchArray(SQLITE3_ASSOC)) { + $user_spells = explode(',', $userrow['spells']); + $spellquery = get_spells_from_list($user_spells); + $userrow['magiclist'] = ''; + while ($spell = $spellquery->fetchArray(SQLITE3_ASSOC)) { $spell = false; - foreach($userspells as $a => $b) { - if ($b == $spellrow["id"] && $spellrow["type"] == 1) { $spell = true; } + foreach($user_spells as $id) { + if ($id === $spell['id'] && $spell['type'] == 1) $spell = true; } if ($spell == true) { - $userrow["magiclist"] .= "".$spellrow["name"]."
"; + $userrow['magiclist'] .= "".$spell['name']."
"; } } 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 { - $query = db()->query('SELECT * FROM towns WHERE longitude = ? AND latitude = ? LIMIT 1;', [$x, $y]); - if ($query === false) return false; - return $query->fetchArray(SQLITE3_ASSOC); + $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]); + if ($query === false) return false; + $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'], '/')); } -/** - * 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. */ @@ -613,3 +582,38 @@ function env(string $key, mixed $default = null): mixed 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 '
' . + '
' . + '
'; +} + +function create_stat_table($userrow) +{ + $stat_table = '
' . + '
' . + '
' . generate_stat_bar($userrow['currenthp'], $userrow['maxhp']) . '
HP
' . + '
' . generate_stat_bar($userrow['currentmp'], $userrow['maxmp']) . '
MP
' . + '
' . generate_stat_bar($userrow['currenttp'], $userrow['maxtp']) . '
TP
' . + '
' . + '
'; + + return $stat_table; +} diff --git a/templates/rightnav.php b/templates/rightnav.php index 0054709..159d86f 100644 --- a/templates/rightnav.php +++ b/templates/rightnav.php @@ -6,7 +6,7 @@ Gold:
HP:
MP:
- TP:
+ TP:


Extended Stats