Jump to content

Trying to make it say an error if using up points you don't have (or more pts)


Recommended Posts

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>

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
}

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?

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, ".")

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.