Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
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
Posted on 08-31-05 01:58 PM Link | Quote
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:
LevelExp
10
21,000
33,000
46,000
510,000
615,000
721,000
828,000
936,000
1045,000
1155,000
1266,000
1378,000
1491,000
15105,000
16120,000
17136,000
18153,000
19171,000
20190,000


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
Posted on 08-31-05 03:10 PM Link | Quote
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.
Posted on 08-31-05 03:43 PM Link | Quote
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);
}
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
Posted on 09-02-05 01:03 AM Link | Quote
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.
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
Posted on 09-02-05 01:31 AM Link | Quote
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
Posted on 09-02-05 01:34 AM Link | Quote
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
Posted on 09-08-05 10:14 AM Link | Quote
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
Posted on 09-08-05 01:07 PM Link | Quote
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
<||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
Posted on 09-09-05 05:29 AM Link | Quote
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

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
Posted on 09-09-05 08:37 AM Link | Quote
...

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
Posted on 09-10-05 01:58 AM Link | Quote
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
Posted on 09-10-05 02:28 AM Link | Quote
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
<||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
Posted on 09-10-05 10:32 AM Link | Quote
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
Posted on 09-10-05 11:33 AM Link | Quote
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
<||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
Posted on 09-10-05 03:37 PM Link | Quote
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
Posted on 09-11-05 09:01 AM Link | Quote
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
Posted on 09-11-05 09:48 PM Link | Quote
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 | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.016 seconds.