2013-04-06 86 views
1

我正在嘗試爲我的遊戲製作一個簡單的戰鬥腳本,但在戰鬥和所有後,我不希望玩家能夠點擊刷新並再次與怪物戰鬥..或者能夠一次又一次地提交提交以獲得獲勝的獎勵..那麼在該人擊中攻擊按鈕並且戰鬥顯示結果之後我需要這樣做,以使其不可能發生?如果我嘗試使用session_destroy()來記錄玩家並解決問題:/摧毀大多數會話

這裏是我的代碼的任何幫助嗎?

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); 

      $totalskill=$yourmonster3[att] * $row['level'] + $selmonster3[att] * 5; 
      $randomnumber=rand(1,$totalskill); 
      if($randomnumber<=$yourmonster3[att] * $row['level']) 
      { 
      echo "<center>"; 
      echo "you have won!"; 
      echo "</center>"; 

      } else { 
       echo "<center>"; 
       echo "you have lost!"; 
       echo "</center>"; 
      } 
     } 
    } 

再次更新。

$battle_id = md5(uniqid(rand(), true)); 
    echo $battle_id; 
    // $battle_id would be something like 9a8ab59df7079208843086e9b49a7862 

    // initialise the battle log 
    if(!isset($_SESSION['battle_log']) || !is_array($_SESSION['battle_log'])) 
    { 
     $_SESSION['battle_log'] = array(); 
    } 

    // Check if the battle hasn't been played 
    if(!in_array($battle_id, $_SESSION['battle_log'])) 
    { 
     // add played battle to the log 

     // ... your battle code goes here 



    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); 

      $totalskill=$yourmonster3[att] * $row['level'] + $selmonster3[att] * 5; 
      $randomnumber=rand(1,$totalskill); 
      if($randomnumber<=$yourmonster3[att] * $row['level']) 
      { 
      echo "<center>"; 
      echo "you have won!"; 
      echo "</center>"; 

      } else { 
       echo "<center>"; 
       echo "you have lost!"; 
       echo "</center>"; 
      } 
     } 
    } 
      $_SESSION['battle_log'][] = $battle_id; 
    }else { 

     echo "Don't try to cheat..."; 
    } 
+0

您可以將它們轉發到其他頁面並在其中顯示結果。除了在會話中存儲一段時間或其他東西以防止他們再次進行戰鬥直到存儲的時間。 – 2013-04-06 04:28:29

回答

0

在戰鬥開始前生成一個特殊的鍵並將其指定到一個會話中。也可以將它作爲HTML表單中的隱藏輸入推送到HTML。

當用戶提交表單時,同時檢查提交和特殊鍵。

如果它們匹配,則用戶一次玩過戰鬥。當你處理你的戰鬥腳本時,確保刪除/重新生成密鑰。

如果用戶按F5並重新發布表單,它將無法工作,因爲密鑰無效。他們需要刷新頁面以獲取新密鑰。

至於session_destroy(),你不需要這個。只需unset適當的會議,或給該會議一個新的價值。

+0

我不完全明白你在說什麼,但我會繼續前進,試一試XD你的意思是這樣嗎?我會用我認爲你在說的話來更新我的文章。 – Sakai 2013-04-06 04:37:08

+0

好吧,我明白你現在在說什麼,但我不知道該怎麼做。 – Sakai 2013-04-06 05:03:49

1

只需在會話中設置一個值,表示用戶已經播放了戰鬥。然後,您可以檢查該值,看看您的用戶是否已經參與了戰鬥。

在會話中保存的值將是唯一的,如戰鬥ID。如果這是你沒有的東西,那麼你可以通過對戰鬥中的所有獨特值進行簽名來創建獨特的戰鬥參考。像這樣: -

$battle_id = md5($player.$row['pokemon'].$monstername); 
    // $battle_id would be something like 9a8ab59df7079208843086e9b49a7862 

在腳本的開始初始化起到了日誌中的所有戰役: -

// initialise the battle log 
    if(!isset($_SESSION['battle_log']) || !is_array($_SESSION['battle_log'])) 
    { 
     $_SESSION['battle_log'] = array(); 
    } 

那麼任何戰鬥檢查開始前,如果它尚未發揮

// 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; 
    } 

所以這些方針的東西應該工作: -

// 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<=$yourmonster3[att] * $row['level']) 
     { 
      echo "<center>you have won!</center>"; 
     } else { 
      echo "<center>you have lost!</center>"; 
     } 

     // 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; 
     } 
    } 
} 

注意:請記住,會話只是暫時的,所以一旦會話被銷燬,所有已發生的戰鬥歷史都會丟失,因此請堅持數據。您可以創建一個battle表來執行此操作。

+0

我真的不知道該怎麼做:/ – Sakai 2013-04-06 04:48:22

+0

我已經用示例代碼更新了我的答案,以幫助 – 2013-04-06 05:18:22

+0

感謝您抽出時間做所有這些事情,但我仍然無法使其工作:/ – Sakai 2013-04-06 05:31:05