Jump to content

Never ending page load


almightyegg

Recommended Posts

When I run this script, it tries to load but after aout 10 minutes the whole browser locks up because it can't do it.

I have a hunch it is because the while loop doesn't ever stop, but I don't know how to prevent it.

 

if($defender[agility] >= $mem[agility]){
$whosturn = defender ;
} else {
$whosturn = attacker;
}
$aweap = mysql_query("SELECT power FROM weapons WHERE id = '{$mem['wid']}'");
$aarm = mysql_query("SELECT power FROM armor WHERE id = '{$mem['wid']}'");
$dweap = mysql_query("SELECT power FROM weapons WHERE id = '{$defender['wid']}'");
$darm = mysql_query("SELECT power FROM armor WHERE id = '{$defender['wid']}'");
$aform = $mem[magic]*($mem[agility]/4)*($mem[stamina]/2)*$mem[strength]+($aweap-$darm);
$aform2 = $mem[magic]*($mem[agility]/4)*($mem[stamina]/2)*$mem[strength]+($aweap-$darm)*1.5;
$aformula = rand($aform, $aform2);
$dform = $defender[magic]*($defender[agility]/4)*($defender[stamina]/2)*$defender[strength]+($dweap-$aarm);
$dform2 = $defender[magic]*($defender[agility]/4)*($defender[stamina]/2)*$defender[strength]+($dweap-$aarm)*1.5;
$dformula = rand($dform, $dform2);
while($mem[curlf] > 0 OR $defender[curlf] > 0){
if($whosturn == attacker){
	$whosturn = defender;
	$defender[curlf] = $defender[curlf] - $aformula;
	if($defender[curlf] <= 0){ $winner = attacker; }  else {
		echo "You did $aformula to $defender[username]!";
	}
} else {
	$whosturn = attacker;
	$mem[curlf] = $mem[curlf] - $dformula;
	if($mem[curlf] <= 0){
		$winner = defender;
		echo "$defender[username] did $dformula to you and killed you!";
	} else {
		echo "$defender[username] did $dformula to you!";
	}
	if($winner == attacker){
		echo "Victor: $mem[username]";
	} else {
		echo "Victor: $defender[username]";
	}
}
}

Link to comment
https://forums.phpfreaks.com/topic/47112-never-ending-page-load/
Share on other sites

i have had some problems with compound while or if commands before and they seem to work if I break them out a bit more and make them more specific, try changing your while command to

 


while(($mem[curlf] > 0) || ($defender[curlf] > 0))

 

its worth a go at least

I have move the winner IF statement out of the while loop because you do not want to display the winner untill the battle while loop is compete, that makes sense to me, I have also made the  attacker and defender string variables

 

<?php
if($defender[agility] >= $mem[agility])
{
$whosturn = "defender" ;
} 
else 
{
$whosturn = "attacker";
}
$aweap = mysql_query("SELECT power FROM weapons WHERE id = '{$mem['wid']}'");
$aarm = mysql_query("SELECT power FROM armor WHERE id = '{$mem['wid']}'");
$dweap = mysql_query("SELECT power FROM weapons WHERE id = '{$defender['wid']}'");
$darm = mysql_query("SELECT power FROM armor WHERE id = '{$defender['wid']}'");
$aform = $mem[magic]*($mem[agility]/4)*($mem[stamina]/2)*$mem[strength]+($aweap-$darm);
$aform2 = $mem[magic]*($mem[agility]/4)*($mem[stamina]/2)*$mem[strength]+($aweap-$darm)*1.5;
$aformula = rand($aform, $aform2);
$dform = $defender[magic]*($defender[agility]/4)*($defender[stamina]/2)*$defender[strength]+($dweap-$aarm);
$dform2 = $defender[magic]*($defender[agility]/4)*($defender[stamina]/2)*$defender[strength]+($dweap-$aarm)*1.5;
$dformula = rand($dform, $dform2);
while(($mem[curlf] > 0) || ($defender[curlf] > 0))
{
if($whosturn == "attacker")
{
	$whosturn = "defender";
	$defender[curlf] = $defender[curlf] - $aformula;
	if($defender[curlf] <= 0)
	{ 
 		$winner = "attacker"; 
	}  
	else 
	{
		echo "You did $aformula to $defender[username]!";
	}
} 
else 
{
	$whosturn = "attacker";
	$mem[curlf] = $mem[curlf] - $dformula;
	if($mem[curlf] <= 0)
	{
		$winner = "defender";
		echo "$defender[username] did $dformula to you and killed you!";
	} 
	else 
	{
		echo "$defender[username] did $dformula to you!";
	}

}
}
if($winner == "attacker")
{
echo "Victor: $mem[username]";
} 
else 
{
echo "Victor: $defender[username]";
}
?>

