Continue updating rendering and user access

This commit is contained in:
Sky Johnson 2024-12-18 17:00:23 -06:00
parent 58012f5808
commit 5fa648ab7b
16 changed files with 286 additions and 320 deletions

View File

@ -8,7 +8,7 @@ html {
} }
body { body {
background-image: url('/img/backgrounds/background.jpg'); background-image: url('/img/backgrounds/classic.jpg');
padding: 2rem; padding: 2rem;
} }
table { table {

View File

@ -50,13 +50,14 @@ $l = $r->lookup($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
if (is_int($l)) exit("Error: $l"); if (is_int($l)) exit("Error: $l");
$content = $l['handler'](...$l['params'] ?? []); $content = $l['handler'](...$l['params'] ?? []);
if (is_htmx()) { if (is_htmx() && $uri[0] !== 'babblebox') {
$content .= '<title>'.page_title().'</title>'; $content .= '<title>'.page_title().'</title>';
$content .= Render\debug_db_info(); $content .= Render\debug_db_info();
if (env('debug', false)) $content .= Render\debug_query_log(); if (env('debug', false)) $content .= Render\debug_query_log();
if ($GLOBALS['state']['user-state-changed'] ?? false) { if ($GLOBALS['state']['user-state-changed'] ?? false) {
$content .= Render\right_nav(); $content .= Render\right_nav();
$content .= Render\left_nav();
} }
} }
echo $content; echo $content;

View File

@ -10,12 +10,12 @@ function move() {
// Validate direction // Validate direction
$form = validate($_POST, ['direction' => ['in:north,west,east,south']]); $form = validate($_POST, ['direction' => ['in:north,west,east,south']]);
if (!$form['valid']) display(ul_from_validate_errors($form['errors']), 'Move Error'); if (!$form['valid']) return display(ul_from_validate_errors($form['errors']), 'Move Error');
// Current game state // Current game state
$game_size = $controlrow['gamesize']; $game_size = $controlrow['gamesize'];
$latitude = user('latitude'); $latitude = user()->latitude;
$longitude = user('longitude'); $longitude = user()->longitude;
$direction = $form['data']['direction']; $direction = $form['data']['direction'];
// Calculate new coordinates with boundary checks // Calculate new coordinates with boundary checks
@ -37,20 +37,20 @@ function move() {
// Check for town // Check for town
$town = get_town_by_xy($longitude, $latitude); $town = get_town_by_xy($longitude, $latitude);
if ($town !== false) { if ($town !== false) {
Towns\travelto($town['id'], false); return Towns\travelto($town['id'], false);
return;
} }
// Determine action (1 in 5 chance of fighting) // Determine action (1 in 5 chance of fighting)
$action = (rand(1, 5) === 1) if (rand(1, 5) === 1) {
? "currentaction='Fighting', currentfight='1'," user()->currentaction = 'Fighting';
: "currentaction='Exploring',"; user()->currentfight = 1;
} else {
user()->currentaction = 'Exploring';
}
// Update user's position user()->latitude = $latitude;
db()->query( user()->longitude = $longitude;
"UPDATE users SET $action latitude = ?, longitude = ?, dropcode = 0 WHERE id = ?;", user()->save();
[$latitude, $longitude, user()->id]
);
redirect('/'); redirect('/');
} }

View File

@ -20,56 +20,55 @@ function register_routes(Router $r): Router
*/ */
function fight() function fight()
{ {
global $userrow; if (user()->currentaction !== 'Fighting') exit('Cheat attempt detected.<br><br>Get a life, loser.');
if ($userrow["currentaction"] != "Fighting") display("Cheat attempt detected.<br><br>Get a life, loser.", "Error");
$pagearray = ["magiclist" => "", "yourturn" => "", "monsterturn" => "", "monsterhp" => "", "command" => ""]; $page = ["magiclist" => "", "yourturn" => "", "monsterturn" => "", "monsterhp" => "", "command" => ""];
$playerisdead = 0; $playerisdead = 0;
// Populate magic list // Generate spell list
$userspells = explode(",", $userrow["spells"]); $user_spells = user()->spells();
$spellquery = db()->query('SELECT id, name FROM spells ORDER BY id;'); if (!empty($user_spells)) {
while ($spellrow = $spellquery->fetchArray(SQLITE3_ASSOC)) { $page['magiclist'] = '<select name="userspell">';
if (in_array($spellrow["id"], $userspells)) { foreach ($user_spells as $spell) {
$pagearray["magiclist"] .= "<option value=\"{$spellrow["id"]}\">{$spellrow["name"]}</option>\n"; $page['magiclist'] .= "<option value=\"{$spell['id']}\">{$spell['name']}</option>\n";
} }
} $page['magiclist'] .= '</select> <input type="submit" name="spell" value="Spell"><br><br>';
$pagearray["magiclist"] = $pagearray["magiclist"] ?: "<option value=\"0\">None</option>\n"; }
$magiclist = $pagearray["magiclist"];
// Determine initial combat parameters // Determine initial combat parameters
$chancetoswingfirst = rand(1, 10) + (int)ceil(sqrt($userrow["dexterity"])); $chancetoswingfirst = rand(1, 10) + (int)ceil(sqrt(user()->dexterity));
if ($userrow["currentfight"] == 1) { if (user()->currentfight === 1) {
$maxlevel = (int)floor(max(abs($userrow["latitude"]) + 5, abs($userrow["longitude"]) + 5) / 5); $maxlevel = (int)floor(max(abs(user()->latitude) + 5, abs(user()->longitude) + 5) / 5);
$minlevel = max(1, $maxlevel - 2); $minlevel = max(1, $maxlevel - 2);
$monsterrow = db()->query('SELECT * FROM monsters WHERE level >= ? AND level <= ? ORDER BY RANDOM() LIMIT 1;', [ $monster = db()->query('SELECT * FROM monsters WHERE level >= ? AND level <= ? ORDER BY RANDOM() LIMIT 1;', [
$minlevel, $maxlevel $minlevel, $maxlevel
])->fetchArray(SQLITE3_ASSOC); ])->fetchArray(SQLITE3_ASSOC);
$userrow["currentmonster"] = $monsterrow["id"]; user()->currentmonster = $monster["id"];
$userrow["currentmonsterhp"] = rand((int)(($monsterrow["maxhp"]/5)*4), $monsterrow["maxhp"]); user()->currentmonsterhp = rand((int)(($monster["maxhp"]/5)*4), $monster["maxhp"]);
$userrow["currentmonstersleep"] = 0; user()->currentmonstersleep = 0;
$userrow["currentmonsterimmune"] = $monsterrow["immune"]; user()->currentmonsterimmune = $monster["immune"];
$chancetoswingfirst = ($chancetoswingfirst > (rand(1,7) + (int)ceil(sqrt($monsterrow["maxdam"])))) ? 1 : 0; $chancetoswingfirst = ($chancetoswingfirst > (rand(1,7) + (int)ceil(sqrt($monster["maxdam"])))) ? 1 : 0;
} }
// Get monster statistics // Get monster statistics
$monsterrow = get_monster($userrow['currentmonster']); $monster = get_monster(user()->currentmonster);
$pagearray["monstername"] = $monsterrow["name"]; $page['monstername'] = $monster['name'];
// Run action // Run action
if (isset($_POST["run"])) { if (isset($_POST["run"])) {
$chancetorun = rand(4,10) + (int)ceil(sqrt($userrow["dexterity"])); $chancetorun = rand(4,10) + (int)ceil(sqrt(user()->dexterity));
if ($chancetorun <= (rand(1,5) + (int)ceil(sqrt($monsterrow["maxdam"])))) { if ($chancetorun <= (rand(1,5) + (int)ceil(sqrt($monster["maxdam"])))) {
$pagearray["yourturn"] = "You tried to run away, but were blocked in front!<br><br>"; $page["yourturn"] = "You tried to run away, but were blocked in front!<br><br>";
$pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "<br><br>"; $page["monsterhp"] = "Monster's HP: " . user()->currentmonsterhp . "<br><br>";
// Monster turn logic (similar to original function) // Monster turn logic (similar to original function)
$pagearray["monsterturn"] = handleMonsterTurn($userrow, $monsterrow); $page["monsterturn"] = handleMonsterTurn($userrow, $monster);
db()->query("UPDATE users SET currentaction='Exploring' WHERE id=?;", [$userrow['id']]); user()->currentaction = 'Exploring';
user()->save();
redirect('/'); redirect('/');
} }
} }
@ -77,147 +76,125 @@ function fight()
// Fight action // Fight action
if (isset($_POST["fight"])) { if (isset($_POST["fight"])) {
// Player's attack // Player's attack
$min = (int)($userrow["attackpower"] * 0.75); $min = (int)(user()->attackpower * 0.75);
$max = (int)($userrow["attackpower"] / 3); $max = (int)(user()->attackpower / 3);
$tohit = (int)ceil(mt_rand(min($min, $max), max($min, $max))); $tohit = (int)ceil(mt_rand(min($min, $max), max($min, $max)));
$toexcellent = rand(1,150); $toexcellent = rand(1,150);
if ($toexcellent <= sqrt($userrow["strength"])) { if ($toexcellent <= sqrt(user()->strength)) {
$tohit *= 2; $tohit *= 2;
$pagearray["yourturn"] .= "Excellent hit!<br>"; $page["yourturn"] .= "Excellent hit!<br>";
} }
$min = (int)($monsterrow["armor"] * 0.75); $min = (int)($monster["armor"] * 0.75);
$max = (int)$monsterrow["armor"]; $max = (int)$monster["armor"];
$toblock = (int)ceil(rand(min($min, $max), max($min, $max)) / 3); $toblock = (int)ceil(rand(min($min, $max), max($min, $max)) / 3);
$tododge = rand(1,200); $tododge = rand(1, 100);
$monsterdamage = max(1, $tohit - $toblock); $monsterdamage = max(1, $tohit - $toblock);
if ($tododge <= sqrt($monsterrow["armor"])) { if ($tododge <= sqrt($monster["armor"])) {
$monsterdamage = 0; $monsterdamage = 0;
$pagearray["yourturn"] .= "The monster is dodging. No damage has been scored.<br>"; $page["yourturn"] .= "The monster is dodging. No damage has been scored.<br>";
} }
if ($userrow["currentuberdamage"] != 0) { if (user()->currentuberdamage != 0) {
$monsterdamage += (int)ceil($monsterdamage * ($userrow["currentuberdamage"]/100)); $monsterdamage += (int)ceil($monsterdamage * (user()->currentuberdamage / 100));
} }
$userrow["currentmonsterhp"] -= $monsterdamage; user()->currentmonsterhp -= $monsterdamage;
$pagearray["yourturn"] .= "You attack the monster for $monsterdamage damage.<br><br>"; $page["yourturn"] .= "You attack the monster for $monsterdamage damage.<br><br>";
$pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "<br><br>"; $page["monsterhp"] = "Monster's HP: " . user()->currentmonsterhp . "<br><br>";
// Check for monster defeat // Check for monster defeat
if ($userrow["currentmonsterhp"] <= 0) { if (user()->currentmonsterhp <= 0) {
db()->query('UPDATE users SET currentmonsterhp=0 WHERE id=?;', [$userrow['id']]); user()->currentmonsterhp = 0;
user()->save();
redirect('/victory'); redirect('/victory');
} }
// Monster's turn // Monster's turn
$pagearray["monsterturn"] = handleMonsterTurn($userrow, $monsterrow); $page["monsterturn"] = handleMonsterTurn($userrow, $monster);
} }
// Spell action // Spell action
if (isset($_POST["spell"])) { if (isset($_POST["spell"])) {
$pickedspell = $_POST["userspell"]; $pickedspell = $_POST["userspell"];
if ($pickedspell == 0) { if ($pickedspell == 0) {
display("You must select a spell first. Please go back and try again.", "Error"); return display("You must select a spell first. Please go back and try again.", "Error");
die(); die();
} }
$newspellrow = get_spell($pickedspell); $newspellrow = get_spell($pickedspell);
$spell = in_array($pickedspell, $userspells); $spell = in_array($pickedspell, explode(',', user()->spells));
if (!$spell) { if (!$spell) {
display("You have not yet learned this spell. Please go back and try again.", "Error"); return display("You have not yet learned this spell. Please go back and try again.", "Error");
die(); die();
} }
if ($userrow["currentmp"] < $newspellrow["mp"]) { if (user()->currentmp < $newspellrow["mp"]) {
display("You do not have enough Magic Points to cast this spell. Please go back and try again.", "Error"); return display("You do not have enough Magic Points to cast this spell. Please go back and try again.", "Error");
die(); die();
} }
// Spell type handling (similar to original function) // Spell type handling (similar to original function)
$pagearray["yourturn"] = handleSpellCast($userrow, $newspellrow); $page["yourturn"] = handleSpellCast($userrow, $newspellrow);
$pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "<br><br>"; $page["monsterhp"] = "Monster's HP: " . user()->currentmonsterhp . "<br><br>";
// Check for monster defeat // Check for monster defeat
if ($userrow["currentmonsterhp"] <= 0) { if (user()->currentmonsterhp <= 0) {
db()->query('UPDATE users SET currentmonsterhp=0, currenthp=?, currentmp=? WHERE id=?;', [ user()->currentmonsterhp = 0;
$userrow['currenthp'], $userrow['currentmp'], $userrow['id'] user()->save();
]);
redirect('/victory'); redirect('/victory');
} }
// Monster's turn // Monster's turn
$pagearray["monsterturn"] = handleMonsterTurn($userrow, $monsterrow); $page["monsterturn"] = handleMonsterTurn($userrow, $monster);
} }
// Monster's turn if player lost first swing // Monster's turn if player lost first swing
if (!isset($_POST["run"]) && !isset($_POST["fight"]) && !isset($_POST["spell"]) && $chancetoswingfirst == 0) { if (!isset($_POST["run"]) && !isset($_POST["fight"]) && !isset($_POST["spell"]) && $chancetoswingfirst == 0) {
$pagearray["yourturn"] = "The monster attacks before you are ready!<br><br>"; $page["yourturn"] = "The monster attacks before you are ready!<br><br>";
$pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "<br><br>"; $page["monsterhp"] = "Monster's HP: " . user()->currentmonsterhp . "<br><br>";
$pagearray["monsterturn"] = handleMonsterTurn($userrow, $monsterrow); $page["monsterturn"] = handleMonsterTurn($userrow, $monster);
} }
// Prepare command or death message // Prepare command or death message
if ($playerisdead != 1) { if ($playerisdead != 1) {
$pagearray["command"] = <<<HTML $page["command"] = <<<HTML
Command?<br><br> Command?<br><br>
<form action="/fight" method="post"> <form action="/fight" method="post">
<input type="submit" name="fight" value="Fight" /><br><br> <input type="submit" name="fight" value="Fight"><br><br>
<select name="userspell"><option value="0">Choose One</option>$magiclist</select> <input type="submit" name="spell" value="Spell" /><br><br> {$page['magiclist']}
<input type="submit" name="run" value="Run" /><br><br> <input type="submit" name="run" value="Run"><br><br>
</form> </form>
HTML; HTML;
db()->query("UPDATE users SET user()->currentfight += 1;
currentaction='Fighting',
currenthp=?,
currentmp=?,
currentfight=?,
currentmonster=?,
currentmonsterhp=?,
currentmonstersleep=?,
currentmonsterimmune=?,
currentuberdamage=?,
currentuberdefense=?
WHERE id=?;", [
$userrow['currenthp'],
$userrow['currentmp'],
$userrow['currentfight'] + 1,
$userrow['currentmonster'],
$userrow['currentmonsterhp'],
$userrow['currentmonstersleep'],
$userrow['currentmonsterimmune'],
$userrow['currentuberdamage'],
$userrow['currentuberdefense'],
$userrow['id']
]);
} else { } else {
$pagearray["command"] = "<b>You have died.</b><br><br>As a consequence, you've lost half of your gold. However, you have been given back a portion of your hit points to continue your journey.<br><br>You may now continue back to <a href=\"/\">town</a>, and we hope you fair better next time."; $pagearray["command"] = "<b>You have died.</b><br><br>As a consequence, you've lost half of your gold. However, you have been given back a portion of your hit points to continue your journey.<br><br>You may now continue back to <a href=\"/\">town</a>, and we hope you fair better next time.";
} }
user()->save();
// Finalize page and display it // Finalize page and display it
display(render('fight', ['page' => $pagearray]), "Fighting"); return display(render('fight', ['page' => $page]), "Fighting");
} }
function victory() function victory()
{ {
global $userrow; if (user()->currentmonsterhp != 0) redirect('/fight');
if (user()->currentfight == 0) redirect('/');
if ($userrow["currentmonsterhp"] != 0) redirect('/fight'); $monsterrow = get_monster(user()->currentmonster);
if ($userrow["currentfight"] == 0) redirect('/');
$monsterrow = get_monster($userrow['currentmonster']);
$min = (int)(($monsterrow["maxexp"] / 6) * 5); $min = (int)(($monsterrow["maxexp"] / 6) * 5);
$max = (int)$monsterrow["maxexp"]; $max = (int)$monsterrow["maxexp"];
$exp = mt_rand(min($min, $max), max($min, $max)); $exp = mt_rand(min($min, $max), max($min, $max));
if ($exp < 1) { $exp = 1; } if ($exp < 1) { $exp = 1; }
if ($userrow["expbonus"] != 0) { $exp += ceil(($userrow["expbonus"]/100)*$exp); } if (user()->expbonus != 0) { $exp += ceil((user()->expbonus/100)*$exp); }
$min = (int)(($monsterrow["maxgold"] / 6) * 5); $min = (int)(($monsterrow["maxgold"] / 6) * 5);
$max = (int)$monsterrow["maxgold"]; $max = (int)$monsterrow["maxgold"];
@ -225,42 +202,33 @@ function victory()
$gold = mt_rand(min($min, $max), max($min, $max)); $gold = mt_rand(min($min, $max), max($min, $max));
if ($gold < 1) { $gold = 1; } if ($gold < 1) { $gold = 1; }
if ($userrow["goldbonus"] != 0) { $gold += ceil(($userrow["goldbonus"]/100)*$exp); } if (user()->goldbonus != 0) { $gold += ceil((user()->goldbonus/100)*$exp); }
if ($userrow["experience"] + $exp < 16777215) { $newexp = $userrow["experience"] + $exp; $warnexp = ""; } else { $newexp = $userrow["experience"]; $exp = 0; $warnexp = "You have maxed out your experience points."; } if (user()->experience + $exp < 16777215) { $newexp = user()->experience += $exp; $warnexp = ""; } else { $newexp = user()->experience; $exp = 0; $warnexp = "You have maxed out your experience points."; }
if ($userrow["gold"] + $gold < 16777215) { $newgold = $userrow["gold"] + $gold; $warngold = ""; } else { $newgold = $userrow["gold"]; $gold = 0; $warngold = "You have maxed out your experience points."; } if (user()->gold + $gold < 16777215) { $newgold = user()->gold += $gold; $warngold = ""; } else { $newgold = user()->gold; $gold = 0; $warngold = "You have maxed out your gold."; }
$levelrow = db()->query('SELECT * FROM levels WHERE id=? LIMIT 1;', [$userrow['level'] + 1])->fetchArray(SQLITE3_ASSOC); $levelrow = db()->query('SELECT * FROM levels WHERE id=? LIMIT 1;', [user()->level + 1])->fetchArray(SQLITE3_ASSOC);
if ($userrow["level"] < 100) { if (user()->level < 100) {
if ($newexp >= $levelrow[$userrow["charclass"]."_exp"]) { if ($newexp >= $levelrow[user()->charclass."_exp"]) {
$newhp = $userrow["maxhp"] + $levelrow[$userrow["charclass"]."_hp"]; user()->maxhp += $levelrow[user()->charclass."_hp"];
$newmp = $userrow["maxmp"] + $levelrow[$userrow["charclass"]."_mp"]; user()->maxmp += $levelrow[user()->charclass."_mp"];
$newtp = $userrow["maxtp"] + $levelrow[$userrow["charclass"]."_tp"]; user()->maxtp += $levelrow[user()->charclass."_tp"];
$newstrength = $userrow["strength"] + $levelrow[$userrow["charclass"]."_strength"]; user()->strength += $levelrow[user()->charclass."_strength"];
$newdexterity = $userrow["dexterity"] + $levelrow[$userrow["charclass"]."_dexterity"]; user()->dexterity += $levelrow[user()->charclass."_dexterity"];
$newattack = $userrow["attackpower"] + $levelrow[$userrow["charclass"]."_strength"]; user()->attackpower += $levelrow[user()->charclass."_strength"];
$newdefense = $userrow["defensepower"] + $levelrow[$userrow["charclass"]."_dexterity"]; user()->defensepower += $levelrow[user()->charclass."_dexterity"];
user()->level += 1;
$newlevel = $levelrow["id"]; $newlevel = $levelrow["id"];
if ($levelrow[$userrow["charclass"]."_spells"] != 0) { if ($levelrow[user()->charclass."_spells"] != 0) {
$userspells = $userrow["spells"] . ",".$levelrow[$userrow["charclass"]."_spells"]; user()->spells .= ",".$levelrow[user()->charclass."_spells"];
$newspell = "spells='$userspells',";
$spelltext = "You have learned a new spell.<br>"; $spelltext = "You have learned a new spell.<br>";
} else { $spelltext = ""; $newspell=""; } } else { $spelltext = ""; $newspell=""; }
$page = "Congratulations. You have defeated the ".$monsterrow["name"].".<br>You gain $exp experience. $warnexp <br>You gain $gold gold. $warngold <br><br><b>You have gained a level!</b><br><br>You gain ".$levelrow[$userrow["charclass"]."_hp"]." hit points.<br>You gain ".$levelrow[$userrow["charclass"]."_mp"]." magic points.<br>You gain ".$levelrow[$userrow["charclass"]."_tp"]." travel points.<br>You gain ".$levelrow[$userrow["charclass"]."_strength"]." strength.<br>You gain ".$levelrow[$userrow["charclass"]."_dexterity"]." dexterity.<br>$spelltext<br>You can now continue <a href=\"/\">exploring</a>."; $page = "Congratulations. You have defeated the ".$monsterrow["name"].".<br>You gain $exp experience. $warnexp <br>You gain $gold gold. $warngold <br><br><b>You have gained a level!</b><br><br>You gain ".$levelrow[user()->charclass."_hp"]." hit points.<br>You gain ".$levelrow[user()->charclass."_mp"]." magic points.<br>You gain ".$levelrow[user()->charclass."_tp"]." travel points.<br>You gain ".$levelrow[user()->charclass."_strength"]." strength.<br>You gain ".$levelrow[user()->charclass."_dexterity"]." dexterity.<br>$spelltext<br>You can now continue <a href=\"/\">exploring</a>.";
$title = "Courage and Wit have served thee well!"; $title = "Courage and Wit have served thee well!";
$dropcode = ""; $dropcode = "";
} else { } else {
$newhp = $userrow["maxhp"];
$newmp = $userrow["maxmp"];
$newtp = $userrow["maxtp"];
$newstrength = $userrow["strength"];
$newdexterity = $userrow["dexterity"];
$newattack = $userrow["attackpower"];
$newdefense = $userrow["defensepower"];
$newlevel = $userrow["level"];
$newspell = "";
$page = "Congratulations. You have defeated the ".$monsterrow["name"].".<br>You gain $exp experience. $warnexp <br>You gain $gold gold. $warngold <br><br>"; $page = "Congratulations. You have defeated the ".$monsterrow["name"].".<br>You gain $exp experience. $warnexp <br>You gain $gold gold. $warngold <br><br>";
if (rand(1, 30) === 1) { if (rand(1, 30) === 1) {
@ -276,74 +244,82 @@ function victory()
} }
} }
db()->query("UPDATE users SET currentaction='Exploring', level=?, maxhp=?, maxmp=?, maxtp=?, strength=?, dexterity=?, attackpower=?, defensepower=?, $newspell currentfight=0, currentmonster=0, currentmonsterhp=0, currentmonstersleep=0, currentmonsterimmune=0, currentuberdamage=0, currentuberdefense=0,$dropcode experience=?, gold=? WHERE id=?;", [ user()->currentaction = 'Exploring';
$newlevel, $newhp, $newmp, $newtp, $newstrength, $newdexterity, $newattack, $newdefense, $newexp, $newgold, $userrow['id'] user()->currentfight = 0;
]); user()->currentuberdamage = 0;
user()->currentuberdefense = 0;
user()->currentmonstersleep = 0;
user()->currentmonsterimmune = 0;
user()->save();
display($page, $title); return display($page, $title);
} }
function drop() function drop()
{ {
global $userrow; if (user()->dropcode == 0) redirect('/');
if ($userrow["dropcode"] == 0) redirect('/'); $droprow = get_drop(user()->dropcode);
$droprow = get_drop($userrow['dropcode']);
if (isset($_POST["submit"])) { if (isset($_POST["submit"])) {
$slot = $_POST["slot"]; $slot = $_POST["slot"];
if ($slot == 0) { display("Please go back and select an inventory slot to continue.","Error"); } if ($slot == 0) { return display("Please go back and select an inventory slot to continue.","Error"); }
if ($userrow["slot{$slot}id"] != 0) { $slotstr = 'slot'.$slot.'id';
$slotrow = get_drop($userrow["slot{$slot}id"]); if (user()->$slotstr != 0) {
$slotrow = get_drop(user()->$slotstr);
$old1 = explode(",",$slotrow["attribute1"]); $old1 = explode(",",$slotrow["attribute1"]);
if ($slotrow["attribute2"] != "X") { $old2 = explode(",",$slotrow["attribute2"]); } else { $old2 = array(0=>"maxhp",1=>0); } if ($slotrow["attribute2"] != "X") { $old2 = explode(",",$slotrow["attribute2"]); } else { $old2 = array(0=>"maxhp",1=>0); }
$new1 = explode(",",$droprow["attribute1"]); $new1 = explode(",",$droprow["attribute1"]);
if ($droprow["attribute2"] != "X") { $new2 = explode(",",$droprow["attribute2"]); } else { $new2 = array(0=>"maxhp",1=>0); } if ($droprow["attribute2"] != "X") { $new2 = explode(",",$droprow["attribute2"]); } else { $new2 = array(0=>"maxhp",1=>0); }
$userrow[$old1[0]] -= $old1[1]; user()->$old1[0] -= $old1[1];
$userrow[$old2[0]] -= $old2[1]; user()->$old2[0] -= $old2[1];
if ($old1[0] == "strength") { $userrow["attackpower"] -= $old1[1]; } if ($old1[0] == "strength") { user()->attackpower -= $old1[1]; }
if ($old1[0] == "dexterity") { $userrow["defensepower"] -= $old1[1]; } if ($old1[0] == "dexterity") { user()->defensepower -= $old1[1]; }
if ($old2[0] == "strength") { $userrow["attackpower"] -= $old2[1]; } if ($old2[0] == "strength") { user()->attackpower -= $old2[1]; }
if ($old2[0] == "dexterity") { $userrow["defensepower"] -= $old2[1]; } if ($old2[0] == "dexterity") { user()->defensepower -= $old2[1]; }
$userrow[$new1[0]] += $new1[1]; user()->$new1[0] += $new1[1];
$userrow[$new2[0]] += $new2[1]; user()->$new2[0] += $new2[1];
if ($new1[0] == "strength") { $userrow["attackpower"] += $new1[1]; } if ($new1[0] == "strength") { user()->attackpower += $new1[1]; }
if ($new1[0] == "dexterity") { $userrow["defensepower"] += $new1[1]; } if ($new1[0] == "dexterity") { user()->defensepower += $new1[1]; }
if ($new2[0] == "strength") { $userrow["attackpower"] += $new2[1]; } if ($new2[0] == "strength") { user()->attackpower += $new2[1]; }
if ($new2[0] == "dexterity") { $userrow["defensepower"] += $new2[1]; } if ($new2[0] == "dexterity") { user()->defensepower += $new2[1]; }
if ($userrow["currenthp"] > $userrow["maxhp"]) { $userrow["currenthp"] = $userrow["maxhp"]; } if (user()->currenthp > user()->maxhp) { user()->currenthp = user()->maxhp; }
if ($userrow["currentmp"] > $userrow["maxmp"]) { $userrow["currentmp"] = $userrow["maxmp"]; } if (user()->currentmp > user()->maxmp) { user()->currentmp = user()->maxmp; }
if ($userrow["currenttp"] > $userrow["maxtp"]) { $userrow["currenttp"] = $userrow["maxtp"]; } if (user()->currenttp > user()->maxtp) { user()->currenttp = user()->maxtp; }
$slot_s = 'slot'.$_POST["slot"]; $slot_s = 'slot'.$_POST["slot"];
db()->query("UPDATE users SET {$slot_s}name=?, {$slot_s}id=?, {$old1[0]}=?, {$old2[0]}=?, {$new1[0]}=?, {$new2[0]}=?, attackpower=?, defensepower=?, currenthp=?, currentmp=?, currenttp=?, dropcode=0 WHERE id=?;", [ $slot_name = "{$slot_s}name";
$droprow["name"], $droprow["id"], $userrow[$old1[0]], $userrow[$old2[0]], $userrow[$new1[0]], $userrow[$new2[0]], $userrow["attackpower"], $userrow["defensepower"], $userrow["currenthp"], $userrow["currentmp"], $userrow["currenttp"], $userrow['id'] $slot_id = "{$slot_s}id";
]);
user()->$slot_name = $droprow['name'];
user()->$slot_id = $droprow['id'];
} else { } else {
$new1 = explode(",",$droprow["attribute1"]); $new1 = explode(",",$droprow["attribute1"]);
if ($droprow["attribute2"] != "X") { $new2 = explode(",",$droprow["attribute2"]); } else { $new2 = array(0=>"maxhp",1=>0); } if ($droprow["attribute2"] != "X") { $new2 = explode(",",$droprow["attribute2"]); } else { $new2 = array(0=>"maxhp",1=>0); }
$userrow[$new1[0]] += $new1[1]; user()->$new1[0] += $new1[1];
$userrow[$new2[0]] += $new2[1]; user()->$new2[0] += $new2[1];
if ($new1[0] == "strength") { $userrow["attackpower"] += $new1[1]; } if ($new1[0] == "strength") { user()->attackpower += $new1[1]; }
if ($new1[0] == "dexterity") { $userrow["defensepower"] += $new1[1]; } if ($new1[0] == "dexterity") { user()->defensepower += $new1[1]; }
if ($new2[0] == "strength") { $userrow["attackpower"] += $new2[1]; } if ($new2[0] == "strength") { user()->attackpower += $new2[1]; }
if ($new2[0] == "dexterity") { $userrow["defensepower"] += $new2[1]; } if ($new2[0] == "dexterity") { user()->defensepower += $new2[1]; }
$slot_s = 'slot'.$_POST["slot"]; $slot_s = 'slot'.$_POST["slot"];
db()->query("UPDATE users SET {$slot_s}name=?, {$slot_s}id=?, {$new1[0]}=?, {$new2[0]}=?, attackpower=?, defensepower=?, currenthp=?, currentmp=?, currenttp=?, dropcode=0 WHERE id=?;", [ $slot_name = "{$slot_s}name";
$droprow["name"], $droprow["id"], $userrow[$new1[0]], $userrow[$new2[0]], $userrow["attackpower"], $userrow["defensepower"], $userrow["currenthp"], $userrow["currentmp"], $userrow["currenttp"], $userrow['id'] $slot_id = "{$slot_s}id";
]);
user()->$slot_name = $droprow['name'];
user()->$slot_id = $droprow['id'];
} }
display("The item has been equipped. You can now continue <a href=\"/\">exploring</a>.", "Item Drop"); user()->save();
return display("The item has been equipped. You can now continue <a href=\"/\">exploring</a>.", "Item Drop");
} }
$attributearray = array("maxhp"=>"Max HP", $attributearray = array("maxhp"=>"Max HP",
@ -370,10 +346,10 @@ function drop()
} }
$page .= "<br>Select an inventory slot from the list below to equip this item. If the inventory slot is already full, the old item will be discarded."; $page .= "<br>Select an inventory slot from the list below to equip this item. If the inventory slot is already full, the old item will be discarded.";
$page .= "<form action=\"/drop\" method=\"post\"><select name=\"slot\"><option value=\"0\">Choose One</option><option value=\"1\">Slot 1: ".$userrow["slot1name"]."</option><option value=\"2\">Slot 2: ".$userrow["slot2name"]."</option><option value=\"3\">Slot 3: ".$userrow["slot3name"]."</option></select> <input type=\"submit\" name=\"submit\" value=\"Submit\" /></form>"; $page .= "<form action=\"/drop\" method=\"post\"><select name=\"slot\"><option value=\"0\">Choose One</option><option value=\"1\">Slot 1: ".user()->slot1name."</option><option value=\"2\">Slot 2: ".user()->slot2name."</option><option value=\"3\">Slot 3: ".user()->slot3name."</option></select> <input type=\"submit\" name=\"submit\" value=\"Submit\" /></form>";
$page .= "You may also choose to just continue <a href=\"/\">exploring</a> and give up this item."; $page .= "You may also choose to just continue <a href=\"/\">exploring</a> and give up this item.";
display($page, "Item Drop"); return display($page, "Item Drop");
} }
@ -385,45 +361,45 @@ function dead()
to continue your journey.<br><br> to continue your journey.<br><br>
You may now continue back to <a href="/">town</a>, and we hope you fair better next time. You may now continue back to <a href="/">town</a>, and we hope you fair better next time.
HTML; HTML;
display($page, 'You Died'); return display($page, 'You Died');
} }
function handleMonsterTurn(&$userrow, $monsterrow) function handleMonsterTurn(&$userrow, $monsterrow)
{ {
$pagearray = ""; $pagearray = "";
if ($userrow["currentmonstersleep"] != 0) { if (user()->currentmonstersleep != 0) {
$chancetowake = rand(1,15); $chancetowake = rand(1,15);
if ($chancetowake > $userrow["currentmonstersleep"]) { if ($chancetowake > user()->currentmonstersleep) {
$userrow["currentmonstersleep"] = 0; user()->currentmonstersleep = 0;
$pagearray .= "The monster has woken up.<br>"; $pagearray .= "The monster has woken up.<br>";
} else { } else {
$pagearray .= "The monster is still asleep.<br>"; $pagearray .= "The monster is still asleep.<br>";
} }
} }
if ($userrow["currentmonstersleep"] == 0) { if (user()->currentmonstersleep == 0) {
$tohit = (int)ceil(mt_rand((int)($monsterrow["maxdam"] * 0.5), (int)$monsterrow["maxdam"])); $tohit = (int)ceil(mt_rand((int)($monsterrow["maxdam"] * 0.5), (int)$monsterrow["maxdam"]));
$toblock = (int)ceil(mt_rand((int)($userrow["defensepower"] * 0.75), (int)$userrow["defensepower"]) / 4); $toblock = (int)ceil(mt_rand((int)(user()->defensepower * 0.75), (int)user()->defensepower) / 4);
$tododge = rand(1, 150); $tododge = rand(1, 150);
if ($tododge <= sqrt($userrow["dexterity"])) { if ($tododge <= sqrt(user()->dexterity)) {
$tohit = 0; $tohit = 0;
$pagearray .= "You dodge the monster's attack. No damage has been scored.<br>"; $pagearray .= "You dodge the monster's attack. No damage has been scored.<br>";
$persondamage = 0; $persondamage = 0;
} else { } else {
$persondamage = max(1, $tohit - $toblock); $persondamage = max(1, $tohit - $toblock);
if ($userrow["currentuberdefense"] != 0) { if (user()->currentuberdefense != 0) {
$persondamage -= (int)ceil($persondamage * ($userrow["currentuberdefense"]/100)); $persondamage -= (int)ceil($persondamage * (user()->currentuberdefense/100));
} }
$persondamage = max(1, $persondamage); $persondamage = max(1, $persondamage);
} }
$pagearray .= "The monster attacks you for $persondamage damage.<br><br>"; $pagearray .= "The monster attacks you for $persondamage damage.<br><br>";
$userrow["currenthp"] -= $persondamage; user()->currenthp -= $persondamage;
if ($userrow["currenthp"] <= 0) { if (user()->currenthp <= 0) {
$newgold = (int)ceil($userrow["gold"]/2); $newgold = (int)ceil(user()->gold/2);
$newhp = (int)ceil($userrow["maxhp"]/4); $newhp = (int)ceil(user()->maxhp/4);
db()->query("UPDATE users SET currenthp=?, currentaction='In Town', currentmonster=0, currentmonsterhp=0, currentmonstersleep=0, currentmonsterimmune=0, currentfight=0, latitude=0, longitude=0, gold=? WHERE id=?;", [ db()->query("UPDATE users SET currenthp=?, currentaction='In Town', currentmonster=0, currentmonsterhp=0, currentmonstersleep=0, currentmonsterimmune=0, currentfight=0, latitude=0, longitude=0, gold=? WHERE id=?;", [
$newhp, $newgold, $userrow['id'] $newhp, $newgold, $userrow['id']
]); ]);
@ -438,38 +414,38 @@ function handleSpellCast(&$userrow, $newspellrow)
$pagearray = ""; $pagearray = "";
switch ($newspellrow["type"]) { switch ($newspellrow["type"]) {
case 1: // Heal spell case 1: // Heal spell
$newhp = min($userrow["currenthp"] + $newspellrow["attribute"], $userrow["maxhp"]); $newhp = min(user()->currenthp + $newspellrow["attribute"], user()->maxhp);
$userrow["currenthp"] = $newhp; user()->currenthp = $newhp;
$userrow["currentmp"] -= $newspellrow["mp"]; user()->currentmp -= $newspellrow["mp"];
$pagearray = "You have cast the {$newspellrow["name"]} spell, and gained {$newspellrow["attribute"]} Hit Points.<br><br>"; $pagearray = "You have cast the {$newspellrow["name"]} spell, and gained {$newspellrow["attribute"]} Hit Points.<br><br>";
break; break;
case 2: // Hurt spell case 2: // Hurt spell
if ($userrow["currentmonsterimmune"] == 0) { if (user()->currentmonsterimmune == 0) {
$monsterdamage = mt_rand((int)(($newspellrow["attribute"]/6)*5), $newspellrow["attribute"]); $monsterdamage = mt_rand((int)(($newspellrow["attribute"]/6)*5), $newspellrow["attribute"]);
$userrow["currentmonsterhp"] -= $monsterdamage; user()->currentmonsterhp -= $monsterdamage;
$pagearray = "You have cast the {$newspellrow["name"]} spell for $monsterdamage damage.<br><br>"; $pagearray = "You have cast the {$newspellrow["name"]} spell for $monsterdamage damage.<br><br>";
} else { } else {
$pagearray = "You have cast the {$newspellrow["name"]} spell, but the monster is immune to it.<br><br>"; $pagearray = "You have cast the {$newspellrow["name"]} spell, but the monster is immune to it.<br><br>";
} }
$userrow["currentmp"] -= $newspellrow["mp"]; user()->currentmp -= $newspellrow["mp"];
break; break;
case 3: // Sleep spell case 3: // Sleep spell
if ($userrow["currentmonsterimmune"] != 2) { if (user()->currentmonsterimmune != 2) {
$userrow["currentmonstersleep"] = $newspellrow["attribute"]; user()->currentmonstersleep = $newspellrow["attribute"];
$pagearray = "You have cast the {$newspellrow["name"]} spell. The monster is asleep.<br><br>"; $pagearray = "You have cast the {$newspellrow["name"]} spell. The monster is asleep.<br><br>";
} else { } else {
$pagearray = "You have cast the {$newspellrow["name"]} spell, but the monster is immune to it.<br><br>"; $pagearray = "You have cast the {$newspellrow["name"]} spell, but the monster is immune to it.<br><br>";
} }
$userrow["currentmp"] -= $newspellrow["mp"]; user()->currentmp -= $newspellrow["mp"];
break; break;
case 4: // +Damage spell case 4: // +Damage spell
$userrow["currentuberdamage"] = $newspellrow["attribute"]; user()->currentuberdamage = $newspellrow["attribute"];
$userrow["currentmp"] -= $newspellrow["mp"]; user()->currentmp -= $newspellrow["mp"];
$pagearray = "You have cast the {$newspellrow["name"]} spell, and will gain {$newspellrow["attribute"]}% damage until the end of this fight.<br><br>"; $pagearray = "You have cast the {$newspellrow["name"]} spell, and will gain {$newspellrow["attribute"]}% damage until the end of this fight.<br><br>";
break; break;
case 5: // +Defense spell case 5: // +Defense spell
$userrow["currentuberdefense"] = $newspellrow["attribute"]; user()->currentuberdefense = $newspellrow["attribute"];
$userrow["currentmp"] -= $newspellrow["mp"]; user()->currentmp -= $newspellrow["mp"];
$pagearray = "You have cast the {$newspellrow["name"]} spell, and will gain {$newspellrow["attribute"]}% defense until the end of this fight.<br><br>"; $pagearray = "You have cast the {$newspellrow["name"]} spell, and will gain {$newspellrow["attribute"]}% defense until the end of this fight.<br><br>";
break; break;
} }

View File

@ -34,7 +34,7 @@ function donothing($start = 0)
$page .= "</table></td></tr></table>"; $page .= "</table></td></tr></table>";
display($page, "Forum"); return display($page, "Forum");
} }
function showthread($id, $start) function showthread($id, $start)
@ -49,7 +49,7 @@ function showthread($id, $start)
$page .= "</table></td></tr></table><br>"; $page .= "</table></td></tr></table><br>";
$page .= "<table width=\"100%\"><tr><td><b>Reply To This Thread:</b><br><form action=\"/forum/reply\" method=\"post\"><input type=\"hidden\" name=\"parent\" value=\"$id\" /><input type=\"hidden\" name=\"title\" value=\"Re: ".$title["title"]."\" /><textarea name=\"content\" rows=\"7\" cols=\"40\"></textarea><br><input type=\"submit\" name=\"submit\" value=\"Submit\" /> <input type=\"reset\" name=\"reset\" value=\"Reset\" /></form></td></tr></table>"; $page .= "<table width=\"100%\"><tr><td><b>Reply To This Thread:</b><br><form action=\"/forum/reply\" method=\"post\"><input type=\"hidden\" name=\"parent\" value=\"$id\" /><input type=\"hidden\" name=\"title\" value=\"Re: ".$title["title"]."\" /><textarea name=\"content\" rows=\"7\" cols=\"40\"></textarea><br><input type=\"submit\" name=\"submit\" value=\"Submit\" /> <input type=\"reset\" name=\"reset\" value=\"Reset\" /></form></td></tr></table>";
display($page, "Forum"); return display($page, "Forum");
} }
function reply() function reply()
@ -68,7 +68,7 @@ function reply()
$form = $form['data']; $form = $form['data'];
db()->query('INSERT INTO forum (author, title, content, parent) VALUES (?, ?, ?, ?);', [ db()->query('INSERT INTO forum (author, title, content, parent) VALUES (?, ?, ?, ?);', [
$userrow['username'], $form['title'], $form['content'], $form['parent'] user()->username, $form['title'], $form['content'], $form['parent']
]); ]);
db()->query('UPDATE forum SET newpostdate=CURRENT_TIMESTAMP, replies=replies + 1 WHERE id=?;', [$form['parent']]); db()->query('UPDATE forum SET newpostdate=CURRENT_TIMESTAMP, replies=replies + 1 WHERE id=?;', [$form['parent']]);
redirect("/forum/thread/{$form['parent']}/0"); redirect("/forum/thread/{$form['parent']}/0");
@ -76,8 +76,6 @@ function reply()
function newthread() function newthread()
{ {
global $userrow;
if (isset($_POST["submit"])) { if (isset($_POST["submit"])) {
$form = validate($_POST, [ $form = validate($_POST, [
'title' => ['length:2-30'], 'title' => ['length:2-30'],
@ -90,11 +88,11 @@ function newthread()
$form = $form['data']; $form = $form['data'];
db()->query('INSERT INTO forum (author, title, content) VALUES (?, ?, ?);', [ db()->query('INSERT INTO forum (author, title, content) VALUES (?, ?, ?);', [
$userrow['username'], $form['title'], $form['content'] user()->username, $form['title'], $form['content']
]); ]);
redirect('/forum'); redirect('/forum');
} }
$page = "<table width=\"100%\"><tr><td><b>Make A New Post:</b><br><br/ ><form action=\"/forum/new\" method=\"post\">Title:<br><input type=\"text\" name=\"title\" size=\"50\" maxlength=\"50\" /><br><br>Message:<br><textarea name=\"content\" rows=\"7\" cols=\"40\"></textarea><br><br><input type=\"submit\" name=\"submit\" value=\"Submit\" /> <input type=\"reset\" name=\"reset\" value=\"Reset\" /></form></td></tr></table>"; $page = "<table width=\"100%\"><tr><td><b>Make A New Post:</b><br><br/ ><form action=\"/forum/new\" method=\"post\">Title:<br><input type=\"text\" name=\"title\" size=\"50\" maxlength=\"50\" /><br><br>Message:<br><textarea name=\"content\" rows=\"7\" cols=\"40\"></textarea><br><br><input type=\"submit\" name=\"submit\" value=\"Submit\" /> <input type=\"reset\" name=\"reset\" value=\"Reset\" /></form></td></tr></table>";
display($page, "Forum"); return display($page, "Forum");
} }

View File

@ -12,11 +12,11 @@ function healspells($id)
// All the various ways to error out. // All the various ways to error out.
$spell = false; $spell = false;
foreach ($userspells as $b) if ($b == $id) $spell = true; foreach ($userspells as $b) if ($b == $id) $spell = true;
if ($spell !== true) display("You have not yet learned this spell. Please go back and try again.", "Error"); if ($spell !== true) return display("You have not yet learned this spell. Please go back and try again.", "Error");
if ($spellrow["type"] != 1) display("This is not a healing spell. Please go back and try again.", "Error"); if ($spellrow["type"] != 1) return display("This is not a healing spell. Please go back and try again.", "Error");
if ($userrow["currentmp"] < $spellrow["mp"]) display("You do not have enough Magic Points to cast this spell. Please go back and try again.", "Error"); if ($userrow["currentmp"] < $spellrow["mp"]) return display("You do not have enough Magic Points to cast this spell. Please go back and try again.", "Error");
if ($userrow["currentaction"] == "Fighting") display("You cannot use the Quick Spells list during a fight. Please go back and select the Healing Spell you wish to use from the Spells box on the main fighting screen to continue.", "Error"); if ($userrow["currentaction"] == "Fighting") return display("You cannot use the Quick Spells list during a fight. Please go back and select the Healing Spell you wish to use from the Spells box on the main fighting screen to continue.", "Error");
if ($userrow["currenthp"] == $userrow["maxhp"]) display("Your Hit Points are already full. You don't need to use a Healing spell now.", "Error"); if ($userrow["currenthp"] == $userrow["maxhp"]) return display("Your Hit Points are already full. You don't need to use a Healing spell now.", "Error");
$newhp = $userrow["currenthp"] + $spellrow["attribute"]; $newhp = $userrow["currenthp"] + $spellrow["attribute"];
if ($userrow["maxhp"] < $newhp) { $spellrow["attribute"] = $userrow["maxhp"] - $userrow["currenthp"]; $newhp = $userrow["currenthp"] + $spellrow["attribute"]; } if ($userrow["maxhp"] < $newhp) { $spellrow["attribute"] = $userrow["maxhp"] - $userrow["currenthp"]; $newhp = $userrow["currenthp"] + $spellrow["attribute"]; }
@ -24,5 +24,5 @@ function healspells($id)
db()->query('UPDATE users SET currenthp=?, currentmp=? WHERE id=?;', [$newhp, $newmp, $userrow['id']]); db()->query('UPDATE users SET currenthp=?, currentmp=? WHERE id=?;', [$newhp, $newmp, $userrow['id']]);
display("You have cast the ".$spellrow["name"]." spell, and gained ".$spellrow["attribute"]." Hit Points. You can now continue <a href=\"/\">exploring</a>.", "Healing Spell"); return display("You have cast the ".$spellrow["name"]." spell, and gained ".$spellrow["attribute"]." Hit Points. You can now continue <a href=\"/\">exploring</a>.", "Healing Spell");
} }

View File

@ -246,7 +246,7 @@ function main()
[ <a href="#top">Top</a> ] [ <a href="#top">Top</a> ]
HTML; HTML;
display_help(parse($page, $controlrow)); return display_help(parse($page, $controlrow));
} }
function items() function items()
@ -313,7 +313,7 @@ function items()
} }
$page .= '</table>'; $page .= '</table>';
display_help($page); return display_help($page);
} }
@ -349,7 +349,7 @@ function spells()
</ul> </ul>
HTML; HTML;
display_help($page); return display_help($page);
} }
function monsters() function monsters()
@ -368,7 +368,7 @@ function monsters()
$page .= "<tr><td width=\"30%\">".$m["name"]."</td><td width=\"10%\">".$m["maxhp"]."</td><td width=\"10%\">".$m["maxdam"]."</td><td width=\"10%\">".$m["armor"]."</td><td width=\"10%\">".$m["level"]."</td><td width=\"10%\">".$m["maxexp"]."</td><td width=\"10%\">".$m["maxgold"]."</td><td width=\"20%\">$immune</td></tr>\n"; $page .= "<tr><td width=\"30%\">".$m["name"]."</td><td width=\"10%\">".$m["maxhp"]."</td><td width=\"10%\">".$m["maxdam"]."</td><td width=\"10%\">".$m["armor"]."</td><td width=\"10%\">".$m["level"]."</td><td width=\"10%\">".$m["maxexp"]."</td><td width=\"10%\">".$m["maxgold"]."</td><td width=\"20%\">$immune</td></tr>\n";
} }
display_help($page.'</table>'); return display_help($page.'</table>');
} }
function levels() function levels()
@ -489,13 +489,13 @@ function levels()
Experience points listed are total values up until that point. All other values are just the new amount that you gain for each level. Experience points listed are total values up until that point. All other values are just the new amount that you gain for each level.
HTML; HTML;
display_help(parse($page, $controlrow)); return display_help(parse($page, $controlrow));
} }
function display_help(string $content) function display_help(string $content)
{ {
global $controlrow; global $controlrow;
echo render('layouts/help', [ return render('layouts/help', [
'control' => $controlrow, 'control' => $controlrow,
'content' => $content, 'content' => $content,
'version' => VERSION, 'version' => VERSION,

View File

@ -27,28 +27,31 @@ function town()
{ {
global $controlrow; global $controlrow;
$townrow = get_town_by_xy(user()->longitude, user()->latitude); $town = get_town_by_xy(user()->longitude, user()->latitude);
if ($townrow === false) display("There is an error with your user account, or with the town data. Please try again.","Error"); if ($town === false) exit('There is an error with your user account, or with the town data. Please try again.');
$townrow["news"] = ""; $page = ['news' => '', 'whos_online' => ''];
$townrow["whosonline"] = "";
$townrow["babblebox"] = "";
// News box. Grab latest news entry and display it. Something a little more graceful coming soon maybe. // News box. Grab latest news entry and display it. Something a little more graceful coming soon maybe.
if ($controlrow["shownews"] == 1) { if ($controlrow['shownews'] === 1) {
$newsrow = db()->query('SELECT * FROM news ORDER BY id DESC LIMIT 1;')->fetchArray(SQLITE3_ASSOC); $news = db()->query('SELECT * FROM news ORDER BY id DESC LIMIT 1;')->fetchArray(SQLITE3_ASSOC);
$townrow["news"] = '<div class="title">Latest News</div>'; $news_date = pretty_date($news["postdate"]);
$townrow["news"] .= "<span class=\"light\">[".pretty_date($newsrow["postdate"])."]</span><br>".nl2br($newsrow["content"]); $news_content = nl2br($news["content"]);
$page['news'] = <<<HTML
<div class="title">Latest News</div>
<span class="light">$news_date</span><br>
$news_content
HTML;
} }
// 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(<<<SQL $onlinequery = db()->query(<<<SQL
SELECT id, username SELECT id, username
FROM users FROM users
WHERE onlinetime >= datetime('now', '-600 seconds') WHERE onlinetime >= datetime('now', '-600 seconds')
ORDER BY username; ORDER BY username;
SQL); SQL);
$online_count = 0; $online_count = 0;
$online_rows = []; $online_rows = [];
@ -58,23 +61,15 @@ function town()
$online_rows[] = "<a href=\"javascript:opencharpopup({$onlinerow['id']})\">".$onlinerow["username"]."</a>"; $online_rows[] = "<a href=\"javascript:opencharpopup({$onlinerow['id']})\">".$onlinerow["username"]."</a>";
} }
$townrow["whosonline"] = '<div class="title">Who\'s Online</div>'; $online_rows = implode(', ', $online_rows);
$townrow["whosonline"] .= "There are <b>$online_count</b> user(s) online within the last 10 minutes: "; $page['whos_online'] = <<<HTML
$townrow["whosonline"] .= rtrim(implode(', ', $online_rows), ', '); <div class="title">Who's Online</div>
} There are <b>$online_count</b> user(s) online within the last 10 minutes: $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; HTML;
} }
page_title($townrow['name']); page_title($town['name']);
return render('towns', ['town' => $townrow]); return render('towns', ['town' => $town, 'news' => $page['news'], 'whos_online' => $page['whos_online']]);
} }
/** /**
@ -383,21 +378,20 @@ function travelto($id, bool $usepoints = true)
if ($usepoints) { if ($usepoints) {
if (user()->currenttp < $townrow["travelpoints"]) { if (user()->currenttp < $townrow["travelpoints"]) {
display("You do not have enough TP to travel here. Please go back and try again when you get more TP.", "Travel To"); return display("You do not have enough TP to travel here. Please go back and try again when you get more TP.", "Travel To");
} }
$mapped = explode(",",user()->towns); $mapped = explode(",",user()->towns);
if (!in_array($id, $mapped)) { display("Cheat attempt detected.<br><br>Get a life, loser.", "Error"); } if (!in_array($id, $mapped)) { display("Cheat attempt detected.<br><br>Get a life, loser.", "Error"); }
} }
if ((user()->latitude == $townrow["latitude"]) && (user()->longitude == $townrow["longitude"])) { if ((user()->latitude == $townrow["latitude"]) && (user()->longitude == $townrow["longitude"])) {
display("You are already in this town. <a href=\"/\">Click here</a> to return to the main town screen.", "Travel To"); return display("You are already in this town. <a href=\"/\">Click here</a> to return to the main town screen.", "Travel To");
} }
$newtp = ($usepoints) ? user()->currenttp - $townrow["travelpoints"] : user()->currenttp; $newtp = ($usepoints) ? user()->currenttp - $townrow["travelpoints"] : user()->currenttp;
$newlat = $townrow["latitude"]; $newlat = $townrow["latitude"];
$newlon = $townrow["longitude"]; $newlon = $townrow["longitude"];
$newid = user()->id;
// If they got here by exploring, add this town to their map. // If they got here by exploring, add this town to their map.
$mapped = explode(",",user()->towns); $mapped = explode(",",user()->towns);
@ -406,12 +400,13 @@ function travelto($id, bool $usepoints = true)
$mapped = implode(",", $mapped); $mapped = implode(",", $mapped);
if ($town == false) $mapped .= ",$id"; if ($town == false) $mapped .= ",$id";
$mapped = "towns='".$mapped."',"; user()->currentaction = 'In Town';
user()->towns = $mapped;
db()->query("UPDATE users SET currentaction='In Town',$mapped currenttp=?, latitude=?, longitude=? WHERE id=?;", [ user()->currenttp = $newtp;
$newtp, $newlat, $newlon, $newid user()->longitude = $newlon;
]); user()->latitude = $newlat;
user()->save();
$page = "You have travelled to ".$townrow["name"].". You may now <a href=\"/\">enter this town</a>."; $page = "You have travelled to ".$townrow["name"].". You may now <a href=\"/\">enter this town</a>.";
display($page, "Travel To"); return display($page, "Travel To");
} }

View File

@ -47,7 +47,7 @@ function login()
redirect('/'); redirect('/');
} }
display(render('login'), 'Log In', true, false, false); return display(render('login'), 'Log In', true, false, false);
} }
/** /**
@ -109,7 +109,7 @@ function register()
$page = render('register', ['controlrow' => $controlrow]); $page = render('register', ['controlrow' => $controlrow]);
} }
display($page, 'Register', true, false, false); return display($page, 'Register', true, false, false);
} }
function verify() function verify()
@ -124,10 +124,10 @@ function verify()
db()->query("UPDATE users SET verify='g2g' WHERE username=?;", [$u]); db()->query("UPDATE users SET verify='g2g' WHERE username=?;", [$u]);
display("Your account was verified successfully.<br><br>You may now continue to the <a href=\"/login\">Login Page</a> and start playing the game.<br><br>Thanks for playing!","Verify Email",false,false,false); return display("Your account was verified successfully.<br><br>You may now continue to the <a href=\"/login\">Login Page</a> and start playing the game.<br><br>Thanks for playing!","Verify Email",false,false,false);
} }
display(render('verify'), "Verify Email", true, false, false); return display(render('verify'), "Verify Email", true, false, false);
} }
function lostpassword() function lostpassword()
@ -143,13 +143,13 @@ function lostpassword()
db()->query('UPDATE users SET password=? WHERE email=?;', [$hashed, $e]); db()->query('UPDATE users SET password=? WHERE email=?;', [$hashed, $e]);
if (sendpassemail($e, $newpass)) { if (sendpassemail($e, $newpass)) {
display("Your new password was emailed to the address you provided.<br><br>Once you receive it, you may <a href=\"/login\">Log In</a> and continue playing.<br><br>Thank you.","Lost Password",false,false,false); return display("Your new password was emailed to the address you provided.<br><br>Once you receive it, you may <a href=\"/login\">Log In</a> and continue playing.<br><br>Thank you.","Lost Password",false,false,false);
} else { } else {
display("There was an error sending your new password.<br><br>Please check with the game administrator for more information.<br><br>We apologize for the inconvience.","Lost Password",false,false,false); return display("There was an error sending your new password.<br><br>Please check with the game administrator for more information.<br><br>We apologize for the inconvience.","Lost Password",false,false,false);
} }
} }
display(render('lostpassword'), "Lost Password", true, false, false); return display(render('lostpassword'), "Lost Password", true, false, false);
} }
function changepassword() function changepassword()
@ -179,10 +179,10 @@ function changepassword()
set_cookie('dkgame', '', -3600); set_cookie('dkgame', '', -3600);
display("Your password was changed successfully.<br><br>You have been logged out of the game to avoid errors.<br><br>Please <a href=\"/login\">log back in</a> to continue playing.","Change Password",false,false,false); return display("Your password was changed successfully.<br><br>You have been logged out of the game to avoid errors.<br><br>Please <a href=\"/login\">log back in</a> to continue playing.","Change Password",false,false,false);
} }
display(render('changepassword'), "Change Password", true, false, false); return display(render('changepassword'), "Change Password", true, false, false);
} }
function settings() function settings()
@ -198,10 +198,10 @@ function settings()
user()->save(); user()->save();
$alert = '<div class="alert">Settings updated</div>'; $alert = '<div class="alert">Settings updated</div>';
display($alert . render('settings'), "Account Settings"); return display($alert . render('settings'), "Account Settings");
} }
display(render('settings'), "Account Settings"); return display(render('settings'), "Account Settings");
} }
function sendpassemail($emailaddress, $password) function sendpassemail($emailaddress, $password)

View File

@ -27,7 +27,7 @@ if (!file_exists('../.installed') && $uri[0] !== 'install') {
redirect('/install'); redirect('/install');
} elseif (file_exists(('../.installed')) && $uri[0] === 'install') { } elseif (file_exists(('../.installed')) && $uri[0] === 'install') {
redirect('/'); redirect('/');
} else { } elseif (file_exists(('../.installed')) && $uri[0] !== 'install') {
$controlrow = get_control_row(); $controlrow = get_control_row();
if (!$controlrow["gameopen"]) { if (!$controlrow["gameopen"]) {

View File

@ -107,45 +107,13 @@ function display($content, $title, bool $topnav = true, bool $leftnav = true, bo
{ {
global $controlrow; global $controlrow;
$game_skin = 0; $game_skin = user() !== false ? user()->game_skin : 0;
$topnav = $topnav ? Render\header_links() : '';
if (user() !== false) {
$game_skin = user()->game_skin;
if (user()->currentaction == 'In Town') {
$town = get_town_by_xy(user()->latitude, user()->longitude);
$current_town = "Welcome to <b>{$town['name']}</b>.<br><br>";
} else {
$current_town = '';
}
// Format various userrow stuffs...
if (user()->latitude < 0) { user()->latitude = user()->latitude * -1 . "S"; } else { user()->latitude .= "N"; }
if (user()->longitude < 0) { user()->longitude = user()->longitude * -1 . "W"; } else { user()->longitude .= "E"; }
// Travel To list.
$townslist = explode(",",user()->towns);
$townquery2 = db()->query('SELECT * FROM towns ORDER BY id;');
$town_list_html = '';
while ($townrow2 = $townquery2->fetchArray(SQLITE3_ASSOC)) {
$town = false;
foreach($townslist as $id) {
if ($id == $townrow2["id"]) { $town = true; }
}
if ($town == true) {
$town_list_html .= "<a href=\"/gotown/{$townrow2["id"]}\">".$townrow2["name"]."</a><br>\n";
}
}
}
return render('layouts/primary', [ return render('layouts/primary', [
"dkgamename" => $controlrow["gamename"], "dkgamename" => $controlrow["gamename"],
"content" => $content, "content" => $content,
"game_skin" => $game_skin, "game_skin" => $game_skin,
"leftnav" => $leftnav ? render('leftnav', ['town_list' => $town_list_html, 'current_town' => $current_town]) : '', "topnav" => $topnav ? Render\header_links() : ''
"topnav" => $topnav,
]); ]);
} }
@ -555,6 +523,8 @@ function get_spells_from_list(array|string $spell_ids): array|false
function generate_stat_bar(int $current, int $max): string function generate_stat_bar(int $current, int $max): string
{ {
$percent = $max > 0 ? round(max(0, $current) / $max * 100, 4) : 0; $percent = $max > 0 ? round(max(0, $current) / $max * 100, 4) : 0;
if ($percent < 0) $percent = 0;
if ($percent > 100) $percent = 100;
$color = $percent >= 66 ? 'green' : ($percent >= 33 ? 'yellow' : 'red'); $color = $percent >= 66 ? 'green' : ($percent >= 33 ? 'yellow' : 'red');
return <<<HTML return <<<HTML

View File

@ -39,6 +39,15 @@ function right_nav(): string
return $template; return $template;
} }
function left_nav(): string
{
if (user() === false) return '';
$template = render('left_nav');
if (is_htmx()) $template = '<section id="left" hx-swap-oob="true">'.$template."</section>";
return $template;
}
function babblebox(): string function babblebox(): string
{ {
return render('babblebox', ['messages' => babblebox_messages()]); return render('babblebox', ['messages' => babblebox_messages()]);

View File

@ -28,7 +28,7 @@
</header> </header>
<main> <main>
<section id="left"><?= $leftnav ?></section> <section id="left"><?= Render\left_nav() ?></section>
<section id="middle"><?= $content ?></section> <section id="middle"><?= $content ?></section>
<section id="right"><?= Render\right_nav() ?></section> <section id="right"><?= Render\right_nav() ?></section>
</main> </main>

View File

@ -1,8 +1,14 @@
<section> <section>
<div class="title"><img src="/img/button_location.gif" alt="Location" title="Location"></div> <div class="title"><img src="/img/button_location.gif" alt="Location" title="Location"></div>
Currently: <?= user()->currentaction ?><br> Currently: <?= user()->currentaction ?><br>
Latitude: <?= user()->latitude ?><br> <?php
Longitude: <?= user()->longitude ?><br> $lat = user()->latitude;
$lon = user()->longitude;
if ($lat < 0) { $lat = ($lat * -1) . "S"; } else { $lat .= "N"; }
if ($lon < 0) { $lon = ($lon * -1) . "W"; } else { $lon .= "E"; }
?>
Latitude: <?= $lat ?><br>
Longitude: <?= $lon ?><br>
<a href="javascript:openmappopup()">View Map</a><br> <a href="javascript:openmappopup()">View Map</a><br>
<form action="/move" method="post" class="move-compass"> <form action="/move" method="post" class="move-compass">
<button type="submit" name="direction" value="north" class="north">North</button> <button type="submit" name="direction" value="north" class="north">North</button>
@ -17,13 +23,24 @@
<section> <section>
<div class="title"><img src="/img/button_towns.gif" alt="Towns" title="Towns"></div> <div class="title"><img src="/img/button_towns.gif" alt="Towns" title="Towns"></div>
<?php <?php
if (user()->currentaction == 'In Town') { if (user()->currentaction == 'In Town') {
$town = get_town_by_xy((int) user()->latitude, (int) user()->longitude); $town = get_town_by_xy((int) user()->latitude, (int) user()->longitude);
echo "Welcome to <b>{$town['name']}</b>.<br><br>"; echo "Welcome to <b>{$town['name']}</b>.<br><br>";
} }
?> ?>
Travel To:<br> Travel To:<br>
<?= $town_list ?> <?php
$town_list = explode(",", user()->towns);
$towns = db()->query('SELECT * FROM towns ORDER BY id;');
$mapped = false;
while ($row = $towns->fetchArray(SQLITE3_ASSOC)) {
$mapped = true;
if (in_array($row['id'], $town_list)) {
echo "<a href=\"/gotown/{$row["id"]}\">".$row["name"]."</a><br>";
}
}
if (!$mapped) echo 'You have no towns mapped.';
?>
</section> </section>
<section> <section>

View File

@ -13,9 +13,9 @@
<section> <section>
<div class="title"><img src="/img/button_inventory.gif" alt="Inventory" title="Inventory"></div> <div class="title"><img src="/img/button_inventory.gif" alt="Inventory" title="Inventory"></div>
<img src="/img/icon_weapon.gif" alt="Weapon" title="Weapon"> Weapon: <?= user()->weaponname ?><br> <img src="/img/icon_weapon.gif" alt="Weapon" title="Weapon"> <?= user()->weaponname ?><br>
<img src="/img/icon_armor.gif" alt="Armor" title="Armor"> Armor: <?= user()->armorname ?><br> <img src="/img/icon_armor.gif" alt="Armor" title="Armor"> <?= user()->armorname ?><br>
<img src="/img/icon_shield.gif" alt="Shield" title="Shield"> Shield: <?= user()->shieldname ?><br> <img src="/img/icon_shield.gif" alt="Shield" title="Shield"> <?= user()->shieldname ?><br>
Slot 1: <?= user()->slot1name ?><br> Slot 1: <?= user()->slot1name ?><br>
Slot 2: <?= user()->slot2name ?><br> Slot 2: <?= user()->slot2name ?><br>
Slot 3: <?= user()->slot3name ?> Slot 3: <?= user()->slot3name ?>

View File

@ -10,11 +10,11 @@
</div> </div>
<div class="news"> <div class="news">
<?= $town['news'] ?> <?= $news ?>
</div> </div>
<div class="whos-online"> <div class="whos-online">
<?= $town['whosonline'] ?> <?= $whos_online ?>
</div> </div>
<div class="babblebox"> <div class="babblebox">