Skip to content

Commit 6b46658

Browse files
committed
Faster and more efficient LevelUtil
1 parent 3a26ced commit 6b46658

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

src/main/java/de/presti/ree6/sql/util/LevelUtil.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
/**
99
* Utility used to determine data for levels.
10+
* The current implementation works using these formulas.
11+
* Level: x * root(XP)
12+
* XP: (level/x) ^ y
1013
*/
1114
public class LevelUtil {
1215

@@ -17,12 +20,7 @@ public class LevelUtil {
1720
* @return which Level.
1821
*/
1922
public static long calculateLevel(UserLevel userLevel) {
20-
int i = 0;
21-
while (true) {
22-
long requiredXP = getTotalExperienceForLevel(i, userLevel);
23-
if (userLevel.getExperience() <= requiredXP) return (i == 0 ? 1 : i - 1);
24-
i++;
25-
}
23+
return calculateLevel(userLevel, userLevel.getExperience());
2624
}
2725

2826
/**
@@ -33,12 +31,8 @@ public static long calculateLevel(UserLevel userLevel) {
3331
* @return which Level.
3432
*/
3533
public static long calculateLevel(UserLevel userLevel, long experience) {
36-
int i = 0;
37-
while (true) {
38-
long requiredXP = getTotalExperienceForLevel(i, userLevel);
39-
if (experience <= requiredXP) return (i == 0 ? 1 : i - 1);
40-
i++;
41-
}
34+
if (experience == 0) return 0;
35+
return (long)(getLevelingValues(userLevel)[0] * Math.sqrt(experience));
4236
}
4337

4438
/**
@@ -49,11 +43,10 @@ public static long calculateLevel(UserLevel userLevel, long experience) {
4943
* @return the needed Experience.
5044
*/
5145
public static long getTotalExperienceForLevel(long level, UserLevel userLevel) {
52-
long requiredXP = 0;
53-
for (int i = 0; i <= level; i++) {
54-
requiredXP += getExperienceForLevel(i, userLevel);
55-
}
56-
return requiredXP;
46+
if (level == 0) return 0;
47+
float[] values = getLevelingValues(userLevel);
48+
49+
return (long)((level + 1) / values[0]) ^ (long)values[1];
5750
}
5851

5952
/**
@@ -63,13 +56,25 @@ public static long getTotalExperienceForLevel(long level, UserLevel userLevel) {
6356
* @param userLevel the UserLevel.
6457
* @return the needed Experience.
6558
*/
66-
public static long getExperienceForLevel(long level, UserLevel userLevel) {
59+
public static long getExperienceNeededForLevel(long level, UserLevel userLevel) {
60+
if (level < userLevel.getLevel()) {
61+
return userLevel.getExperience() - getTotalExperienceForLevel(level, userLevel);
62+
}
63+
return getTotalExperienceForLevel(level, userLevel) - userLevel.getExperience();
64+
}
65+
66+
/**
67+
* Get the Leveling Values for the UserLevel.
68+
*
69+
* @param userLevel the UserLevel.
70+
* @return the Leveling Values.
71+
*/
72+
public static float[] getLevelingValues(UserLevel userLevel) {
6773
if (userLevel instanceof ChatUserLevel) {
68-
return (long) (1000 + (1000 * Math.pow(level, 0.55)));
69-
} else if (userLevel instanceof VoiceUserLevel) {
70-
return (long) (1000 + (1000 * Math.pow(level, 1.05)));
74+
return new float[] { 0.1f, 2 };
75+
} else {
76+
return new float[] { 0.08f, 2 };
7177
}
72-
return level;
7378
}
7479

7580
/**

0 commit comments

Comments
 (0)