while(($mem[curlf] > 0) || ($defender[curlf] > 0))

 

the problem about this loop is that it only stops when both variables hit zero or less, and because the winner always have more then 0 hp will this result in an neverending loop, therefor i suggest changing the "or" (||) into an "and" (&&) which will close the loop because the loops checks for both variables being greater then 0

 

Also 10 minutes execution time is not really recommended, you could run out of memory and be looking at a blue screen! Ask google how to set your execution time to something more "recommended", i should use 30 seconds for development, and remove it, when going live

Wow! It loads now BUT shows:

Kofu did 1 to you!

You did -3 to Kofu!

Kofu did 1 to you!

You did -3 to Kofu!

Kofu did 1 to you!

You did -3 to Kofu!

Kofu did 1 to you!

You did -3 to Kofu!

Kofu did 1 to you!

You did -3 to Kofu!

Kofu did 1 to you!

You did -3 to Kofu!

Kofu did 1 to you!

You did -3 to Kofu!

Kofu did 1 to you!

You did -3 to Kofu!

Kofu did 1 to you!

You did -3 to Kofu!

Kofu did 1 to you!

You did -3 to Kofu!

Kofu did 1 to you!

You did -3 to Kofu!

Kofu did 1 to you!

You did -3 to Kofu!

Kofu did 1 to you!

You did -3 to Kofu!

Kofu did 1 to you!

You did -3 to Kofu!

Kofu did 1 to you and killed you!

Victor: Kofu

 

 

1. Why isn't it doing the rand() function?

2. Why is one damage + and one -?

3. Can you guys help me to make it more acturate, EG. If player1 has 2x amount of agility, it will go twice before player2 gets 1 turn?

the reason it does it that way is that you calculate the $aformula and $dformula once at the beginning then use them same values all the way through try writing two small functions for calculating them on a "per turn" basis?

 

function aform($aform, $aform2)
{
$aformula = rand($aform, $aform2);
return $aformula;  
}

function dform($dform, $dform2)
{
$dformula = rand($dform, $dform2);
return $dformula;
}

 

then

 

<?php
while(($mem[curlf] > 0) || ($defender[curlf] > 0))
{
if($whosturn == "attacker")
{
	$whosturn = "defender";
	$aformula = aform($aform, $aform2);
	$defender[curlf] = $defender[curlf] - $aformula;
	if($defender[curlf] <= 0)
	{ 
 		$winner = "attacker"; 
	}  
	else 
	{
		echo "You did $aformula to $defender[username]!";
	}
} 
else 
{
	$whosturn = "attacker";
	$dformula = dform($dform, $dform2);
	$mem[curlf] = $mem[curlf] - $dformula;
	if($mem[curlf] <= 0)
	{
		$winner = "defender";
		echo "$defender[username] did $dformula to you and killed you!";
	} 
	else 
	{
		echo "$defender[username] did $dformula to you!";
	}

}
}
?>

I changed to what you suggested + editted something else because in my formula I had put an incorrect column name so I now get:

Kofu did 2 to you, 13 left.

You did -1 to Kofu, 16 left.

Kofu did 3 to you, 10 left.

