Jump to content
MakeWebGames

Clean up my code


Dragon Blade

Recommended Posts

I need someone to clean up my code! If you can I will be great full!

 

function fight() 
{
global $db, $ir, $c, $h, $userid;


if (isset($_POST['submit']) && !empty($_POST['ID']) && !preg_match('[^1-9]', $_POST['ID'])) 
{
$_GET['ID'] = abs((int) $_GET['ID']);

$robot = mysql_query("SELECT * FROM robots WHERE userid='{$_POST['ID']}'");
$opp=mysql_fetch_array($robot);

$yourrobot= mysql_query("SELECT * FROM robots WHERE userid='{$ir['userid']}'");
$you=mysql_fetch_array($yourrobot);


if ($_POST['ID'] == $_SESSION['userid']) 
{ 
echo "<hr width='50%'>Please read the error message.<hr width='50%'><h3>! ERROR</h3>You can not fight your own robot!<br />
<hr width='50%'><a href='robots.php'>> Go Back</a><hr width='50%'><br /><br />";
$h->endpage();
exit;
}
if (empty($opp['ID'])) 
{ 
echo "<hr width='50%'>Please read the error message.<hr width='50%'><h3>! ERROR</h3>This user does not have a robot!<br />
<hr width='50%'><a href='robots.php'>> Go Back</a><hr width='50%'><br /><br />";
$h->endpage();
exit;
}



echo"<hr width = 95%><font color='red'><b>".$you['name']." vs ".$opp['name']."</b></font><br/><br/>";




	$opp_stats = $opp['speed'] + $opp['aggression'] + $opp['health'];
	$you_stats = $you['speed'] + $you['aggression'] + $you['health'];

	$winner = ($opp_stats > $you_stats) ? $opp['name'] : $you['name'];
	$winner_ID = ($opp_stats > $you_stats) ? $opp['userid'] : $you['userid'];

	$loser = ($opp_stats > $you_stats) ? $you['userid'] : $opp['userid'];

	$speed_winner = ($opp['speed'] > $you['speed']) ? $opp['name'] : $you['name'];
	$agg_winner = ($opp['aggression'] > $you['aggression']) ? $opp['name'] : $you['name'];
	$health_winner = ($opp['health'] > $you['health']) ? $opp['name'] : $you['name'];

	echo 'The robots both enter the arena.<br />
	<b>1.</b> <b>'.$speed_winner.'</b> runs in and starts swinging away, the other robot has no chance, <b>'.$speed_winner.'</b> is obviously faster<br />
	<b>2.</b> The robots scrap each other to the ground, <b>'.$agg_winner.'</b> is alot more aggressive and punches furiously!<br />
	<b>3.</b> The robots are both drained out, <b>'.$health_winner.'</b> seems to have had the most health throughout this fight, but its not over yet!<br />
	<br />
	<b>Result:</b> Both the robots are giving it their best. But to finish it off <b>'.$winner.'</b> jumps in the air and lands a critical big punch to the opponents neck!<br />
	<br /><b>'.$winner.' is the winner!</b><hr width=95%><a href="robots.php">> Go Back</a><hr width=95%><br /><br />';



	 event_add($opp['userid'], '<a href="viewuser.php?u='.$you['userid'].'">'.$you['name'].'</a> fought your robot, the winning robot was '.$winner.'!');


	$SQL = sprintf("UPDATE `cocks` SET `lost` = `lost` + 1, `health` = 0 WHERE `userid` = %u", $loser);
	$SQL_2 = sprintf("UPDATE `cocks` SET `won` = `won` + 1 WHERE `userid` = %u", $winner_ID);

	mysql_query($SQL);
	mysql_query($SQL_2);










}
else 
      {

	echo '<hr width=75%><br /><form action="robots.php?x=fight" method="POST">
	Userid: <input type="text" name="ID" /><br />
	<input type="submit" name="submit" value="Fight" />
	</form><br /><hr width=75%><a href="robots.php">> Go Back</a><hr width=75%><br /><br />';
}
     }



 

 

For some reason this also dont work!

 


	$SQL = sprintf("UPDATE `cocks` SET `lost` = `lost` + 1, `health` = 0 WHERE `userid` = %u", $loser);
	$SQL_2 = sprintf("UPDATE `cocks` SET `won` = `won` + 1 WHERE `userid` = %u", $winner_ID);

	mysql_query($SQL);
	mysql_query($SQL_2);
Link to comment
Share on other sites

  • 1 month later...
