<?php// towns.php :: Handles all actions you can do in town.
functioninn(){// Staying at the inn resets all expendable stats to their max values.
global$userrow,$numqueries;
$townquery=doquery("SELECT name,innprice FROM {{table}} WHERE latitude='".$userrow["latitude"]."' AND longitude='".$userrow["longitude"]."' LIMIT 1","towns");
$townrow=mysql_fetch_array($townquery);
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");die();}
if(isset($_POST["submit"])){
$newgold=$userrow["gold"]-$townrow["innprice"];
$query=doquery("UPDATE {{table}} SET gold='$newgold',currenthp='".$userrow["maxhp"]."',currentmp='".$userrow["maxmp"]."',currenttp='".$userrow["maxtp"]."' WHERE id='".$userrow["id"]."' LIMIT 1","users");
$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"])){
header("Location: index.php");die();
}else{
$title="Inn";
$page="Resting at the inn will refill your current HP, MP, and TP to their maximum levels.<br /><br />\n";
$page.="A night's sleep at this Inn will cost you <b>".$townrow["innprice"]." gold</b>. Is that ok?<br /><br />\n";
functionbuy(){// Displays a list of available items for purchase.
global$userrow,$numqueries;
$townquery=doquery("SELECT name,itemslist FROM {{table}} WHERE latitude='".$userrow["latitude"]."' AND longitude='".$userrow["longitude"]."' LIMIT 1","towns");
$townrow=mysql_fetch_array($townquery);
$itemslist=explode(",",$townrow["itemslist"]);
$querystring="";
foreach($itemslistas$a=>$b){
$querystring.="id='$b' OR ";
}
$querystring=rtrim($querystring," OR ");
$itemsquery=doquery("SELECT * FROM {{table}} WHERE $querystring ORDER BY id","items");
$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.="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);
}
functionbuy2($id){// Confirm user's intent to purchase item.
global$userrow,$numqueries;
$itemsquery=doquery("SELECT * FROM {{table}} WHERE id='$id' LIMIT 1","items");
$itemsrow=mysql_fetch_array($itemsquery);
if($userrow["gold"]<$itemsrow["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");die();}
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?<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 ".$itemsrow["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>";
}
}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?<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 ".$itemsrow["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>";
}
}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?<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 ".$itemsrow["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>";
}
}
$title="Buy Items";
display($page,$title);
}
functionbuy3($id){// Update user profile with new item & stats.
$itemsquery=doquery("SELECT * FROM {{table}} WHERE id='$id' LIMIT 1","items");
$itemsrow=mysql_fetch_array($itemsquery);
if($userrow["gold"]<$itemsrow["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");die();}
if($itemsrow["type"]==1){// weapon
// 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");
$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");
}
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");
}
functionmaps(){// List maps the user can buy.
global$userrow,$numqueries;
$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";
$townquery=doquery("SELECT * FROM {{table}} ORDER BY id","towns");
$page.="If you've changed your mind, you may also return back to <a href=\"index.php\">town</a>.\n";
display($page,"Buy Maps");
}
functionmaps2($id){// Confirm user's intent to purchase map.
global$userrow,$numqueries;
$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.<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");die();}
$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");
}
functionmaps3($id){// Add new map to user's profile.
$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.<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");die();}
$mappedtowns=explode(",",$userrow["towns"]);
$mappedtowns[$id]=1;
$mappedtowns=implode(",",$mappedtowns);
$newgold=$userrow["gold"]-$townrow["mapprice"];
$updatequery=doquery("UPDATE {{table}} SET towns='$mappedtowns',gold='$newgold' WHERE id='".$userrow["id"]."' LIMIT 1","users");
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");
}
functiontravelto($id,$usepoints=true){// Send a user to a town from the Travel To menu.
global$userrow,$numqueries;
$townquery=doquery("SELECT name,travelpoints,latitude,longitude FROM {{table}} WHERE id='$id' LIMIT 1","towns");
display("You do not have enough TP to travel here. Please go back and try again when you get more TP.","Travel To");die();
}
}
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");die();}
$updatequery=doquery("UPDATE {{table}} SET currentaction='In Town',$mapped currenttp='$newtp',latitude='$newlat',longitude='$newlon' WHERE id='$newid' LIMIT 1","users");
$page="You have travelled to ".$townrow["name"].". You may now <a href=\"index.php\">enter this town</a>.";