You did -1 to Kofu, 17 left.

Kofu did 2 to you, 8 left.

You did -1 to Kofu, 18 left.

Kofu did 2 to you, 6 left.

You did -1 to Kofu, 19 left.

Kofu did 2 to you, 4 left.

You did -1 to Kofu, 20 left.

Kofu did 3 to you, 1 left.

You did -1 to Kofu, 21 left.

Kofu did 3 to you, 0 left.

Victor: Kofu

 

Also I echoed $aform + $aform2 +  $dform + $dform2:

a: -1.5, -3

d: 2.5, 3

 

They should equal exactly the same.

aform & dform should be 1.5

aform2 & dform2 should be 2.25

 

Formulas:

$aform = $mem[magic]*($mem[agility]/4)*($mem[stamina]/2)*$mem[strength]+($aweap-$darm);

$aform2 = $aform*1.5;

$dform = $defender[magic]*($defender[agility]/4)*($defender[stamina]/2)*$defender[strength]+($dweap-$aarm);

$dform2 = $dform*1.5;

 

ALL magics = 1

ALL strengths = 3

ALL staminas = 2

ALL agilities = 2

ALL darms, dweaps, aarms, aweaps = 0

Hold up! I've just found that it isn't reading the $darm $dweap etc. correctly, it was coming out as resource ID #blah etc. So I did this:

$a1 = mysql_query("SELECT * FROM weapons WHERE id = '{$mem['wiu']}'");

$a2 = mysql_query("SELECT * FROM armor WHERE id = '{$mem['aiu']}'");

$d1 = mysql_query("SELECT * FROM weapons WHERE id = '{$defender['wiu']}'");

$d2 = mysql_query("SELECT * FROM armor WHERE id = '{$defender['aiu']}'");

$aw = mysql_fetch_array($a1);

$aa = mysql_fetch_array($a2);

$dw = mysql_fetch_array($d1);

$da = mysql_fetch_array($d2);

$aarm = $aa[power];

$aweap = $aw[power];

$darm = $da[power];

$dweap = $dw[power];

 

Do their values are blank when it should be 0

if you are using row variables there must be single tick around the column names

<?php
$aarm = $aa['power'];
$aweap = $aw['power'];
$darm = $da['power'];
$dweap = $dw['power'];
?>

 

and then Formulas:

<?php
$aform = $mem['magic']*($mem['agility']/4)*($mem['stamina']/2)*$mem['strength']+($aweap-$darm);
$aform2 = $aform*1.5;
$dform = $defender['magic']*($defender['agility']/4)*($defender['stamina']/2)*$defender['strength']+($dweap-$aarm);
$dform2 = $dform*1.5;
?>

Thanks that is all working now... I tried to fix a few problems like 'What happens if one player is so strong that the other player gets a negativ formula' this means they miss, So I tried this script:

while(($mem[curlf] > 0) && ($defender[curlf] > 0))
{
if($whosturn == "attacker")
{
	$whosturn = "defender";
	$aformula = aform($aform, $aform2);
	if($aformula <= 0)
	{
		echo "You missed $defender[username].<br>";
	}
	elseif($defender[curlf] <= 0)
	{ 
		$defender[curlf] = $defender[curlf] - $aformula;
 		$winner = "attacker"; 
		echo "You did $aformula to $defender[username], 0 left.<br>";
	}  
	else
	{
		$defender[curlf] = $defender[curlf] - $aformula;
		echo "You did $aformula to $defender[username], $defender[curlf] left.<br>";		
	}
	} 
else 
{
	$whosturn = "attacker";
	$dformula = dform($dform, $dform2);
	if($dformula <= 0)
	{
		echo "$defender[username] missed you.<br>";
	}
	elseif(($mem[curlf] = $mem[curlf] - $dformula) <= 0)
	{
		$winner = "defender";
		echo "$defender[username] did $dformula to you, 0 left.<br>";
	} 
	else
	{
		$mem[curlf] = $mem[curlf] - $dformula;
		echo "$defender[username] did $dformula to you, $mem[curlf] left.<br>";
	}

}
}

 

