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 = <<Log In - Register + Register Help 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 ?> -
- -
+ diff --git a/templates/primary.php b/templates/primary.php index 67e6fb2..54f3be0 100644 --- a/templates/primary.php +++ b/templates/primary.php @@ -9,13 +9,15 @@ $template = << diff --git a/templates/showchar.php b/templates/showchar.php index bf47026..ad6b372 100644 --- a/templates/showchar.php +++ b/templates/showchar.php @@ -1,46 +1,36 @@ - -Character - -{{username}}

+
Character
+

-Class: {{charclass}}

+Class: $controlrow["class1name"], + 2 => $controlrow["class2name"], + 3 => $controlrow["class3name"] +}; ?>

-Level: {{level}}
-Experience: {{experience}} {{plusexp}}
-Next Level: {{nextlevel}}
-Gold: {{gold}} {{plusgold}}
-Hit Points: {{currenthp}} / {{maxhp}}
-Magic Points: {{currentmp}} / {{maxmp}}
-Travel Points: {{currenttp}} / {{maxtp}}

+Level:
+Experience: + ( 0 ? '+' : '' ?>) %)
+Next Level: None
+Gold: + ( 0 ? '+' : '' ?>) %)
+Hit Points: /
+Magic Points: /
+Travel Points: /

-Strength: {{strength}}
-Dexterity: {{dexterity}}
-Attack Power: {{attackpower}}
-Defense Power: {{defensepower}}
- -
+Strength:
+Dexterity:
+Attack Power:
+Defense Power:
+
- - - -
Inventory
- - - - -
WeaponWeapon: {{weaponname}}
ArmorArmor: {{armorname}}
ShieldShield: {{shieldname}}
-Slot 1: {{slot1name}}
-Slot 2: {{slot2name}}
-Slot 3: {{slot3name}} -

+
Inventory
+Weapon Weapon:
+Armor Armor:
+Shield Shield:
+Slot 1:
+Slot 2:
+Slot 3: +
- - - -
Spells
-{{magiclist}} -

-THEVERYENDOFYOU; -?> +
Spells
+