Page 1 of 1

PvP Battles Only Working for 1 player[Solved]

Posted: Tue Oct 25, 2011 2:22 pm
by MikeD
So I finished up my PVP battle system last night. It works just fine when I use the character 'test'

However when I switch to any other character it won't work.

I get this error "Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\uneditedwr\battleformula.php on line 44"

Here is my code

Code: Select all

<?php
$player1=$warriorinfo3['charname'];
$player2=$_POST['fighter'];

$warrinfo="SELECT * FROM `characters` WHERE `charname`='$player2'";
$warrinfo2=mysql_query($warrinfo) or die("could not get character stats!");
$warrinfo3=mysql_fetch_array($warrinfo2);

$warriorinfo="SELECT * FROM `characters` WHERE `charname`='$player1'";
$warriorinfo2=mysql_query($warriorinfo) or die("could not get character stats!");
$warriorinfo3=mysql_fetch_array($warriorinfo2);

$player1speed=$warriorinfo3['tempspeed'];
$player1power=$warriorinfo3['temppower'];
$player1melee=$warriorinfo3['tempmelee'];
$player1magic=$warriorinfo3['tempmagic'];
$player1archery=$warriorinfo3['temparchery'];
$player1meldef=$warriorinfo3['tempmeldef'];
$player1magedef=$warriorinfo3['tempmagedef'];
$player1archdef=$warriorinfo3['temparchdef'];
$player1agility=$warriorinfo3['tempagility'];
$player1hp=$warriorinfo3['temphp'];
$player1mana=$warriorinfo3['tempmana'];
$player1attack=$warriorinfo3['attack'];

$player2speed=$warrinfo3['tempspeed'];
$player2power=$warrinfo3['temppower'];
$player2melee=$warrinfo3['tempmelee'];
$player2magic=$warrinfo3['tempmagic'];
$player2archery=$warrinfo3['temparchery'];
$player2meldef=$warrinfo3['tempmeldef'];
$player2magedef=$warrinfo3['tempmagedef'];
$player2archdef=$warrinfo3['temparchdef'];
$player2agility=$warrinfo3['tempagility'];
$player2hp=$warrinfo3['temphp'];
$player2mana=$warrinfo3['tempmana'];
$player2attack=$warriorinfo3['attack'];



while ($player1hp >=1 OR $player2hp >=1)
{
if ($player1speed > $player2speed)
{

if ($player1attack == 'melee')
{
/////////////////////////////Do Attack/Def Rolls
$halfmelee=($player1melee / 2);
$halfdef=($player2meldef / 2);
$player1meleeroll = rand($halfmelee,$player1melee);
$player2defroll = rand($halfdef,$player2meldef);


if ($player1meleeroll > $player2defroll)
{
//////////////////////////Regular Damage Roll
$damageroll=((($player1melee+$player1power)*2) + $player1meleeroll);
$defroll=((($player2meldef+$player2agility)*2) - $player2defroll);

$damage=($damageroll - $defroll);
}
elseif ($player1meleeroll < $player2defroll)
{
///////////////////////////////////////////Small Damage Roll
$damageroll=($player1melee + $player1power + $player1meleeroll);
$defroll=($player2meldef + $player2agility - $player2defroll);
$damage=($damageroll - $defroll);
}}
//if (mage)
{
//Do Attack/Def Rolls
//if ($p1attack > $p2def)
{
//damage roll
}
//elseif ($p1attack > $p2def)
{
//smaller damage roll
}}
//if (range)
{
//Do Attack/Def Rolls
//if ($p1attack > $p2def)
{
//damage roll
}
///elseif ($p1attack > $p2def)
{
///smaller damage roll
}}
////////////////////////////////////////////////Inflict Damage
echo  $player1 . " Hits " . $player2 . " for " . $damage ." Damage<br>";

///////////////////////////////////////////////Update HP
$player2hp = ($player2hp - $damage);
///////////////////////////////////////////////Check to see if P2 is dead
if ($player2hp <= 0)
{
echo $player1 . " Has Killed " . $player2 . "<br>";
/////////////////////////////////////Check Levels
if ($warriorinfo3['level'] <= $warrinfo3['level'])
{
//////////////////////////////////////Update Players
$newxp = ($warrinfo3['level'] / 10);
$newxp = (int)$newxp;

echo "<b>You gain " . $newxp . " experience.</b><br>";

$updateplayer1="UPDATE `characters` SET `battles`=`battles`-1, `exper`=`exper`+'$newxp', `bwins`=`bwins`+1 WHERE `charname`='$player1'";
mysql_query($updateplayer1) or die(mysql_error());

$updateplayer2="UPDATE `characters` SET `bloss`=`bloss`+1 WHERE `charname`='$player2'";
mysql_query($updateplayer2) or die("Could not update player2");
exit;

}
elseif ($warriorinfo3['level'] >= $warrinfo3['level'])
{
////////////////Update Players
echo "<b>You didn't gain any experience because your opponent was a lower level.</b><br>";

$updateplayer1="UPDATE `characters` SET `battles`=`battles`-1, `bwins`=`bwins`+1 WHERE `charname`='$player1'";
mysql_query($updateplayer1) or die(mysql_error());

$updateplayer2="UPDATE `characters` SET `bloss`=`bloss`+1 WHERE `charname`='$player2'";
mysql_query($updateplayer2) or die("Could not update player2");
exit;
}} 
//////////////////////////////////////////////p2 turn
elseif ($player2hp > 0)
{
if ($player1attack == 'melee')
{
///////////////////////////////////////////////Do Attack/Def Rolls
$halfmelee=($player2melee / 2);
$halfdef=($player1meldef / 2);
$player2meleeroll = rand($halfmelee,$player2melee);
$player1defroll = rand($halfdef,$player1meldef);


if ($player2meleeroll > $player2defroll)
{
//////////////////////////////////Regular damage roll
$damageroll=(($player2melee + $player2power) * 2 + $player2meleeroll);
$defroll=(($player1meldef + $player1agility) * 2 - $player1defroll);
$damage=($damageroll - $defroll);
}
elseif ($player2meleeroll > $player2defroll)
{
////////////////////////////////////////smaller damage roll
$damageroll=($player2melee + $player2power + $player2meleeroll);
$defroll=($player1meldef + $player1agility  - $player1defroll);
$damage=($damageroll - $defroll);
}}
//elseif (mage)
{
//Do Attack/Def Rolls
//if ($p1attack > $p2def)
{
//damage roll
}
//elseif ($p1attack > $p2def)
{
//smaller damage roll
}}
//if (range)
{
//Do Attack/Def Rolls
//if ($p1attack > $p2def)
{
//damage roll
}
//elseif ($p1attack > $p2def)
{
//smaller damage roll
}}
///////////////////////////////////////////////do damage
 
echo  $player2 . " Hits " . $player1 . " for " . $damage ."<br>";

///////////////////////////////////////////////update hp
$player1hp = ($player1hp - $damage);

if ($player1hp <= 0)
{
/////////////////////////////exp
echo $player2 . " Has Killed " . $player1 . "<br>";
$newxp = ($warriorinfo3['level'] / 10);
$newxp = (int)$newxp;
/////////////////////////////update wins/losses
$updateplayer2="UPDATE `characters` SET `exper`=`exper`+'$newxp', `bwins`=`bwins`+1 WHERE `charname`='$player2'";
mysql_query($updateplayer2) or die(mysql_error());

$updateplayer1="UPDATE `characters` SET `bloss`=`bloss`+1 WHERE `charname`='$player1'";
mysql_query($updateplayer1) or die("Could not update player2");
} }}
}
?>

