diff --git a/src/main/java/de/voidtech/gerald/commands/utils/ExperienceCommand.java b/src/main/java/de/voidtech/gerald/commands/utils/ExperienceCommand.java index 8cf25b2c..7fa9f312 100644 --- a/src/main/java/de/voidtech/gerald/commands/utils/ExperienceCommand.java +++ b/src/main/java/de/voidtech/gerald/commands/utils/ExperienceCommand.java @@ -143,8 +143,8 @@ private void sendLevelCard(MessageChannel channel, Member member, long serverID) Experience userXP = xpService.getUserExperience(member.getId(), serverID); byte[] xpCard = xpService.getExperienceCard(member.getUser().getAvatarUrl(), - userXP.getCurrentExperience(), xpService.xpNeededForLevel(userXP.getLevel() + 1), - userXP.getLevel(), 1, member.getUser().getName() + "#" + member.getUser().getDiscriminator(), + userXP.getCurrentExperience(), xpService.xpNeededForLevel(userXP.getNextLevel()), + userXP.getCurrentLevel(), 1, member.getUser().getName() + "#" + member.getUser().getDiscriminator(), "#FF0000", "#2E2E2E"); channel.sendFile(xpCard, "xpcard.png").queue(); } diff --git a/src/main/java/de/voidtech/gerald/entities/Experience.java b/src/main/java/de/voidtech/gerald/entities/Experience.java index a60b5d05..8ac23cab 100644 --- a/src/main/java/de/voidtech/gerald/entities/Experience.java +++ b/src/main/java/de/voidtech/gerald/entities/Experience.java @@ -33,6 +33,9 @@ public class Experience { @Column private long lastMessageTime; + @Column + private long totalExperience; + @Deprecated //ONLY FOR HIBERNATE, DO NOT USE Experience() { @@ -51,6 +54,11 @@ public void setLevel(long level) { this.level = level; } + public void incrementExperience(long xp) { + this.experienceGainedToNextLevel = this.experienceGainedToNextLevel + xp; + this.totalExperience = this.totalExperience + xp; + } + public void setCurrentXP(long xp) { this.experienceGainedToNextLevel = xp; } @@ -63,10 +71,14 @@ public void incrementMessageCount() { this.messageCount++; } - public long getLevel() { + public long getCurrentLevel() { return this.level; } + public long getNextLevel() { + return this.level + 1; + } + public long getCurrentExperience() { return this.experienceGainedToNextLevel; } diff --git a/src/main/java/de/voidtech/gerald/service/ExperienceService.java b/src/main/java/de/voidtech/gerald/service/ExperienceService.java index 10ca2047..57ffe146 100644 --- a/src/main/java/de/voidtech/gerald/service/ExperienceService.java +++ b/src/main/java/de/voidtech/gerald/service/ExperienceService.java @@ -126,7 +126,6 @@ public boolean serverHasRoleForLevel(long id, long level) { } private void saveUserExperience(Experience userXP) { - System.out.println("Saving user XP"); try(Session session = sessionFactory.openSession()) { session.getTransaction().begin(); @@ -170,8 +169,7 @@ public long xpNeededForLevel(long level) { return 5 * (level ^ 2) + (50 * level) + 100; } - private long xpToNextLevel(long currentLevel, long currentXP) { - long nextLevel = currentLevel + 1; + private long xpToNextLevel(long nextLevel, long currentXP) { return xpNeededForLevel(nextLevel) - currentXP; } @@ -189,11 +187,12 @@ public void updateUserExperience(Member member, String guildID, String channelID return; } - long currentExperience = userXP.getCurrentExperience() + generateExperience(); - long xpToNextLevel = xpToNextLevel(userXP.getLevel(), currentExperience); + userXP.incrementExperience(generateExperience()); + long currentExperience = userXP.getCurrentExperience(); + long xpToNextLevel = xpToNextLevel(userXP.getNextLevel(), currentExperience); if (xpToNextLevel <= 0) { - userXP.setLevel(userXP.getLevel() + 1); + userXP.setLevel(userXP.getNextLevel()); userXP.setCurrentXP(-1 * xpToNextLevel); performLevelUpActions(userXP, server, member, channelID); } else userXP.setCurrentXP(currentExperience); @@ -206,7 +205,7 @@ public void updateUserExperience(Member member, String guildID, String channelID private void performLevelUpActions(Experience userXP, Server server, Member member, String channelID) { ServerExperienceConfig config = getServerExperienceConfig(server.getId()); - List roles = getRolesForLevelFromServer(server.getId(), userXP.getLevel()); + List roles = getRolesForLevelFromServer(server.getId(), userXP.getCurrentLevel()); if (roles.isEmpty()) return; List memberRoles = member.getRoles();