uwannadonkey Posted August 13, 2007 Share Posted August 13, 2007 $expfornextlevel == 500*$user->level; mysql_query("UPDATE `users` SET exp_required = ($expfornextlevel) WHERE ID = $user->ID"); if ( $user->exp = $expfornextlevel;) { mysql_query("UPDATE `users` SET level = level+(1) WHERE ID = $user->ID"); mysql_query("UPDATE `users` SET exp = 0 WHERE ID = $user->ID"); } if ($user->exp > $expfornextlevel); { $difference = ($user->exp - $expfornextlevel); mysql_query("UPDATE `users` SET level = level+(1) WHERE ID = $user->ID"); mysql_query("UPDATE `users` SET exp = $difference WHERE ID = $user->ID"); } mysql_query('INSERT INTO battles(Loser, Winner) VALUES("' . $enemy->ID. '", "' . $user->ID. '")'); mysql_query("UPDATE `users` SET hp = 0 WHERE ID = '$enemy->ID'"); } thats the part of the code that pertains to my question the thing is, when the battle has occured, i need to check if the user has enough exp to level, and to make sure his exp_required(the max HP) really is correct. For instance, lets say i want the Exp required to be 500 * $user->level, and to check for exp NOW, after every battle, i end up having 0 exp, and gain 2 levels, which makes no sense! Quote Link to comment https://forums.phpfreaks.com/topic/64736-solved-leveling-problem-help-if-you-can/ Share on other sites More sharing options...
Orio Posted August 13, 2007 Share Posted August 13, 2007 if ( $user->exp = $expfornextlevel;) Should be: if ($user->exp == $expfornextlevel) Orio. Quote Link to comment https://forums.phpfreaks.com/topic/64736-solved-leveling-problem-help-if-you-can/#findComment-322838 Share on other sites More sharing options...
roopurt18 Posted August 13, 2007 Share Posted August 13, 2007 Your very first line of code is wrong as well: $expfornextlevel == 500*$user->level; Should be $expfornextlevel = 500*$user->level; Quote Link to comment https://forums.phpfreaks.com/topic/64736-solved-leveling-problem-help-if-you-can/#findComment-322840 Share on other sites More sharing options...
uwannadonkey Posted August 13, 2007 Author Share Posted August 13, 2007 i did as you said, and as a result: i gained 2 levels, my exp is now 0, and my EXP_required(max) is now 0 also sigh Quote Link to comment https://forums.phpfreaks.com/topic/64736-solved-leveling-problem-help-if-you-can/#findComment-322844 Share on other sites More sharing options...
roopurt18 Posted August 13, 2007 Share Posted August 13, 2007 I have to wonder why you included that first if test: if ( $user->exp = $expfornextlevel;) { mysql_query("UPDATE `users` SET level = level+(1) WHERE ID = $user->ID"); mysql_query("UPDATE `users` SET exp = 0 WHERE ID = $user->ID"); } if ($user->exp > $expfornextlevel); { $difference = ($user->exp - $expfornextlevel); // Why do you have parens here? mysql_query("UPDATE `users` SET level = level+(1) WHERE ID = $user->ID"); mysql_query("UPDATE `users` SET exp = $difference WHERE ID = $user->ID"); } Why not: if ($user->exp >= $expfornextlevel); // NOTICE DIFFERENCE IN COMPARISON { $difference = $user->exp - $expfornextlevel; mysql_query("UPDATE `users` SET level = level+(1) WHERE ID = $user->ID"); mysql_query("UPDATE `users` SET exp = $difference WHERE ID = $user->ID"); } i gained 2 energy, my exp is now 0, and my EXP_required(max) is now 0 also sigh None of what you originally pasted deals with any of this; you probably have an error elsewhere in your program. Quote Link to comment https://forums.phpfreaks.com/topic/64736-solved-leveling-problem-help-if-you-can/#findComment-322851 Share on other sites More sharing options...
uwannadonkey Posted August 13, 2007 Author Share Posted August 13, 2007 i meant i gained 2 levels, 0 exp, and my exp_required is now 0 there is no error in my program besides in this code, i KNOW its here, but thanks for trying i copied your code, and instead of gaining 2 levels, i gained 1, my exp = 0 and my exp_required, set by $expfornextlevel , is 0 Quote Link to comment https://forums.phpfreaks.com/topic/64736-solved-leveling-problem-help-if-you-can/#findComment-322852 Share on other sites More sharing options...
roopurt18 Posted August 13, 2007 Share Posted August 13, 2007 Post the whole file if you can. You have an error somewhere and I'm guessing its bad logic / syntax. Quote Link to comment https://forums.phpfreaks.com/topic/64736-solved-leveling-problem-help-if-you-can/#findComment-322862 Share on other sites More sharing options...
uwannadonkey Posted August 13, 2007 Author Share Posted August 13, 2007 thx for giving this a shot i guess! <?php include('inc/header.php'); $uID = $user->ID; $eID = (int)$_GET['ID']; if ($uID > 0) { #if called from header! $enemysql = @mysql_query("SELECT * FROM users WHERE ID = '$eID'"); $enemy = @mysql_fetch_object($enemysql); $usersql = @mysql_query("SELECT * FROM users WHERE ID = '$uID'"); $user = @mysql_fetch_object($usersql); echo "$user->display_name"; echo ' VS. ';echo "$enemy->display_name<br>"; }else{ die("error"); } if ($enemy->ID == $user->ID) { echo' You cant attack yourself!'; } elseif ($user->hp <= 0) { echo ' You are in NO condition to do battle<br><br>'; } elseif ($enemy->hp <= 0) { echo ' Your Enemy Is Already Dead!<br><br>'; } elseif ($user->energy <= 4) { echo ' Not enough energy!'; } elseif ($user->attack < $enemy->defense) { echo ' You look at your opponent, and wet your pants at his/her strength. You lose the battle.<br><br><br><br><br><br><br><br><br>'; mysql_query("UPDATE `users` SET hp = 0 WHERE ID = '$user->ID'"); } elseif ($user->defense > $enemy->attack) { echo' Your opponent takes one look at you, and dies in fear<br><br>'; } else { echo' Welcome to the Battle Arena .'; echo "User Start HP:".$user->hp; echo "<br>Enemy Start HP:".$enemy->hp;echo ' <br><br> <center> ------Battle Begins------'; $attacker = ($user->agility > $enemy->agility); $uHP = $user->hp; $eHP = $enemy->hp; //temp values $edmg = 1; $udmg = 1; while ( $uHP > 0 && $eHP > 0 && ($edmg > 0 || $udmg > 0) ) { echo '<center>'; echo "User HP:".$uHP;echo '<br></center'; echo "<br>Enemy HP: ".$eHP; echo ' <br>'; $edmg = 0; $udmg = 0; if($attacker) { $edmg = (int)($user->attack - $enemy->defense); $eHP = (int)($eHP - $edmg); echo $user->display_name." has done $edmg damage against ".$enemy->display_name."<br>"; }else{ $udmg = (int)($enemy->attack - $user->defense); $uHP = (int)($uHP - $udmg); echo $enemy->display_name." has done $udmg damage against ".$user->display_name."<br>"; } $attacker = !$attacker; flush(); //add } if( $uHP <= 0) { echo $enemy->display_name." Wins<br>"; }elseif( $eHP <= 0){ $tUnixTime = time(); echo '<br>';echo $user->display_name." Wins<br>"; mysql_query("UPDATE `users` SET energy= energy-(5) WHERE ID = $user->ID"); mysql_query("UPDATE `users` SET battle_won= battle_won + (1) WHERE ID = $user->ID"); mysql_query("UPDATE `users` SET total_battles= total_battles+(1) WHERE ID = $user->ID"); mysql_query("UPDATE `users` SET exp = exp + ($enemy->level*4) WHERE ID = $user->ID"); mysql_query("UPDATE `users` SET hp = max_hp WHERE ID = $user->ID"); mysql_query("UPDATE `users` SET gold = gold + (($enemy->gold/2)*2) WHERE ID = $user->id"); Echo ' <center>You Gain '; echo "$enemy->gold"; echo ' Gold Pieces From The Battle.</center><br><br>'; mysql_query("UPDATE `users` SET gold = gold - (gold/2) WHERE ID = $enemy->ID"); mysql_query("UPDATE `users` SET hp = 0 WHERE ID = $enemy->ID"); $expfornextlevel = 500*$user->level; mysql_query("UPDATE `users` SET exp_required = $expfornextlevel WHERE ID = $user->ID"); if ($user->exp >= $expfornextlevel); // NOTICE DIFFERENCE IN COMPARISON { $difference = $user->exp - $expfornextlevel; mysql_query("UPDATE `users` SET level = level+(1) WHERE ID = $user->ID"); mysql_query("UPDATE `users` SET exp = $difference WHERE ID = $user->ID"); } mysql_query('INSERT INTO battles(Loser, Winner) VALUES("' . $enemy->ID. '", "' . $user->ID. '")'); } } include('inc/footer.php'); ?> heres the full code Quote Link to comment https://forums.phpfreaks.com/topic/64736-solved-leveling-problem-help-if-you-can/#findComment-322872 Share on other sites More sharing options...
uwannadonkey Posted August 13, 2007 Author Share Posted August 13, 2007 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/64736-solved-leveling-problem-help-if-you-can/#findComment-322897 Share on other sites More sharing options...
roopurt18 Posted August 13, 2007 Share Posted August 13, 2007 I'm looking at your script, but here's a quick note: <?php echo "$user->display_name"; echo ' VS. ';echo "$enemy->display_name<br>"; // Can be written as (slightly more condense than extra echo statements): echo "{$user->display_name} VS. {$enemy->display_name}<br>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/64736-solved-leveling-problem-help-if-you-can/#findComment-322927 Share on other sites More sharing options...
roopurt18 Posted August 13, 2007 Share Posted August 13, 2007 These three lines look like a logic error: mysql_query("UPDATE `users` SET gold = gold + (($enemy->gold/2)*2) WHERE ID = $user->id"); Echo ' <center>You Gain '; echo "$enemy->gold"; echo ' Gold Pieces From The Battle.</center><br><br>'; mysql_query("UPDATE `users` SET gold = gold - (gold/2) WHERE ID = $enemy->ID"); You are setting the winner's gold equal to the enemy's gold and then echo'ing that the winner is receiving gold equal to the amount held by the enemy, which is fine. But then you are decreasing the enemy's gold by half instead of the amount received by the victor; I'm not sure if that's what you intended. Also, in the first line that I pasted you have $user->id while every where else in your code it's $user->ID Quote Link to comment https://forums.phpfreaks.com/topic/64736-solved-leveling-problem-help-if-you-can/#findComment-322932 Share on other sites More sharing options...
roopurt18 Posted August 13, 2007 Share Posted August 13, 2007 <?php mysql_query("UPDATE `users` SET exp = exp + ($enemy->level*4) WHERE ID = $user->ID"); // ... if ($user->exp >= $expfornextlevel); // NOTICE DIFFERENCE IN COMPARISON { // ... } ?> You are updating the user's exp in the database and then comparing the user's exp in the object with what's required to level without updating the exp in the object that the user just won. You probably need to add this between the mysql query that updates the DB and the if test: $user->exp += $enemy->level * 4; Also, I just noticed that you have a semi-colon after the closing paren of the if conditional. You copied that from my post, but I copied it from your original (and then just missed the mistake when I made my original reply). That semi-colon is effectively acting as an "empty" if body. The section of code you intended to run as the if body is therefore executing every time, regardless of the if test. Quote Link to comment https://forums.phpfreaks.com/topic/64736-solved-leveling-problem-help-if-you-can/#findComment-322941 Share on other sites More sharing options...
uwannadonkey Posted August 13, 2007 Author Share Posted August 13, 2007 thank you for helping, you are a kind person have a nice day! Quote Link to comment https://forums.phpfreaks.com/topic/64736-solved-leveling-problem-help-if-you-can/#findComment-322982 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.