Re: PvP Battles Only Working for 1 player.

Posted: Tue Oct 25, 2011 3:02 pm
by Chris
I think you've got an infinite loop there on line 41: replace the OR with &&

Code: Select all

while ($player1hp >=1 && $player2hp >=1)
 

Re: PvP Battles Only Working for 1 player.

Posted: Tue Oct 25, 2011 3:19 pm
by MikeD
Nope, doesn't work properly with the && in there.

Re: PvP Battles Only Working for 1 player.

Posted: Tue Oct 25, 2011 4:13 pm
by Ark
Ya definitely an infinite loop. I'm not sure what you're trying to do, but why do you need the script to run more than once?!

if you don't need to, then change the 'while' to 'if'

Code: Select all

if($player1hp >=1 || $player2hp >=1) 

Re: PvP Battles Only Working for 1 player.

Posted: Tue Oct 25, 2011 4:41 pm
by MikeD
I need the script to run until one of the fighters is dead.

Re: PvP Battles Only Working for 1 player.

Posted: Tue Oct 25, 2011 9:54 pm
by Chris
Have you tried? I'm pretty sure it's an and, not an or you want.

By using an and the script will keep looping as long as $player1hp is greater or equal to 1 and $player2hp is greater or equal to 1. This means once on of them reaches below or equal to 1 the loop will stop. Because both are no longer grater or equal to 1.

If you use or it's going to loop forever until both are dead because one of them is always going to have greater than 1 hp.

Re: PvP Battles Only Working for 1 player.

Posted: Tue Oct 25, 2011 11:08 pm
by MikeD
Yeah I tried with the && earlier and it wasn't working. Just tried again and it is :?

However it's still not working for any other player. This is what happens.

http://gyazo.com/353393ceff90310b5355fd1224f087be

Re: PvP Battles Only Working for 1 player.

Posted: Thu Oct 27, 2011 1:14 pm
by MikeD
Bump, still haven't figured this out :(

Re: PvP Battles Only Working for 1 player.

Posted: Thu Oct 27, 2011 6:55 pm
by Chris
It has to do with your while loop. The loop is taking longer to process than the PHP execution time. Have a good look over the lot again and make sure all your variables being calculated properly.

Re: PvP Battles Only Working for 1 player.

Posted: Thu Oct 27, 2011 7:26 pm
by MikeD
If it works with one player, why wouldn't it work with others?