Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update level_id in experiences table when levelled up #48

Merged
merged 2 commits into from
Oct 4, 2023

Conversation

7OMI
Copy link
Contributor

@7OMI 7OMI commented Oct 3, 2023

No description provided.

@cjmellor
Copy link
Owner

cjmellor commented Oct 3, 2023

@7OMI I don't understand what this fixes? level_id is always updated when the User is levelled up.

Can you add a description to the PR that explains why this fix is needed? Provide examples of when the code fails.

@cjmellor cjmellor added invalid This doesn't seem right question Further information is requested labels Oct 3, 2023
@7OMI
Copy link
Contributor Author

7OMI commented Oct 4, 2023

@cjmellor

The level_id is only updated in the user table, not in the experiences table.
The getLevel() method retrieves the current level of the user from the experiences table.
The nextLevelAt() method returns 0 because it also retrieves the current level from the experiences table.

Q. Why is the user level stored in two places anyway?

users table:

id ... level_id ...
3 ... 2 ...

experience_audits table:

id user_id points levelled_up level_to type reason ...
1 3 10 0 NULL add NULL ...
2 3 100 0 NULL add NULL ...
3 3 110 1 2 level_up NULL ...

experiences table:

id user_id level_id experience_points ...
1 3 1 110 ...

@cjmellor
Copy link
Owner

cjmellor commented Oct 4, 2023

What code are you running to get this data?

It looks like $user->addPoints(110)

Please let me know the steps you've taken so I can try to replicate.

Also list if you have any of the config settings different to the defaults.

Thanks.

@7OMI
Copy link
Contributor Author

7OMI commented Oct 4, 2023

@cjmellor

What code are you running to get this data?
It looks like $user->addPoints(110)
Please let me know the steps you've taken so I can try to replicate.

If it is written out immediately after the point is given, it works:

$user = \App\Models\User::whereId(3)->first(); // or: auth()->user()
$user->addPoints(10);
$user->addPoints(100);
echo 'LEVEL: ' . $user->getLevel() . '<br>'; // Get: `LEVEL: 2`
echo 'NEXT LEVEL AT: ' . $user->nextLevelAt() . '<br>'; // Get: `NEXT LEVEL AT: 130`

If you don't add a point, but simply want to write out, it won't work:

$user = \App\Models\User::whereId(3)->first(); // or: auth()->user()
echo 'LEVEL: ' . $user->getLevel() . '<br>'; // Get: `LEVEL: 1`
echo 'NEXT LEVEL AT: ' . $user->nextLevelAt() . '<br>'; // Get: `NEXT LEVEL AT: 0`

Also list if you have any of the config settings different to the defaults.

Only one change has been made. This has been added to the .env:

AUDIT_POINTS=true

Additional information:

levels table:

id level next_level_experience ...
1 1 0 ...
2 2 100 ...
3 3 240 ...
4 4 520 ...
5 5 1080 ...
6 6 2200 ...
  • Package Version: 1.0.0
  • Laravel Version: 10.26.2
  • PHP Version: 8.2.8
  • Which operating systems does with happen with? Windows

@cjmellor cjmellor self-assigned this Oct 4, 2023
@cjmellor cjmellor added bug Something isn't working and removed invalid This doesn't seem right question Further information is requested labels Oct 4, 2023
tests: Added a new sanity test
@cjmellor cjmellor merged commit e809a42 into cjmellor:main Oct 4, 2023
6 checks passed
@cjmellor
Copy link
Owner

cjmellor commented Oct 4, 2023

@7OMI Thanks man for pointing this out!

I've changed the way it is fixed though to better suit the rest of the code. I also added another sanity test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants