diff --git a/public/index.php b/public/index.php
index 891f829..63fed66 100644
--- a/public/index.php
+++ b/public/index.php
@@ -37,8 +37,8 @@ Admin\register_routes($r);
$r->post('/move', 'move');
$r->get('/spell/:id', 'healspells');
-$r->get('/showchar', 'showchar');
-$r->get('/onlinechar/:id', 'onlinechar');
+$r->get('/character', 'show_character_info');
+$r->get('/character/:id', 'show_character_info');
$r->get('/showmap', 'showmap');
$r->form('/babblebox', 'babblebox');
@@ -96,7 +96,7 @@ function dotown()
while ($onlinerow = $onlinequery->fetchArray(SQLITE3_ASSOC)) {
$online_count++;
- $online_rows[] = "".$onlinerow["username"]."";
+ $online_rows[] = "".$onlinerow["username"]."";
}
$townrow["whosonline"] = '
Who\'s Online
';
@@ -133,73 +133,40 @@ function doexplore()
HTML;
}
-function showchar()
-{
- global $userrow, $controlrow;
-
- $userrow["experience"] = number_format($userrow["experience"]);
- $userrow["gold"] = number_format($userrow["gold"]);
-
- $userrow["plusexp"] = $userrow["expbonus"] != 0
- ? "(" . ($userrow["expbonus"] > 0 ? "+" : "") . $userrow["expbonus"] . "%)"
- : "";
-
- $userrow["plusgold"] = $userrow["goldbonus"] != 0
- ? "(" . ($userrow["goldbonus"] > 0 ? "+" : "") . $userrow["goldbonus"] . "%)"
- : "";
-
- $levelrow = db()->query("SELECT `{$userrow["charclass"]}_exp` FROM levels WHERE id=? LIMIT 1;", [$userrow['level'] + 1])->fetchArray(SQLITE3_ASSOC);
- $userrow["nextlevel"] = $userrow['level'] < 99 ? number_format($levelrow[$userrow["charclass"]."_exp"]) : 'None';
-
- $userrow['charclass'] = match ((int) $userrow['charclass']) {
- 1 => $controlrow["class1name"],
- 2 => $controlrow["class2name"],
- 3 => $controlrow["class3name"]
- };
-
- $spells = db()->query('SELECT id, name FROM spells;');
- $userspells = explode(',', $userrow['spells']);
- $userrow["magiclist"] = '';
- while ($spellrow = $spells->fetchArray(SQLITE3_ASSOC)) {
- $spell = false;
- foreach($userspells as $b) if ($b == $spellrow["id"]) $spell = true;
- if ($spell == true) $userrow["magiclist"] .= $spellrow["name"]."
";
- }
- if ($userrow["magiclist"] == "") $userrow["magiclist"] = "None";
-
- $array = ["content" => parsetemplate(gettemplate("showchar"), $userrow), "title" => "Character Information"];
- echo render('minimal', $array);
-}
-
-function onlinechar($id)
+/**
+ * Show a character's info. Defaults to the currently logged in user.
+ */
+function show_character_info(int $id = 0): void
{
global $controlrow;
- $query = db()->query('SELECT * FROM users WHERE id=? LIMIT 1;', [$id]);
- if ($query !== false) { $userrow = $query->fetchArray(SQLITE3_ASSOC); } else { display("No such user.", "Error"); }
- unset($userrow['password']);
+ if ($id === 0) {
+ global $userrow;
+ } else {
+ $userrow = get_user_by_id($id);
+ }
- $userrow["experience"] = number_format($userrow["experience"]);
- $userrow["gold"] = number_format($userrow["gold"]);
-
- $userrow["plusexp"] = $userrow["expbonus"] != 0
- ? "(" . ($userrow["expbonus"] > 0 ? "+" : "") . $userrow["expbonus"] . "%)"
- : "";
-
- $userrow["plusgold"] = $userrow["goldbonus"] != 0
- ? "(" . ($userrow["goldbonus"] > 0 ? "+" : "") . $userrow["goldbonus"] . "%)"
- : "";
+ 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);
- $userrow["nextlevel"] = $userrow['level'] < 99 ? number_format($levelrow[$userrow["charclass"]."_exp"]) : 'None';
- $userrow['charclass'] = match ((int) $userrow['charclass']) {
- 1 => $controlrow["class1name"],
- 2 => $controlrow["class2name"],
- 3 => $controlrow["class3name"]
- };
+ $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"]."
";
+ }
+ if ($magic_list == "") $magic_list = "None";
- display(parsetemplate(gettemplate("onlinechar"), $userrow), "Character Information");
+ $showchar = render('showchar', [
+ 'char' => $userrow,
+ 'level' => $levelrow,
+ 'magic_list' => $magic_list,
+ 'controlrow' => $controlrow
+ ]);
+ echo render('minimal', ['content' => $showchar, 'title' => $userrow['username'].' Information']);
}
function showmap()
diff --git a/src/lib.php b/src/lib.php
index e9219fd..2416257 100644
--- a/src/lib.php
+++ b/src/lib.php
@@ -138,7 +138,7 @@ function display($content, $title, $topnav=true, $leftnav=true, $rightnav=true,
} else {
$topnav = <<
-
+
HTML;
}
@@ -310,6 +310,16 @@ function get_town_by_id(int $id): array|false
return $query->fetchArray(SQLITE3_ASSOC);
}
+/**
+ * Get a user's data by their ID.
+ */
+function get_user_by_id(int $id): array|false
+{
+ $query = db()->query('SELECT * FROM users WHERE id = ? LIMIT 1;', [$id]);
+ if ($query === false) return false;
+ return $query->fetchArray(SQLITE3_ASSOC);
+}
+
/**
* Get an item by it's ID.
*/
diff --git a/templates/minimal.php b/templates/minimal.php
index 56f4d5a..dd6c7e7 100644
--- a/templates/minimal.php
+++ b/templates/minimal.php
@@ -3,7 +3,7 @@
= $title ?>
-
- = $content ?>
-
+ = $content ?>