7
7
8
8
/**
9
9
* 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
10
13
*/
11
14
public class LevelUtil {
12
15
@@ -17,12 +20,7 @@ public class LevelUtil {
17
20
* @return which Level.
18
21
*/
19
22
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 ());
26
24
}
27
25
28
26
/**
@@ -33,12 +31,8 @@ public static long calculateLevel(UserLevel userLevel) {
33
31
* @return which Level.
34
32
*/
35
33
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 ));
42
36
}
43
37
44
38
/**
@@ -49,11 +43,10 @@ public static long calculateLevel(UserLevel userLevel, long experience) {
49
43
* @return the needed Experience.
50
44
*/
51
45
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 ];
57
50
}
58
51
59
52
/**
@@ -63,13 +56,25 @@ public static long getTotalExperienceForLevel(long level, UserLevel userLevel) {
63
56
* @param userLevel the UserLevel.
64
57
* @return the needed Experience.
65
58
*/
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 ) {
67
73
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 } ;
71
77
}
72
- return level ;
73
78
}
74
79
75
80
/**
0 commit comments