more refactoring, add src dir, update index logic

This commit is contained in:
Sky Johnson 2024-12-11 22:05:45 -06:00
parent ce06aecf84
commit 0511da22fb
9 changed files with 416 additions and 377 deletions

View File

@ -1,63 +1,77 @@
<?php // index.php :: Primary program script, evil alien overlord, you decide.
<?php
if (file_exists('install.php')) { die("Please delete <b>install.php</b> from your Dragon Knight directory before continuing."); }
include('lib.php');
include('cookies.php');
$link = opendb();
$controlquery = doquery("SELECT * FROM {{table}} WHERE id='1' LIMIT 1", "control");
$controlrow = mysql_fetch_array($controlquery);
// index.php :: Primary program script, evil alien overlord, you decide.
if (!file_exists('../.installed')) {
header('Location: install.php');
exit;
}
require_once '../src/lib.php';
$controlrow = get_control_row();
// Login (or verify) if not logged in.
$userrow = checkcookies();
if ($userrow == false) {
if (isset($_GET["do"])) {
if ($_GET["do"] == "verify") { header("Location: users.php?do=verify"); die(); }
if (($userrow = checkcookies()) === false) {
if (isset($_GET['do']) && $_GET['do'] === 'verify') {
header("Location: users.php?do=verify");
exit;
}
header("Location: login.php?do=login"); die();
header("Location: login.php?do=login");
exit;
}
// Close game.
if ($controlrow["gameopen"] == 0) { display("The game is currently closed for maintanence. Please check back later.","Game Closed"); die(); }
if ((bool) $controlrow["gameopen"] === false) {
display("The game is currently closed for maintanence. Please check back later.", "Game Closed");
exit;
}
// Force verify if the user isn't verified yet.
if ($controlrow["verifyemail"] == 1 && $userrow["verify"] != 1) { header("Location: users.php?do=verify"); die(); }
if ((bool) $controlrow["verifyemail"] && (bool) $userrow["verify"] === false) {
header("Location: users.php?do=verify");
exit;
}
// Block user if he/she has been banned.
if ($userrow["authlevel"] == 2) { die("Your account has been blocked. Please try back later."); }
if ($userrow["authlevel"] === 2) {
exit("Your account has been blocked.");
}
if (isset($_GET["do"])) {
$do = explode(":",$_GET["do"]);
// Town functions.
if ($do[0] == "inn") { include('towns.php'); inn(); }
elseif ($do[0] == "buy") { include('towns.php'); buy(); }
elseif ($do[0] == "buy2") { include('towns.php'); buy2($do[1]); }
elseif ($do[0] == "buy3") { include('towns.php'); buy3($do[1]); }
elseif ($do[0] == "sell") { include('towns.php'); sell(); }
elseif ($do[0] == "maps") { include('towns.php'); maps(); }
elseif ($do[0] == "maps2") { include('towns.php'); maps2($do[1]); }
elseif ($do[0] == "maps3") { include('towns.php'); maps3($do[1]); }
elseif ($do[0] == "gotown") { include('towns.php'); travelto($do[1]); }
// Exploring functions.
elseif ($do[0] == "move") { include('explore.php'); move(); }
// Fighting functions.
elseif ($do[0] == "fight") { include('fight.php'); fight(); }
elseif ($do[0] == "victory") { include('fight.php'); victory(); }
elseif ($do[0] == "drop") { include('fight.php'); drop(); }
elseif ($do[0] == "dead") { include('fight.php'); dead(); }
// Misc functions.
elseif ($do[0] == "verify") { header("Location: users.php?do=verify"); die(); }
elseif ($do[0] == "spell") { include('heal.php'); healspells($do[1]); }
elseif ($do[0] == "showchar") { showchar(); }
elseif ($do[0] == "onlinechar") { onlinechar($do[1]); }
elseif ($do[0] == "showmap") { showmap(); }
elseif ($do[0] == "babblebox") { babblebox(); }
elseif ($do[0] == "ninja") { ninja(); }
} else { donothing(); }
require_once '../src/towns.php';
require_once '../src/explore.php';
require_once '../src/fight.php';
require_once '../src/heal.php';
function donothing() {
$do = explode(':', $_GET['do'] ?? '');
match ($do[0]) {
'inn' => inn(),
'buy' => buy(),
'buy2' => buy2($do[1]),
'buy3' => buy3($do[1]),
// 'sell' => sell(),
'maps' => maps(),
'maps2' => maps2($do[1]),
'maps3' => maps3($do[1]),
'gotown' => travelto($do[1]),
'move' => move(),
'fight' => fight(),
'victory' => victory(),
'drop' => drop(),
'dead' => dead(),
'verify' => header("Location: users.php?do=verify"),
'spell' => healspells($do[1]),
'showchar' => showchar(),
'onlinechar' => onlinechar($do[1]),
'showmap' => showmap(),
'babblebox' => babblebox(),
'ninja' => ninja(),
default => donothing()
};
function donothing()
{
global $userrow;
if ($userrow["currentaction"] == "In Town") {
@ -70,92 +84,108 @@ function donothing() {
$page = dofight();
$title = "Fighting";
}
display($page, $title);
}
function dotown() { // Spit out the main town page.
global $userrow, $controlrow, $numqueries;
$townquery = doquery("SELECT * FROM {{table}} WHERE latitude='".$userrow["latitude"]."' AND longitude='".$userrow["longitude"]."' LIMIT 1", "towns");
if (mysql_num_rows($townquery) == 0) { display("There is an error with your user account, or with the town data. Please try again.","Error"); }
$townrow = mysql_fetch_array($townquery);
/**
* Spit out the main town page.
*/
function dotown()
{
global $userrow, $controlrow;
$townquery = db()->query('SELECT * FROM towns WHERE latitude = ? AND longitude = ? LIMIT 1;', [$userrow["latitude"], $userrow["longitude"]]);
if ($townquery === false) display("There is an error with your user account, or with the town data. Please try again.", "Error");
$townrow = $townquery->fetchArray(SQLITE3_ASSOC);
if ($townrow === false) display("There is an error with your user account, or with the town data. Please try again.","Error");
// News box. Grab latest news entry and display it. Something a little more graceful coming soon maybe.
if ($controlrow["shownews"] == 1) {
$newsquery = doquery("SELECT * FROM {{table}} ORDER BY id DESC LIMIT 1", "news");
$newsrow = mysql_fetch_array($newsquery);
if ($controlrow["shownews"] == 1) {
$newsrow = db()->query('SELECT * FROM news ORDER BY id DESC LIMIT 1;')->fetchArray(SQLITE3_ASSOC);
$townrow["news"] = "<table width=\"95%\"><tr><td class=\"title\">Latest News</td></tr><tr><td>\n";
$townrow["news"] .= "<span class=\"light\">[".prettydate($newsrow["postdate"])."]</span><br />".nl2br($newsrow["content"]);
$townrow["news"] .= "</td></tr></table>\n";
} else { $townrow["news"] = ""; }
} else {
$townrow["news"] = "";
}
// Who's Online. Currently just members. Guests maybe later.
if ($controlrow["showonline"] == 1) {
$onlinequery = doquery("SELECT * FROM {{table}} WHERE UNIX_TIMESTAMP(onlinetime) >= '".(time()-600)."' ORDER BY charname", "users");
$onlinequery = db()->query("SELECT * FROM news WHERE strftime('%s', onlinetime) >= strftime('%s', 'now') - 600 ORDER BY charname");
$online_count = 0;
$online_rows = [];
foreach ($onlinequery->fetchArray(SQLITE3_ASSOC) as $onlinerow) {
$online_count++;
$online_rows[] = "<a href=\"index.php?do=onlinechar:".$onlinerow["id"]."\">".$onlinerow["charname"]."</a>" . ", ";
}
$townrow["whosonline"] = "<table width=\"95%\"><tr><td class=\"title\">Who's Online</td></tr><tr><td>\n";
$townrow["whosonline"] .= "There are <b>" . mysql_num_rows($onlinequery) . "</b> user(s) online within the last 10 minutes: ";
while ($onlinerow = mysql_fetch_array($onlinequery)) { $townrow["whosonline"] .= "<a href=\"index.php?do=onlinechar:".$onlinerow["id"]."\">".$onlinerow["charname"]."</a>" . ", "; }
$townrow["whosonline"] = rtrim($townrow["whosonline"], ", ");
$townrow["whosonline"] .= "There are <b>$online_count</b> user(s) online within the last 10 minutes: ";
$townrow["whosonline"] .= rtrim(implode(', ', $online_rows), ', ');
$townrow["whosonline"] .= "</td></tr></table>\n";
} else { $townrow["whosonline"] = ""; }
} else {
$townrow["whosonline"] = "";
}
if ($controlrow["showbabble"] == 1) {
$townrow["babblebox"] = "<table width=\"95%\"><tr><td class=\"title\">Babble Box</td></tr><tr><td>\n";
$townrow["babblebox"] .= "<iframe src=\"index.php?do=babblebox\" name=\"sbox\" width=\"100%\" height=\"250\" frameborder=\"0\" id=\"bbox\">Your browser does not support inline frames! The Babble Box will not be available until you upgrade to a newer <a href=\"http://www.mozilla.org\" target=\"_new\">browser</a>.</iframe>";
$townrow["babblebox"] .= "</td></tr></table>\n";
} else { $townrow["babblebox"] = ""; }
} else {
$townrow["babblebox"] = "";
}
$page = gettemplate("towns");
$page = parsetemplate($page, $townrow);
return $page;
}
function doexplore() { // Just spit out a blank exploring page.
// Exploring without a GET string is normally when they first log in, or when they've just finished fighting.
$page = <<<END
<table width="100%">
<tr><td class="title"><img src="images/title_exploring.gif" alt="Exploring" /></td></tr>
<tr><td>
You are exploring the map, and nothing has happened. Continue exploring using the direction buttons or the Travel To menus.
</td></tr>
</table>
END;
return $page;
}
function dofight() { // Redirect to fighting.
/**
* Just spit out a blank exploring page. Exploring without a GET string is normally when they first log in, or when
* they've just finished fighting.
*/
function doexplore()
{
return <<<HTML
<table width="100%">
<tr><td class="title"><img src="images/title_exploring.gif" alt="Exploring" /></td></tr>
<tr><td>
You are exploring the map, and nothing has happened. Continue exploring using the direction buttons or the Travel To menus.
</td></tr>
</table>
HTML;
}
/**
* Redirect to fighting.
*/
function dofight()
{
header("Location: index.php?do=fight");
}
function showchar() {
function showchar()
{
global $userrow, $controlrow;
// Format various userrow stuffs.
$userrow["experience"] = number_format($userrow["experience"]);
$userrow["gold"] = number_format($userrow["gold"]);
if ($userrow["expbonus"] > 0) {
$userrow["plusexp"] = "<span class=\"light\">(+".$userrow["expbonus"]."%)</span>";
if ($userrow["expbonus"] > 0) {
$userrow["plusexp"] = "<span class=\"light\">(+".$userrow["expbonus"]."%)</span>";
} elseif ($userrow["expbonus"] < 0) {
$userrow["plusexp"] = "<span class=\"light\">(".$userrow["expbonus"]."%)</span>";
} else { $userrow["plusexp"] = ""; }
if ($userrow["goldbonus"] > 0) {
$userrow["plusgold"] = "<span class=\"light\">(+".$userrow["goldbonus"]."%)</span>";
} elseif ($userrow["goldbonus"] < 0) {
if ($userrow["goldbonus"] > 0) {
$userrow["plusgold"] = "<span class=\"light\">(+".$userrow["goldbonus"]."%)</span>";
} elseif ($userrow["goldbonus"] < 0) {
$userrow["plusgold"] = "<span class=\"light\">(".$userrow["goldbonus"]."%)</span>";
} else { $userrow["plusgold"] = ""; }
$levelquery = doquery("SELECT ". $userrow["charclass"]."_exp FROM {{table}} WHERE id='".($userrow["level"]+1)."' LIMIT 1", "levels");
$levelrow = mysql_fetch_array($levelquery);
if ($userrow["level"] < 99) { $userrow["nextlevel"] = number_format($levelrow[$userrow["charclass"]."_exp"]); } else { $userrow["nextlevel"] = "<span class=\"light\">None</span>"; }
@ -163,11 +193,11 @@ function showchar() {
if ($userrow["charclass"] == 1) { $userrow["charclass"] = $controlrow["class1name"]; }
elseif ($userrow["charclass"] == 2) { $userrow["charclass"] = $controlrow["class2name"]; }
elseif ($userrow["charclass"] == 3) { $userrow["charclass"] = $controlrow["class3name"]; }
if ($userrow["difficulty"] == 1) { $userrow["difficulty"] = $controlrow["diff1name"]; }
elseif ($userrow["difficulty"] == 2) { $userrow["difficulty"] = $controlrow["diff2name"]; }
elseif ($userrow["difficulty"] == 3) { $userrow["difficulty"] = $controlrow["diff3name"]; }
$spellquery = doquery("SELECT id,name FROM {{table}}","spells");
$userspells = explode(",",$userrow["spells"]);
$userrow["magiclist"] = "";
@ -181,40 +211,39 @@ function showchar() {
}
}
if ($userrow["magiclist"] == "") { $userrow["magiclist"] = "None"; }
// Make page tags for XHTML validation.
$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"DTD/xhtml1-transitional.dtd\">\n"
. "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n";
$charsheet = gettemplate("showchar");
$page = $xml . gettemplate("minimal");
$array = array("content"=>parsetemplate($charsheet, $userrow), "title"=>"Character Information");
echo parsetemplate($page, $array);
die();
}
function onlinechar($id) {
global $controlrow;
$userquery = doquery("SELECT * FROM {{table}} WHERE id='$id' LIMIT 1", "users");
if (mysql_num_rows($userquery) == 1) { $userrow = mysql_fetch_array($userquery); } else { display("No such user.", "Error"); }
// Format various userrow stuffs.
$userrow["experience"] = number_format($userrow["experience"]);
$userrow["gold"] = number_format($userrow["gold"]);
if ($userrow["expbonus"] > 0) {
$userrow["plusexp"] = "<span class=\"light\">(+".$userrow["expbonus"]."%)</span>";
if ($userrow["expbonus"] > 0) {
$userrow["plusexp"] = "<span class=\"light\">(+".$userrow["expbonus"]."%)</span>";
} elseif ($userrow["expbonus"] < 0) {
$userrow["plusexp"] = "<span class=\"light\">(".$userrow["expbonus"]."%)</span>";
} else { $userrow["plusexp"] = ""; }
if ($userrow["goldbonus"] > 0) {
$userrow["plusgold"] = "<span class=\"light\">(+".$userrow["goldbonus"]."%)</span>";
} elseif ($userrow["goldbonus"] < 0) {
if ($userrow["goldbonus"] > 0) {
$userrow["plusgold"] = "<span class=\"light\">(+".$userrow["goldbonus"]."%)</span>";
} elseif ($userrow["goldbonus"] < 0) {
$userrow["plusgold"] = "<span class=\"light\">(".$userrow["goldbonus"]."%)</span>";
} else { $userrow["plusgold"] = ""; }
$levelquery = doquery("SELECT ". $userrow["charclass"]."_exp FROM {{table}} WHERE id='".($userrow["level"]+1)."' LIMIT 1", "levels");
$levelrow = mysql_fetch_array($levelquery);
$userrow["nextlevel"] = number_format($levelrow[$userrow["charclass"]."_exp"]);
@ -222,37 +251,37 @@ function onlinechar($id) {
if ($userrow["charclass"] == 1) { $userrow["charclass"] = $controlrow["class1name"]; }
elseif ($userrow["charclass"] == 2) { $userrow["charclass"] = $controlrow["class2name"]; }
elseif ($userrow["charclass"] == 3) { $userrow["charclass"] = $controlrow["class3name"]; }
if ($userrow["difficulty"] == 1) { $userrow["difficulty"] = $controlrow["diff1name"]; }
elseif ($userrow["difficulty"] == 2) { $userrow["difficulty"] = $controlrow["diff2name"]; }
elseif ($userrow["difficulty"] == 3) { $userrow["difficulty"] = $controlrow["diff3name"]; }
$charsheet = gettemplate("onlinechar");
$page = parsetemplate($charsheet, $userrow);
display($page, "Character Information");
}
function showmap() {
global $userrow;
global $userrow;
// Make page tags for XHTML validation.
$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"DTD/xhtml1-transitional.dtd\">\n"
. "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n";
$page = $xml . gettemplate("minimal");
$array = array("content"=>"<center><img src=\"images/map.gif\" alt=\"Map\" /></center>", "title"=>"Map");
echo parsetemplate($page, $array);
die();
}
function babblebox() {
global $userrow;
if (isset($_POST["babble"])) {
$safecontent = makesafe($_POST["babble"]);
if ($safecontent == "" || $safecontent == " ") { //blank post. do nothing.
@ -260,17 +289,17 @@ function babblebox() {
header("Location: index.php?do=babblebox");
die();
}
$babblebox = array("content"=>"");
$bg = 1;
$babblequery = doquery("SELECT * FROM {{table}} ORDER BY id DESC LIMIT 20", "babble");
while ($babblerow = mysql_fetch_array($babblequery)) {
if ($bg == 1) { $new = "<div style=\"width:98%; background-color:#eeeeee;\">[<b>".$babblerow["author"]."</b>] ".$babblerow["babble"]."</div>\n"; $bg = 2; }
else { $new = "<div style=\"width:98%; background-color:#ffffff;\">[<b>".$babblerow["author"]."</b>] ".stripslashes($babblerow["babble"])."</div>\n"; $bg = 1; }
else { $new = "<div style=\"width:98%; background-color:#ffffff;\">[<b>".$babblerow["author"]."</b>] ".stripslashes($babblerow["babble"])."</div>\n"; $bg = 1; }
$babblebox["content"] = $new . $babblebox["content"];
}
$babblebox["content"] .= "<center><form action=\"index.php?do=babblebox\" method=\"post\"><input type=\"text\" name=\"babble\" size=\"15\" maxlength=\"120\" /><br /><input type=\"submit\" name=\"submit\" value=\"Babble\" /> <input type=\"reset\" name=\"reset\" value=\"Clear\" /></form></center>";
// Make page tags for XHTML validation.
$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"DTD/xhtml1-transitional.dtd\">\n"
@ -281,8 +310,10 @@ function babblebox() {
}
function ninja() {
header("Location: http://www.se7enet.com/img/shirtninja.jpg");
/**
* NINJA! 🥷
*/
function ninja(): void
{
exit('NINJA! 🥷');
}
?>

View File

@ -1,15 +1,13 @@
<?php
if (file_exists('../.installed')) {
echo 'Game already installed.';
header('Location: index.php');
exit;
}
require_once '../lib.php';
require_once '../src/lib.php';
define('START', microtime(true));
match ((int) $_GET['page'] ?? 0) {
match ((int) $_GET['page'] ?? 1) {
2 => second(),
3 => third(),
4 => fourth(),
@ -17,13 +15,12 @@ match ((int) $_GET['page'] ?? 0) {
default => first(),
};
/**
* First page - show warnings and gather info
*/
function first()
{
echo <<<END
echo <<<HTML
<html>
<head>
<title>Dragon Knight Installation</title>
@ -44,7 +41,7 @@ function first()
</form>
</body>
</html>
END;
HTML;
exit;
}
@ -58,18 +55,18 @@ function second()
$full = isset($_POST["complete"]);
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
CREATE TABLE babble (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`posttime` TEXT NOT NULL DEFAULT '00:00:00',
`author` TEXT NOT NULL DEFAULT '',
`babble` TEXT NOT NULL DEFAULT ''
);
END);
SQL);
echo $query === true ? 'Babble Box table created.<br />' : 'Error creating Babble Box table.';
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
CREATE TABLE control (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`gamename` TEXT NOT NULL DEFAULT '',
@ -93,7 +90,7 @@ function second()
`showbabble` INTEGER NOT NULL DEFAULT 0,
`showonline` INTEGER NOT NULL DEFAULT 0
);
END);
SQL);
echo $query === true ? 'Control table created.<br />' : 'Error creating Control table.';
@ -101,7 +98,7 @@ function second()
echo $query === true ? 'Control table populated.<br />' : 'Error populating Control table.';
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
CREATE TABLE drops (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`name` TEXT NOT NULL DEFAULT '',
@ -110,12 +107,12 @@ function second()
`attribute1` TEXT NOT NULL DEFAULT '',
`attribute2` TEXT NOT NULL DEFAULT ''
);
END);
SQL);
echo $query == true ? 'Drops table created.<br />' : 'Error creating Drops table.';
if ($full) {
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
INSERT INTO drops VALUES
(1, 'Life Pebble', 1, 1, 'maxhp,10', 'X'),
(2, 'Life Stone', 10, 1, 'maxhp,25', 'X'),
@ -149,12 +146,12 @@ function second()
(30, 'Diamond', 50, 1, 'defensepower,150', 'X'),
(31, 'Memory Drop', 5, 1, 'expbonus,10', 'X'),
(32, 'Fortune Drop', 5, 1, 'goldbonus,10', 'X');
END);
SQL);
echo $query === true ? 'Drops table populated.<br />' : 'Error populating Drops table.';
}
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
CREATE TABLE forum (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`postdate` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
@ -165,11 +162,11 @@ function second()
`title` TEXT NOT NULL DEFAULT '',
`content` TEXT NOT NULL
);
END);
SQL);
echo $query === true ? 'Forum table created.<br />' : 'Error creating Forum table.';
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
CREATE TABLE items (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`type` INTEGER NOT NULL DEFAULT 0,
@ -178,12 +175,12 @@ function second()
`attribute` INTEGER NOT NULL DEFAULT 0,
`special` TEXT NOT NULL DEFAULT ''
);
END);
SQL);
echo $query === true ? 'Items table created.<br />' : 'Error creating Items table.';
if ($full) {
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
INSERT INTO items VALUES
(1, 1, 'Stick', 10, 2, 'X'),
(2, 1, 'Branch', 30, 4, 'X'),
@ -218,12 +215,12 @@ function second()
(31, 3, 'Large Shield', 2500, 30, 'X'),
(32, 3, 'Silver Shield', 10000, 60, 'X'),
(33, 3, 'Destiny Aegis', 25000, 100, 'maxhp,50');
END);
SQL);
echo $query === true ? 'Items table populated.<br />' : 'Error populating Items table.';
}
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
CREATE TABLE levels (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`1_exp` INTEGER NOT NULL DEFAULT 0,
@ -248,12 +245,12 @@ function second()
`3_dexterity` INTEGER NOT NULL DEFAULT 0,
`3_spells` INTEGER NOT NULL DEFAULT 0
);
END);
SQL);
echo $query === true ? 'Levels table created.<br />' : 'Error creating Levels table.';
if ($full) {
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
INSERT INTO levels VALUES
(1, 0, 15, 0, 5, 5, 5, 0, 0, 15, 0, 5, 5, 5, 0, 0, 15, 0, 5, 5, 5, 0),
(2, 15, 2, 5, 1, 0, 1, 1, 18, 2, 4, 1, 2, 1, 1, 20, 2, 5, 1, 0, 2, 1),
@ -355,12 +352,12 @@ function second()
(98, 7837308, 4, 5, 5, 4, 3, 0, 8492119, 4, 3, 5, 7, 3, 0, 9461106, 4, 4, 5, 4, 4, 0),
(99, 7964068, 50, 5, 5, 6, 5, 0, 8627330, 50, 3, 5, 9, 5, 0, 9609488, 50, 4, 5, 6, 6, 0),
(100, 16777215, 0, 0, 0, 0, 0, 0, 16777215, 0, 0, 0, 0, 0, 0, 16777215, 0, 0, 0, 0, 0, 0);
END);
SQL);
echo $query === true ? 'Levels table populated.<br />' : 'Error populating Levels table.';
}
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
CREATE TABLE monsters (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`name` TEXT NOT NULL DEFAULT '',
@ -372,12 +369,12 @@ function second()
`maxgold` INTEGER NOT NULL DEFAULT 0,
`immune` INTEGER NOT NULL DEFAULT 0
);
END);
SQL);
echo $query === true ? 'Monsters table created.<br />' : 'Error creating Monsters table.';
if ($full) {
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
INSERT INTO monsters VALUES
(1, 'Blue Slime', 4, 3, 1, 1, 1, 1, 0),
(2, 'Red Slime', 6, 5, 1, 1, 2, 1, 0),
@ -530,18 +527,18 @@ function second()
(149, 'Titan', 360, 340, 270, 50, 2400, 800, 0),
(150, 'Black Daemon', 400, 400, 280, 50, 3000, 1000, 1),
(151, 'Lucifuge', 600, 600, 400, 50, 10000, 10000, 2);
END);
SQL);
echo $query === true ? 'Monsters table populated.<br />' : 'Error populating Monsters table.';
}
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
CREATE TABLE news (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`postdate` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`content` TEXT NOT NULL
);
END);
SQL);
echo $query === true ? 'News table created.<br />' : 'Error creating News table.';
@ -549,7 +546,7 @@ function second()
echo $query === true ? 'News table populated.<br />' : 'Error populating News table.';
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
CREATE TABLE spells (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`name` TEXT NOT NULL,
@ -557,12 +554,12 @@ function second()
`attribute` INTEGER NOT NULL DEFAULT 0,
`type` INTEGER NOT NULL DEFAULT 0
);
END);
SQL);
echo $query === true ? 'Spells table created.<br />' : 'Error creating Spells table.';
if ($full) {
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
INSERT INTO spells VALUES
(1, 'Heal', 5, 10, 1),
(2, 'Revive', 10, 25, 1),
@ -583,12 +580,12 @@ function second()
(17, 'Ward', 10, 10, 5),
(18, 'Fend', 20, 25, 5),
(19, 'Barrier', 30, 50, 5);
END);
SQL);
echo $query === true ? 'Spells table populated.<br />' : 'Error populating Spells table.';
}
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
CREATE TABLE towns (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`name` TEXT NOT NULL,
@ -599,12 +596,12 @@ function second()
`travelpoints` INTEGER NOT NULL DEFAULT 0,
`itemslist` TEXT NOT NULL
);
END);
SQL);
echo $query === true ? 'Towns table created.<br />' : 'Error creating Towns table.';
if ($full) {
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
INSERT INTO towns VALUES
(1, 'Midworld', 0, 0, 5, 0, 0, '1,2,3,17,18,19,28,29'),
(2, 'Roma', 30, 30, 10, 25, 5, '2,3,4,18,19,29'),
@ -614,65 +611,65 @@ function second()
(6, 'Hambry', 170, 170, 90, 1000, 80, '10,11,12,13,14,23,24,30,31'),
(7, 'Gilead', 200, -200, 100, 3000, 110, '12,13,14,15,24,25,26,32'),
(8, 'Endworld', -250, -250, 125, 9000, 160, '16,27,33');
END);
SQL);
echo $query === true ? 'Towns table populated.<br />' : 'Error populating Towns table.';
}
$query = db()->exec(<<<END
$query = db()->exec(<<<SQL
CREATE TABLE users (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`username` TEXT NOT NULL,
`password` TEXT NOT NULL,
`email` TEXT NOT NULL,
`verify` INTEGER NOT NULL default 0,
`regdate` datetime NOT NULL default CURRENT_TIMESTAMP,
`onlinetime` datetime NOT NULL default CURRENT_TIMESTAMP,
`authlevel` INTEGER NOT NULL default 0,
`latitude` INTEGER NOT NULL default 0,
`longitude` INTEGER NOT NULL default 0,
`difficulty` INTEGER NOT NULL default 0,
`charclass` INTEGER NOT NULL default 0,
`currentaction` TEXT NOT NULL default 'In Town',
`currentfight` INTEGER NOT NULL default 0,
`currentmonster` INTEGER NOT NULL default 0,
`currentmonsterhp` INTEGER NOT NULL default 0,
`currentmonstersleep` INTEGER NOT NULL default 0,
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`username` TEXT NOT NULL,
`password` TEXT NOT NULL,
`email` TEXT NOT NULL,
`verify` INTEGER NOT NULL default 0,
`regdate` datetime NOT NULL default CURRENT_TIMESTAMP,
`onlinetime` datetime NOT NULL default CURRENT_TIMESTAMP,
`authlevel` INTEGER NOT NULL default 0,
`latitude` INTEGER NOT NULL default 0,
`longitude` INTEGER NOT NULL default 0,
`difficulty` INTEGER NOT NULL default 0,
`charclass` INTEGER NOT NULL default 0,
`currentaction` TEXT NOT NULL default 'In Town',
`currentfight` INTEGER NOT NULL default 0,
`currentmonster` INTEGER NOT NULL default 0,
`currentmonsterhp` INTEGER NOT NULL default 0,
`currentmonstersleep` INTEGER NOT NULL default 0,
`currentmonsterimmune` INTEGER NOT NULL default 0,
`currentuberdamage` INTEGER NOT NULL default 0,
`currentuberdefense` INTEGER NOT NULL default 0,
`currenthp` INTEGER NOT NULL default 15,
`currentmp` INTEGER NOT NULL default 0,
`currenttp` INTEGER NOT NULL default 10,
`maxhp` INTEGER NOT NULL default 15,
`maxmp` INTEGER NOT NULL default 0,
`maxtp` INTEGER NOT NULL default 10,
`level` INTEGER NOT NULL default 1,
`gold` INTEGER NOT NULL default 100,
`experience` INTEGER NOT NULL default 0,
`goldbonus` INTEGER NOT NULL default 0,
`expbonus` INTEGER NOT NULL default 0,
`strength` INTEGER NOT NULL default 5,
`dexterity` INTEGER NOT NULL default 5,
`attackpower` INTEGER NOT NULL default 5,
`defensepower` INTEGER NOT NULL default 5,
`weaponid` INTEGER NOT NULL default 0,
`armorid` INTEGER NOT NULL default 0,
`shieldid` INTEGER NOT NULL default 0,
`slot1id` INTEGER NOT NULL default 0,
`slot2id` INTEGER NOT NULL default 0,
`slot3id` INTEGER NOT NULL default 0,
`weaponname` TEXT NOT NULL default 'None',
`armorname` TEXT NOT NULL default 'None',
`shieldname` TEXT NOT NULL default 'None',
`slot1name` TEXT NOT NULL default 'None',
`slot2name` TEXT NOT NULL default 'None',
`slot3name` TEXT NOT NULL default 'None',
`dropcode` INTEGER NOT NULL default 0,
`spells` TEXT NOT NULL default '0',
`towns` TEXT NOT NULL default '0'
`currentuberdamage` INTEGER NOT NULL default 0,
`currentuberdefense` INTEGER NOT NULL default 0,
`currenthp` INTEGER NOT NULL default 15,
`currentmp` INTEGER NOT NULL default 0,
`currenttp` INTEGER NOT NULL default 10,
`maxhp` INTEGER NOT NULL default 15,
`maxmp` INTEGER NOT NULL default 0,
`maxtp` INTEGER NOT NULL default 10,
`level` INTEGER NOT NULL default 1,
`gold` INTEGER NOT NULL default 100,
`experience` INTEGER NOT NULL default 0,
`goldbonus` INTEGER NOT NULL default 0,
`expbonus` INTEGER NOT NULL default 0,
`strength` INTEGER NOT NULL default 5,
`dexterity` INTEGER NOT NULL default 5,
`attackpower` INTEGER NOT NULL default 5,
`defensepower` INTEGER NOT NULL default 5,
`weaponid` INTEGER NOT NULL default 0,
`armorid` INTEGER NOT NULL default 0,
`shieldid` INTEGER NOT NULL default 0,
`slot1id` INTEGER NOT NULL default 0,
`slot2id` INTEGER NOT NULL default 0,
`slot3id` INTEGER NOT NULL default 0,
`weaponname` TEXT NOT NULL default 'None',
`armorname` TEXT NOT NULL default 'None',
`shieldname` TEXT NOT NULL default 'None',
`slot1name` TEXT NOT NULL default 'None',
`slot2name` TEXT NOT NULL default 'None',
`slot3name` TEXT NOT NULL default 'None',
`dropcode` INTEGER NOT NULL default 0,
`spells` TEXT NOT NULL default '0',
`towns` TEXT NOT NULL default '0'
);
END);
SQL);
echo $query === true ? 'Users table created.<br />' : 'Error creating Users table.';
@ -686,7 +683,7 @@ function second()
*/
function third()
{
echo <<<END
echo <<<HTML
<html>
<head>
<title>Dragon Knight Installation</title>
@ -708,7 +705,7 @@ function third()
</form>
</body>
</html>
END;
HTML;
exit;
}
@ -764,7 +761,7 @@ function fourth()
file_put_contents('../.installed', date('Y-m-d H:i:s'));
echo <<<END
echo <<<HTML
<html>
<head>
<title>Dragon Knight Installation</title>
@ -782,7 +779,7 @@ function fourth()
<a href="install.php?page=5">click here</a>.
</body>
</html>
END;
HTML;
exit;
}
@ -796,7 +793,7 @@ function fifth()
die('Dragon Knight was unable to send your URL. Please go back and try again, or just continue on to <a href=\"index.php\">the game</a>.');
}
echo <<<END
echo <<<HTML
<html>
<head>
<title>Dragon Knight Installation</title>
@ -807,7 +804,7 @@ function fifth()
You are now ready to <a href="index.php">play the game</a>. Note that you must log in through the public section before being allowed into the control panel. Once logged in, an "Admin" link will appear in the Functions box of the left sidebar panel.
</body>
</html>
END;
HTML;
exit;
}

