form('/inn', 'Towns\inn'); $r->get('/buy', 'Towns\buy'); $r->get('/buy2/:id', 'Towns\buy2'); $r->post('/buy3/:id', 'Towns\buy3'); // $r->get('/sell', 'Towns\sell'); $r->get('/maps', 'Towns\maps'); $r->get('/maps2/:id', 'Towns\maps2'); $r->post('/maps3/:id', 'Towns\maps3'); $r->get('/gotown/:id', 'Towns\travelto'); return $r; } /** * Staying at the inn resets all expendable stats to their max values. */ function inn() { $townrow = get_town_by_xy(user()->longitude, user()->latitude); if ($townrow === false) { display("Cheat attempt detected.

Get a life, loser.", "Error"); } if (user()->gold < $townrow["innprice"]) { display("You do not have enough gold to stay at this Inn tonight.

You may return to town, or use the direction buttons on the left to start exploring.", "Inn"); } if (isset($_POST["submit"])) { $newgold = user()->gold - $townrow["innprice"]; db()->query( 'UPDATE users SET gold=?, currenthp=?, currentmp=?, currenttp=? WHERE id=?', [$newgold, user()->maxhp, user()->maxmp, user()->maxtp, user()->id ]); $title = "Inn"; $page = "You wake up feeling refreshed and ready for action.

You may return to town, or use the direction buttons on the left to start exploring."; } elseif (isset($_POST["cancel"])) { redirect('/'); } else { $title = "Inn"; $page = <<
A night's sleep at this Inn will cost you {$townrow["innprice"]} gold. Is that ok?

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

Get a life, loser.", "Error"); $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 = $items->fetchArray(SQLITE3_ASSOC)) { $attrib = ($itemsrow["type"] == 1) ? "Attack Power:" : "Defense Power:"; $page .= "', 2 => 'armor', 3 => 'shield' }; if (user()->weaponid == $itemsrow["id"] || user()->armorid == $itemsrow["id"] || user()->shieldid == $itemsrow["id"]) { $page .= "\n"; } else { if ($itemsrow["special"] != "X") { $specialdot = "*"; } else { $specialdot = ""; } $page .= "\n"; } } $page .= "
"; $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); } /** * Confirm user's intent to purchase item. */ function buy2($id) { $townrow = get_town_by_xy(user()->longitude, user()->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"); $item = get_item($id); if (user()->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"); } $type_to_row_mapping = [1 => 'weaponid', 2 => 'armorid', 3 => 'shieldid']; $current_equipped_id = user()[$type_to_row_mapping[$item['type']] ?? 0]; 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"); } /** * Update user profile with new item & stats. */ function buy3($id) { if (isset($_POST["cancel"])) redirect('/'); $townrow = get_town_by_xy(user()->longitude, user()->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"); $item = get_item($id); if (user()->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"); } $type_mapping = [ 1 => ['id' => 'weaponid', 'name' => 'weaponname', 'power' => 'attackpower'], 2 => ['id' => 'armorid', 'name' => 'armorname', 'power' => 'defensepower'], 3 => ['id' => 'shieldid', 'name' => 'shieldname', 'power' => 'defensepower'] ]; // Validate item type if (!isset($type_mapping[$item["type"]])) { display("Invalid item type.", "Error"); } // Retrieve current equipped item or create a default $current_equip_id = user()[$type_mapping[$item["type"]]['id']]; if ($current_equip_id != 0) { $item2 = get_item($current_equip_id); } else { $item2 = ["attribute" => 0, "buycost" => 0, "special" => "X"]; } // Process special item effects $specialFields = []; $specialValues = []; $powerAdjustments = 0; 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]; user()[$toChange] += $changeAmount; $specialFields[] = "$toChange = ?"; $specialValues[] = user()[$toChange]; // Adjust attack or defense power if ($toChange == "strength" || $toChange == "dexterity") { $powerType = $toChange == "strength" ? "attackpower" : "defensepower"; $powerAdjustments += $changeAmount; } } } // Determine power and type-specific updates $currentType = $type_mapping[$item["type"]]; $powerField = $currentType['power']; $newPower = user()[$powerField] + $item["attribute"] - $item2["attribute"]; // Calculate new gold with trade-in value $newGold = user()->gold + ceil($item2["buycost"]/2) - $item["buycost"]; // Ensure current HP/MP/TP don't exceed max values $newhp = min(user()->currenthp, user()->maxhp); $newmp = min(user()->currentmp, user()->maxmp); $newtp = min(user()->currenttp, user()->maxtp); $updateFields = array_merge( $specialFields, [ "gold = ?", "{$powerField} = ?", "{$currentType['id']} = ?", "{$currentType['name']} = ?", "currenthp = ?", "currentmp = ?", "currenttp = ?" ] ); $updateValues = array_merge( $specialValues, [ $newGold, $newPower, $item["id"], $item["name"], $newhp, $newmp, $newtp, user()->id ] ); $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"); } /** * List maps the user can buy. */ function maps() { $mappedtowns = explode(",", user()->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"; $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 $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"); } /** * Confirm user's intent to purchase map. */ function maps2($id) { $townrow = get_town_by_id($id); if (user()->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"); } /** * Add new map to user's profile. */ function maps3($id) { if (isset($_POST["cancel"])) redirect('/'); $townrow = get_town_by_id($id); if (user()->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 = user()->towns.",$id"; $newgold = user()->gold - $townrow["mapprice"]; db()->query('UPDATE users SET towns=?, gold=? WHERE id=?;', [$mappedtowns, $newgold, user()->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"); } /** * Send a user to a town from the Travel To menu. */ function travelto($id, bool $usepoints = true) { if (user()->currentaction == "Fighting") redirect('/fight'); $townrow = get_town_by_id($id); if ($usepoints) { 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"); } $mapped = explode(",",user()->towns); if (!in_array($id, $mapped)) { display("Cheat attempt detected.

Get a life, loser.", "Error"); } } if ((user()->latitude == $townrow["latitude"]) && (user()->longitude == $townrow["longitude"])) { display("You are already in this town. Click here to return to the main town screen.", "Travel To"); } $newtp = ($usepoints) ? user()->currenttp - $townrow["travelpoints"] : user()->currenttp; $newlat = $townrow["latitude"]; $newlon = $townrow["longitude"]; $newid = user()->id; // If they got here by exploring, add this town to their map. $mapped = explode(",",user()->towns); $town = false; foreach($mapped as $b) if ($b == $id) $town = true; $mapped = implode(",", $mapped); if ($town == false) $mapped .= ",$id"; $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"); }