acidpunk Posted July 20, 2014 Share Posted July 20, 2014 (edited) So i'm writing a script for a game online, basically it's a clan battle. What happens is, users within the clan attack (each seperatly) at the end of the round, all the users attacks get added up, then subtracts from the boss' health. if the boss isn't dead, the boss attacks. now this is the part that isn't working properly whats supposed to be happening is: after the boss attacks, if any of the users health within the clan attack falls to 0 or below, they get dropped from the array, now this works, but only during the 1st round. foreach ($attackers as $key => $value) { $value['hp'] -= $boss_atk; if ($value['hp'] < 1) { unset($attackers[$key]); } } After it checks if anyones health falls below 0, it than checks if anyones left in the array. If someone is left, then it sets back to the users attack, and redoes the loop. I'm hoping fresh eyes could lend me a hand! Heres the whole script: <?php require 'db.connection.php'; $raidid = 1; $raidover = 0; $attackers = array(); $user_query = $dbh->prepare('SELECT `hp`,`attack`,`criticalhit`,`block`,`username` FROM `stats` WHERE `raidid` = :raidid'); $user_query->execute(array(':raidid'=>$raidid)); $boss_hp = 5000; //testing purposes. $boss_atk = 100; //testing purposes. foreach ($user_query as $row) { $attackers[] = $row; //sets the array for users in attack. } while ($raidover == 0) { /* * clan's turn to attack. * each user within the clan attacks seperatly * they will continue to attack as long as their HP is over 0. */ static $attackTurn = "p"; if($attackTurn === "p") { $totalAttack = 0; foreach ($attackers as $key => $value) { echo $value['username'].' attacks for '.$value['attack'].'<br />'; $totalAttack += $value["attack"]; } } $boss_hp -= $totalAttack; if ($boss_hp < 1) { echo 'Crew has won!'; $raidover = 1; break; } /* * boss' turn to attack. */ $attackTurn = "b"; if($attackTurn === "b") { echo 'Boss attacks for '.$boss_atk.'<br />'; } /* * if any of the users have less than 0 health points * delete them from the loop. */ foreach ($attackers as $key => $value) { $value['hp'] -= $boss_atk; if ($value['hp'] < 1) { unset($attackers[$key]); } } /* * if there arn't any users left in the array * the raid is over, the boss wins. */ if (empty($attackers)) { echo 'Boss has won!'; $raidover = 1; break; } $attackTurn = "p"; //back to the top of the loop. } ?> Edited July 20, 2014 by acidpunk Quote Link to comment Share on other sites More sharing options...
acidpunk Posted July 20, 2014 Author Share Posted July 20, 2014 (edited) I thought i needed the & operator before $value, but nope. Edited July 20, 2014 by acidpunk Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.