level-up: [Bug]: Event fired only once even when user has points for multiple levels

What happened?

Hello I was messing around with your package and noticed very bad bug. When points earned by user are enough to gain more than one level it gets increased only by one closest to level user currently has. When next points are awarded next level is unlocked but it should be done in one run in my opinion as there may be along time before user earns next points and he will be stuck at lower level making functions like:

$user->nextLevelAt();

useless as they will display 0 as points requirements for next level are already met. Is there any way to award multiple levels at one points increase operation?

How to reproduce the bug

  1. Setup default levels
Level::add(
    ['level' => 1, 'next_level_experience' => null],
    ['level' => 2, 'next_level_experience' => 100],
    ['level' => 3, 'next_level_experience' => 250],
);
  1. Award user 300 points
$user->addPoints(300);
  1. Only one UserLevelledUp event is fired with level 2
  2. Award one more point:
$user->addPoints(1);
  1. User level gets increased to level 3

Package Version

v0.0.10

PHP Version

8.1

Laravel Version

10.2

Which operating systems does with happen with?

Windows

Notes

No response

About this issue

  • Original URL
  • State: closed
  • Created 10 months ago
  • Comments: 21 (10 by maintainers)

Most upvoted comments

I have managed to modify package to handle all issues I had properly. It now works with starting 0 balance, fires leveled up event on every level user gains. Instead of throwing exception when given XP is too high it just replaces amount with highest possible. I will try to optimize whole package code especially multiple, unnecessary DB calls and n+1 queries and probably will make PR next week.

@cjmellor I did check commit and what you changed was not the issue. Problem is still with leveling only by one level even when points awarded should give more levels. Lets say we have 5 levels Level - required XP 1 - null 2 - 150 3 - 250 4 - 350 5 - 450 When user is awarded 300 points in one addPoints() method call he will still level up to only level 2 even though he should reach level 3 (both level increases should emit proper events for correct logging and another operations like assigning awards handling). Level 3 will be increased only when another addPoints() call is made.

Thanks for the report @theveloperspl

I will look into a fix.