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

GH-132 Move level-related calculations to LevelUtil. #132

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,6 @@ public LevelController(LevelConfig levelConfig, LevelService levelService, JDA j
this.jda = jda;
}

/**
* This method is called when an ExperienceChangeEvent occurs. It updates the user's level based on their experience points
* and sends a message to a designated TextChannel when the user levels up.
* <p>
* It calculates the next level using the formula: N = P * L^2 + P * L + P
* where:
* <p> - N represents the points needed to reach the next level,
* <p> - P is the points needed for one level (provided by LevelConfig),
* <p> - L is the current level.
* <p>
* Example level calculations:
* <p> Level 1: N = 100 * 1^2 + 100 * 1 + 100 = 300
* <p> Level 2: N = 100 * 2^2 + 100 * 2 + 100 = 700
* <p> Level 3: N = 100 * 3^2 + 100 * 3 + 100 = 1200
* <p> Level 4: N = 100 * 4^2 + 100 * 4 + 100 = 1700
* <p> Level 5: N = 100 * 5^2 + 100 * 5 + 100 = 2300
*/

@Override
public void update(ExperienceChangeEvent event) {
Experience experience = event.experience();
Expand All @@ -53,9 +35,7 @@ public void update(ExperienceChangeEvent event) {
this.levelService.find(userId).thenApply(userLevel -> {
int currentLevel = userLevel.getCurrentLevel();

int pointsNeededForNextLevel = (int) (pointsNeededForOneLevel * Math.pow(currentLevel + 1, 2)
+ pointsNeededForOneLevel * (currentLevel + 1)
+ pointsNeededForOneLevel);
int pointsNeededForNextLevel = LevelUtil.calculatePointsForNextLevel(currentLevel, pointsNeededForOneLevel);

if (experiencePoints <= pointsNeededForNextLevel) {
return null;
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/eternalcode/discordapp/leveling/LevelUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.eternalcode.discordapp.leveling;

public final class LevelUtil {

public LevelUtil() {
throw new UnsupportedOperationException("This class cannot be instantiated");
}

/**
* Calculates the points needed to achieve the next level.
* <p>
* This method is called when an ExperienceChangeEvent occurs. It updates the user's level based on their experience points
* and sends a message to a designated TextChannel when the user levels up.
* <p>
* It calculates the next level using the formula: N = P * L^2 + P * L + P
* where:
* <p> - N represents the points needed to reach the next level,
* <p> - P is the points needed for one level (provided by LevelConfig),
* <p> - L is the current level.
* <p>
* Example level calculations:
* <p> Level 1: N = 100 * 1^2 + 100 * 1 + 100 = 300
* <p> Level 2: N = 100 * 2^2 + 100 * 2 + 100 = 700
* <p> Level 3: N = 100 * 3^2 + 100 * 3 + 100 = 1200
* <p> Level 4: N = 100 * 4^2 + 100 * 4 + 100 = 1700
* <p> Level 5: N = 100 * 5^2 + 100 * 5 + 100 = 2300
*
* @param currentLevel Current level.
* @param pointsNeededForOneLevel Points needed for one level.
* @return The calculated points needed for the next level.
*/
public static int calculatePointsForNextLevel(int currentLevel, double pointsNeededForOneLevel) {
return (int) (pointsNeededForOneLevel * Math.pow(currentLevel + 1, 2)
+ pointsNeededForOneLevel * (currentLevel + 1)
+ pointsNeededForOneLevel);
}
}