Blade Maker Posted November 20, 2009 Posted November 20, 2009 <?php session_start(); $user = $_SESSION['username']; //open my database $connect = mysql_connect('____', '_______', ________') or die('Couldn\'t connect!'); mysql_select_db('______castlewarslogin'); $query = mysql_query("SELECT * FROM `castle wars users` WHERE username = '$user'"); $username = mysql_fetch_array($query); $last = $username['lastupdate']; //timestamp from db $extraTime = time() - $last; $money = floor($extraTime / 60); if (($username['money']<500&&$username['bank']==0) || ($username['money']<600&&$username['bank']==1) || ($username['money']<700&&$username['bank']==2)) { if ($money > 0) { //lets update users table mysql_query("UPDATE `castle wars users` SET money=money+$money WHERE id= '$username[id]'") or die(mysql_error()); } echo 'user\'s money:'. ((int)$username['money']+(int)$money) .'money.'; } else echo 'user\'s money:'. ((int)$username['money']+(int)$money) .'money.'; //lets update users table mysql_query("UPDATE `castle wars users` SET lastupdate=lastupdate+$extraTime WHERE id= '$username[id]'") or die(mysql_error()); ?> Does anyone know why the number/money goes over the limit, if the bank is at 0 it is suppose to stay at 500 not over, but it goes over the limit anyone know why? Quote
snailian Posted November 20, 2009 Posted November 20, 2009 If the player has less than $500 and their bank is not upgraded, they get what they are owed. It looks like they earn a dollar a minute. You are NOT checking to see if the owed money plus the player's money exceeds the $500 limit. $money + $username['money'] Currently, anyone who has less than their bank max (500,600,700) will make ALL of the money they are owed. Give this a try: <?php session_start(); $user = $_SESSION['username']; //open my database $connect = mysql_connect('____', '_______', ________') or die('Couldn't connect!'); mysql_select_db('______castlewarslogin'); $query = mysql_query("SELECT * FROM `castle wars users` WHERE username = '$user'"); $username = mysql_fetch_array($query); $last = $username['lastupdate']; //timestamp from db $extraTime = time() - $last; $money = floor($extraTime / 60); switch ($username['bank']): // Set the max amount a player can have in their bank at one time case '0': $bankmax=500; break; case '1': $bankmax=600; break; case '2': $bankmax=700; break; default : $bankmax=500; endswitch; // Add the owed money to the players bank $newmoney = $username['money']+$money; if ($newmoney > $bankmax) { // If the owed money + current money exceeds what can be placed in bank $newmoney == $bankmax; // limit the bank to the max amount allowed. } if $newmoney != $username['money'] { // If the player`s money has changed, update the DB mysql_query("UPDATE `castle wars users` SET money=$newmoney WHERE id= '$username[id]'") or die(mysql_error()); } echo "User's money: ".$newmoney." money"; //lets update users table mysql_query("UPDATE `castle wars users` SET lastupdate=lastupdate+$extraTime WHERE id= '$username[id]'") or die(mysql_error()); ?> Quote
snailian Posted November 20, 2009 Posted November 20, 2009 Did you replace the information required for connecting to your database? Quote
Blade Maker Posted November 23, 2009 Author Posted November 23, 2009 Well the money goes over the limit and then goes back to the number. Can anyone stop this with my code? I do not want to use die, it messes a lot of things up. Quote
snailian Posted November 25, 2009 Posted November 25, 2009 Take a look at the code I posted above. Walk through it step by step and consider the logic behind it. Compare the steps in my code to the steps in your code. Consider the value of the variable which represents the player's money at each step. If you sit down and do this you should be able to figure it out. If you can't, I will explain where your error lies. Quote
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.