Skip to content

Commit

Permalink
GH-132 Move level-related calculations to LevelUtil. (#132)
Browse files Browse the repository at this point in the history
* Move level-related calculations to LevelUtil.

* Move to level package.

* Fix checkstyle.
  • Loading branch information
vLuckyyy authored Oct 25, 2023
1 parent 031aa7f commit 9f33cb7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
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);
}
}

0 comments on commit 9f33cb7

Please sign in to comment.