Repair most of fight

This commit is contained in:
Sky Johnson 2024-12-12 18:52:12 -06:00
parent 9348c17179
commit 98204ee9f1

View File

@ -1,26 +1,27 @@
<?php // fight.php :: Handles all fighting action. <?php
function fight() { // One big long function that determines the outcome of the fight. // fight.php :: Handles all fighting action.
global $userrow, $controlrow; /**
if ($userrow["currentaction"] != "Fighting") { display("Cheat attempt detected.<br><br>Get a life, loser.", "Error"); } * One big long function that determines the outcome of the fight.
*/
function fight()
{
global $userrow;
if ($userrow["currentaction"] != "Fighting") display("Cheat attempt detected.<br><br>Get a life, loser.", "Error");
$pagearray = array(); $pagearray = array();
$playerisdead = 0; $playerisdead = 0;
$pagearray["magiclist"] = ""; $pagearray["magiclist"] = "";
$userspells = explode(",", $userrow["spells"]); $userspells = explode(",", $userrow["spells"]);
$spellquery = doquery("SELECT id,name FROM {{table}}", "spells"); $spellquery = db()->query('SELECT id, name FROM spells ORDER BY id;');
while ($spellrow = mysql_fetch_array($spellquery)) { while ($spellrow = $spellquery->fetchArray(SQLITE3_ASSOC)) {
$spell = false; $spell = false;
foreach ($userspells as $a => $b) { foreach ($userspells as $b) if ($b == $spellrow["id"]) $spell = true;
if ($b == $spellrow["id"]) { $spell = true; } if ($spell == true) $pagearray["magiclist"] .= "<option value=\"".$spellrow["id"]."\">".$spellrow["name"]."</option>\n";
}
if ($spell == true) {
$pagearray["magiclist"] .= "<option value=\"".$spellrow["id"]."\">".$spellrow["name"]."</option>\n";
}
unset($spell); unset($spell);
} }
if ($pagearray["magiclist"] == "") { $pagearray["magiclist"] = "<option value=\"0\">None</option>\n"; } if ($pagearray["magiclist"] == "") $pagearray["magiclist"] = "<option value=\"0\">None</option>\n";
$magiclist = $pagearray["magiclist"]; $magiclist = $pagearray["magiclist"];
$chancetoswingfirst = 1; $chancetoswingfirst = 1;
@ -37,8 +38,9 @@ function fight() { // One big long function that determines the outcome of the f
// Pick a monster. // Pick a monster.
$monsterquery = doquery("SELECT * FROM {{table}} WHERE level>='$minlevel' AND level<='$maxlevel' ORDER BY RAND() LIMIT 1", "monsters"); $monsterrow = db()->query('SELECT * FROM monsters WHERE level >= ? AND level <= ? ORDER BY RANDOM() LIMIT 1;', [
$monsterrow = mysql_fetch_array($monsterquery); $minlevel, $maxlevel
])->fetchArray(SQLITE3_ASSOC);
$userrow["currentmonster"] = $monsterrow["id"]; $userrow["currentmonster"] = $monsterrow["id"];
$userrow["currentmonsterhp"] = rand((($monsterrow["maxhp"]/5)*4),$monsterrow["maxhp"]); $userrow["currentmonsterhp"] = rand((($monsterrow["maxhp"]/5)*4),$monsterrow["maxhp"]);
$userrow["currentmonstersleep"] = 0; $userrow["currentmonstersleep"] = 0;
@ -49,12 +51,10 @@ function fight() { // One big long function that determines the outcome of the f
unset($monsterquery); unset($monsterquery);
unset($monsterrow); unset($monsterrow);
} }
// Next, get the monster statistics. // Next, get the monster statistics.
$monsterquery = doquery("SELECT * FROM {{table}} WHERE id='".$userrow["currentmonster"]."' LIMIT 1", "monsters"); $monsterrow = get_monster($userrow['currentmonster']);
$monsterrow = mysql_fetch_array($monsterquery);
$pagearray["monstername"] = $monsterrow["name"]; $pagearray["monstername"] = $monsterrow["name"];
// Do run stuff. // Do run stuff.
@ -96,15 +96,16 @@ function fight() { // One big long function that determines the outcome of the f
if ($userrow["currenthp"] <= 0) { if ($userrow["currenthp"] <= 0) {
$newgold = ceil($userrow["gold"]/2); $newgold = ceil($userrow["gold"]/2);
$newhp = ceil($userrow["maxhp"]/4); $newhp = ceil($userrow["maxhp"]/4);
$updatequery = doquery("UPDATE {{table}} SET currenthp='$newhp',currentaction='In Town',currentmonster='0',currentmonsterhp='0',currentmonstersleep='0',currentmonsterimmune='0',currentfight='0',latitude='0',longitude='0',gold='$newgold' WHERE id='".$userrow["id"]."' LIMIT 1", "users"); db()->query("UPDATE users SET currenthp=?, currentaction='In Town', currentmonster=0, currentmonsterhp=0, currentmonstersleep=0, currentmonsterimmune=0, currentfight=0, latitude=0, longitude=0, gold=? WHERE id=?;", [
$newhp, $newgold, $userrow['id']
]);
$playerisdead = 1; $playerisdead = 1;
} }
} }
} }
$updatequery = doquery("UPDATE {{table}} SET currentaction='Exploring' WHERE id='".$userrow["id"]."' LIMIT 1", "users"); db()->query("UPDATE users SET currentaction='Exploring' WHERE id=?;", [$userrow['id']]);
header("Location: index.php"); redirect('index.php');
die();
// Do fight stuff. // Do fight stuff.
} elseif (isset($_POST["fight"])) { } elseif (isset($_POST["fight"])) {
@ -130,9 +131,8 @@ function fight() { // One big long function that determines the outcome of the f
$userrow["currentmonsterhp"] -= $monsterdamage; $userrow["currentmonsterhp"] -= $monsterdamage;
$pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "<br><br>"; $pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "<br><br>";
if ($userrow["currentmonsterhp"] <= 0) { if ($userrow["currentmonsterhp"] <= 0) {
$updatequery = doquery("UPDATE {{table}} SET currentmonsterhp='0' WHERE id='".$userrow["id"]."' LIMIT 1", "users"); db()->query('UPDATE users SET currentmonsterhp=0 WHERE id=?;', [$userrow['id']]);
header("Location: index.php?do=victory"); redirect('index.php?do=victory');
die();
} }
// Monster's turn. // Monster's turn.
@ -166,7 +166,9 @@ function fight() { // One big long function that determines the outcome of the f
if ($userrow["currenthp"] <= 0) { if ($userrow["currenthp"] <= 0) {
$newgold = ceil($userrow["gold"]/2); $newgold = ceil($userrow["gold"]/2);
$newhp = ceil($userrow["maxhp"]/4); $newhp = ceil($userrow["maxhp"]/4);
$updatequery = doquery("UPDATE {{table}} SET currenthp='$newhp',currentaction='In Town',currentmonster='0',currentmonsterhp='0',currentmonstersleep='0',currentmonsterimmune='0',currentfight='0',latitude='0',longitude='0',gold='$newgold' WHERE id='".$userrow["id"]."' LIMIT 1", "users"); db()->query("UPDATE users SET currenthp=?, currentaction='In Town', currentmonster=0, currentmonsterhp=0, currentmonstersleep=0, currentmonsterimmune=0, currentfight=0, latitude=0, longitude=0, gold=? WHERE id=?;", [
$newhp, $newgold, $userrow['id']
]);
$playerisdead = 1; $playerisdead = 1;
} }
} }
@ -178,8 +180,7 @@ function fight() { // One big long function that determines the outcome of the f
$pickedspell = $_POST["userspell"]; $pickedspell = $_POST["userspell"];
if ($pickedspell == 0) { display("You must select a spell first. Please go back and try again.", "Error"); die(); } if ($pickedspell == 0) { display("You must select a spell first. Please go back and try again.", "Error"); die(); }
$newspellquery = doquery("SELECT * FROM {{table}} WHERE id='$pickedspell' LIMIT 1", "spells"); $newspellrow = get_spell($pickedspell);
$newspellrow = mysql_fetch_array($newspellquery);
$spell = false; $spell = false;
foreach($userspells as $a => $b) { foreach($userspells as $a => $b) {
if ($b == $pickedspell) { $spell = true; } if ($b == $pickedspell) { $spell = true; }
@ -222,9 +223,10 @@ function fight() { // One big long function that determines the outcome of the f
$pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "<br><br>"; $pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "<br><br>";
if ($userrow["currentmonsterhp"] <= 0) { if ($userrow["currentmonsterhp"] <= 0) {
$updatequery = doquery("UPDATE {{table}} SET currentmonsterhp='0',currenthp='".$userrow["currenthp"]."',currentmp='".$userrow["currentmp"]."' WHERE id='".$userrow["id"]."' LIMIT 1", "users"); db()->query('UPDATE users SET currentmonsterhp=0, currenthp=?, currentmp=? WHERE id=?;', [
header("Location: index.php?do=victory"); $userrow['currenthp'], $userrow['currentmp'], $userrow['id']
die(); ]);
redirect('index.php?do=victory');
} }
// Monster's turn. // Monster's turn.
@ -258,7 +260,9 @@ function fight() { // One big long function that determines the outcome of the f
if ($userrow["currenthp"] <= 0) { if ($userrow["currenthp"] <= 0) {
$newgold = ceil($userrow["gold"]/2); $newgold = ceil($userrow["gold"]/2);
$newhp = ceil($userrow["maxhp"]/4); $newhp = ceil($userrow["maxhp"]/4);
$updatequery = doquery("UPDATE {{table}} SET currenthp='$newhp',currentaction='In Town',currentmonster='0',currentmonsterhp='0',currentmonstersleep='0',currentmonsterimmune='0',currentfight='0',latitude='0',longitude='0',gold='$newgold' WHERE id='".$userrow["id"]."' LIMIT 1", "users"); db()->query("UPDATE users SET currenthp=?, currentaction='In Town', currentmonster=0, currentmonsterhp=0, currentmonstersleep=0, currentmonsterimmune=0, currentfight=0, latitude=0, longitude=0, gold=? WHERE id=?;", [
$newhp, $newgold, $userrow['id']
]);
$playerisdead = 1; $playerisdead = 1;
} }
} }
@ -297,7 +301,9 @@ function fight() { // One big long function that determines the outcome of the f
if ($userrow["currenthp"] <= 0) { if ($userrow["currenthp"] <= 0) {
$newgold = ceil($userrow["gold"]/2); $newgold = ceil($userrow["gold"]/2);
$newhp = ceil($userrow["maxhp"]/4); $newhp = ceil($userrow["maxhp"]/4);
$updatequery = doquery("UPDATE {{table}} SET currenthp='$newhp',currentaction='In Town',currentmonster='0',currentmonsterhp='0',currentmonstersleep='0',currentmonsterimmune='0',currentfight='0',latitude='0',longitude='0',gold='$newgold' WHERE id='".$userrow["id"]."' LIMIT 1", "users"); db()->query("UPDATE users SET currenthp=?, currentaction='In Town', currentmonster=0, currentmonsterhp=0, currentmonstersleep=0, currentmonsterimmune=0, currentfight=0, latitude=0, longitude=0, gold=? WHERE id=?;", [
$newhp, $newgold, $userrow['id']
]);
$playerisdead = 1; $playerisdead = 1;
} }
} }
@ -320,25 +326,24 @@ function fight() { // One big long function that determines the outcome of the f
$newmp = $userrow["currentmp"]; $newmp = $userrow["currentmp"];
if ($playerisdead != 1) { if ($playerisdead != 1) {
$pagearray["command"] = <<<END $pagearray["command"] = <<<HTML
Command?<br><br> Command?<br><br>
<form action="index.php?do=fight" method="post"> <form action="index.php?do=fight" method="post">
<input type="submit" name="fight" value="Fight" /><br><br> <input type="submit" name="fight" value="Fight" /><br><br>
<select name="userspell"><option value="0">Choose One</option>$magiclist</select> <input type="submit" name="spell" value="Spell" /><br><br> <select name="userspell"><option value="0">Choose One</option>$magiclist</select> <input type="submit" name="spell" value="Spell" /><br><br>
<input type="submit" name="run" value="Run" /><br><br> <input type="submit" name="run" value="Run" /><br><br>
</form> </form>
END; HTML;
$updatequery = doquery("UPDATE {{table}} SET currentaction='Fighting',currenthp='$newhp',currentmp='$newmp',currentfight='$newfight',currentmonster='$newmonster',currentmonsterhp='$newmonsterhp',currentmonstersleep='$newmonstersleep',currentmonsterimmune='$newmonsterimmune',currentuberdamage='$newuberdamage',currentuberdefense='$newuberdefense' WHERE id='".$userrow["id"]."' LIMIT 1", "users");
db()->query("UPDATE users SET currentaction='Fighting', currenthp=?, currentmp=?, currentfight=?, currentmonster=?, currentmonsterhp=?, currentmonstersleep=?, currentmonsterimmune=?, currentuberdamage=?, currentuberdefense=? WHERE id=?;", [
$newhp, $newmp, $newfight, $newmonster, $newmonsterhp, $newmonstersleep, $newmonsterimmune, $newuberdamage, $newuberdefense, $userrow['id']
]);
} else { } else {
$pagearray["command"] = "<b>You have died.</b><br><br>As a consequence, you've lost half of your gold. However, you have been given back a portion of your hit points to continue your journey.<br><br>You may now continue back to <a href=\"index.php\">town</a>, and we hope you fair better next time."; $pagearray["command"] = "<b>You have died.</b><br><br>As a consequence, you've lost half of your gold. However, you have been given back a portion of your hit points to continue your journey.<br><br>You may now continue back to <a href=\"index.php\">town</a>, and we hope you fair better next time.";
} }
// Finalize page and display it. // Finalize page and display it.
$template = gettemplate("fight"); display(parsetemplate(gettemplate("fight"), $pagearray), "Fighting");
$page = parsetemplate($template,$pagearray);
display($page, "Fighting");
} }
function victory() { function victory() {
@ -515,12 +520,12 @@ function drop() {
} }
function dead() { function dead()
{
$page = "<b>You have died.</b><br><br>As a consequence, you've lost half of your gold. However, you have been given back a portion of your hit points to continue your journey.<br><br>You may now continue back to <a href=\"index.php\">town</a>, and we hope you fair better next time."; return <<<HTML
<b>You have died.</b><br><br>
As a consequence, you've lost half of your gold. However, you have been given back a portion of your hit points
to continue your journey.<br><br>
You may now continue back to <a href="index.php">town</a>, and we hope you fair better next time.
HTML;
} }
?>