Skip to content

Commit

Permalink
feat(service): Finished level up roles and experience system, commands
Browse files Browse the repository at this point in the history
are still needed and output formatting is not done (#87)
  • Loading branch information
Seb committed Mar 29, 2022
1 parent c23ced4 commit 0d1cb16
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 9 deletions.
12 changes: 12 additions & 0 deletions src/main/java/de/voidtech/gerald/entities/LevelUpRole.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
98 changes: 90 additions & 8 deletions src/main/java/de/voidtech/gerald/service/ExperienceService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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())
Expand All @@ -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<LevelUpRole> getRolesForLevel(long id, long level) {
try(Session session = sessionFactory.openSession())
{
@SuppressWarnings("unchecked")
List<LevelUpRole> roles = (List<LevelUpRole>) 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())
{
Expand All @@ -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;
Expand All @@ -53,26 +107,54 @@ 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);

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<LevelUpRole> roles = getRolesForLevel(server.getId(), userXP.getLevel());
if (roles.isEmpty()) return;

List<Role> 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();
}

}

0 comments on commit 0d1cb16

Please sign in to comment.