Acmlm's Board - I2 Archive - Programming - PHP Function Needed
User | Post |
HyperLamer
Posts: 6998/8210 |
Well when this happens I usually go through the code with a fine-tooth comb. Check for mixed up variables, and add a lot of print/echo statements in the related code to report what the variables are and what each function returns. It can be a bit of a pain to do but it should tell you what the problem is pretty quick. |
Elric
Posts: 522/687 |
Good. 'Cause it turns out, it doesn't work. After EVERY battle, it said you gained a level, and sometimes it would even boost the level by one.
Oh, and $newlvl=$user[level]+1; and not just $user[level]... |
HyperLamer
Posts: 6976/8210 |
Yeah, the old "it shouldn't work and I have no idea how it's working, but it works so I'm not gonna fuck with it" policy. I use it often.
If $newlvl is defined as $user[level], and you're working with $loguser[level], I think I can tell you what the problem is. |
Elric
Posts: 520/687 |
It's odd, becuase the MySQL query works just fine for my original leveling thinger, which works like this:
if($loguser[level] == 1 AND $newxp>999 AND $newxp<3000) { $newlvl=$loguser[level]+1; $matk="$matk You have gained a level! You are now Level $newlvl!<br>"; mysql_query("UPDATE users SET level=$newlvl WHERE id='$loguserid'") or exit(mysql_error()); }elseif($loguser[level] == 2 AND $newxp>2999 AND $newxp<6000) { $newlvl=$loguser[level]+1; $matk="$matk You have gained a level! You are now Level $newlvl!<br>"; mysql_query("UPDATE users SET level=$newlvl WHERE id='$loguserid'") or exit(mysql_error()); }elseif($loguser[level] == 3 AND $newxp>5999 AND $newxp<10000) { $newlvl=$loguser[level]+1; $matk="$matk You have gained a level! You are now Level $newlvl!<br>"; mysql_query("UPDATE users SET level=$newlvl WHERE id='$loguserid'") or exit(mysql_error()); }elseif($loguser[level] == 4 AND $newxp>9999) { $matk="You have broken the level barrier! Please stop battling, and PM Elric, so he can upgrade the Level Barrier.<br>"; } As you can see, it's the exact same query, yet it doesn't work...
Still, I'll try putting apostrophies around $newlvl first, then users, if that doesn't work...
Hmm... oddly, I got it to work like this:
if ($newexp >= xp('$loguser[level]' + 1)) { $matk="$matk You have gained a Level! You are now Level $newlvl!<br>"; mysql_query("UPDATE users SET level=$newlvl WHERE id='$loguserid'") or exit(mysql_error()); } I needed to have apostrophies around $loguser[level]. But I wonder why $mylvl wouldn't work, since it was defined as $user[level] in the array that got user stats and stuff...
Fuggit. It works, so I'm not gonna agure. Thanks for all the help, guys!
P.S.: Someone make a reply to this, just in case something goes wrong, and I need to post in here again... |
HyperLamer
Posts: 6966/8210 |
UPDATE 'users' SET level='$newlvl'
I don't have a lot of MySQL experience but that seems right. |
Elric
Posts: 519/687 |
For some reason, when I try to actually use it, it doesn't work. Initially, it would say you gained a level, but it wouldn't update the level in the users table. Then, when I changed it, not only will it not update the level, it won't even say you gained a level.
Originally, it was this:
if ($newexp >= xp($loguser[level] + 1)) { $newlvl=$loguser[level]+1; $matk="$matk You have gained a Level! You are now Level $newlvl!<br>"; mysql_query("UPDATE users SET level=$newlvl WHERE id='$loguserid'") or exit(mysql_error()); }
Then, I defined $newlvl in the $users array, which also already had $mylvl defined:
if ($newexp >= xp($mylvl + 1)) { $matk="$matk You have gained a Level! You are now Level $newlvl!<br>"; mysql_query("UPDATE users SET level=$newlvl WHERE id='$loguserid'") or exit(mysql_error()); }
No idea why it won't work, but I'm sure it's yet another stupid, obvious mistake. |
HyperLamer
Posts: 6956/8210 |
Yeah, that's the thing about PHP. Since there's no variable declaration it can't tell you when you use a variable that doesn't exist. |
Elric
Posts: 517/687 |
...
Yeah. I knew I made a stupid mistake somewhere. That would be it. Now it works. Thanks! |
HyperLamer
Posts: 6940/8210 |
Originally posted by Elric
function xp($level) { $l2=$level*$level; $l3=500*$lv2; $l4=500*$level; return ($l3 - $l4); }
What is $lv2? Should that be $l2? |
Elric
Posts: 512/687 |
OK, not surprisingly, I did something wrong...
I added this to function.php:
function xp($level) { $l2=$level*$level; $l3=500*$lv2; $l4=500*$level; return ($l3 - $l4); }
Then I added this to a page to test it:
$users=mysql_query("SELECT level,exp FROM users WHERE id=1") or exit(mysql_error()); while($user=mysql_fetch_array($users)) { $current_exp=$user[exp]; $current_level=$user[level]; } if ($current_exp >= xp($current_level + 1)) { $lup="Level: $current_level - Exp: $current_exp ~ Level up!"; }else{ $lup="Level: $current_level - Exp: $current_exp ~ No Level up."; }
The result I got was this: Level: 2 - Exp: 2911 ~ Level up!
That, of course, isn't right, since 3,000 Exp is needed to make Level 3. So, what did I do wrong?
And, BTW, my first post is edited, since, as HH pointed out, I had 1,000 Exp needed to hit Level 1, and it should have been 0... |
HyperLamer
Posts: 6920/8210 |
I wonder if you could do something crazy, like add another user statistic into the equation to vary the EXP rates a bit.
What is this Maple anyway? Some program that can make a formula out of a table of values? I'd love to have something like that... |
Elric
Posts: 494/687 |
Thanks for the help, Narf, Cellar Dweller, and sloat.
And sloat, THANK YOU! I KNEW there was a pattern there somewhere, but I just couldn't find it. In case you don't know (and not that you probably care ), the exp chart is from D&D v3.5
Once again, thanks for the help! Now to screw up things by adding this.
EDIT: Whoops! Thanks HH! I never noticed I had 1000 for Level 1. It should be 0. |
HyperLamer
Posts: 6756/8210 |
Wait... are these numbers the amount needed for the next level, or the total amount for a given level? Because levels 1 and 2 have the same value. |
sloat
Posts: 78/85 |
I plugged the level-exp values in to maple and got a little formula: exp = 500 * level^2 - 500 * level
function exp($level) { return (500*$level*$level - 500*$level); }
then you could just do:
if ($current_exp >= exp($current_level + 1)) //do whatever
this would be more practical if you wanted to keep it going past level 20. |
Cellar Dweller
Posts: 243/269 |
like this? :
/* exp2level -- Given an exp value, return the level */
function exp2level($exp) { $alevels=array(1 -> 1000, 1000, 3000, 6000, 10000, 15000, 21000, 28000, 36000, 45000, 55000, 66000, 78000, 91000, 105000, 120000, 136000, 153000, 171000, 190000); $record = 1; for($i=$record, $i<=20, $i++) { if ($exp >= $alevels[$i]) { $record=$i; } else { return $record; } } return $record; }
or like this? :
/* levelup -- Given a level value and an exp value, return true if the exp is for a higher level, else return false. */
function levelup($level, $exp) { $alevels=array(1 -> 1000, 1000, 3000, 6000, 10000, 15000, 21000, 28000, 36000, 45000, 55000, 66000, 78000, 91000, 105000, 120000, 136000, 153000, 171000, 190000); $record = $level; for($i=$record, $i<=20, $i++) { if ($exp >= $alevels[$i]) { $record=$i; } else { return ($record>$level); } } return ($record>$level); } |
Narf
Posts: 85/100 |
A simple function like this would do the trick just fine:
function calculate_level($exp){ if($exp>=0){$level=1;} if($exp>=1000){$level=2;} if($exp>=3000){$level=3;} // etcetera, etcetera... return $level; } |
Elric
Posts: 491/687 |
OK, I want a function to calculate when someone would gain a level, based on an existing level chart. I want it to look at the exp of someone, and if their exp is high enough, they gain a level.
Here's the level chart:
Level | Exp | 1 | 0 | 2 | 1,000 | 3 | 3,000 | 4 | 6,000 | 5 | 10,000 | 6 | 15,000 | 7 | 21,000 | 8 | 28,000 | 9 | 36,000 | 10 | 45,000 | 11 | 55,000 | 12 | 66,000 | 13 | 78,000 | 14 | 91,000 | 15 | 105,000 | 16 | 120,000 | 17 | 136,000 | 18 | 153,000 | 19 | 171,000 | 20 | 190,000 |
Of course, if anyone knows of a more efficent way to do this, other than a function, that's fine, too. |
|