Jump to content

[SOLVED] leveling problem, help if you can!


uwannadonkey

Recommended Posts

$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!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>";
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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.