Skip to content

Commit

Permalink
GH-133 Fix working of voice experience system. (#136)
Browse files Browse the repository at this point in the history
* Fix working of voice experience system.

* Update src/main/java/com/eternalcode/discordapp/leveling/experience/voice/ExperienceLeaveVoiceController.java

Co-authored-by: DMK <[email protected]>

---------

Co-authored-by: Krzysztof Haller <[email protected]>
Co-authored-by: DMK <[email protected]>
  • Loading branch information
3 people authored Nov 6, 2023
1 parent 81277d7 commit 8f509b9
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 98 deletions.
15 changes: 9 additions & 6 deletions src/main/java/com/eternalcode/discordapp/DiscordApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 -> {
Expand Down Expand Up @@ -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),

Expand Down

This file was deleted.

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

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

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Long, Instant> usersOnVoiceChannel = new HashMap<>();
public Map<Long, Instant> usersOnVoiceChannel = new HashMap<>();

@Override
public Resource resource(File folder) {
Expand Down

0 comments on commit 8f509b9

Please sign in to comment.