I've been working with this for the entire week now, this is about a leveling system of my application. When a user gets the enough exp needed, it gets leveled up, everything works fine. But, when a user gets a bulk exp, it won't get a bulk level as well. The default for exp needed for every user is 1000. For example:
The user gets 1000 exp, for level 1, it gets leveled up to level 2. It works fine that way, but if it gets more than the exp needed the flow won't work the same way. Because the exp needed for level 2, is 1400. So the algorithm for this if the user gets enough or more than the exp needed, the current exp_needed for the next level is multiplied by 1.4.
if($user->level <= 19){
while($user->curr_exp >= $user->exp_needed){
$user->level += 1;
$user->curr_exp -= $user->exp_needed;
$user->prev_exp = $user->exp_needed;
$user->exp_needed = $user->prev_exp * 1.4;
$user->save();
}
}
elseif($user->level >= 20){
while($user->curr_exp >= $user->exp_needed){
$user->level += 1;
$user->curr_exp -= $user->exp_needed;
$user->prev_exp = $user->exp_needed;
$user->exp_needed = $user->prev_exp * 1.1;
$user->save();
}
}
Edit : The levels 1-20 and their exp_needed is working fine now, but when I'm at Level 21, I'm having troubles with the exp, because the exp_needed at level 21 should only be multiplied by 1.1,
For example,
Scenario 1: The User will earn a curr_exp(current exp) of 2,100,000 the user will jump on to level 21 with an experience of 836,6825.55 which is wrong because the exp_needed at level 21 should be the sum of the prev_exp(level 20 = 597,630.40) multiplied by 1.1 which will be
(prev_exp of level 20) 597,630.40 * 1.1 = 657,393.44
Scenario 2:
The user gets an exp of 2,000,000 it will be on the level 20 with an exp of 508,424.01 with an exp_needed of 597,630.40 and when it gets another exp of 100,000 the user will be leveled up to 21 with an exp_needed of 657,393.44 which is correct.
So to summarize each scenarios results:
Scenario 1:
Level 21
Exp:10,793.61
Previous Exp: 597,630.44
Exp Needed for the next level: 836,682.55
Scenario 2:
Level 21
Exp: 10,793.61
Previous Exp: 597,630.40
Exp needed for the next level: 657,393.44
Scenario 2 should be the results I am seeking for but it doesn't work that way on my while loop. Any ideas how to deal with this? Thank you.