Register | Login | |||||
Main
| Memberlist
| Active users
| ACS
| Commons
| Calendar
| Online users Ranks | FAQ | Color Chart | Photo album | IRC Chat |
| |
0 user currently in Programming. | 3 guests |
Acmlm's Board - I2 Archive - Programming - PHP Function Needed | | | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread |
User | Post | |||||||||||||||||||||||||||||||||||||||||
Elric Chasupa Currently Playing: You Like A Lute. Level: 40 Posts: 491/687 EXP: 440016 For next: 1293 Since: 03-15-04 From: Melniboné Since last post: 6 hours Last activity: 6 hours |
| |||||||||||||||||||||||||||||||||||||||||
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:
Of course, if anyone knows of a more efficent way to do this, other than a function, that's fine, too. (edited by Elric on 09-01-05 04:35 PM) |
||||||||||||||||||||||||||||||||||||||||||
Narf Hi Tuvai! (reregistering while banned) Level: 16 Posts: 85/100 EXP: 17634 For next: 2622 Since: 12-26-04 Since last post: 22 hours Last activity: 14 hours |
| |||||||||||||||||||||||||||||||||||||||||
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; } |
||||||||||||||||||||||||||||||||||||||||||
Cellar Dweller Flurry !!! Level: 27 Posts: 243/269 EXP: 107817 For next: 8342 Since: 03-15-04 From: Arkansas Since last post: 16 days Last activity: 34 min. |
| |||||||||||||||||||||||||||||||||||||||||
like this? :/* exp2level -- Given an exp value, return the level */ 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. */ |
||||||||||||||||||||||||||||||||||||||||||
sloat Level: 16 Posts: 78/85 EXP: 18044 For next: 2212 Since: 05-21-04 From: South Central Delaware Since last post: 19 days Last activity: 5 hours |
| |||||||||||||||||||||||||||||||||||||||||
I plugged the level-exp values in to maple and got a little formula: exp = 500 * level^2 - 500 * level function exp($level) this would be more practical if you wanted to keep it going past level 20. |
||||||||||||||||||||||||||||||||||||||||||
HyperLamer <||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people Sesshomaru Tamaranian Level: 118 Posts: 6756/8210 EXP: 18171887 For next: 211027 Since: 03-15-04 From: Canada, w00t! LOL FAD Since last post: 2 hours Last activity: 2 hours |
| |||||||||||||||||||||||||||||||||||||||||
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. | ||||||||||||||||||||||||||||||||||||||||||
Elric Chasupa Currently Playing: You Like A Lute. Level: 40 Posts: 494/687 EXP: 440016 For next: 1293 Since: 03-15-04 From: Melniboné Since last post: 6 hours Last activity: 6 hours |
| |||||||||||||||||||||||||||||||||||||||||
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. (edited by Elric on 09-01-05 04:35 PM) |
||||||||||||||||||||||||||||||||||||||||||
HyperLamer <||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people Sesshomaru Tamaranian Level: 118 Posts: 6920/8210 EXP: 18171887 For next: 211027 Since: 03-15-04 From: Canada, w00t! LOL FAD Since last post: 2 hours Last activity: 2 hours |
| |||||||||||||||||||||||||||||||||||||||||
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... (edited by HyperHacker on 09-08-05 01:23 AM) |
||||||||||||||||||||||||||||||||||||||||||
Elric Chasupa Currently Playing: You Like A Lute. Level: 40 Posts: 512/687 EXP: 440016 For next: 1293 Since: 03-15-04 From: Melniboné Since last post: 6 hours Last activity: 6 hours |
| |||||||||||||||||||||||||||||||||||||||||
OK, not surprisingly, I did something wrong... I added this to function.php: function xp($level) { 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()); 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 <||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people Sesshomaru Tamaranian Level: 118 Posts: 6940/8210 EXP: 18171887 For next: 211027 Since: 03-15-04 From: Canada, w00t! LOL FAD Since last post: 2 hours Last activity: 2 hours |
| |||||||||||||||||||||||||||||||||||||||||
Originally posted by Elric What is $lv2? Should that be $l2? |
||||||||||||||||||||||||||||||||||||||||||
Elric Chasupa Currently Playing: You Like A Lute. Level: 40 Posts: 517/687 EXP: 440016 For next: 1293 Since: 03-15-04 From: Melniboné Since last post: 6 hours Last activity: 6 hours |
| |||||||||||||||||||||||||||||||||||||||||
... Yeah. I knew I made a stupid mistake somewhere. That would be it. Now it works. Thanks! |
||||||||||||||||||||||||||||||||||||||||||
HyperLamer <||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people Sesshomaru Tamaranian Level: 118 Posts: 6956/8210 EXP: 18171887 For next: 211027 Since: 03-15-04 From: Canada, w00t! LOL FAD Since last post: 2 hours Last activity: 2 hours |
| |||||||||||||||||||||||||||||||||||||||||
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 Chasupa Currently Playing: You Like A Lute. Level: 40 Posts: 519/687 EXP: 440016 For next: 1293 Since: 03-15-04 From: Melniboné Since last post: 6 hours Last activity: 6 hours |
| |||||||||||||||||||||||||||||||||||||||||
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)) { Then, I defined $newlvl in the $users array, which also already had $mylvl defined: if ($newexp >= xp($mylvl + 1)) { No idea why it won't work, but I'm sure it's yet another stupid, obvious mistake. |
||||||||||||||||||||||||||||||||||||||||||
HyperLamer <||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people Sesshomaru Tamaranian Level: 118 Posts: 6966/8210 EXP: 18171887 For next: 211027 Since: 03-15-04 From: Canada, w00t! LOL FAD Since last post: 2 hours Last activity: 2 hours |
| |||||||||||||||||||||||||||||||||||||||||
UPDATE 'users' SET level='$newlvl' I don't have a lot of MySQL experience but that seems right. |
||||||||||||||||||||||||||||||||||||||||||
Elric Chasupa Currently Playing: You Like A Lute. Level: 40 Posts: 520/687 EXP: 440016 For next: 1293 Since: 03-15-04 From: Melniboné Since last post: 6 hours Last activity: 6 hours |
| |||||||||||||||||||||||||||||||||||||||||
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) {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)) {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 <||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people Sesshomaru Tamaranian Level: 118 Posts: 6976/8210 EXP: 18171887 For next: 211027 Since: 03-15-04 From: Canada, w00t! LOL FAD Since last post: 2 hours Last activity: 2 hours |
| |||||||||||||||||||||||||||||||||||||||||
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 Chasupa Currently Playing: You Like A Lute. Level: 40 Posts: 522/687 EXP: 440016 For next: 1293 Since: 03-15-04 From: Melniboné Since last post: 6 hours Last activity: 6 hours |
| |||||||||||||||||||||||||||||||||||||||||
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 <||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people Sesshomaru Tamaranian Level: 118 Posts: 6998/8210 EXP: 18171887 For next: 211027 Since: 03-15-04 From: Canada, w00t! LOL FAD Since last post: 2 hours Last activity: 2 hours |
| |||||||||||||||||||||||||||||||||||||||||
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. |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread |
Acmlm's Board - I2 Archive - Programming - PHP Function Needed | | | |