Skip to content

Commit

Permalink
1.7.9
Browse files Browse the repository at this point in the history
* Fixed the Math behind the Level Sytem.
  • Loading branch information
DxsSucuk committed May 10, 2022
1 parent e306417 commit 865273e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 58 deletions.
3 changes: 3 additions & 0 deletions src/main/java/de/presti/ree6/sql/SQLWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,9 @@ public void createSettings(String guildId) {
// Create the Chat Prefix Setting.
if (!hasSetting(guildId, "chatprefix")) setSetting(guildId, new Setting("chatprefix", "ree!"));

// Create the Level Message Setting.
if (!hasSetting(guildId, "level_message")) setSetting(guildId, new Setting("level_message", false));

// Create Command Settings.
for (ICommand command : Main.getInstance().getCommandManager().getCommands()) {

Expand Down
111 changes: 55 additions & 56 deletions src/main/java/de/presti/ree6/sql/entities/UserLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,23 @@ public class UserLevel {
// The Rank of the User.
int rank;

// Is this a Voice related Level?
boolean isVoice;

/**
* Constructor to create a UserLevel with the needed Data.
*
* @param userId the ID of the User.
* @param rank the current Rank in the leaderboard.
* @param experience his XP count.
* @param voice is this related to VoiceXP?
* @param voice is this related to VoiceXP?
*/
public UserLevel(String userId, int rank, long experience, boolean voice) {
this.userId = userId;
this.experience = experience;
this.rank = rank;

long tempXp = experience;

if (voice) {
while (tempXp >= (1000 * level)) {
tempXp -= (1000 * level);
level++;
}
} else {
while (tempXp >= (1000 * (level * 0.5))) {
tempXp -= (1000 * (level * 0.5));
level++;
}
}
this.isVoice = voice;
level = calculateLevel(experience);
}


Expand Down Expand Up @@ -138,6 +129,7 @@ public void setLevel(long level) {

/**
* Get the current Rank of the User.
*
* @return the current rank.
*/
public int getRank() {
Expand All @@ -146,6 +138,7 @@ public int getRank() {

/**
* Added experience to the UserLevel.
*
* @param experience the experience that should be added.
* @return true, if you leveled up | false, if not.
*/
Expand All @@ -166,74 +159,81 @@ public boolean addExperience(long experience) {

/**
* Get the needed Experience for the next Level.
*
* @return the needed Experience.
*/
public long getExperienceForNextLevel() {
float tempXp = getExperience();
return getExperienceForLevel(level + 1);
}

/**
* Get the needed Experience for the next Level.
*
* @return the needed Experience.
*/
public long getTotalExperienceForNextLevel() {
return getTotalExperienceForLevel(level + 1);
}

int multiplier = 1;

while (tempXp > 1000) {
tempXp -= 1000;
multiplier++;
/**
* Get the needed Experience for the next Level.
*
* @param level The level.
* @return the needed Experience.
*/
public long getTotalExperienceForLevel(long level) {
long requiredXP = 0;
for (int i = 0; i <= level; i++) {
requiredXP += getExperienceForLevel(i);
}
return 1000L * multiplier;
return requiredXP;
}

/**
* Get the needed Experience for the next Level.
*
* @param level The level.
* @return the needed Experience.
*/
public long getExperienceForLevel(long level) {
return (long) (1000 + (1000 * Math.pow(level, isVoice ? 1.05 : 0.55)));
}

/**
* Get the current Progress of the User.
*
* @return the Progress.
*/
public int getProgress() {
float tempXp = getExperience();

while (tempXp > 1000) {
tempXp -= 1000;
}

return Math.round(((tempXp / 1000F) * 100F));
public double getProgress() {
return (int)((getExperience() * 100) / getTotalExperienceForNextLevel());
}

/**
* Get the current Experience but formatted.
*
* @return a formatted version of the current Experience.
*/
public String getFormattedExperience() {
String end;

if(getExperience() >= 1000000000000L) {
end = ((getExperience() / 1000000000000L) + "").replace("l", "") + "mil";
} else if(getExperience() >= 1000000000) {
end = ((getExperience() / 1000000000) + "").replace("l", "") + "mil";
} else if(getExperience() >= 1000000) {
end = ((getExperience() / 1000000) + "").replace("l", "") + "mio";
} else if(getExperience() >= 1000) {
end = ((getExperience() / 1000) + "").replace("l", "") + "k";
} else {
end = "" + getExperience();
}

return end;
return getFormattedExperience(getExperience());
}

/**
* Get the Experience but formatted.
*
* @param experience the Experience that should be formatted.
*
* @return a formatted version of the Experience.
*/
public String getFormattedExperience(long experience) {
String end;

if(experience >= 1000000000000L) {
if (experience >= 1000000000000L) {
end = ((experience / 1000000000000L) + "").replace("l", "") + "mil";
} else if(experience >= 1000000000) {
} else if (experience >= 1000000000) {
end = ((experience / 1000000000) + "").replace("l", "") + "mil";
} else if(experience >= 1000000) {
} else if (experience >= 1000000) {
end = ((experience / 1000000) + "").replace("l", "") + "mio";
} else if(experience >= 1000) {
} else if (experience >= 1000) {
end = ((experience / 1000) + "").replace("l", "") + "k";
} else {
end = "" + getExperience();
Expand All @@ -244,17 +244,16 @@ public String getFormattedExperience(long experience) {

/**
* Calculate on which Level you would be by your experience.
*
* @param experience the experience.
* @return which Level.
*/
public long calculateLevel(long experience) {
long calculatedLevel = 0;

while (experience > 1000) {
experience -= 1000;
calculatedLevel++;
int i = 0;
while (true) {
long requiredXP = getTotalExperienceForLevel(i);
if (experience <= requiredXP) return (i == 0 ? 1 : i -1);
i++;
}

return calculatedLevel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static byte[] createRankImage(UserLevel userLevel) throws IOException {
graphics2D.setFont(verdana40);
graphics2D.drawString(userLevel.getFormattedExperience() + "", (base.getWidth() - 800) - (graphics2D.getFontMetrics().stringWidth("/" + userLevel.getFormattedExperience(userLevel.getExperienceForNextLevel()))) - 5 - graphics2D.getFontMetrics().stringWidth(userLevel.getFormattedExperience() + ""), 675);
graphics2D.setColor(Color.GRAY);
graphics2D.drawString("/" + userLevel.getFormattedExperience(userLevel.getExperienceForNextLevel()), (base.getWidth() - 800) - (graphics2D.getFontMetrics().stringWidth("/" + userLevel.getFormattedExperience(userLevel.getExperienceForNextLevel()))), 675);
graphics2D.drawString("/" + userLevel.getFormattedExperience(userLevel.getTotalExperienceForNextLevel()), (base.getWidth() - 800) - (graphics2D.getFontMetrics().stringWidth("/" + userLevel.getFormattedExperience(userLevel.getExperienceForNextLevel()))), 675);

//endregion

Expand Down Expand Up @@ -113,7 +113,7 @@ public static byte[] createRankImage(UserLevel userLevel) throws IOException {

// Draw the Progressbar.
graphics2D.setColor(Color.magenta);
graphics2D.fillRoundRect(175, 705, (base.getWidth() - 950) * userLevel.getProgress() / 100, 50, 50, 50);
graphics2D.fillRoundRect(175, 705, (base.getWidth() - 950) * (int) userLevel.getProgress() / 100, 50, 50, 50);

// Close the Graphics2d instance.
graphics2D.dispose();
Expand Down

0 comments on commit 865273e

Please sign in to comment.