zelig Posted February 23, 2012 Share Posted February 23, 2012 Okay, I'm trying to make it so that when someone levels up in my game, they can use stat bonus points to increase their abilities. However, there doesn't seem to be a break in there to ensure that someone can't spend points that they don't have. For instance, they get 3 per level, but if they spend 6, the system lets them and shows a -3 in their stat_bonus field. Any help would be appreciated! Thanks in advance!! <?php include("fightfunc.php"); include("lib.php"); //include('cookies.php'); //$link = opendb(); define("PAGENAME", "Stat Bonus"); $player = check_user($secret_key, $db); $x = $player->username; if($_SESSION['234asdfas']){$y = $_SESSION['234asdfas'];}else{echo 'Error: You do not have a session variable set'; exit;} $command = 1; $choice = $_POST['choice']; $amount = $_POST['amount']; if (isset($_POST["statbonus"])) { include("tmpconfig.php"); $checkquery = $db->execute("SELECT * FROM `users` WHERE `id`=?", $player->id); //$rec = mysql_fetch_array($checkquery); extract($_POST); $errors = 0; $errorlist = ""; if ($errors == 0) { $query = $db->execute("UPDATE `users` SET `stat_bonus`=? WHERE `id`=?", array($player->stat_bonus - $amount, $player->id)) or die("querya failed: ". mysql_error()); if($query) $qry = $db->execute("select * from `users` where `id`=?", array($player->id)) or die("qry failed: ".mysql_error()); $rw = $qry->fetchrow(); if($qry){ $query2 = $db->execute("UPDATE `users` SET `stat_bonus`=?, $choice=$choice+$amount WHERE `id`=?", array($player->stat_bonus - $amount, $player->id)) or die("query2 failed: ". mysql_error()); if($query2){ echo("<br><br>You have successfully used your stat bonus points.<br>"); } elseif ($amount > $player["stat_bonus"]) { echo "<font color='red'>Enter an amount higher than 0!</font>"; } elseif ($amount == 0) { echo "<font color='red'>Enter an amount higher than 0!</font>"; } elseif (!is_numeric($amount)) { echo "<font color='red'>Enter an amount!</font."; } else { echo "<font color=green><b>You have succesfully spend skill points!</b></font><br /><br />"; }}}} $player = check_user($secret_key, $db); ?> <table width="100%"> <tr> <td colspan="2"><fieldset> <form action="" method="post"> <font size='4'><b>Skill points</b></font><br /> Every time you raise a level, you get 3 skillpoints. You can spend them here. <br /> You currently have <font color=green><b><?=$player->stat_bonus?> Skill Points.</b></font>. Please choose an attribute:<br /> <br /> <INPUT TYPE="radio" NAME="choice" VALUE="strength" CHECKED>Strength<br /> <INPUT TYPE="radio" NAME="choice" VALUE="vitality" >Vitality<br /> <INPUT TYPE="radio" NAME="choice" VALUE="agility">Agility<br /> <INPUT TYPE="radio" NAME="choice" VALUE="wisdom">Wisdom<br /> <br /> How many skill points do you want to spend? (+1 for every skill point): <br /><br /> Amount: <INPUT TYPE="text" NAME="amount" SIZE="8" MAXLENGTH="8"> <INPUT TYPE="submit" NAME="statbonus" VALUE="Submit"> </form></fieldset></td></tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/257641-trying-to-make-it-say-an-error-if-using-up-points-you-dont-have-or-more-pts/ Share on other sites More sharing options...
digibucc Posted February 23, 2012 Share Posted February 23, 2012 wrap the whole update part in a while statement that runs as long as they have points Quote Link to comment https://forums.phpfreaks.com/topic/257641-trying-to-make-it-say-an-error-if-using-up-points-you-dont-have-or-more-pts/#findComment-1320507 Share on other sites More sharing options...
Psycho Posted February 23, 2012 Share Posted February 23, 2012 I see two places where you are updating the value of 'stat_bonus' and in both the value is set as $player->stat_bonus - $amount I assume $player->stat_bonus is the users current value. So, just implement a check as follows before running those queries if($player->stat_bonus - $amount < 0) { //The amount would create a negative balance - implement error condition } else { //Allow the update } Quote Link to comment https://forums.phpfreaks.com/topic/257641-trying-to-make-it-say-an-error-if-using-up-points-you-dont-have-or-more-pts/#findComment-1320510 Share on other sites More sharing options...
zelig Posted February 23, 2012 Author Share Posted February 23, 2012 Fantastic! That worked like a charm! One quick question. How do I get it so that they can't put in decimal points? Quote Link to comment https://forums.phpfreaks.com/topic/257641-trying-to-make-it-say-an-error-if-using-up-points-you-dont-have-or-more-pts/#findComment-1320535 Share on other sites More sharing options...
Zane Posted February 23, 2012 Share Posted February 23, 2012 floor, round, or ceil whatever they put in. Otherwise, you'd have to use a Javascript alternative, which is never a reliable way of error checking. Quote Link to comment https://forums.phpfreaks.com/topic/257641-trying-to-make-it-say-an-error-if-using-up-points-you-dont-have-or-more-pts/#findComment-1320537 Share on other sites More sharing options...
zelig Posted February 23, 2012 Author Share Posted February 23, 2012 True. I was wanting it to create some sort of error if they did try to put in a decimal point though. Something like this: elseif (!is_numeric($amount)) { echo "Enter a numerical amount!"; } Is there an !is_X statement that will check for that? Quote Link to comment https://forums.phpfreaks.com/topic/257641-trying-to-make-it-say-an-error-if-using-up-points-you-dont-have-or-more-pts/#findComment-1320539 Share on other sites More sharing options...
Zane Posted February 23, 2012 Share Posted February 23, 2012 You could simply look for a decimal point, but IMO, it would be best to just floor whatever they put in to avoid user aggravation... Not only that, it'll save you from using another if statement. if(strpos($amount, ".") Quote Link to comment https://forums.phpfreaks.com/topic/257641-trying-to-make-it-say-an-error-if-using-up-points-you-dont-have-or-more-pts/#findComment-1320540 Share on other sites More sharing options...
zelig Posted February 23, 2012 Author Share Posted February 23, 2012 Okay, so I would write it like this, correct? $amount = floor($_POST['amount']); Obviously it's getting $amount from whatever they enter in the form. Quote Link to comment https://forums.phpfreaks.com/topic/257641-trying-to-make-it-say-an-error-if-using-up-points-you-dont-have-or-more-pts/#findComment-1320544 Share on other sites More sharing options...
Zane Posted February 23, 2012 Share Posted February 23, 2012 yup Quote Link to comment https://forums.phpfreaks.com/topic/257641-trying-to-make-it-say-an-error-if-using-up-points-you-dont-have-or-more-pts/#findComment-1320546 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.