forked from Sky/Dragon-Knight
338 lines
13 KiB
PHP
338 lines
13 KiB
PHP
<?php
|
|
|
|
// towns.php :: Handles all actions you can do in town.
|
|
|
|
/**
|
|
* Staying at the inn resets all expendable stats to their max values.
|
|
*/
|
|
function inn()
|
|
{
|
|
global $userrow;
|
|
|
|
$townrow = get_town_by_xy($userrow["longitude"], $userrow["latitude"]);
|
|
if ($townrow === false) { display("Cheat attempt detected.<br><br>Get a life, loser.", "Error"); }
|
|
|
|
if ($userrow["gold"] < $townrow["innprice"]) {
|
|
display("You do not have enough gold to stay at this Inn tonight.<br><br>You may return to <a href=\"index.php\">town</a>, or use the direction buttons on the left to start exploring.", "Inn");
|
|
}
|
|
|
|
if (isset($_POST["submit"])) {
|
|
$newgold = $userrow["gold"] - $townrow["innprice"];
|
|
db()->query(
|
|
'UPDATE users SET gold=?, currenthp=?, currentmp=?, currenttp=? WHERE id=?',
|
|
[$newgold, $userrow['maxhp'], $userrow['maxmp'], $userrow['maxtp'], $userrow['id']
|
|
]);
|
|
$title = "Inn";
|
|
$page = "You wake up feeling refreshed and ready for action.<br><br>You may return to <a href=\"index.php\">town</a>, or use the direction buttons on the left to start exploring.";
|
|
} elseif (isset($_POST["cancel"])) {
|
|
redirect('index.php');
|
|
} 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="index.php?do=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()
|
|
{
|
|
global $userrow;
|
|
|
|
$townrow = get_town_by_xy($userrow['longitude'], $userrow['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="images/icon_weapon.gif" alt="weapon" /></td>',
|
|
2 => '<img src="images/icon_armor.gif" alt="armor" /></td>',
|
|
3 => '<img src="images/icon_shield.gif" alt="shield" /></td>'
|
|
};
|
|
if ($userrow["weaponid"] == $itemsrow["id"] || $userrow["armorid"] == $itemsrow["id"] || $userrow["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=\"index.php?do=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=\"index.php\">town</a>.\n";
|
|
$title = "Buy Items";
|
|
|
|
display($page, $title);
|
|
}
|
|
|
|
/**
|
|
* Confirm user's intent to purchase item.
|
|
*/
|
|
function buy2($id)
|
|
{
|
|
global $userrow;
|
|
|
|
$townrow = get_town_by_xy($userrow['longitude'], $userrow['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 ($userrow["gold"] < $item["buycost"]) {
|
|
display("You do not have enough gold to buy this item.<br><br>You may return to <a href=\"index.php\">town</a>, <a href=\"index.php?do=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 = $userrow[$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=\"index.php?do=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=\"index.php?do=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('index.php');
|
|
|
|
global $userrow;
|
|
|
|
$townrow = get_town_by_xy($userrow['longitude'], $userrow['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 ($userrow["gold"] < $item["buycost"]) {
|
|
display("You do not have enough gold to buy this item.<br><br>You may return to <a href=\"index.php\">town</a>, <a href=\"index.php?do=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 = $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"];
|
|
}
|
|
|
|
// 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];
|
|
|
|
$userrow[$toChange] += $changeAmount;
|
|
$specialFields[] = "$toChange = ?";
|
|
$specialValues[] = $userrow[$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 = $userrow[$powerField] + $item["attribute"] - $item2["attribute"];
|
|
|
|
// Calculate new gold with trade-in value
|
|
$newGold = $userrow["gold"] + ceil($item2["buycost"]/2) - $item["buycost"];
|
|
|
|
// 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"]);
|
|
|
|
$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,
|
|
$userrow["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=\"index.php\">town</a>, <a href=\"index.php?do=buy\">store</a>, or use the direction buttons on the left to start exploring.", "Buy Items");
|
|
}
|
|
|
|
/**
|
|
* List maps the user can buy.
|
|
*/
|
|
function maps()
|
|
{
|
|
global $userrow;
|
|
|
|
$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.<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=\"index.php?do=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=\"index.php\">town</a>.\n";
|
|
|
|
display($page, "Buy Maps");
|
|
}
|
|
|
|
/**
|
|
* Confirm user's intent to purchase map.
|
|
*/
|
|
function maps2($id)
|
|
{
|
|
global $userrow;
|
|
|
|
$townrow = get_town_by_id($id);
|
|
|
|
if ($userrow["gold"] < $townrow["mapprice"]) {
|
|
display("You do not have enough gold to buy this map.<br><br>You may return to <a href=\"index.php\">town</a>, <a href=\"index.php?do=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=\"index.php?do=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('index.php');
|
|
|
|
global $userrow;
|
|
|
|
$townrow = get_town_by_id($id);
|
|
|
|
if ($userrow["gold"] < $townrow["mapprice"]) {
|
|
display("You do not have enough gold to buy this map.<br><br>You may return to <a href=\"index.php\">town</a>, <a href=\"index.php?do=maps\">store</a>, or use the direction buttons on the left to start exploring.", "Buy Maps");
|
|
}
|
|
|
|
$mappedtowns = $userrow["towns"].",$id";
|
|
$newgold = $userrow["gold"] - $townrow["mapprice"];
|
|
|
|
db()->query('UPDATE users SET towns=?, gold=? WHERE id=?;', [$mappedtowns, $newgold, $userrow['id']]);
|
|
|
|
display("Thank you for purchasing this map.<br><br>You may return to <a href=\"index.php\">town</a>, <a href=\"index.php?do=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)
|
|
{
|
|
global $userrow;
|
|
|
|
if ($userrow["currentaction"] == "Fighting") redirect('index.php?do=fight');
|
|
|
|
$townrow = get_town_by_id($id);
|
|
|
|
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");
|
|
}
|
|
$mapped = explode(",",$userrow["towns"]);
|
|
if (!in_array($id, $mapped)) { display("Cheat attempt detected.<br><br>Get a life, loser.", "Error"); }
|
|
}
|
|
|
|
if (($userrow["latitude"] == $townrow["latitude"]) && ($userrow["longitude"] == $townrow["longitude"])) {
|
|
display("You are already in this town. <a href=\"index.php\">Click here</a> to return to the main town screen.", "Travel To");
|
|
}
|
|
|
|
$newtp = ($usepoints) ? $userrow["currenttp"] - $townrow["travelpoints"] : $userrow["currenttp"];
|
|
|
|
$newlat = $townrow["latitude"];
|
|
$newlon = $townrow["longitude"];
|
|
$newid = $userrow["id"];
|
|
|
|
// If they got here by exploring, add this town to their map.
|
|
$mapped = explode(",",$userrow["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=\"index.php\">enter this town</a>.";
|
|
display($page, "Travel To");
|
|
}
|