340 lines
13 KiB
PHP
340 lines
13 KiB
PHP
<?php
|
|
|
|
// towns.php :: Handles all actions you can do in town.
|
|
|
|
namespace Towns;
|
|
|
|
use Router;
|
|
|
|
function register_routes(Router $r): Router
|
|
{
|
|
$r->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.<br><br>Get a life, loser.", "Error"); }
|
|
|
|
if (user()->gold < $townrow["innprice"]) {
|
|
display("You do not have enough gold to stay at this Inn tonight.<br><br>You may return to <a href=\"/\">town</a>, 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.<br><br>You may return to <a href=\"/\">town</a>, or use the direction buttons on the left to start exploring.";
|
|
} elseif (isset($_POST["cancel"])) {
|
|
redirect('/');
|
|
} else {
|
|
$title = "Inn";
|
|
$page = <<<HTML
|
|
Resting at the inn will refill your current HP, MP, and TP to their maximum levels.<br><br>
|
|
A night's sleep at this Inn will cost you <b>{$townrow["innprice"]} gold</b>. Is that ok?<br><br>
|
|
<form action="/inn" method="post">
|
|
<input type="submit" name="submit" value="Yes"> <input type="submit" name="cancel" value="No">
|
|
</form>
|
|
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.<br><br>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.<br><br>Click an item name to purchase it.<br><br>The following items are available at this town:<br><br>\n";
|
|
$page .= "<table width=\"80%\">\n";
|
|
while ($itemsrow = $items->fetchArray(SQLITE3_ASSOC)) {
|
|
$attrib = ($itemsrow["type"] == 1) ? "Attack Power:" : "Defense Power:";
|
|
$page .= "<tr><td width=\"4%\">";
|
|
$page .= match ($itemsrow["type"]) {
|
|
1 => '<img src="/img/icon_weapon.gif" alt="weapon" /></td>',
|
|
2 => '<img src="/img/icon_armor.gif" alt="armor" /></td>',
|
|
3 => '<img src="/img/icon_shield.gif" alt="shield" /></td>'
|
|
};
|
|
if (user()->weaponid == $itemsrow["id"] || user()->armorid == $itemsrow["id"] || user()->shieldid == $itemsrow["id"]) {
|
|
$page .= "<td width=\"32%\"><span class=\"light\">".$itemsrow["name"]."</span></td><td width=\"32%\"><span class=\"light\">$attrib ".$itemsrow["attribute"]."</span></td><td width=\"32%\"><span class=\"light\">Already purchased</span></td></tr>\n";
|
|
} else {
|
|
if ($itemsrow["special"] != "X") { $specialdot = "<span class=\"highlight\">*</span>"; } else { $specialdot = ""; }
|
|
$page .= "<td width=\"32%\"><b><a href=\"/buy2/{$itemsrow["id"]}\">".$itemsrow["name"]."</a>$specialdot</b></td><td width=\"32%\">$attrib <b>".$itemsrow["attribute"]."</b></td><td width=\"32%\">Price: <b>".$itemsrow["buycost"]." gold</b></td></tr>\n";
|
|
}
|
|
}
|
|
$page .= "</table><br>\n";
|
|
$page .= "If you've changed your mind, you may also return back to <a href=\"/\">town</a>.\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.<br><br>Get a life, loser.", "Error");
|
|
$townitems = explode(",", $townrow["itemslist"]);
|
|
if (!in_array($id, $townitems)) display("Cheat attempt detected.<br><br>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.<br><br>You may return to <a href=\"/\">town</a>, <a href=\"/buy\">store</a>, 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?<br><br><form action=\"/buy3/$id\" method=\"post\"><input type=\"submit\" name=\"submit\" value=\"Yes\" /> <input type=\"submit\" name=\"cancel\" value=\"No\" /></form>";
|
|
} else {
|
|
$page = "You are buying the ".$item["name"].", is that ok?<br><br><form action=\"/buy3/$id\" method=\"post\"><input type=\"submit\" name=\"submit\" value=\"Yes\" /> <input type=\"submit\" name=\"cancel\" value=\"No\" /></form>";
|
|
}
|
|
|
|
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.<br><br>Get a life, loser.", "Error");
|
|
$townitems = explode(",", $townrow["itemslist"]);
|
|
if (!in_array($id, $townitems)) display("Cheat attempt detected.<br><br>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.<br><br>You may return to <a href=\"/\">town</a>, <a href=\"/buy\">store</a>, 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.<br><br>You may return to <a href=\"/\">town</a>, <a href=\"/buy\">store</a>, 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.<br><br>\n";
|
|
$page .= "Click a town name to purchase its map.<br><br>\n";
|
|
$page .= "<table width=\"90%\">\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 .= "<tr><td width=\"25%\"><a href=\"/maps2/{$townrow["id"]}\">".$townrow["name"]."</a></td><td width=\"25%\">Price: ".$townrow["mapprice"]." gold</td><td width=\"50%\" colspan=\"2\">Buy map to reveal details.</td></tr>\n";
|
|
} else {
|
|
$page .= "<tr><td width=\"25%\"><span class=\"light\">".$townrow["name"]."</span></td><td width=\"25%\"><span class=\"light\">Already mapped.</span></td><td width=\"35%\"><span class=\"light\">Location: $latitude $longitude</span></td><td width=\"15%\"><span class=\"light\">TP: ".$townrow["travelpoints"]."</span></td></tr>\n";
|
|
}
|
|
}
|
|
|
|
$page .= "</table><br>\n";
|
|
$page .= "If you've changed your mind, you may also return back to <a href=\"/\">town</a>.\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.<br><br>You may return to <a href=\"/\">town</a>, <a href=\"/maps\">store</a>, or use the direction buttons on the left to start exploring.", "Buy Maps");
|
|
}
|
|
|
|
$page = "You are buying the ".$townrow["name"]." map. Is that ok?<br><br><form action=\"/maps3/$id\" method=\"post\"><input type=\"submit\" name=\"submit\" value=\"Yes\" /> <input type=\"submit\" name=\"cancel\" value=\"No\" /></form>";
|
|
|
|
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.<br><br>You may return to <a href=\"/\">town</a>, <a href=\"/maps\">store</a>, 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.<br><br>You may return to <a href=\"/\">town</a>, <a href=\"/maps\">store</a>, 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.<br><br>Get a life, loser.", "Error"); }
|
|
}
|
|
|
|
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");
|
|
}
|
|
|
|
$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 <a href=\"/\">enter this town</a>.";
|
|
display($page, "Travel To");
|
|
}
|