From 0d1cb167bd0dda32ab745c89f44417db85c3b2e4 Mon Sep 17 00:00:00 2001 From: Seb Date: Tue, 29 Mar 2022 12:48:49 +0100 Subject: [PATCH] feat(service): Finished level up roles and experience system, commands are still needed and output formatting is not done (#87) --- .../voidtech/gerald/entities/LevelUpRole.java | 12 +++ .../entities/ServerExperienceConfig.java | 8 ++ .../routines/utils/ExperienceRoutine.java | 2 +- .../gerald/service/ExperienceService.java | 98 +++++++++++++++++-- 4 files changed, 111 insertions(+), 9 deletions(-) diff --git a/src/main/java/de/voidtech/gerald/entities/LevelUpRole.java b/src/main/java/de/voidtech/gerald/entities/LevelUpRole.java index 35caa18a..8b520b74 100644 --- a/src/main/java/de/voidtech/gerald/entities/LevelUpRole.java +++ b/src/main/java/de/voidtech/gerald/entities/LevelUpRole.java @@ -34,5 +34,17 @@ public LevelUpRole(String roleID, long serverID, long level) { this.serverID = serverID; this.level = level; } + + public String getRoleID() { + return this.roleID; + } + + public long getServerID() { + return this.serverID; + } + + public long getLevel() { + return this.level; + } } diff --git a/src/main/java/de/voidtech/gerald/entities/ServerExperienceConfig.java b/src/main/java/de/voidtech/gerald/entities/ServerExperienceConfig.java index 50c51c6a..9a445b54 100644 --- a/src/main/java/de/voidtech/gerald/entities/ServerExperienceConfig.java +++ b/src/main/java/de/voidtech/gerald/entities/ServerExperienceConfig.java @@ -31,4 +31,12 @@ public ServerExperienceConfig(long serverID) { this.levelUpMessagesEnabled = false; } + public boolean levelUpMessagesEnabled() { + return this.levelUpMessagesEnabled; + } + + public void setLevelUpMessagesEnabled(boolean enabled) { + this.levelUpMessagesEnabled = enabled; + } + } \ No newline at end of file diff --git a/src/main/java/de/voidtech/gerald/routines/utils/ExperienceRoutine.java b/src/main/java/de/voidtech/gerald/routines/utils/ExperienceRoutine.java index 1d4c330e..1f9da189 100644 --- a/src/main/java/de/voidtech/gerald/routines/utils/ExperienceRoutine.java +++ b/src/main/java/de/voidtech/gerald/routines/utils/ExperienceRoutine.java @@ -18,7 +18,7 @@ public class ExperienceRoutine extends AbstractRoutine { @Override public void executeInternal(Message message) { if (message.getChannel().getType().equals(ChannelType.PRIVATE)) return; - xpService.updateUserExperience(message.getAuthor().getId(), message.getGuild().getId(), message.getChannel().getId()); + xpService.updateUserExperience(message.getMember(), message.getGuild().getId(), message.getChannel().getId()); } @Override diff --git a/src/main/java/de/voidtech/gerald/service/ExperienceService.java b/src/main/java/de/voidtech/gerald/service/ExperienceService.java index 24d68ee2..f050d356 100644 --- a/src/main/java/de/voidtech/gerald/service/ExperienceService.java +++ b/src/main/java/de/voidtech/gerald/service/ExperienceService.java @@ -1,6 +1,7 @@ package main.java.de.voidtech.gerald.service; import java.time.Instant; +import java.util.List; import java.util.Random; import org.hibernate.Session; @@ -9,7 +10,11 @@ import org.springframework.stereotype.Service; import main.java.de.voidtech.gerald.entities.Experience; +import main.java.de.voidtech.gerald.entities.LevelUpRole; import main.java.de.voidtech.gerald.entities.Server; +import main.java.de.voidtech.gerald.entities.ServerExperienceConfig; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Role; @Service public class ExperienceService { @@ -20,7 +25,7 @@ public class ExperienceService { @Autowired private ServerService serverService; - private static final int EXPERIENCE_DELAY = 60; //Delay between incrementing XP in seconds + private static final int EXPERIENCE_DELAY = 0; //Delay between incrementing XP in seconds private Experience getUserExperience(String userID, long serverID) { try(Session session = sessionFactory.openSession()) @@ -29,12 +34,39 @@ private Experience getUserExperience(String userID, long serverID) { .setParameter("userID", userID) .setParameter("serverID", serverID) .uniqueResult(); + if (xp == null) xp = new Experience(userID, serverID); - return xp; } } + private ServerExperienceConfig getServerExperienceConfig(long serverID) { + try(Session session = sessionFactory.openSession()) + { + ServerExperienceConfig xpConf = (ServerExperienceConfig) session.createQuery("FROM ServerExperienceConfig WHERE serverID = :serverID") + .setParameter("serverID", serverID) + .uniqueResult(); + + if (xpConf == null) { + xpConf = new ServerExperienceConfig(serverID); + saveServerExperienceConfig(xpConf); + } + return xpConf; + } + } + + private List getRolesForLevel(long id, long level) { + try(Session session = sessionFactory.openSession()) + { + @SuppressWarnings("unchecked") + List roles = (List) session.createQuery("FROM LevelUpRole WHERE serverID = :serverID AND level <= :level") + .setParameter("serverID", id) + .setParameter("level", level) + .list(); + return roles; + } + } + private void saveUserExperience(Experience userXP) { try(Session session = sessionFactory.openSession()) { @@ -44,6 +76,28 @@ private void saveUserExperience(Experience userXP) { } } + private void saveServerExperienceConfig(ServerExperienceConfig config) { + try(Session session = sessionFactory.openSession()) + { + session.getTransaction().begin(); + session.saveOrUpdate(config); + session.getTransaction().commit(); + } + } + + + private void removeLevelUpRole(LevelUpRole role) { + try(Session session = sessionFactory.openSession()) + { + session.getTransaction().begin(); + session.createQuery("DELETE FROM LevelUpRole WHERE roleID = :roleID AND serverID = :serverID") + .setParameter("roleID", role.getRoleID()) + .setParameter("serverID", role.getServerID()) + .executeUpdate(); + session.getTransaction().commit(); + } + } + private long xpToNextLevel(long currentLevel, long currentXP) { long nextLevel = currentLevel + 1; return 5 * (nextLevel ^ 2) + (50 * nextLevel) + 100 - currentXP; @@ -53,12 +107,15 @@ private int generateExperience() { return new Random().nextInt(16); } - public void updateUserExperience(String userID, String guildID, String channelID) { + public void updateUserExperience(Member member, String guildID, String channelID) { Server server = serverService.getServer(guildID); - Experience userXP = getUserExperience(userID, server.getId()); + Experience userXP = getUserExperience(member.getId(), server.getId()); userXP.incrementMessageCount(); - if ((userXP.getLastMessageTime() + EXPERIENCE_DELAY) > Instant.now().getEpochSecond()) return; + if ((userXP.getLastMessageTime() + EXPERIENCE_DELAY) > Instant.now().getEpochSecond()) { + saveUserExperience(userXP); + return; + } long currentExperience = userXP.getCurrentExperience() + generateExperience(); long xpToNextLevel = xpToNextLevel(userXP.getLevel(), currentExperience); @@ -66,13 +123,38 @@ public void updateUserExperience(String userID, String guildID, String channelID if (xpToNextLevel < 0) { userXP.setLevel(userXP.getLevel() + 1); userXP.setCurrentXP(-1 * xpToNextLevel); - } else { - userXP.setCurrentXP(currentExperience); - } + performLevelUpActions(userXP, server, member, channelID); + } else userXP.setCurrentXP(currentExperience); userXP.setLastMessageTime(Instant.now().getEpochSecond()); saveUserExperience(userXP); } + private void performLevelUpActions(Experience userXP, Server server, Member member, String channelID) { + ServerExperienceConfig config = getServerExperienceConfig(server.getId()); + + List roles = getRolesForLevel(server.getId(), userXP.getLevel()); + if (roles.isEmpty()) return; + + List memberRoles = member.getRoles(); + + for (LevelUpRole role : roles) { + Role roleToBeGiven = member.getGuild().getRoleById(role.getRoleID()); + if (roleToBeGiven == null) removeLevelUpRole(role); + else { + if (!memberRoles.contains(roleToBeGiven)) { + member.getGuild().addRoleToMember(member, roleToBeGiven).complete(); + if (config.levelUpMessagesEnabled()) sendLevelUpMessage(role, member, channelID); + } + } + } + } + + private void sendLevelUpMessage(LevelUpRole role, Member member, String channelID) { + member.getGuild().getTextChannelById(channelID).sendMessage(member.getAsMention() + " reached level " + + role.getLevel() + " and was given the role " + + role.getRoleID()).queue(); + } + }