View File

@ -1,42 +1,47 @@
<?php // login.php :: Handles logins and cookies.
<?php
include('lib.php');
if (isset($_GET["do"])) {
if ($_GET["do"] == "login") { login(); }
elseif ($_GET["do"] == "logout") { logout(); }
// login.php :: Handles logins and cookies.
if (!file_exists('../.installed')) {
header('Location: install.php');
exit;
}
function login() {
include('config.php');
$link = opendb();
if (isset($_POST["submit"])) {
$query = doquery("SELECT * FROM {{table}} WHERE username='".$_POST["username"]."' AND password='".md5($_POST["password"])."' LIMIT 1", "users");
if (mysql_num_rows($query) != 1) { die("Invalid username or password. Please go back and try again."); }
$row = mysql_fetch_array($query);
if (isset($_POST["rememberme"])) { $expiretime = time()+31536000; $rememberme = 1; } else { $expiretime = 0; $rememberme = 0; }
$cookie = $row["id"] . " " . $row["username"] . " " . md5($row["password"] . "--" . $dbsettings["secretword"]) . " " . $rememberme;
setcookie("dkgame", $cookie, $expiretime, "/", "", 0);
require_once '../src/lib.php';
match ($_GET['do'] ?? 'login') {
'login' => login(),
'logout' => logout()
};
function login()
{
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$u = trim($_POST['username'] ?? '');
$query = db()->query('SELECT * FROM users WHERE username = ? LIMIT 1;', [$u]);
if ($query === false) die("Invalid username or password. Please go back and try again.");
$row = $query->fetchArray(SQLITE3_ASSOC);
if (!password_verify($_POST['password'] ?? '', $row['password'])) die("Invalid username or password. Please go back and try again.");
$expiretime = isset($_POST["rememberme"]) ? time() + 31536000 : 0;
$rememberme = isset($_POST["rememberme"]) ? 1 : 0;
$cookie = implode(' ', [$row['id'], $row['username'], $row['password'], $rememberme]);
set_cookie("dkgame", $cookie, $expiretime);
header("Location: index.php");
die();
}
exit;
}
$page = gettemplate("login");
$title = "Log In";
display($page, $title, false, false, false, false);
}
function logout() {
setcookie("dkgame", "", time()-100000, "/", "", 0);
function logout()
{
set_cookie("dkgame", "", -3600);
header("Location: login.php?do=login");
die();
}
?>

View File

@ -2,22 +2,21 @@
require_once __DIR__ . '/database.php';
$starttime = getmicrotime();
$numqueries = 0;
$version = "1.1.11";
$build = "";
define('VERSION', '1.1.11');
define('BUILD', '');
define('START', microtime(true));
/**
* Open/get SQLite database connection.
* Open or get SQLite database connection.
*/
function db(): Database
{
return $GLOBALS['database'] ??= new Database(__DIR__ . '/database.db');
return $GLOBALS['database'] ??= new Database(__DIR__ . '/../database.db');
}
function gettemplate($templatename) { // SQL query for the template.
$filename = "templates/" . $templatename . ".php";
$filename = __DIR__ . "/../templates/" . $templatename . ".php";
include("$filename");
return $template;
@ -71,19 +70,19 @@ function makesafe($d) {
function admindisplay($content, $title) { // Finalize page and output to browser.
global $numqueries, $userrow, $controlrow, $starttime, $version, $build;
global $userrow, $controlrow;
if (!isset($controlrow)) {
$query = db()->query('SELECT * FROM control WHERE id=1 LIMIT 1;');
$controlrow = $query->fetchArray(SQLITE3_ASSOC);
}
$page = parsetemplate(gettemplate("admin"), [
"title"=>$title,
"content"=>$content,
"totaltime"=>round(getmicrotime() - $starttime, 4),
"numqueries"=>$numqueries,
"version"=>$version,
"build"=>$build
"title" => $title,
"content" => $content,
"totaltime" => round(getmicrotime() - START, 4),
"numqueries" => db()->count,
"version" => VERSION,
"build" => BUILD
]);
echo "<html>\n" . $page;
@ -194,16 +193,16 @@ function display($content, $title, $topnav=true, $leftnav=true, $rightnav=true,
}
$page = parsetemplate(gettemplate("primary"), [
"dkgamename"=>$controlrow["gamename"],
"title"=>$title,
"content"=>$content,
"rightnav"=>parsetemplate($rightnav,$userrow),
"leftnav"=>parsetemplate($leftnav,$userrow),
"topnav"=>$topnav,
"totaltime"=>round(microtime(true) - $starttime, 4),
"numqueries"=>$numqueries,
"version"=>$version,
"build"=>$build
"dkgamename" => $controlrow["gamename"],
"title" => $title,
"content" => $content,
"rightnav" => parsetemplate($rightnav, $userrow),
"leftnav" => parsetemplate($leftnav, $userrow),
"topnav" => $topnav,
"totaltime" => round(getmicrotime() - START, 4),
"numqueries" => db()->count,
"version" => VERSION,
"build" => BUILD
]);
echo "<html>\n" . $page;
@ -245,3 +244,13 @@ function set_cookie($name, $value, $expires)
'samesite' => 'Strict' // Enforce SameSite=Strict
]);
}
/**
* Get the current control row from the database.
*/
function get_control_row(): array|false
{
$query = db()->query('SELECT * FROM control WHERE id = 1 LIMIT 1;');
if ($query === false) return false;
return $query->fetchArray(SQLITE3_ASSOC);
}

View File

@ -1,56 +1,56 @@
<?php // towns.php :: Handles all actions you can do in town.
function inn() { // 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");
if (mysql_num_rows($townquery) != 1) { display("Cheat attempt detected.<br /><br />Get a life, loser.", "Error"); }
$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";
$page .= "<form action=\"index.php?do=inn\" method=\"post\">\n";
$page .= "<input type=\"submit\" name=\"submit\" value=\"Yes\" /> <input type=\"submit\" name=\"cancel\" value=\"No\" />\n";
$page .= "</form>\n";
}
display($page, $title);
}
function buy() { // 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");
if (mysql_num_rows($townquery) != 1) { display("Cheat attempt detected.<br /><br />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");
$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";
@ -70,28 +70,28 @@ function buy() { // Displays a list of available items for purchase.
$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);
}
function buy2($id) { // Confirm user's intent to purchase item.
global $userrow, $numqueries;
$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.<br /><br />Get a life, loser.", "Error"); }
$townrow = mysql_fetch_array($townquery);
$townitems = explode(",",$townrow["itemslist"]);
if (! in_array($id, $townitems)) { display("Cheat attempt detected.<br /><br />Get a life, loser.", "Error"); }
$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) {
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>";
@ -99,7 +99,7 @@ function buy2($id) { // Confirm user's intent to purchase item.
$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) {
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>";
@ -107,7 +107,7 @@ function buy2($id) { // Confirm user's intent to purchase item.
$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) {
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>";
@ -115,39 +115,39 @@ function buy2($id) { // Confirm user's intent to purchase item.
$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);
}
function buy3($id) { // Update user profile with new item & stats.
if (isset($_POST["cancel"])) { header("Location: index.php"); die(); }
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.<br /><br />Get a life, loser.", "Error"); }
$townrow = mysql_fetch_array($townquery);
$townitems = explode(",",$townrow["itemslist"]);
if (! in_array($id, $townitems)) { display("Cheat attempt detected.<br /><br />Get a life, loser.", "Error"); }
$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) {
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");
}
// Special item fields.
$specialchange1 = "";
$specialchange2 = "";
@ -167,7 +167,7 @@ function buy3($id) { // Update user profile with new item & stats.
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"];
$newattack = $userrow["attackpower"] + $itemsrow["attribute"] - $itemsrow2["attribute"];
@ -177,20 +177,20 @@ function buy3($id) { // Update user profile with new item & stats.
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', attackpower='$newattack', weaponid='$newid', weaponname='$newname', currenthp='$newhp', currentmp='$newmp', currenttp='$newtp' WHERE id='$userid' LIMIT 1", "users");
} elseif ($itemsrow["type"] == 2) { // Armor
// Check if they already have an item in the slot.
if ($userrow["armorid"] != 0) {
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");
}
// Special item fields.
$specialchange1 = "";
$specialchange2 = "";
@ -210,7 +210,7 @@ function buy3($id) { // Update user profile with new item & stats.
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"];
@ -220,20 +220,20 @@ function buy3($id) { // Update user profile with new item & stats.
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', armorid='$newid', armorname='$newname', currenthp='$newhp', currentmp='$newmp', currenttp='$newtp' WHERE id='$userid' LIMIT 1", "users");
} elseif ($itemsrow["type"] == 3) { // Shield
// Check if they already have an item in the slot.
if ($userrow["shieldid"] != 0) {
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");
}
// Special item fields.
$specialchange1 = "";
$specialchange2 = "";
@ -253,7 +253,7 @@ function buy3($id) { // Update user profile with new item & stats.
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"];
@ -263,32 +263,32 @@ function buy3($id) { // Update user profile with new item & stats.
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");
$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");
}
function maps() { // 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");
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"; }
$mapped = false;
foreach($mappedtowns as $a => $b) {
if ($b == $townrow["id"]) { $mapped = true; }
@ -298,76 +298,76 @@ function maps() { // List maps the user can buy.
} 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");
}
function maps2($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");
}
function maps3($id) { // Add new map to user's profile.
if (isset($_POST["cancel"])) { header("Location: index.php"); die(); }
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(); }
$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");
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");
}
function travelto($id, $usepoints=true) { // Send a user to a town from the Travel To menu.
global $userrow, $numqueries;
if ($userrow["currentaction"] == "Fighting") { header("Location: index.php?do=fight"); die(); }
$townquery = doquery("SELECT name,travelpoints,latitude,longitude FROM {{table}} WHERE id='$id' LIMIT 1", "towns");
$townrow = mysql_fetch_array($townquery);
if ($usepoints==true) {
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();
if ($usepoints==true) {
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();
}
$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"); die(); }
if ($usepoints == true) { $newtp = $userrow["currenttp"] - $townrow["travelpoints"]; } else { $newtp = $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;
@ -375,19 +375,16 @@ function travelto($id, $usepoints=true) { // Send a user to a town from the Trav
if ($b == $id) { $town = true; }
}
$mapped = implode(",",$mapped);
if ($town == false) {
if ($town == false) {
$mapped .= ",$id";
$mapped = "towns='".$mapped."',";
} else {
} else {
$mapped = "towns='".$mapped."',";
}
$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>.";
display($page, "Travel To");
}
?>
}