From 8f509b9a01ca9f28af132edf8fa96f5c61ffedec Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Mon, 6 Nov 2023 18:51:17 +0100 Subject: [PATCH] GH-133 Fix working of voice experience system. (#136) * Fix working of voice experience system. * Update src/main/java/com/eternalcode/discordapp/leveling/experience/voice/ExperienceLeaveVoiceController.java Co-authored-by: DMK <81445555+imDMK@users.noreply.github.com> --------- Co-authored-by: Krzysztof Haller Co-authored-by: DMK <81445555+imDMK@users.noreply.github.com> --- .../eternalcode/discordapp/DiscordApp.java | 15 ++-- .../listener/ExperienceVoiceListener.java | 89 ------------------- .../voice/ExperienceJoinVoiceController.java | 35 ++++++++ .../voice/ExperienceLeaveVoiceController.java | 58 ++++++++++++ .../ExperienceVoiceActivityData.java} | 7 +- 5 files changed, 106 insertions(+), 98 deletions(-) delete mode 100644 src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java create mode 100644 src/main/java/com/eternalcode/discordapp/leveling/experience/voice/ExperienceJoinVoiceController.java create mode 100644 src/main/java/com/eternalcode/discordapp/leveling/experience/voice/ExperienceLeaveVoiceController.java rename src/main/java/com/eternalcode/discordapp/leveling/experience/{data/UsersVoiceActivityData.java => voice/ExperienceVoiceActivityData.java} (60%) diff --git a/src/main/java/com/eternalcode/discordapp/DiscordApp.java b/src/main/java/com/eternalcode/discordapp/DiscordApp.java index a9270cd3..61ebef55 100644 --- a/src/main/java/com/eternalcode/discordapp/DiscordApp.java +++ b/src/main/java/com/eternalcode/discordapp/DiscordApp.java @@ -27,10 +27,11 @@ import com.eternalcode.discordapp.leveling.experience.ExperienceChangeEvent; import com.eternalcode.discordapp.leveling.experience.ExperienceConfig; import com.eternalcode.discordapp.leveling.experience.ExperienceService; -import com.eternalcode.discordapp.leveling.experience.data.UsersVoiceActivityData; +import com.eternalcode.discordapp.leveling.experience.voice.ExperienceJoinVoiceController; +import com.eternalcode.discordapp.leveling.experience.voice.ExperienceLeaveVoiceController; +import com.eternalcode.discordapp.leveling.experience.voice.ExperienceVoiceActivityData; import com.eternalcode.discordapp.leveling.experience.listener.ExperienceMessageListener; import com.eternalcode.discordapp.leveling.experience.listener.ExperienceReactionListener; -import com.eternalcode.discordapp.leveling.experience.listener.ExperienceVoiceListener; import com.eternalcode.discordapp.leveling.games.CodeGameAnswerController; import com.eternalcode.discordapp.leveling.games.CodeGameConfiguration; import com.eternalcode.discordapp.leveling.games.CodeImageGameData; @@ -82,11 +83,10 @@ public static void main(String... args) throws InterruptedException { CodeGameConfiguration codeGameConfiguration = configManager.load(new CodeGameConfiguration()); ConfigManager data = new ConfigManager("data"); - UsersVoiceActivityData usersVoiceActivityData = data.load(new UsersVoiceActivityData()); + ExperienceVoiceActivityData experienceVoiceActivityData = data.load(new ExperienceVoiceActivityData()); CodeImageGameData codeImageGameData = data.load(new CodeImageGameData()); - usersVoiceActivityData.usersOnVoiceChannel.put(0L, Instant.now()); - data.save(usersVoiceActivityData); + data.save(experienceVoiceActivityData); if (!config.sentryDsn.isEmpty()) { Sentry.init(options -> { @@ -155,9 +155,12 @@ public static void main(String... args) throws InterruptedException { // Experience system new ExperienceMessageListener(experienceConfig, experienceService), - new ExperienceVoiceListener(experienceConfig, usersVoiceActivityData, data, experienceService), new ExperienceReactionListener(experienceConfig, experienceService), + // experience voice + new ExperienceLeaveVoiceController(experienceVoiceActivityData, data, experienceConfig, experienceService), + new ExperienceJoinVoiceController(experienceVoiceActivityData, data), + // Message filter new FilterMessageEmbedController(filterService), diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java deleted file mode 100644 index 75dde549..00000000 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.eternalcode.discordapp.leveling.experience.listener; - -import com.eternalcode.discordapp.config.ConfigManager; -import com.eternalcode.discordapp.database.DataAccessException; -import com.eternalcode.discordapp.leveling.experience.ExperienceConfig; -import com.eternalcode.discordapp.leveling.experience.ExperienceService; -import com.eternalcode.discordapp.leveling.experience.data.UsersVoiceActivityData; -import io.sentry.Sentry; -import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent; -import net.dv8tion.jda.api.hooks.ListenerAdapter; -import org.jetbrains.annotations.NotNull; - -import java.time.Duration; -import java.time.Instant; -import java.util.function.LongSupplier; - -public class ExperienceVoiceListener extends ListenerAdapter { - - private final ExperienceConfig experienceConfig; - - private final UsersVoiceActivityData usersVoiceActivityData; - private final ConfigManager configManager; - private final ExperienceService experienceService; - - public ExperienceVoiceListener(ExperienceConfig experienceConfig, UsersVoiceActivityData usersVoiceActivityData, ConfigManager configManager, ExperienceService experienceService) { - this.experienceConfig = experienceConfig; - this.usersVoiceActivityData = usersVoiceActivityData; - this.configManager = configManager; - this.experienceService = experienceService; - } - - @Override - public void onGuildVoiceUpdate(@NotNull GuildVoiceUpdateEvent event) { - try { - if (event.getMember().getUser().isBot()) { - return; - } - - this.leaveVoiceChannel(event); - this.joinVoiceChannel(event); - - this.configManager.save(this.usersVoiceActivityData); - } - catch (Exception exception) { - Sentry.captureException(exception); - throw new DataAccessException("Something went wrong while updating voice experience", exception); - } - } - - private void leaveVoiceChannel(GuildVoiceUpdateEvent event) { - if (event.getChannelLeft() == null) { - return; - } - - long userId = event.getMember().getIdLong(); - this.usersVoiceActivityData.usersOnVoiceChannel.remove(event.getMember().getIdLong()); - - LongSupplier voiceLeftChannelId = () -> event.getChannelLeft().getIdLong(); - this.experienceService.modifyPoints(userId, this.calculatePoints(event), true, voiceLeftChannelId).whenComplete((experience, throwable) -> { - if (throwable != null) { - throwable.printStackTrace(); - } - }); - } - - private void joinVoiceChannel(GuildVoiceUpdateEvent event) { - long userId = event.getMember().getIdLong(); - if (this.usersVoiceActivityData.usersOnVoiceChannel.containsKey(userId)) { - return; - } - - if (event.getChannelJoined() == null) { - return; - } - - this.usersVoiceActivityData.usersOnVoiceChannel.put(userId, Instant.now()); - } - - private double calculatePoints(GuildVoiceUpdateEvent event) { - long userId = event.getMember().getIdLong(); - - Instant joined = this.usersVoiceActivityData.usersOnVoiceChannel.get(userId); - long minutes = Duration.between(joined, Instant.now()).toMinutes(); - - double basePoints = this.experienceConfig.basePoints * this.experienceConfig.voiceExperience.multiplier; - return basePoints * minutes / this.experienceConfig.voiceExperience.howLongTimeSpendInVoiceChannel; - } - -} diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/voice/ExperienceJoinVoiceController.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/voice/ExperienceJoinVoiceController.java new file mode 100644 index 00000000..678b2072 --- /dev/null +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/voice/ExperienceJoinVoiceController.java @@ -0,0 +1,35 @@ +package com.eternalcode.discordapp.leveling.experience.voice; + +import com.eternalcode.discordapp.config.ConfigManager; +import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; + +import java.time.Instant; + +public class ExperienceJoinVoiceController extends ListenerAdapter { + + private final ExperienceVoiceActivityData experienceVoiceActivityData; + private final ConfigManager configManager; + + public ExperienceJoinVoiceController(ExperienceVoiceActivityData experienceVoiceActivityData, ConfigManager configManager) { + this.experienceVoiceActivityData = experienceVoiceActivityData; + this.configManager = configManager; + } + + @Override + public void onGuildVoiceUpdate(GuildVoiceUpdateEvent event) { + long userId = event.getMember().getIdLong(); + + if (this.experienceVoiceActivityData.usersOnVoiceChannel.containsKey(userId)) { + return; + } + + if (event.getChannelJoined() == null) { + return; + } + + this.experienceVoiceActivityData.usersOnVoiceChannel.put(userId, Instant.now()); + this.configManager.save(this.experienceVoiceActivityData); + } + +} diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/voice/ExperienceLeaveVoiceController.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/voice/ExperienceLeaveVoiceController.java new file mode 100644 index 00000000..d90f6feb --- /dev/null +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/voice/ExperienceLeaveVoiceController.java @@ -0,0 +1,58 @@ +package com.eternalcode.discordapp.leveling.experience.voice; + +import com.eternalcode.discordapp.config.ConfigManager; +import com.eternalcode.discordapp.leveling.experience.ExperienceConfig; +import com.eternalcode.discordapp.leveling.experience.ExperienceService; +import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; + +import java.time.Duration; +import java.time.Instant; +import java.util.function.LongSupplier; + +public class ExperienceLeaveVoiceController extends ListenerAdapter { + + private final ExperienceVoiceActivityData experienceVoiceActivityData; + private final ConfigManager configManager; + private final ExperienceConfig experienceConfig; + private final ExperienceService experienceService; + + public ExperienceLeaveVoiceController(ExperienceVoiceActivityData experienceVoiceActivityData, ConfigManager configManager, + ExperienceConfig experienceConfig, ExperienceService experienceService) { + this.experienceVoiceActivityData = experienceVoiceActivityData; + this.configManager = configManager; + this.experienceConfig = experienceConfig; + this.experienceService = experienceService; + } + + @Override + public void onGuildVoiceUpdate(GuildVoiceUpdateEvent event) { + if (event.getChannelLeft() == null) { + return; + } + + long userId = event.getMember().getIdLong(); + + LongSupplier voiceLeftChannelId = () -> event.getChannelLeft().getIdLong(); + this.experienceService.modifyPoints(userId, this.calculatePoints(event), true, voiceLeftChannelId).whenComplete((experience, throwable) -> { + if (throwable != null) { + throwable.printStackTrace(); + } + }); + + this.experienceVoiceActivityData.usersOnVoiceChannel.remove(userId); + this.configManager.save(this.experienceVoiceActivityData); + } + + + private double calculatePoints(GuildVoiceUpdateEvent event) { + long userId = event.getMember().getIdLong(); + + Instant joined = this.experienceVoiceActivityData.usersOnVoiceChannel.get(userId); + long minutes = Duration.between(joined, Instant.now()).toMinutes(); + + double basePoints = this.experienceConfig.basePoints * this.experienceConfig.voiceExperience.multiplier; + return basePoints * minutes / this.experienceConfig.voiceExperience.howLongTimeSpendInVoiceChannel; + } + +} diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/data/UsersVoiceActivityData.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/voice/ExperienceVoiceActivityData.java similarity index 60% rename from src/main/java/com/eternalcode/discordapp/leveling/experience/data/UsersVoiceActivityData.java rename to src/main/java/com/eternalcode/discordapp/leveling/experience/voice/ExperienceVoiceActivityData.java index c0928161..5cf02910 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/data/UsersVoiceActivityData.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/voice/ExperienceVoiceActivityData.java @@ -1,4 +1,4 @@ -package com.eternalcode.discordapp.leveling.experience.data; +package com.eternalcode.discordapp.leveling.experience.voice; import com.eternalcode.discordapp.config.CdnConfig; import net.dzikoysk.cdn.source.Resource; @@ -7,10 +7,11 @@ import java.io.File; import java.time.Instant; import java.util.HashMap; +import java.util.Map; -public class UsersVoiceActivityData implements CdnConfig { +public class ExperienceVoiceActivityData implements CdnConfig { - public HashMap usersOnVoiceChannel = new HashMap<>(); + public Map usersOnVoiceChannel = new HashMap<>(); @Override public Resource resource(File folder) {