Reputation: 216
So I have a members website written in PHP and MYSQL.
Say for a second I want to let my users level up as a reward system and they get XP from doing certain things on the site like posting and what not. Giving the XP is the easy bit, the bit I am kinda stuck on is how the levelling will actually work.
When someone joins it will auto be at lv 1. Now say it lv x 2 for XP to level up per time;
lv1 = 2 xp lv2 = 4 xp lv3 = 6xp lv4 = 8xp
How do I factor this into a formula, so when the XP is reached it will for the certain LV it will level the user up and double the amount of XP needed?
Upvotes: 0
Views: 1308
Reputation: 103
I did my leveling and EXP system like this, but mine's more advanced with skill points..
<?php
$level_up = ($level + 1);
if ($exp >= $max_exp)
{
$sql = "UPDATE users SET level=(level + 1) , max_exp=(exp + 300) , skill_points=(skill_points + 3) WHERE id='".$id."' LIMIT 1";
$res = mysql_query($sql);
echo '<div class="Leveled">' . 'You sucessfully leveled up to ' . $level_up . '!' . ' As a reward you were given 3 skill points!' . '</div>';
}
else
{
}
?>
Upvotes: 0
Reputation: 86506
If we start at level 0, and require 2*level xp to get from level-1
to level
(ie: 2 xp gets you to level 1, 2+4 total gets you to level 2, 2+4+6 total for level 3, etc), then we have an arithmetic sequence, and the sum is equal to (level/2) * (2 + (2*level))
Simplifying further:
$total_xp_required = $level * (1 + $level);
Now, if we use the quadratic formula to solve level^2 + level + -xp = 0
for level
, we get level = (-1 ± sqrt(1 - 4*(-xp))) / 2
.
The positive root will always be the one we want, so of the +/-, we only care about the +. Also, non-integer levels don't make sense, so turn it into an int. The only catch is, floats are kinda flaky -- numbers could come up to like 1.99999999998 or something rather than 2.0. We can add a tiny fudge factor to the number before truncating it.
$level = int((sqrt(1 + ($xp*4)) - 1) / 2 + .000000005);
Now, if you want to double the xp required each time, it gets even easier. Say level 1 requires 2 xp, level 2 takes 4, level 3 takes 8, etc. Then your total xp required for a given level is 2 ^ level
.
Powers of 2 being a special case in binary, 2^x can be represented by 1 << x
.
$total_xp_required = 1 << $level;
And to calculate the level, there are a number of tricks. Mathematically, the level is the log2 of the score.
$level = intval(log($xp) / log(2) + .000000005);
Or stringwise, we can just count the number of digits in the number's base-2 representation. No fudge factor needed here, since floats never come into the picture.
$level = strlen(sprintf("%b", $xp)) - 1;
Either way, at this point, since we can calculate level from xp and vice versa, you don't really need to store the level at all -- just calculate it when you need it.
Upvotes: 1
Reputation: 2900
formulas: your sample:
//level => required XP to reach it 1 => 0; 2=>2; 3=>6; 4=>8....
$xpForNextLevel = ($currentLevel)*2;
Double required xp after each level
//level => required XP to reach it 1 => 0; 2=>2; 3=>4; 4=>8; 5=>16; 6=>32....
$xpForNextLevel = pow(2,$currentLevel);
EDIT: Where $xpForNextLevel is the total amount of xp required to reach the next level, NOT the difference betwean this level and the next.
Upvotes: 0