function fight() {
global $db, $ir, $c, $h, $userid;
$_POST['ID'] = isset($_POST['ID']) && ctype_digit($_POST['ID']) ? abs(@intval($_POST['ID'])) : null; // Hate me all you want. I've never had an issue with this!
if(isset($_POST['submit']) && !empty($_POST['ID'])) {
	$robot      = $db->query("SELECT * FROM `robots` WHERE `userid` = ".$_POST['ID']);
	$opp        = $db->fetch_row($robot);
	$yourrobot  = $db->query("SELECT * FROM `robots` WHERE `userid` = ".$ir['userid']);
	$you        = $db->fetch_row($yourrobot);
	if($_POST['ID'] == $ir['userid']) {
		echo "<hr width='50%'>Please read the error message.<hr width='50%'><h3>! ERROR</h3>You can not fight your own robot!<hr width='50%'><a href='robots.php'>→ Go Back</a><hr width='50%'>";
		$h->endpage();
		exit;
	}
	if(empty($opp['ID'])) {
		echo "<hr width='50%'>Please read the error message.<hr width='50%'><h3>! ERROR</h3>This user does not have a robot!<hr width='50%'><a href='robots.php'>→ Go Back</a><hr width='50%'>";
		$h->endpage();
		exit;
	}
	echo "<hr width='95%'><span style='color:red;font-weight:700;'>" , $you['name'] , " vs " , $opp['name'] , "</span>";
	$opp_stats     = $opp['speed'] + $opp['aggression'] + $opp['health'];
	$you_stats     = $you['speed'] + $you['aggression'] + $you['health'];
	$winner        = ($opp_stats > $you_stats) ? $opp['name'] : $you['name'];
	$winner_ID     = ($opp_stats > $you_stats) ? $opp['userid'] : $you['userid'];
	$loser         = ($opp_stats > $you_stats) ? $you['userid'] : $opp['userid'];
	$speed_winner  = ($opp['speed'] > $you['speed']) ? $opp['name'] : $you['name'];
	$agg_winner    = ($opp['aggression'] > $you['aggression']) ? $opp['name'] : $you['name'];
	$health_winner = ($opp['health'] > $you['health']) ? $opp['name'] : $you['name'];
	echo 'The robots both enter the arena.
       <strong>1, ' , $speed_winner , '</strong> runs in and starts swinging away, the other robot has no chance, <strong>' , $speed_winner , '</strong> is obviously faster<br />
       <strong>2.</strong> The robots scrap each other to the ground, <strong>' , $agg_winner , '</strong> is alot more aggressive and punches furiously!<br />
       <strong>3.</strong> The robots are both drained out, <strong>' , $health_winner , '</strong> seems to have had the most health throughout this fight, but its not over yet!<br />
        <strong>Result:</strong> Both the robots are giving it their best. But to finish it off <strong>' , $winner , '</strong> jumps in the air and lands a critical big punch to the opponents neck!<br />
       <strong>' , $winner , ' is the winner!</strong><hr width=95%><a href="robots.php">→ Go Back</a><hr width=95%>';
	event_add($opp['userid'], '<a href="viewuser.php?u=' , $you['userid'] , '">' , $you['name'] , '</a> fought your robot, the winning robot was ' , $winner , '!');
	$db->query("UPDATE `cocks` SET `lost` = `lost` + 1, `health` = 0 WHERE `userid` = ".$loser);
	$db->query("UPDATE `cocks` SET `won` = `won` + 1 WHERE `userid` = ".$winner_ID);
} else {
	echo '<hr width="75%"><form action="robots.php?x=fight" method="post">
       Userid: <input type="number" name="ID" />
       <input type="submit" name="submit" value="Fight" />
       </form><hr width="75%"><a href="robots.php">→ Go Back</a><hr width="75%">';
}
}

*Added line:

$_POST['ID'] = isset($_POST['ID']) && ctype_digit($_POST['ID']) ? abs(@intval($_POST['ID'])) : null; // Hate me all you want, I've never had an issue with this

*Removed:

 && !preg_match('[^1-9]', $_POST['ID'])

*Removed:

$_GET['ID'] = abs((int) $_GET['ID']);

- 1. There's no call for it. 2. I abhor typecasting. Sanitize and verify your user data properly!

*Converted from v1 to v2 (I spotted the global declaration of $db)

*Replaced $_SESSION['userid'] with $ir['userid'] (makes more sense to keep the same code throughout the system)

*Replaced "> Go Back" with "→ Go Back"

*Replaced

echo"<hr width = 95%><font color='red'><b>".$you['name']." vs ".$opp['name']."</b></font>";

with

echo "<hr width='95%'><span style='color:red;font-weight:700;'>" , $you['name'] , " vs " , $opp['name'] , "</span>";

*Replaced all instances of legacy HTML bold tags to strong tags

*Moved the $SQL and $SQL_2 into the query function itself

*Replaced all instances of '<hr width=75%>' with '<hr width="75%">' (Pet peeve..)

*Replaced case of 'method="POST"' to lowercase

*Replaced 'Userid: <input type="text" ...' with 'Userid: <input type="number" ...'

...

The word "replaced" looks weird now

Edited by Magictallguy
Coloured-coded formatting
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...