Skip to content

Commit

Permalink
feat(service): Added a total xp count to the xp system (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seb committed Apr 18, 2022
1 parent 41140c4 commit a11b6e6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/de/voidtech/gerald/entities/Experience.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public class Experience {
@Column
private long lastMessageTime;

@Column
private long totalExperience;

@Deprecated
//ONLY FOR HIBERNATE, DO NOT USE
Experience() {
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/de/voidtech/gerald/service/ExperienceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand All @@ -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<LevelUpRole> roles = getRolesForLevelFromServer(server.getId(), userXP.getLevel());
List<LevelUpRole> roles = getRolesForLevelFromServer(server.getId(), userXP.getCurrentLevel());
if (roles.isEmpty()) return;

List<Role> memberRoles = member.getRoles();
Expand Down

0 comments on commit a11b6e6

Please sign in to comment.