Problems are that it thinks my character only has 14lf when it has 15 and it no longer recognises if somebody makes the other players lf go down to 0 (Kofu did 2 to you, -1 left.)

 

???

w00t! I got it working!

 

while(($mem[curlf] > 0) && ($defender[curlf] > 0))
{
if($whosturn == "attacker")
{
	$whosturn = "defender";
	$aformula = aform($aform, $aform2);
	if($aformula <= 0)
	{
		echo "You missed $defender[username].<br>";
	}
	elseif(($defender[curlf] = $defender[curlf] - $aformula) <= 0)
	{ 
 		$winner = "attacker"; 
		echo "You did $aformula to $defender[username], 0 left.<br>";
	}  
	else
	{
		echo "You did $aformula to $defender[username], $defender[curlf] left.<br>";		
	}
	} 
else 
{
	$whosturn = "attacker";
	$dformula = dform($dform, $dform2);
	if($dformula <= 0)
	{
		echo "$defender[username] missed you.<br>";
	}
	elseif(($mem[curlf] = $mem[curlf] - $dformula) <= 0)
	{
		$winner = "defender";
		echo "$defender[username] did $dformula to you, 0 left.<br>";
	} 
	else
	{
		echo "$defender[username] did $dformula to you, $mem[curlf] left.<br>";
	}

}
}
if($winner == "attacker")
{
echo "Victor: <a href='http://www.lordoftheabyss.com/player/view.php?id=$mem[id]'>$mem[username]</a><br>";
} 
else 
{
echo "Victor: <a href='http://www.lordoftheabyss.com/player/view.php?id=$defender[id]'>$defender[username]</a><br>";
}

 

Finally:

3. Can you guys help me to make it more acturate, EG. If player1 has 2x amount of agility, it will go twice before player2 gets 1 turn?

 

i take it then that the fighters agility comes from $mem[agility] and $defender[agility]. you could then have two new variables called something like $memag and $defag and loop there attacks with further while loops such as

<?php
$memag = $mem['agility'];
$defag = $defender['agility'];

//then

while(($mem[curlf] > 0) && ($defender[curlf] > 0))
{
if($whosturn == "attacker")
{
 	while ($memag > 0)
 	{
	$whosturn = "defender";
	$aformula = aform($aform, $aform2);
	if($aformula <= 0)
	{
		echo "You missed $defender[username].<br>";
	}
	elseif($defender[curlf] <= 0)
	{ 
		$defender[curlf] = $defender[curlf] - $aformula;
		$winner = "attacker"; 
		echo "You did $aformula to $defender[username], 0 left.<br>";
	}  
	else
	{
		$defender[curlf] = $defender[curlf] - $aformula;
		echo "You did $aformula to $defender[username], $defender[curlf] left.<br>";		
	}
	$memag = $memag - 1;
	}
} 
else 
{
 	while ($defag > 0)
 	{
	$whosturn = "attacker";
	$dformula = dform($dform, $dform2);
	if($dformula <= 0)
	{
		echo "$defender[username] missed you.<br>";
	}
	elseif(($mem[curlf] = $mem[curlf] - $dformula) <= 0)
	{
		$winner = "defender";
		echo "$defender[username] did $dformula to you, 0 left.<br>";
	} 
	else
	{
		$mem[curlf] = $mem[curlf] - $dformula;
		echo "$defender[username] did $dformula to you, $mem[curlf] left.<br>";
	}
	$defag = $defag - 1;
	}

}
}
if($winner == "attacker")
{
echo "Victor: <a href='http://www.lordoftheabyss.com/player/view.php?id=$mem[id]'>$mem[username]</a><br>";
} 
else 
{
echo "Victor: <a href='http://www.lordoftheabyss.com/player/view.php?id=$defender[id]'>$defender[username]</a><br>";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.