0
\$\begingroup\$

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.

\$\endgroup\$
1

1 Answer 1

4
\$\begingroup\$

It's usually as simple as

while (expReceived > expNeededForNextLevel) {
    expReceived -= expNeededForNextLevel
    levelup()
}
\$\endgroup\$
10
  • \$\begingroup\$ sir, what if I'll change the equations, from levels 1-20 the exp_needed will be multiplied by 1.4, and 21 to 40 will be multiplied by 1.1? \$\endgroup\$
    – John Carlo
    Mar 25, 2019 at 7:15
  • \$\begingroup\$ I'm actually having troubles when the user gets more than enough exp at level 20, that the user will eventually level to 21, at level 20, its exp is 597,630. so the exp at level 21 should be 657,393.44 \$\endgroup\$
    – John Carlo
    Mar 25, 2019 at 9:14
  • \$\begingroup\$ @John You could even use a lookup table for exp values per level. Levelup() would in this case manipulate the expNeededForNextLevel variable, which is then reassessed in the same while loop. \$\endgroup\$
    – Peethor
    Mar 25, 2019 at 9:38
  • \$\begingroup\$ @Peethor sir, I don't actually get that part, could you please show me an example on how to do that? \$\endgroup\$
    – John Carlo
    Mar 25, 2019 at 9:43
  • \$\begingroup\$ I just saw you edited the question. Nevermind my comment. Make one big while loop, and do the if statement inside that \$\endgroup\$
    – Peethor
    Mar 25, 2019 at 9:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .