Jump to content

[SOLVED] PHP efficiency help


ILYAS415

Recommended Posts

Okay im making a battle engine and i need help amking my script much more efficient.

At the moment i am getting maximum execution time errors quite a lot.

 

Heres the link to the malfunctioning script...

http://ragingmortals.com/engine/

 

Nothing seems to work on fight.php

fight.php - script

<? session_start(); 
include "includes/engine.php"; ?>
<b>BATTLE SYSTEM ENGINE 1.2</b><br /><br />

<?
if (isset($_SESSION['enemy']) && isset($_SESSION['stats'])){
?>

<?
//ENEMY
set_time_limit(60);
$enemy= $_SESSION['enemy'];
$enemystats= explode("[seperator]", $enemy);
$ename= $enemystats[0];
$ehealth= $enemystats[1];
$emaxhealth= $enemystats[2];
$monster= mysql_query("SELECT * FROM monsters WHERE name='$ename'");
$the= mysql_fetch_object($monster);
//YOU
$stats= $_SESSION['stats'];
$health= $_SESSION['health'];
$stat= explode("[seperator]", $stats);
$name= ucfirst($stat[0]);
$offense= $stat[1];
$defense= $stat[2];
$yourhealth= $health;

if ($ehealth <= 0 && $yourhealth > 0){ unset($_SESSION['enemy']); ?>
You Have won!<br />
<a href="index.php">Go to Index</a><br />
<? }elseif ($yourhealth <= 0 && $ehealth > 0){ unset($_SESSION['enemy']); ?>
You have been killed!<br />
<a href="index.php">Go to Index</a><br />
<? }elseif ($yourhealth <= 0 && $ehealth <= 0){ unset($_SESSION['enemy']);  ?>
Both you and <?= $ename ?> has been killed!<br />
<a href="index.php">Go to Index</a><br />
<? }else{ ?>
<?
$turns= 1;

do{
$yourattack= $offense+$defence;
$yourattack= rand($yourattack-1, $yourattack+1);
//YOURS AND ENEMIES ATTACK POWER
$eattack= $the->offense+$the->defense;
$eattack= rand($eattack-1, $eattack+1);

if ($ehealth <= "0" && $yourhealth > "0"){
$output="<font color=red>Enemy killed</font><br />";
}elseif ($yourhealth <= "0"){
$output="<fon color=red>You were killed</font><br />";
}else{
$output="";

$message1="You attacked $the->name for $yourattack damage!";
$message2="$the->name attacked you for $eattack damage!";

$ehealth= $ehealth-$yourattack;
$yourhealth= $yourhealth-$eattack;

if ($ehealth < "0"){
$message1 .= " <font color=red><b>OVERKILL</b></font>";
}
if ($yourhealth < "0"){
$message2 .= " <font color=red><b>OVERKILL</b></font>";
}

$message1 .= " <br />";
$message2 .= " <br /><br />";

echo "$message1 $message2";

--$turns;

}

}while($turns>=0);

if ($yourhealth < "0"){
$yourhealth= 0;
}
if ($ehealth < "0"){
$ehealth= 0;
}

echo "You have $yourhealth left!<br />";
echo "$the->name has $ehealth left!<br /><br />";

$_SESSION['health'] = $yourhealth;
$enemys="".$the->name."[seperator]".$ehealth."[seperator]".$emaxhealth."";
$_SESSION['enemy'] = $enemys;
?>


<div id="choices" style="border:solid 1px; padding:2; width:20%;">
<? if ($yourhealth <= "0" || $ehealth <= "0"){ ?>
<a href="?choice=confirm&userid=<?= $name ?>">Done</a><br />
<? }else{ ?>
<a href="?choice=attack&confirm=1&userid=<?= $name ?>">Attack again</a><br />
<? } ?>
</div>

<?
if ($_SESSION['debug'] == "On"){
?>
<br /><div id="debugger" style="border:solid 1px; padding:2; width:20%;">
<b>DEBUGGER</b><br />
Your name: <?= $name ?><br />
Your health: <?= $yourhealth ?><br />
Your offense: <?= $offense ?><br />
Your defense: <?= $defense ?><br />
Enemy name: <?= $the->name ?><br />
Enemy health: <?= $ehealth ?><br />
Enemy offense: <?= $the->offense ?><br />
Enemy defense: <?= $the->defense ?><br />
</div>
<? }else{ ?>
<br /><? if ($_SESSION['debug'] == "Off" || !$_SESSION['debug']){ ?>
<a href="?action=debugon">Turn Debugging On</a>
<? }elseif ($_SESSION['debug'] == "On"){ ?>
<a href="?action=debugoff">Turn Debugging Off</a>
<? } ?>
<? } //end of debugging ?>

<? } ?>

<? }else{ ?>
<script language="javascript">window.location='index.php';</script>
<? } ?>

<br /><b>Open Source and Copyright-Free. Battle System 1.2</b>

 

How can i make it more efficient so the maximum execution time error doesnt come up?

I tried setting the maximum execution time to 60 but to no effect...

 

Thanks

Link to comment
Share on other sites

Don't think it is a problem with max time. I think you have a problem with a loop somewhere running infinitely. Because if I go to the site and run though everything at the end the script sits there. Even if I close the window and come back to the main page. It is still running. This means the php executable on the server is still parsing a previous script and needs to wait for the execution time to kick in. Check your loops and post back.

 

Ray

Link to comment
Share on other sites

craygo  beat me to the post button, but yes there is a logic error in the code.

 

There is a do() loop where the "exit" condition is changed inside of a conditional statement. If the conditional statement is not true, the loop never ends. You need to correct your logic, this has nothing to do with efficiency.

 

P.S. It would help you if you formatted your code using indentation so that you can see what code is part of what block/loop... Also, don't use short open php tags <? as you will eventually end up on a server where short open tags don't work (and you won't be able to turn them on) and you will end up needing to go through all your code and change them to full tags <?php

Link to comment
Share on other sites

I guess I'll offer a little advice about do/while loops. There are very few situations where a do/while loop should be used, since the code in them is always executed at least once.

 

In your case, the conditions that would cause a "turn" to be "taken" don't exist at the start of the loop and the loop should have never been entered. Using a normal while() loop will prevent this type of problem, since the condition is tested first and the code in the loop will only be executed if the condition is true.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

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.