diff --git a/.gitignore b/.gitignore index 28d54e6..379dd0a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .installed database.db +database.db-* diff --git a/src/lib.php b/src/lib.php index 7cacfe7..3fbc3b2 100644 --- a/src/lib.php +++ b/src/lib.php @@ -273,3 +273,23 @@ function get_town_by_xy(int $x, int $y): array|false if ($query === false) return false; return $query->fetchArray(SQLITE3_ASSOC); } + +/** + * Get a town's data by it's ID. + */ +function get_town_by_id(int $id): array|false +{ + $query = db()->query('SELECT * FROM towns WHERE id = ? LIMIT 1;', [$id]); + if ($query === false) return false; + return $query->fetchArray(SQLITE3_ASSOC); +} + +/** + * Get an item by it's ID. + */ +function get_item(int $id): array|false +{ + $query = db()->query('SELECT * FROM items WHERE id=? LIMIT 1;', [$id]); + if ($query === false) return false; + return $query->fetchArray(SQLITE3_ASSOC); +} diff --git a/src/towns.php b/src/towns.php index b49f64a..ae18f33 100644 --- a/src/towns.php +++ b/src/towns.php @@ -28,343 +28,292 @@ function inn() redirect('index.php'); } else { $title = "Inn"; - $page = "Resting at the inn will refill your current HP, MP, and TP to their maximum levels.

\n"; - $page .= "A night's sleep at this Inn will cost you " . $townrow["innprice"] . " gold. Is that ok?

\n"; - $page .= "
\n"; - $page .= " \n"; - $page .= "
\n"; + $page = <<
+ A night's sleep at this Inn will cost you {$townrow["innprice"]} gold. Is that ok?

+
+ +
+ HTML; } display($page, $title); } -function buy() { // Displays a list of available items for purchase. +/** + * Displays a list of available items for purchase. + */ +function buy() +{ + global $userrow; - global $userrow, $numqueries; + $townrow = get_town_by_xy($userrow['longitude'], $userrow['latitude']); + if ($townrow === false) display("Cheat attempt detected.

Get a life, loser.", "Error"); - $townquery = doquery("SELECT name,itemslist FROM {{table}} WHERE latitude='".$userrow["latitude"]."' AND longitude='".$userrow["longitude"]."' LIMIT 1", "towns"); - if (mysql_num_rows($townquery) != 1) { display("Cheat attempt detected.

Get a life, loser.", "Error"); } - $townrow = mysql_fetch_array($townquery); - - $itemslist = explode(",",$townrow["itemslist"]); - $querystring = ""; - foreach($itemslist as $a=>$b) { - $querystring .= "id='$b' OR "; - } - $querystring = rtrim($querystring, " OR "); - - $itemsquery = doquery("SELECT * FROM {{table}} WHERE $querystring ORDER BY id", "items"); + $items = db()->query("SELECT * FROM items WHERE id IN ({$townrow["itemslist"]});"); $page = "Buying weapons will increase your Attack Power. Buying armor and shields will increase your Defense Power.

Click an item name to purchase it.

The following items are available at this town:

\n"; $page .= "\n"; - while ($itemsrow = mysql_fetch_array($itemsquery)) { - if ($itemsrow["type"] == 1) { $attrib = "Attack Power:"; } else { $attrib = "Defense Power:"; } - $page .= ""; } - if ($itemsrow["type"] == 2) { $page .= "\"armor\""; } - if ($itemsrow["type"] == 3) { $page .= "\"shield\""; } - if ($userrow["weaponid"] == $itemsrow["id"] || $userrow["armorid"] == $itemsrow["id"] || $userrow["shieldid"] == $itemsrow["id"]) { - $page .= "\n"; - } else { - if ($itemsrow["special"] != "X") { $specialdot = "*"; } else { $specialdot = ""; } - $page .= "\n"; - } - } + while ($itemsrow = $items->fetchArray(SQLITE3_ASSOC)) { + $attrib = ($itemsrow["type"] == 1) ? "Attack Power:" : "Defense Power:"; + $page .= "', + 2 => 'armor', + 3 => 'shield' + }; + if ($userrow["weaponid"] == $itemsrow["id"] || $userrow["armorid"] == $itemsrow["id"] || $userrow["shieldid"] == $itemsrow["id"]) { + $page .= "\n"; + } else { + if ($itemsrow["special"] != "X") { $specialdot = "*"; } else { $specialdot = ""; } + $page .= "\n"; + } + } $page .= "
"; - if ($itemsrow["type"] == 1) { $page .= "\"weapon\"".$itemsrow["name"]."$attrib ".$itemsrow["attribute"]."Already purchased
".$itemsrow["name"]."$specialdot$attrib ".$itemsrow["attribute"]."Price: ".$itemsrow["buycost"]." gold
"; + $page .= match ($itemsrow["type"]) { + 1 => 'weapon".$itemsrow["name"]."$attrib ".$itemsrow["attribute"]."Already purchased
".$itemsrow["name"]."$specialdot$attrib ".$itemsrow["attribute"]."Price: ".$itemsrow["buycost"]." gold

\n"; $page .= "If you've changed your mind, you may also return back to town.\n"; $title = "Buy Items"; display($page, $title); - } -function buy2($id) { // Confirm user's intent to purchase item. +/** + * Confirm user's intent to purchase item. + */ +function buy2($id) +{ + global $userrow; - global $userrow, $numqueries; + $townrow = get_town_by_xy($userrow['longitude'], $userrow['latitude']); + if ($townrow === false) display("Cheat attempt detected.

Get a life, loser.", "Error"); + $townitems = explode(",", $townrow["itemslist"]); + if (!in_array($id, $townitems)) display("Cheat attempt detected.

Get a life, loser.", "Error"); - $townquery = doquery("SELECT name,itemslist FROM {{table}} WHERE latitude='".$userrow["latitude"]."' AND longitude='".$userrow["longitude"]."' LIMIT 1", "towns"); - if (mysql_num_rows($townquery) != 1) { display("Cheat attempt detected.

Get a life, loser.", "Error"); } - $townrow = mysql_fetch_array($townquery); - $townitems = explode(",",$townrow["itemslist"]); - if (! in_array($id, $townitems)) { display("Cheat attempt detected.

Get a life, loser.", "Error"); } + $item = get_item($id); - $itemsquery = doquery("SELECT * FROM {{table}} WHERE id='$id' LIMIT 1", "items"); - $itemsrow = mysql_fetch_array($itemsquery); + if ($userrow["gold"] < $item["buycost"]) { + display("You do not have enough gold to buy this item.

You may return to town, store, or use the direction buttons on the left to start exploring.", "Buy Items"); + } - if ($userrow["gold"] < $itemsrow["buycost"]) { display("You do not have enough gold to buy this item.

You may return to town, store, or use the direction buttons on the left to start exploring.", "Buy Items"); die(); } + $type_to_row_mapping = [1 => 'weaponid', 2 => 'armorid', 3 => 'shieldid']; + $current_equipped_id = $userrow[$type_to_row_mapping[$item['type']] ?? 0]; - if ($itemsrow["type"] == 1) { - if ($userrow["weaponid"] != 0) { - $itemsquery2 = doquery("SELECT * FROM {{table}} WHERE id='".$userrow["weaponid"]."' LIMIT 1", "items"); - $itemsrow2 = mysql_fetch_array($itemsquery2); - $page = "If you are buying the ".$itemsrow["name"].", then I will buy your ".$itemsrow2["name"]." for ".ceil($itemsrow2["buycost"]/2)." gold. Is that ok?

"; - } else { - $page = "You are buying the ".$itemsrow["name"].", is that ok?

"; - } - } elseif ($itemsrow["type"] == 2) { - if ($userrow["armorid"] != 0) { - $itemsquery2 = doquery("SELECT * FROM {{table}} WHERE id='".$userrow["armorid"]."' LIMIT 1", "items"); - $itemsrow2 = mysql_fetch_array($itemsquery2); - $page = "If you are buying the ".$itemsrow["name"].", then I will buy your ".$itemsrow2["name"]." for ".ceil($itemsrow2["buycost"]/2)." gold. Is that ok?

"; - } else { - $page = "You are buying the ".$itemsrow["name"].", is that ok?

"; - } - } elseif ($itemsrow["type"] == 3) { - if ($userrow["shieldid"] != 0) { - $itemsquery2 = doquery("SELECT * FROM {{table}} WHERE id='".$userrow["shieldid"]."' LIMIT 1", "items"); - $itemsrow2 = mysql_fetch_array($itemsquery2); - $page = "If you are buying the ".$itemsrow["name"].", then I will buy your ".$itemsrow2["name"]." for ".ceil($itemsrow2["buycost"]/2)." gold. Is that ok?

"; - } else { - $page = "You are buying the ".$itemsrow["name"].", is that ok?

"; - } - } - - $title = "Buy Items"; - display($page, $title); + if ($current_equipped_id != 0) { + $item2 = get_item($current_equipped_id); + $page = "If you are buying the ".$item["name"].", then I will buy your ".$item2["name"]." for ".ceil($item2["buycost"] / 2)." gold. Is that ok?

"; + } else { + $page = "You are buying the ".$item["name"].", is that ok?

"; + } + display($page, "Buy Items"); } -function buy3($id) { // Update user profile with new item & stats. +/** + * Update user profile with new item & stats. + */ +function buy3($id) +{ - if (isset($_POST["cancel"])) { header("Location: index.php"); die(); } + if (isset($_POST["cancel"])) redirect('index.php'); global $userrow; - $townquery = doquery("SELECT name,itemslist FROM {{table}} WHERE latitude='".$userrow["latitude"]."' AND longitude='".$userrow["longitude"]."' LIMIT 1", "towns"); - if (mysql_num_rows($townquery) != 1) { display("Cheat attempt detected.

Get a life, loser.", "Error"); } - $townrow = mysql_fetch_array($townquery); - $townitems = explode(",",$townrow["itemslist"]); - if (! in_array($id, $townitems)) { display("Cheat attempt detected.

Get a life, loser.", "Error"); } + $townrow = get_town_by_xy($userrow['longitude'], $userrow['latitude']); + if ($townrow === false) display("Cheat attempt detected.

Get a life, loser.", "Error"); + $townitems = explode(",", $townrow["itemslist"]); + if (!in_array($id, $townitems)) display("Cheat attempt detected.

Get a life, loser.", "Error"); - $itemsquery = doquery("SELECT * FROM {{table}} WHERE id='$id' LIMIT 1", "items"); - $itemsrow = mysql_fetch_array($itemsquery); + $item = get_item($id); - if ($userrow["gold"] < $itemsrow["buycost"]) { display("You do not have enough gold to buy this item.

You may return to town, store, or use the direction buttons on the left to start exploring.", "Buy Items"); die(); } + if ($userrow["gold"] < $item["buycost"]) { + display("You do not have enough gold to buy this item.

You may return to town, store, or use the direction buttons on the left to start exploring.", "Buy Items"); + } - if ($itemsrow["type"] == 1) { // weapon + $type_mapping = [ + 1 => ['id' => 'weaponid', 'name' => 'weaponname', 'power' => 'attackpower'], + 2 => ['id' => 'armorid', 'name' => 'armorname', 'power' => 'defensepower'], + 3 => ['id' => 'shieldid', 'name' => 'shieldname', 'power' => 'defensepower'] + ]; - // Check if they already have an item in the slot. - if ($userrow["weaponid"] != 0) { - $itemsquery2 = doquery("SELECT * FROM {{table}} WHERE id='".$userrow["weaponid"]."' LIMIT 1", "items"); - $itemsrow2 = mysql_fetch_array($itemsquery2); - } else { - $itemsrow2 = array("attribute"=>0,"buycost"=>0,"special"=>"X"); - } + // Validate item type + if (!isset($type_mapping[$item["type"]])) { + display("Invalid item type.", "Error"); + } - // Special item fields. - $specialchange1 = ""; - $specialchange2 = ""; - if ($itemsrow["special"] != "X") { - $special = explode(",",$itemsrow["special"]); - $tochange = $special[0]; - $userrow[$tochange] = $userrow[$tochange] + $special[1]; - $specialchange1 = "$tochange='".$userrow[$tochange]."',"; - if ($tochange == "strength") { $userrow["attackpower"] += $special[1]; } - if ($tochange == "dexterity") { $userrow["defensepower"] += $special[1]; } - } - if ($itemsrow2["special"] != "X") { - $special2 = explode(",",$itemsrow2["special"]); - $tochange2 = $special2[0]; - $userrow[$tochange2] = $userrow[$tochange2] - $special2[1]; - $specialchange2 = "$tochange2='".$userrow[$tochange2]."',"; - if ($tochange2 == "strength") { $userrow["attackpower"] -= $special2[1]; } - if ($tochange2 == "dexterity") { $userrow["defensepower"] -= $special2[1]; } - } + // Retrieve current equipped item or create a default + $current_equip_id = $userrow[$type_mapping[$item["type"]]['id']]; + if ($current_equip_id != 0) { + $item2 = get_item($current_equip_id); + } else { + $item2 = ["attribute" => 0, "buycost" => 0, "special" => "X"]; + } - // New stats. - $newgold = $userrow["gold"] + ceil($itemsrow2["buycost"]/2) - $itemsrow["buycost"]; - $newattack = $userrow["attackpower"] + $itemsrow["attribute"] - $itemsrow2["attribute"]; - $newid = $itemsrow["id"]; - $newname = $itemsrow["name"]; - $userid = $userrow["id"]; - if ($userrow["currenthp"] > $userrow["maxhp"]) { $newhp = $userrow["maxhp"]; } else { $newhp = $userrow["currenthp"]; } - if ($userrow["currentmp"] > $userrow["maxmp"]) { $newmp = $userrow["maxmp"]; } else { $newmp = $userrow["currentmp"]; } - if ($userrow["currenttp"] > $userrow["maxtp"]) { $newtp = $userrow["maxtp"]; } else { $newtp = $userrow["currenttp"]; } + // Process special item effects + $specialFields = []; + $specialValues = []; + $powerAdjustments = 0; - // Final update. - $updatequery = doquery("UPDATE {{table}} SET $specialchange1 $specialchange2 gold='$newgold', attackpower='$newattack', weaponid='$newid', weaponname='$newname', currenthp='$newhp', currentmp='$newmp', currenttp='$newtp' WHERE id='$userid' LIMIT 1", "users"); + foreach ([$item, $item2] as $index => $process_item) { + if ($process_item["special"] != "X") { + $special = explode(",", $process_item["special"]); + $toChange = $special[0]; + $changeAmount = $index === 0 ? $special[1] : -$special[1]; - } elseif ($itemsrow["type"] == 2) { // Armor + $userrow[$toChange] += $changeAmount; + $specialFields[] = "$toChange = ?"; + $specialValues[] = $userrow[$toChange]; - // Check if they already have an item in the slot. - if ($userrow["armorid"] != 0) { - $itemsquery2 = doquery("SELECT * FROM {{table}} WHERE id='".$userrow["armorid"]."' LIMIT 1", "items"); - $itemsrow2 = mysql_fetch_array($itemsquery2); - } else { - $itemsrow2 = array("attribute"=>0,"buycost"=>0,"special"=>"X"); - } + // Adjust attack or defense power + if ($toChange == "strength" || $toChange == "dexterity") { + $powerType = $toChange == "strength" ? "attackpower" : "defensepower"; + $powerAdjustments += $changeAmount; + } + } + } - // Special item fields. - $specialchange1 = ""; - $specialchange2 = ""; - if ($itemsrow["special"] != "X") { - $special = explode(",",$itemsrow["special"]); - $tochange = $special[0]; - $userrow[$tochange] = $userrow[$tochange] + $special[1]; - $specialchange1 = "$tochange='".$userrow[$tochange]."',"; - if ($tochange == "strength") { $userrow["attackpower"] += $special[1]; } - if ($tochange == "dexterity") { $userrow["defensepower"] += $special[1]; } - } - if ($itemsrow2["special"] != "X") { - $special2 = explode(",",$itemsrow2["special"]); - $tochange2 = $special2[0]; - $userrow[$tochange2] = $userrow[$tochange2] - $special2[1]; - $specialchange2 = "$tochange2='".$userrow[$tochange2]."',"; - if ($tochange2 == "strength") { $userrow["attackpower"] -= $special2[1]; } - if ($tochange2 == "dexterity") { $userrow["defensepower"] -= $special2[1]; } - } + // Determine power and type-specific updates + $currentType = $type_mapping[$item["type"]]; + $powerField = $currentType['power']; + $newPower = $userrow[$powerField] + $item["attribute"] - $item2["attribute"]; - // New stats. - $newgold = $userrow["gold"] + ceil($itemsrow2["buycost"]/2) - $itemsrow["buycost"]; - $newdefense = $userrow["defensepower"] + $itemsrow["attribute"] - $itemsrow2["attribute"]; - $newid = $itemsrow["id"]; - $newname = $itemsrow["name"]; - $userid = $userrow["id"]; - if ($userrow["currenthp"] > $userrow["maxhp"]) { $newhp = $userrow["maxhp"]; } else { $newhp = $userrow["currenthp"]; } - if ($userrow["currentmp"] > $userrow["maxmp"]) { $newmp = $userrow["maxmp"]; } else { $newmp = $userrow["currentmp"]; } - if ($userrow["currenttp"] > $userrow["maxtp"]) { $newtp = $userrow["maxtp"]; } else { $newtp = $userrow["currenttp"]; } + // Calculate new gold with trade-in value + $newGold = $userrow["gold"] + ceil($item2["buycost"]/2) - $item["buycost"]; - // Final update. - $updatequery = doquery("UPDATE {{table}} SET $specialchange1 $specialchange2 gold='$newgold', defensepower='$newdefense', armorid='$newid', armorname='$newname', currenthp='$newhp', currentmp='$newmp', currenttp='$newtp' WHERE id='$userid' LIMIT 1", "users"); + // Ensure current HP/MP/TP don't exceed max values + $newhp = min($userrow["currenthp"], $userrow["maxhp"]); + $newmp = min($userrow["currentmp"], $userrow["maxmp"]); + $newtp = min($userrow["currenttp"], $userrow["maxtp"]); - } elseif ($itemsrow["type"] == 3) { // Shield + $updateFields = array_merge( + $specialFields, + [ + "gold = ?", + "{$powerField} = ?", + "{$currentType['id']} = ?", + "{$currentType['name']} = ?", + "currenthp = ?", + "currentmp = ?", + "currenttp = ?" + ] + ); - // Check if they already have an item in the slot. - if ($userrow["shieldid"] != 0) { - $itemsquery2 = doquery("SELECT * FROM {{table}} WHERE id='".$userrow["shieldid"]."' LIMIT 1", "items"); - $itemsrow2 = mysql_fetch_array($itemsquery2); - } else { - $itemsrow2 = array("attribute"=>0,"buycost"=>0,"special"=>"X"); - } + $updateValues = array_merge( + $specialValues, + [ + $newGold, + $newPower, + $item["id"], + $item["name"], + $newhp, + $newmp, + $newtp, + $userrow["id"] + ] + ); - // Special item fields. - $specialchange1 = ""; - $specialchange2 = ""; - if ($itemsrow["special"] != "X") { - $special = explode(",",$itemsrow["special"]); - $tochange = $special[0]; - $userrow[$tochange] = $userrow[$tochange] + $special[1]; - $specialchange1 = "$tochange='".$userrow[$tochange]."',"; - if ($tochange == "strength") { $userrow["attackpower"] += $special[1]; } - if ($tochange == "dexterity") { $userrow["defensepower"] += $special[1]; } - } - if ($itemsrow2["special"] != "X") { - $special2 = explode(",",$itemsrow2["special"]); - $tochange2 = $special2[0]; - $userrow[$tochange2] = $userrow[$tochange2] - $special2[1]; - $specialchange2 = "$tochange2='".$userrow[$tochange2]."',"; - if ($tochange2 == "strength") { $userrow["attackpower"] -= $special2[1]; } - if ($tochange2 == "dexterity") { $userrow["defensepower"] -= $special2[1]; } - } - - // New stats. - $newgold = $userrow["gold"] + ceil($itemsrow2["buycost"]/2) - $itemsrow["buycost"]; - $newdefense = $userrow["defensepower"] + $itemsrow["attribute"] - $itemsrow2["attribute"]; - $newid = $itemsrow["id"]; - $newname = $itemsrow["name"]; - $userid = $userrow["id"]; - if ($userrow["currenthp"] > $userrow["maxhp"]) { $newhp = $userrow["maxhp"]; } else { $newhp = $userrow["currenthp"]; } - if ($userrow["currentmp"] > $userrow["maxmp"]) { $newmp = $userrow["maxmp"]; } else { $newmp = $userrow["currentmp"]; } - if ($userrow["currenttp"] > $userrow["maxtp"]) { $newtp = $userrow["maxtp"]; } else { $newtp = $userrow["currenttp"]; } - - // Final update. - $updatequery = doquery("UPDATE {{table}} SET $specialchange1 $specialchange2 gold='$newgold', defensepower='$newdefense', shieldid='$newid', shieldname='$newname', currenthp='$newhp', currentmp='$newmp', currenttp='$newtp' WHERE id='$userid' LIMIT 1", "users"); - - } + $stmt = db()->query("UPDATE users SET " . implode(", ", $updateFields) . " WHERE id = ?;", $updateValues); + if ($stmt === false) exit("Failed to purchase and equip $id. Go back and try again."); display("Thank you for purchasing this item.

You may return to town, store, or use the direction buttons on the left to start exploring.", "Buy Items"); - } -function maps() { // List maps the user can buy. +/** + * List maps the user can buy. + */ +function maps() +{ + global $userrow; - global $userrow, $numqueries; - - $mappedtowns = explode(",",$userrow["towns"]); + $mappedtowns = explode(",", $userrow["towns"]); $page = "Buying maps will put the town in your Travel To box, and it won't cost you as many TP to get there.

\n"; $page .= "Click a town name to purchase its map.

\n"; $page .= "\n"; - $townquery = doquery("SELECT * FROM {{table}} ORDER BY id", "towns"); - while ($townrow = mysql_fetch_array($townquery)) { - - if ($townrow["latitude"] >= 0) { $latitude = $townrow["latitude"] . "N,"; } else { $latitude = ($townrow["latitude"]*-1) . "S,"; } - if ($townrow["longitude"] >= 0) { $longitude = $townrow["longitude"] . "E"; } else { $longitude = ($townrow["longitude"]*-1) . "W"; } + $towns = db()->query('SELECT * FROM towns ORDER BY id;'); + while ($townrow = $towns->fetchArray(SQLITE3_ASSOC)) { + $latitude = ($townrow["latitude"] >= 0) ? $townrow["latitude"] . "N," : ($townrow["latitude"] * -1) . "S,"; + $longitude = ($townrow["longitude"] >= 0) ? $townrow["longitude"] . "E" : ($townrow["longitude"] * -1) . "W"; $mapped = false; - foreach($mappedtowns as $a => $b) { - if ($b == $townrow["id"]) { $mapped = true; } - } + foreach($mappedtowns as $b) if ($b == $townrow["id"]) $mapped = true; + if ($mapped == false) { $page .= "\n"; } else { $page .= "\n"; } - } $page .= "
".$townrow["name"]."Price: ".$townrow["mapprice"]." goldBuy map to reveal details.
".$townrow["name"]."Already mapped.Location: $latitude $longitudeTP: ".$townrow["travelpoints"]."

\n"; $page .= "If you've changed your mind, you may also return back to town.\n"; display($page, "Buy Maps"); - } -function maps2($id) { // Confirm user's intent to purchase map. +/** + * Confirm user's intent to purchase map. + */ +function maps2($id) +{ + global $userrow; - global $userrow, $numqueries; + $townrow = get_town_by_id($id); - $townquery = doquery("SELECT name,mapprice FROM {{table}} WHERE id='$id' LIMIT 1", "towns"); - $townrow = mysql_fetch_array($townquery); - - if ($userrow["gold"] < $townrow["mapprice"]) { display("You do not have enough gold to buy this map.

You may return to town, store, or use the direction buttons on the left to start exploring.", "Buy Maps"); die(); } + if ($userrow["gold"] < $townrow["mapprice"]) { + display("You do not have enough gold to buy this map.

You may return to town, store, or use the direction buttons on the left to start exploring.", "Buy Maps"); + } $page = "You are buying the ".$townrow["name"]." map. Is that ok?

"; display($page, "Buy Maps"); - } -function maps3($id) { // Add new map to user's profile. +/** + * Add new map to user's profile. + */ +function maps3($id) +{ + if (isset($_POST["cancel"])) redirect('index.php'); - if (isset($_POST["cancel"])) { header("Location: index.php"); die(); } + global $userrow; - global $userrow, $numqueries; + $townrow = get_town_by_id($id); - $townquery = doquery("SELECT name,mapprice FROM {{table}} WHERE id='$id' LIMIT 1", "towns"); - $townrow = mysql_fetch_array($townquery); - - if ($userrow["gold"] < $townrow["mapprice"]) { display("You do not have enough gold to buy this map.

You may return to town, store, or use the direction buttons on the left to start exploring.", "Buy Maps"); die(); } + if ($userrow["gold"] < $townrow["mapprice"]) { + display("You do not have enough gold to buy this map.

You may return to town, store, or use the direction buttons on the left to start exploring.", "Buy Maps"); + } $mappedtowns = $userrow["towns"].",$id"; $newgold = $userrow["gold"] - $townrow["mapprice"]; - $updatequery = doquery("UPDATE {{table}} SET towns='$mappedtowns',gold='$newgold' WHERE id='".$userrow["id"]."' LIMIT 1", "users"); + db()->query('UPDATE users SET towns=?, gold=? WHERE id=?;', [$mappedtowns, $newgold, $userrow['id']]); display("Thank you for purchasing this map.

You may return to town, store, or use the direction buttons on the left to start exploring.", "Buy Maps"); - } -function travelto($id, $usepoints=true) { // Send a user to a town from the Travel To menu. +/** + * Send a user to a town from the Travel To menu. + */ +function travelto($id, bool $usepoints = true) +{ + global $userrow; - global $userrow, $numqueries; + if ($userrow["currentaction"] == "Fighting") redirect('index.php?do=fight'); - if ($userrow["currentaction"] == "Fighting") { header("Location: index.php?do=fight"); die(); } + $townrow = get_town_by_id($id); - $townquery = doquery("SELECT name,travelpoints,latitude,longitude FROM {{table}} WHERE id='$id' LIMIT 1", "towns"); - $townrow = mysql_fetch_array($townquery); - - if ($usepoints==true) { + if ($usepoints) { if ($userrow["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"); die(); + 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(",",$userrow["towns"]); if (!in_array($id, $mapped)) { display("Cheat attempt detected.

Get a life, loser.", "Error"); } } - if (($userrow["latitude"] == $townrow["latitude"]) && ($userrow["longitude"] == $townrow["longitude"])) { display("You are already in this town. Click here to return to the main town screen.", "Travel To"); die(); } + if (($userrow["latitude"] == $townrow["latitude"]) && ($userrow["longitude"] == $townrow["longitude"])) { + display("You are already in this town. Click here to return to the main town screen.", "Travel To"); + } - if ($usepoints == true) { $newtp = $userrow["currenttp"] - $townrow["travelpoints"]; } else { $newtp = $userrow["currenttp"]; } + $newtp = ($usepoints) ? $userrow["currenttp"] - $townrow["travelpoints"] : $userrow["currenttp"]; $newlat = $townrow["latitude"]; $newlon = $townrow["longitude"]; @@ -373,20 +322,16 @@ function travelto($id, $usepoints=true) { // Send a user to a town from the Trav // If they got here by exploring, add this town to their map. $mapped = explode(",",$userrow["towns"]); $town = false; - foreach($mapped as $a => $b) { - if ($b == $id) { $town = true; } - } - $mapped = implode(",",$mapped); - if ($town == false) { - $mapped .= ",$id"; - $mapped = "towns='".$mapped."',"; - } else { - $mapped = "towns='".$mapped."',"; - } + foreach($mapped as $b) if ($b == $id) $town = true; + $mapped = implode(",", $mapped); + if ($town == false) $mapped .= ",$id"; - $updatequery = doquery("UPDATE {{table}} SET currentaction='In Town',$mapped currenttp='$newtp',latitude='$newlat',longitude='$newlon' WHERE id='$newid' LIMIT 1", "users"); + $mapped = "towns='".$mapped."',"; + + db()->query("UPDATE users SET currentaction='In Town',$mapped currenttp=?, latitude=?, longitude=? WHERE id=?;", [ + $newtp, $newlat, $newlon, $newid + ]); $page = "You have travelled to ".$townrow["name"].". You may now enter this town."; display($page, "Travel To"); - }