User Leveling System

by John Carlo   Last Updated May 16, 2019 03:13 AM

I've already asked this before, but I just wanna encode this again with a new leveling format. What I want to do is that, the user will gain exp and once they leveled up, their previous experience will be added to their current exp.

So, to put it in a Scenario it goes like this: User1 is on a Level 1 with an exp_needed of 1000 to reach level 2. He gained an exp of 1000, so he is now on level 2 with a current_exp of 1000 and an exp needed of 1,400. Or if User1 gained an exp of 1,100 he is on level 2 with a current_exp of 1,100 and 1,400 exp needed. And so on, depending on the algorithm. But I get a result of -900/1,400 if the user gains 1,100 exp.

Here is the code below:

while($user->curr_exp >= $user->exp_needed){
       $user->curr_exp -= ($user->exp_needed) + $user->prev_exp;   
       $user->prev_exp = $user->exp_needed;
       if($user->level <= 19){
       $user->exp_needed = $user->prev_exp * 1.4;
       }
       elseif($user->level >= 20 && $user->level <= 39){
       $user->exp_needed = $user->prev_exp * 1.1;
       }
       elseif($user->level >= 40 && $user->level <= 59){
       $user->exp_needed = $user->prev_exp * 1.05;
       }
       elseif($user->level >= 60 && $user->level <= 79){
       $user->exp_needed = $user->prev_exp * 1.04;
       }
       elseif($user->level >= 80 && $user->level <= 99){
       $user->exp_needed = $user->prev_exp * 1.03;
       }
       elseif($user->level == 100){
       $user->exp_needed = $user->exp_needed * 0;
       }
       $user->level += 1;
       $user->save();
      }

Any ideas how to deal with this? Thanks.



Related Questions


Updated August 02, 2017 10:13 AM

Updated March 26, 2018 10:13 AM

Updated January 26, 2018 15:13 PM

Updated January 23, 2018 01:13 AM

Updated July 21, 2016 08:05 AM