Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Simply set a value in the session indicating that the battle has already been played by the user. You can then check against that value to see if your user has played the battle already.</p> <p>The value you would save in the session would be unique, like the Battle ID. If this is something you don't have then you can create a unique battle reference by making a signature of all the unique values in the battle. Like so:-</p> <pre><code> $battle_id = md5($player.$row['pokemon'].$monstername); // $battle_id would be something like 9a8ab59df7079208843086e9b49a7862 </code></pre> <p>At the start of your script initialise a log of all battles played:-</p> <pre><code> // initialise the battle log if(!isset($_SESSION['battle_log']) || !is_array($_SESSION['battle_log'])) { $_SESSION['battle_log'] = array(); } </code></pre> <p>Then before the start of any battle check if it hasn't already been played</p> <pre><code> // Check if the battle hasn't been played if(!in_array($battle_id, $_SESSION['battle_log'])) { // ... your battle code goes here // add played battle to the log $_SESSION['battle_log'][] = $battle_id; } </code></pre> <p>So something along these lines should work:-</p> <pre><code>// initialise the battle log if(!isset($_SESSION['battle_log']) || !is_array($_SESSION['battle_log'])) { $_SESSION['battle_log'] = array(); } if(isset($_POST['Submit'])) { $player=$_SESSION['username']; $playerstats1="SELECT * from users where username='$player'"; $playerstats2=mysql_query($playerstats1) or die ("Could not find player"); $playerstats3=mysql_fetch_array($playerstats2); $pokemonstat1="SELECT * from user_pokemon where belongsto='$player' AND slot='1'"; $pokemonstat2=mysql_query($pokemonstat1) or die ("Could not find pokemon"); while($row = mysql_fetch_array($pokemonstat2)) { $yourmonster="SELECT * from pokemon where name='".$row['pokemon']."'"; $yourmonster2=mysql_query($yourmonster) or die ("Cannot select battle the pokemon"); $yourmonster3=mysql_fetch_array($yourmonster2); $monstername=$_SESSION['pokemon']; $monstername=strip_tags($monstername); $selmonster="SELECT * from pokemon where name='$monstername'"; $selmonster2=mysql_query($selmonster) or die ("Cannot select battle the pokemon"); $selmonster3=mysql_fetch_array($selmonster2); // generate the battle id based on the unique battle details $battle_id = md5($player.$row['pokemon'].$monstername); $totalskill=$yourmonster3[att] * $row['level'] + $selmonster3[att] * 5; $randomnumber=rand(1,$totalskill); if($randomnumber&lt;=$yourmonster3[att] * $row['level']) { echo "&lt;center&gt;you have won!&lt;/center&gt;"; } else { echo "&lt;center&gt;you have lost!&lt;/center&gt;"; } // Check if the battle hasn't been played if(!in_array($battle_id, $_SESSION['battle_log'])) { // any code below will only be run once per battle // ... // add played battle to the log $_SESSION['battle_log'][] = $battle_id; } } } </code></pre> <p><em><strong>Note:</em></strong> Bare in mind that a session is only temporary so once the session is destroyed all history of battles played is lost so persist the data. You could create a <code>battle</code> table to do this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload