<commit_before><?php
/*
	File:		attack.php
	Created: 	4/4/2016 at 11:51PM Eastern Time
	Info: 		File that contains all PVP Logic.
	Author:		TheMasterGeneral
	Website: 	https://github.com/MasterGeneral156/chivalry-engine
*/
$atkpage = 1;
require("globals.php");
if (!isset($_GET['action'])) {
    $_GET['action'] = '';
}
switch ($_GET['action']) {
    case 'xp':
        xp();
        break;
    case 'mug':
        mug();
        break;
    case 'lost':
        lost();
        break;
    case 'beat':
        beat();
        break;
    default:
        attacking();
        break;
}
function attacking()
{
    global $db, $userid, $ir, $h, $api, $set, $atkpage;
    $menuhide = 1;                    //Hide the menu so players cannot load other pages,
    //and lessens the chance of a misclick and losing XP.
    $atkpage = 1;
    $tresder = Random(100, 999);    //RNG to prevent refreshing while attacking, thus
    //breaking progression of the attack system.
    $_GET['user'] = (isset($_GET['user']) && is_numeric($_GET['user'])) ? abs($_GET['user']) : '';
    if (empty($_GET['nextstep'])) {
        $_GET['nextstep'] = 0;
    }
    if ($_GET['nextstep'] > 0) {
        $_GET['tresde'] = (isset($_GET['tresde']) && is_numeric($_GET['tresde'])) ? abs($_GET['tresde']) : 0;
        //If RNG is not set, set it to 0.
        if (!isset($_SESSION['tresde'])) {
            $_SESSION['tresde'] = 0;
        }
        //If RNG is not the same number stored in session
        if (($_SESSION['tresde'] == $_GET['tresde']) || $_GET['tresde'] < 100) {
            alert("danger", "Uh Oh!", "Refreshing while you are attacking is a Federal Jail offense. You can lose all
			                        your experience for that.", true, 'index.php');
            die($h->endpage());
        }
        //Set RNG
        $_SESSION['tresde'] = $_GET['tresde'];
    }
    //If user is not specified.
    if (!$_GET['user']) {
        alert("danger", "Uh Oh!", "You've chosen to attack a non-existent user. Check your source and try again.", true, 'index.php');
        die($h->endpage());
    } //If the user is trying to attack himself.
    else if ($_GET['user'] == $userid) {
        alert("danger", "Uh Oh!", "Depressed or not, you cannot attack yourself.", true, 'index.php');
        die($h->endpage());
    } //If the user has no HP, and is not already attacking.
    else if ($ir['hp'] <= 1 && $ir['attacking'] == 0) {
        alert("danger", "Unconscious!", "You have no HP, so you cannot attack. Come back when your HP has refilled.", true, 'index.php');
        die($h->endpage());
    } //If the user has left a previous after losing.
    else if (isset($_SESSION['attacklost']) && $_SESSION['attacklost'] > 1) {
        $_SESSION['attacklost'] = 0;
        alert("danger", "Uh Oh!", "You cannot start another attack after you ran from the last one.", true, 'index.php');
        die($h->endpage());
    }
    $youdata = $ir;
    $laston = time() - 900;
    $q = $db->query("SELECT `u`.`userid`, `hp`, `equip_armor`, `username`,
	       `equip_primary`, `equip_secondary`, `guild`, `location`, `maxhp`,
	       `guard`, `agility`, `strength`, `gender`, `level`, `laston`
			FROM `users` AS `u`
			INNER JOIN `userstats` AS `us` ON `u`.`userid` = `us`.`userid`
			WHERE `u`.`userid` = {$_GET['user']}
			LIMIT 1");
    //Test for if the specified user is a valid and registered user.
    if ($db->num_rows($q) == 0) {
        alert("danger", "Uh Oh!", "The user you are trying to attack does not exist.", true, 'index.php');
        die($h->endpage());
    }
    $odata = $db->fetch_row($q);
    $db->free_result($q);
    //Check current user's last attacked user, and see that its the specified user.
    if ($ir['attacking'] && $ir['attacking'] != $_GET['user']) {
        $_SESSION['attacklost'] = 0;
        alert("danger", "Uh Oh!", "An unknown error has occurred. Please try again, or contact the admin team.", true, 'index.php');
        $api->UserInfoSetStatic($userid, "attacking", 0);
        die($h->endpage());
    }
    //Check that the opponent has 1 health point.
    if ($odata['hp'] == 1) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Uh Oh!", "{$odata['username']} doesn't have health to be attacked.", true, 'index.php');
        die($h->endpage());
    } //Check if the opponent is currently in the infirmary.
    else if ($api->UserStatus($_GET['user'], 'infirmary') == true) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Unconscious!", "{$odata['username']} is currently in the infirmary. Try again later.", true, 'index.php');
        die($h->endpage());
    } //Check if the current user is in the infirmary.
    else if ($api->UserStatus($ir['userid'], 'infirmary') == true) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Unconscious!", "You are currently in the infirmary. Try again after you heal out.", true, 'index.php');
        die($h->endpage());
    } //Check if the opponent is in the dungeon.
    else if ($api->UserStatus($userid, 'dungeon') == true) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Locked Up!", "{$odata['username']} is currently in the dungeon. Try again later.", true, 'index.php');
        die($h->endpage());
    } //Check if the current user is in the dungeon.
    else if ($api->UserStatus($userid, 'dungeon') == true) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Locked Up!", "You are currently in the dungeon. Try again after you've paid your debt to society.", true, 'index.php');
        die($h->endpage());
    } //Check if opponent has permission to be attacked.
    else if (permission('CanBeAttack', $_GET['user']) == false) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Uh Oh!", "Your opponent cannot be attacked this way.", true, 'index.php');
        die($h->endpage());
    } //Check if the current player has permission to attack.
    else if (permission('CanAttack', $userid) == false) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Uh Oh!", "A magical force keeps you from attacking your opponent. (Or anyone, for that matter)", true, 'index.php');
        die($h->endpage());
    } //Check if the opponent is level 2 or lower, and has been on in the last 15 minutes.
    else if ($odata['level'] < 3 && $odata['laston'] > $laston) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Uh Oh!", "You cannot attack online players who are level two or below.", true, 'index.php');
        die($h->endpage());
    }
    $_GET['weapon'] = (isset($_GET['weapon']) && is_numeric($_GET['weapon'])) ? abs($_GET['weapon']) : '';
    //If weapon is specified via $_GET, attack!!
    if ($_GET['weapon']) {
        if (!$_GET['nextstep']) {
            $_GET['nextstep'] = 1;
        }
        //Check for if current step is greater than the maximum attacks per session.
        if ($_GET['nextstep'] >= $set['MaxAttacksPerSession']) {
            $_SESSION['attacking'] = 0;
            $ir['attacking'] = 0;
            $api->UserInfoSetStatic($userid, "attacking", 0);
            alert("warning", "Uh Oh!", "Get stronger dude. This fight ends in stalemate.", true, 'index.php');
            die($h->endpage());
        }
        //Check if the attack is currently stored in session.
        if ($_SESSION['attacking'] == 0 && $ir['attacking'] == 0) {
            //Check if the current user has enough energy for this attack.
            if ($youdata['energy'] >= $youdata['maxenergy'] / $set['AttackEnergyCost']) {
                $youdata['energy'] -= floor($youdata['maxenergy'] / $set['AttackEnergyCost']);
                $cost = floor($youdata['maxenergy'] / $set['AttackEnergyCost']);
                $api->UserInfoSet($userid, 'energy', "-{$cost}");
                $_SESSION['attackdmg'] = 0;
            } //If not enough energy, stop the fight.
            else {
                $EnergyPercent = floor(100 / $set['AttackEnergyCost']);
                $UserCurrentEnergy = floor($ir['maxenergy'] / $ir['energy']);
                alert("danger", "Uh Oh!", "Attacking someone requires you to have {$EnergyPercent}% Energy. You currently
				                        only have {$UserCurrentEnergy}%", true, 'index.php');
                die($h->endpage());
            }
        }
        $_SESSION['attacking'] = $odata['userid'];
        $ir['attacking'] = $odata['userid'];
        $api->UserInfoSetStatic($userid, "attacking", $ir['attacking']);
        $_GET['nextstep'] = (isset($_GET['nextstep']) && is_numeric($_GET['nextstep'])) ? abs($_GET['nextstep']) : '';
        //Check if the current user is attacking with a weapon that they have equipped.
        if ($_GET['weapon'] != $ir['equip_primary'] && $_GET['weapon'] != $ir['equip_secondary']) {
            $api->UserInfoSet($userid, 'xp', 0);
            alert("danger", "Security Issue!", "You cannot attack with a weapon you don't have equipped... You lost your
			                                experience for that.", true, 'index.php');
            die($h->endpage());
        }
        $winfo_sql = "SELECT `itmname`, `weapon` FROM `items` WHERE `itmid` = {$_GET['weapon']} LIMIT 1";
        $qo = $db->query($winfo_sql);
        //If the weapon chosen is not a valid weapon.
        if ($db->num_rows($qo) == 0) {
            alert("danger", "Uh Oh!", "The weapon you're trying to attack with isn't valid. This likely means the weapon
			                        you chosen doesn't have a weapon value. Contact the admin team.", true, 'index.php');
            die($h->endpage());
        }
        $r1 = $db->fetch_row($qo);
        $db->free_result($qo);
        $mydamage = round(($r1['weapon'] * $youdata['strength'] / ($odata['guard'] / 1.5)) * (Random(10000, 12000) / 10000));
        $hitratio = max(10, min(60 * $ir['agility'] / $odata['agility'], 95));
        //If the attack attempt was connected.
        if (Random(1, 100) <= $hitratio) {
            //If the opponent has armor equipped.
            if ($odata['equip_armor'] > 0) {
                $armorinfo_sql = "SELECT `armor` FROM `items` WHERE `itmid` = {$odata['equip_armor']} LIMIT 1";
                $q3 = $db->query($armorinfo_sql);
                //Check that the armor is valid.
                if ($db->num_rows($q3) > 0) {
                    $mydamage -= $db->fetch_single($q3);
                }
                $db->free_result($q3);
            }
            $theirbeforehp = $odata['hp'];
            //Fix damage...
            if ($mydamage < -100000) {
                $mydamage = abs($mydamage);
            } else if ($mydamage < 1) {
                $mydamage = 1;
            }
            $crit = Random(1, 40);
            //If user makes a critical hit, multiply damage.
            if ($crit == 17) {
                $mydamage *= Random(20, 40) / 10;
            } //If unlucky crit... reduce damage.
            else if ($crit == 25 OR $crit == 8) {
                $mydamage /= (Random(20, 40) / 10);
            }
            if ($mydamage > $theirbeforehp) {
                $mydamage = $odata['hp'];
            }
            $mydamage = round($mydamage);
            $odata['hp'] -= $mydamage;
            if ($odata['hp'] == 1) {
                $odata['hp'] = 0;
                $mydamage += 1;
            }
            //Fixes query error if the opponent HP is lower than 0.
            if (($odata['hp'] - $mydamage) < 0) {
                $mydamage = $mydamage + $odata['hp'];
            }
            //If opponent HP lower than 0, set to 0.
            if ($odata['hp'] < 0) {
                $odata['hp'] = 0;
            }
            //Reduce health.
            $api->UserInfoSet($_GET['user'], "hp", "-{$mydamage}", false);
            echo "{$_GET['nextstep']}) Using your {$r1['itmname']} you manage to strike {$odata['username']} dealing {$mydamage} damage. ({$odata['hp']} HP Remaining)<br />";
            $_SESSION['attackdmg'] += $mydamage;
        } else {
            echo "{$_GET['nextstep']}) You attempt to strike {$odata['username']} but missed. ({$odata['hp']} HP Remaining)<br />";
        }
        //Win fight because opponent's health is 0 or lower.
        if ($odata['hp'] <= 0) {
            $odata['hp'] = 0;
            $_SESSION['attackwon'] = $_GET['user'];
            $api->UserInfoSet($_GET['user'], 'hp', 0);
            echo "<br />
			<b>You have struck down {$odata['username']}. What do you wish to do to them now?</b><br />
			<form action='?action=mug&ID={$_GET['user']}' method='post'><input class='btn btn-primary' type='submit' value='Rob Them' /></form><br />
			<form action='?action=beat&ID={$_GET['user']}' method='post'><input class='btn btn-primary' type='submit' value='Increase Infirmary Time' /></form><br />
			<form action='?action=xp&ID={$_GET['user']}' method='post'><input class='btn btn-primary' type='submit' value='Gain Experience' /></form>";
        } //The opponent is not down... he gets to attack.
        else {
            $eq = $db->query("SELECT `itmname`,`weapon` FROM  `items` WHERE `itmid` IN({$odata['equip_primary']}, {$odata['equip_secondary']})");
            //If opponent does not have a valid weapon equipped, make them punch with fists.
            if ($db->num_rows($eq) == 0) {
                $wep = "Fists";
                $dam = round(round((($odata['strength'] / $ir['guard'] / 100)) + 1) * (Random(10000, 12000) / 10000));
            } else {
                $cnt = 0;
                while ($r = $db->fetch_row($eq)) {
                    $enweps[] = $r;
                    $cnt++;
                }
                $db->free_result($eq);
                $weptouse = Random(0, $cnt - 1);    //Select opponent weapon to use.
                $wep = $enweps[$weptouse]['itmname'];
                $dam = round(($enweps[$weptouse]['weapon'] * $odata['strength'] / ($youdata['guard'] / 1.5)) * (Random(10000, 12000) / 10000));
            }
            $hitratio = max(10, min(60 * $odata['agility'] / $ir['agility'], 95));
            //If hit connects with user.
            if (Random(1, 100) <= $hitratio) {
                //If user has armor equipped.
                if ($ir['equip_armor'] > 0) {
                    $q3 = $db->query("SELECT `armor` FROM `items` WHERE `itmid` = {$ir['equip_armor']} LIMIT 1");
                    //If user has valid armor equipped.
                    if ($db->num_rows($q3) > 0) {
                        $dam -= $db->fetch_single($q3);
                    }
                    $db->free_result($q3);
                }
                $yourbeforehp = $ir['hp'];
                //If the user doesn't have armor equipped.
                if ($dam < -100000) {
                    $dam = abs($dam);
                } //If damage is lower than 1, set to 1.
                else if ($dam < 1) {
                    $dam = 1;
                }
                $crit = Random(1, 40);
                //RNG for critical damage, multiply damage!
                if ($crit == 17) {
                    $dam *= Random(20, 40) / 10;
                } //Unlucky... crit is weak.
                else if ($crit == 25 OR $crit == 8) {
                    $dam /= (Random(20, 40) / 10);
                }
                $dam = round($dam);
                $youdata['hp'] -= $dam;
                //If user has 1 hp.
                if ($youdata['hp'] == 1) {
                    $dam += 1;
                    $youdata['hp'] = 0;
                }
                if (($ir['hp'] - $dam) < 0) {
                    $dam = $ir['hp'];
                    $youdata['hp'] = 0;
                }
                //If user HP less than 0, set to 0.
                if ($ir['hp'] < 0) {
                    $ir['hp'] = 0;
                }
                if ($dam > $yourbeforehp) {
                    $dam = $ir['hp'];
                }
                $api->UserInfoSet($userid, "hp", -$dam);
                $ns = $_GET['nextstep'] + 1;
                echo "{$ns}) Using their {$wep}, {$odata['username']} managed to strike you dealing {$dam} damage. ({$youdata['hp']} HP Remaining)<br />";
            } //Opponent misses their hit.
            else {
                $ns = $_GET['nextstep'] + 1;
                echo "{$ns}) {$odata['username']} attempted to strike you, but missed. ({$youdata['hp']} HP Remaining)<br />";
            }
            //User has no HP left, redirect to loss.
            if ($youdata['hp'] <= 0) {
                $youdata['hp'] = 0;
                $_SESSION['attacklost'] = $_GET['user'];
                $api->UserInfoSet($userid, "hp", 0);
                echo "<form action='?action=lost&ID={$_GET['user']}' method='post'><input type='submit' class='btn btn-primary' value='Lose Fight' />";
            }
        }
    } //Opponent has less than 5 HP, fight cannot start.
    else if ($odata['hp'] < 5) {
        alert("danger", "Uh Oh!", "{$odata['username']}'s health is too low to be attacked.", true, 'index.php');
        die($h->endpage());<commit_msg>Some alerts!<commit_after><?php
/*
	File:		attack.php
	Created: 	4/4/2016 at 11:51PM Eastern Time
	Info: 		File that contains all PVP Logic.
	Author:		TheMasterGeneral
	Website: 	https://github.com/MasterGeneral156/chivalry-engine
*/
$atkpage = 1;
require("globals.php");
if (!isset($_GET['action'])) {
    $_GET['action'] = '';
}
switch ($_GET['action']) {
    case 'xp':
        xp();
        break;
    case 'mug':
        mug();
        break;
    case 'lost':
        lost();
        break;
    case 'beat':
        beat();
        break;
    default:
        attacking();
        break;
}
function attacking()
{
    global $db, $userid, $ir, $h, $api, $set, $atkpage;
    $menuhide = 1;                    //Hide the menu so players cannot load other pages,
    //and lessens the chance of a misclick and losing XP.
    $atkpage = 1;
    $tresder = Random(100, 999);    //RNG to prevent refreshing while attacking, thus
    //breaking progression of the attack system.
    $_GET['user'] = (isset($_GET['user']) && is_numeric($_GET['user'])) ? abs($_GET['user']) : '';
    if (empty($_GET['nextstep'])) {
        $_GET['nextstep'] = 0;
    }
    if ($_GET['nextstep'] > 0) {
        $_GET['tresde'] = (isset($_GET['tresde']) && is_numeric($_GET['tresde'])) ? abs($_GET['tresde']) : 0;
        //If RNG is not set, set it to 0.
        if (!isset($_SESSION['tresde'])) {
            $_SESSION['tresde'] = 0;
        }
        //If RNG is not the same number stored in session
        if (($_SESSION['tresde'] == $_GET['tresde']) || $_GET['tresde'] < 100) {
            alert("danger", "Uh Oh!", "Refreshing while you are attacking is a Federal Jail offense. You can lose all
			                        your experience for that.", true, 'index.php');
            die($h->endpage());
        }
        //Set RNG
        $_SESSION['tresde'] = $_GET['tresde'];
    }
    //If user is not specified.
    if (!$_GET['user']) {
        alert("danger", "Uh Oh!", "You've chosen to attack a non-existent user. Check your source and try again.", true, 'index.php');
        die($h->endpage());
    } //If the user is trying to attack himself.
    else if ($_GET['user'] == $userid) {
        alert("danger", "Uh Oh!", "Depressed or not, you cannot attack yourself.", true, 'index.php');
        die($h->endpage());
    } //If the user has no HP, and is not already attacking.
    else if ($ir['hp'] <= 1 && $ir['attacking'] == 0) {
        alert("danger", "Unconscious!", "You have no HP, so you cannot attack. Come back when your HP has refilled.", true, 'index.php');
        die($h->endpage());
    } //If the user has left a previous after losing.
    else if (isset($_SESSION['attacklost']) && $_SESSION['attacklost'] > 1) {
        $_SESSION['attacklost'] = 0;
        alert("danger", "Uh Oh!", "You cannot start another attack after you ran from the last one.", true, 'index.php');
        die($h->endpage());
    }
    $youdata = $ir;
    $laston = time() - 900;
    $q = $db->query("SELECT `u`.`userid`, `hp`, `equip_armor`, `username`,
	       `equip_primary`, `equip_secondary`, `guild`, `location`, `maxhp`,
	       `guard`, `agility`, `strength`, `gender`, `level`, `laston`
			FROM `users` AS `u`
			INNER JOIN `userstats` AS `us` ON `u`.`userid` = `us`.`userid`
			WHERE `u`.`userid` = {$_GET['user']}
			LIMIT 1");
    //Test for if the specified user is a valid and registered user.
    if ($db->num_rows($q) == 0) {
        alert("danger", "Uh Oh!", "The user you are trying to attack does not exist.", true, 'index.php');
        die($h->endpage());
    }
    $odata = $db->fetch_row($q);
    $db->free_result($q);
    //Check current user's last attacked user, and see that its the specified user.
    if ($ir['attacking'] && $ir['attacking'] != $_GET['user']) {
        $_SESSION['attacklost'] = 0;
        alert("danger", "Uh Oh!", "An unknown error has occurred. Please try again, or contact the admin team.", true, 'index.php');
        $api->UserInfoSetStatic($userid, "attacking", 0);
        die($h->endpage());
    }
    //Check that the opponent has 1 health point.
    if ($odata['hp'] == 1) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Uh Oh!", "{$odata['username']} doesn't have health to be attacked.", true, 'index.php');
        die($h->endpage());
    } //Check if the opponent is currently in the infirmary.
    else if ($api->UserStatus($_GET['user'], 'infirmary') == true) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Unconscious!", "{$odata['username']} is currently in the infirmary. Try again later.", true, 'index.php');
        die($h->endpage());
    } //Check if the current user is in the infirmary.
    else if ($api->UserStatus($ir['userid'], 'infirmary') == true) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Unconscious!", "You are currently in the infirmary. Try again after you heal out.", true, 'index.php');
        die($h->endpage());
    } //Check if the opponent is in the dungeon.
    else if ($api->UserStatus($userid, 'dungeon') == true) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Locked Up!", "{$odata['username']} is currently in the dungeon. Try again later.", true, 'index.php');
        die($h->endpage());
    } //Check if the current user is in the dungeon.
    else if ($api->UserStatus($userid, 'dungeon') == true) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Locked Up!", "You are currently in the dungeon. Try again after you've paid your debt to society.", true, 'index.php');
        die($h->endpage());
    } //Check if opponent has permission to be attacked.
    else if (permission('CanBeAttack', $_GET['user']) == false) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Uh Oh!", "Your opponent cannot be attacked this way.", true, 'index.php');
        die($h->endpage());
    } //Check if the current player has permission to attack.
    else if (permission('CanAttack', $userid) == false) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Uh Oh!", "A magical force keeps you from attacking your opponent. (Or anyone, for that matter)", true, 'index.php');
        die($h->endpage());
    } //Check if the opponent is level 2 or lower, and has been on in the last 15 minutes.
    else if ($odata['level'] < 3 && $odata['laston'] > $laston) {
        $_SESSION['attacking'] = 0;
        $ir['attacking'] = 0;
        $api->UserInfoSetStatic($userid, "attacking", 0);
        alert("danger", "Uh Oh!", "You cannot attack online players who are level two or below.", true, 'index.php');
        die($h->endpage());
    }
    $_GET['weapon'] = (isset($_GET['weapon']) && is_numeric($_GET['weapon'])) ? abs($_GET['weapon']) : '';
    //If weapon is specified via $_GET, attack!!
    if ($_GET['weapon']) {
        if (!$_GET['nextstep']) {
            $_GET['nextstep'] = 1;
        }
        //Check for if current step is greater than the maximum attacks per session.
        if ($_GET['nextstep'] >= $set['MaxAttacksPerSession']) {
            $_SESSION['attacking'] = 0;
            $ir['attacking'] = 0;
            $api->UserInfoSetStatic($userid, "attacking", 0);
            alert("warning", "Uh Oh!", "Get stronger dude. This fight ends in stalemate.", true, 'index.php');
            die($h->endpage());
        }
        //Check if the attack is currently stored in session.
        if ($_SESSION['attacking'] == 0 && $ir['attacking'] == 0) {
            //Check if the current user has enough energy for this attack.
            if ($youdata['energy'] >= $youdata['maxenergy'] / $set['AttackEnergyCost']) {
                $youdata['energy'] -= floor($youdata['maxenergy'] / $set['AttackEnergyCost']);
                $cost = floor($youdata['maxenergy'] / $set['AttackEnergyCost']);
                $api->UserInfoSet($userid, 'energy', "-{$cost}");
                $_SESSION['attackdmg'] = 0;
            } //If not enough energy, stop the fight.
            else {
                $EnergyPercent = floor(100 / $set['AttackEnergyCost']);
                $UserCurrentEnergy = floor($ir['maxenergy'] / $ir['energy']);
                alert("danger", "Uh Oh!", "Attacking someone requires you to have {$EnergyPercent}% Energy. You currently
				                        only have {$UserCurrentEnergy}%", true, 'index.php');
                die($h->endpage());
            }
        }
        $_SESSION['attacking'] = $odata['userid'];
        $ir['attacking'] = $odata['userid'];
        $api->UserInfoSetStatic($userid, "attacking", $ir['attacking']);
        $_GET['nextstep'] = (isset($_GET['nextstep']) && is_numeric($_GET['nextstep'])) ? abs($_GET['nextstep']) : '';
        //Check if the current user is attacking with a weapon that they have equipped.
        if ($_GET['weapon'] != $ir['equip_primary'] && $_GET['weapon'] != $ir['equip_secondary']) {
            $api->UserInfoSet($userid, 'xp', 0);
            alert("danger", "Security Issue!", "You cannot attack with a weapon you don't have equipped... You lost your
			                                experience for that.", true, 'index.php');
            die($h->endpage());
        }
        $winfo_sql = "SELECT `itmname`, `weapon` FROM `items` WHERE `itmid` = {$_GET['weapon']} LIMIT 1";
        $qo = $db->query($winfo_sql);
        //If the weapon chosen is not a valid weapon.
        if ($db->num_rows($qo) == 0) {
            alert("danger", "Uh Oh!", "The weapon you're trying to attack with isn't valid. This likely means the weapon
			                        you chosen doesn't have a weapon value. Contact the admin team.", true, 'index.php');
            die($h->endpage());
        }
        $r1 = $db->fetch_row($qo);
        $db->free_result($qo);
        $mydamage = round(($r1['weapon'] * $youdata['strength'] / ($odata['guard'] / 1.5)) * (Random(10000, 12000) / 10000));
        $hitratio = max(10, min(60 * $ir['agility'] / $odata['agility'], 95));
        //If the attack attempt was connected.
        if (Random(1, 100) <= $hitratio) {
            //If the opponent has armor equipped.
            if ($odata['equip_armor'] > 0) {
                $armorinfo_sql = "SELECT `armor` FROM `items` WHERE `itmid` = {$odata['equip_armor']} LIMIT 1";
                $q3 = $db->query($armorinfo_sql);
                //Check that the armor is valid.
                if ($db->num_rows($q3) > 0) {
                    $mydamage -= $db->fetch_single($q3);
                }
                $db->free_result($q3);
            }
            $theirbeforehp = $odata['hp'];
            //Fix damage...
            if ($mydamage < -100000) {
                $mydamage = abs($mydamage);
            } else if ($mydamage < 1) {
                $mydamage = 1;
            }
            $crit = Random(1, 40);
            //If user makes a critical hit, multiply damage.
            if ($crit == 17) {
                $mydamage *= Random(20, 40) / 10;
            } //If unlucky crit... reduce damage.
            else if ($crit == 25 OR $crit == 8) {
                $mydamage /= (Random(20, 40) / 10);
            }
            if ($mydamage > $theirbeforehp) {
                $mydamage = $odata['hp'];
            }
            $mydamage = round($mydamage);
            $odata['hp'] -= $mydamage;
            if ($odata['hp'] == 1) {
                $odata['hp'] = 0;
                $mydamage += 1;
            }
            //Fixes query error if the opponent HP is lower than 0.
            if (($odata['hp'] - $mydamage) < 0) {
                $mydamage = $mydamage + $odata['hp'];
            }
            //If opponent HP lower than 0, set to 0.
            if ($odata['hp'] < 0) {
                $odata['hp'] = 0;
            }
            //Reduce health.
            $api->UserInfoSet($_GET['user'], "hp", "-{$mydamage}", false);
            alert('success', "Attempt {$_GET['nextstep']}!", "Using your {$r1['itmname']} you manage to strike
            {$odata['username']} dealing {$mydamage} damage. Your opponent has {$odata['hp']} HP remaining.", false);
            $_SESSION['attackdmg'] += $mydamage;
        } else {
            echo "{$_GET['nextstep']}) You attempt to strike {$odata['username']} but missed. ({$odata['hp']} HP remaining)<br />";
            alert('warning', "Attempt {$_GET['nextstep']}!", "You attempt to strike {$odata['username']} but missed. Your
            opponent has {$odata['hp']} HP Remaining.", false);
        }
        //Win fight because opponent's health is 0 or lower.
        if ($odata['hp'] <= 0) {
            $odata['hp'] = 0;
            $_SESSION['attackwon'] = $_GET['user'];
            $api->UserInfoSet($_GET['user'], 'hp', 0);
            echo "<br />
			<b>You have struck down {$odata['username']}. What do you wish to do to them now?</b><br />
			<form action='?action=mug&ID={$_GET['user']}' method='post'><input class='btn btn-primary' type='submit' value='Rob Them' /></form><br />
			<form action='?action=beat&ID={$_GET['user']}' method='post'><input class='btn btn-primary' type='submit' value='Increase Infirmary Time' /></form><br />
			<form action='?action=xp&ID={$_GET['user']}' method='post'><input class='btn btn-primary' type='submit' value='Gain Experience' /></form>";
        } //The opponent is not down... he gets to attack.
        else {
            $eq = $db->query("SELECT `itmname`,`weapon` FROM  `items` WHERE `itmid` IN({$odata['equip_primary']}, {$odata['equip_secondary']})");
            //If opponent does not have a valid weapon equipped, make them punch with fists.
            if ($db->num_rows($eq) == 0) {
                $wep = "Fists";
                $dam = round(round((($odata['strength'] / $ir['guard'] / 100)) + 1) * (Random(10000, 12000) / 10000));
            } else {
                $cnt = 0;
                while ($r = $db->fetch_row($eq)) {
                    $enweps[] = $r;
                    $cnt++;
                }
                $db->free_result($eq);
                $weptouse = Random(0, $cnt - 1);    //Select opponent weapon to use.
                $wep = $enweps[$weptouse]['itmname'];
                $dam = round(($enweps[$weptouse]['weapon'] * $odata['strength'] / ($youdata['guard'] / 1.5)) * (Random(10000, 12000) / 10000));
            }
            $hitratio = max(10, min(60 * $odata['agility'] / $ir['agility'], 95));
            //If hit connects with user.
            if (Random(1, 100) <= $hitratio) {
                //If user has armor equipped.
                if ($ir['equip_armor'] > 0) {
                    $q3 = $db->query("SELECT `armor` FROM `items` WHERE `itmid` = {$ir['equip_armor']} LIMIT 1");
                    //If user has valid armor equipped.
                    if ($db->num_rows($q3) > 0) {
                        $dam -= $db->fetch_single($q3);
                    }
                    $db->free_result($q3);
                }
                $yourbeforehp = $ir['hp'];
                //If the user doesn't have armor equipped.
                if ($dam < -100000) {
                    $dam = abs($dam);
                } //If damage is lower than 1, set to 1.
                else if ($dam < 1) {
                    $dam = 1;
                }
                $crit = Random(1, 40);
                //RNG for critical damage, multiply damage!
                if ($crit == 17) {
                    $dam *= Random(20, 40) / 10;
                } //Unlucky... crit is weak.
                else if ($crit == 25 OR $crit == 8) {
                    $dam /= (Random(20, 40) / 10);
                }
                $dam = round($dam);
                $youdata['hp'] -= $dam;
                //If user has 1 hp.
                if ($youdata['hp'] == 1) {
                    $dam += 1;
                    $youdata['hp'] = 0;
                }
                if (($ir['hp'] - $dam) < 0) {
                    $dam = $ir['hp'];
                    $youdata['hp'] = 0;
                }
                //If user HP less than 0, set to 0.
                if ($ir['hp'] < 0) {
                    $ir['hp'] = 0;
                }
                if ($dam > $yourbeforehp) {
                    $dam = $ir['hp'];
                }
                $api->UserInfoSet($userid, "hp", -$dam);
                $ns = $_GET['nextstep'] + 1;
                alert('danger', "Attempt {$ns}!", "Using their {$wep}, {$odata['username']} managed to strike you dealing
                 {$dam} damage. You have {$youdata['hp']} HP remaining.", false);
            } //Opponent misses their hit.
            else {
                $ns = $_GET['nextstep'] + 1;
                alert('info', "Attempt {$ns}!", "{$odata['username']} attempted to strike you, but missed. You have {$youdata['hp']} HP remaining.", false);
            }
            //User has no HP left, redirect to loss.
            if ($youdata['hp'] <= 0) {
                $youdata['hp'] = 0;
                $_SESSION['attacklost'] = $_GET['user'];
                $api->UserInfoSet($userid, "hp", 0);
                echo "<form action='?action=lost&ID={$_GET['user']}' method='post'><input type='submit' class='btn btn-primary' value='Lose Fight' />";
            }
        }
    } //Opponent has less than 5 HP, fight cannot start.
    else if ($odata['hp'] < 5) {
        alert("danger", "Uh Oh!", "{$odata['username']}'s health is too low to be attacked.", true, 'index.php');
        die($h->endpage());
{'fraction_non_alphanumeric': 0.18215425978827088, 'fraction_numerical': 0.014899456673948356, 'mean_word_length': 2.0451134231622037, 'pattern_counts': {'":': 0, '<': 76, '<?xml version=': 0, '>': 186, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5958688', 'n_tokens_mistral': 11578, 'n_tokens_neox': 10709, 'n_words': 3088}