diff --git a/src/main/java/wtf/beatrice/hidekobot/Cache.java b/src/main/java/wtf/beatrice/hidekobot/Cache.java index 97016b0..c43b87b 100644 --- a/src/main/java/wtf/beatrice/hidekobot/Cache.java +++ b/src/main/java/wtf/beatrice/hidekobot/Cache.java @@ -14,7 +14,10 @@ import wtf.beatrice.hidekobot.util.Logger; import java.awt.*; import java.lang.reflect.Field; import java.time.LocalDateTime; +import java.util.HashMap; import java.util.Random; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; public class Cache { @@ -28,6 +31,12 @@ public class Cache // the seed is updated periodically. private static final Random randomInstance = new Random(); + // map to store results of "love calculator", to avoid people re-running the same command until + // they get what they wanted. + // i didn't think this was worthy of a whole database table with a runnable checking for expiration, + // and it will get cleared after a few minutes anyway, so RAM caching is more than good enough. + private static final HashMap loveCalculatorValues = new HashMap<>(); + private static PropertiesSource propertiesSource = null; private static ConfigurationSource configurationSource = null; private static DatabaseSource databaseSource = null; @@ -42,6 +51,9 @@ public class Cache // used to count e.g. uptime private static LocalDateTime startupTime = null; + // the scheduler that should always be used when running a scheduled task. + private final static ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); // todo: try-with-resources + private final static String execPath = System.getProperty("user.dir"); private static final String botName = "Hideko"; @@ -287,4 +299,31 @@ public class Cache */ public static String getBotPrefix() { return botPrefix; } + public static void cacheLoveCalculatorValue(String userId1, String userId2, int value) + { + String merged = userId1 + "|" + userId2; + loveCalculatorValues.put(merged, value); + } + + @Nullable + public static Integer getLoveCalculatorValue(String userId1, String userId2) + { + String merged1 = userId1 + "|" + userId2; + String merged2 = userId2 + "|" + userId1; + Integer value = null; + value = loveCalculatorValues.get(merged1); + if(value == null) value = loveCalculatorValues.get(merged2); + return value; + } + + public static void removeLoveCalculatorValue(String userId1, String userId2) + { + loveCalculatorValues.remove(userId1 + "|" + userId2); + loveCalculatorValues.remove(userId2 + "|" + userId1); + } + + public static ScheduledExecutorService getScheduler() { + return scheduler; + } + } diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index a7c1b48..b4ea2eb 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -178,7 +178,7 @@ public class HidekoBot } // start scheduled runnables - ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); // todo: try-with-resources + ScheduledExecutorService scheduler = Cache.getScheduler(); ExpiredMessageTask expiredMessageTask = new ExpiredMessageTask(); scheduler.scheduleAtFixedRate(expiredMessageTask, 5, 5, TimeUnit.SECONDS); //every 5 seconds HeartBeatTask heartBeatTask = new HeartBeatTask(); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/LoveCalculator.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/LoveCalculator.java index 72a0cca..b75d750 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/base/LoveCalculator.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/LoveCalculator.java @@ -6,11 +6,23 @@ import net.dv8tion.jda.api.entities.User; import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.util.RandomUtil; +import java.util.concurrent.TimeUnit; + public class LoveCalculator { - public static MessageEmbed buildEmbed(User author, User user1, User user2) + public static MessageEmbed buildEmbedAndCacheResult(User author, User user1, User user2) { - int loveAmount = RandomUtil.getRandomNumber(0, 100); + String userId1 = user1.getId(); + String userId2 = user2.getId(); + + Integer loveAmount = Cache.getLoveCalculatorValue(userId1, userId2); + if(loveAmount == null) + { + loveAmount = RandomUtil.getRandomNumber(0, 100); + Cache.cacheLoveCalculatorValue(userId1, userId2, loveAmount); + Cache.getScheduler().schedule(() -> + Cache.removeLoveCalculatorValue(userId1, userId2), 10, TimeUnit.MINUTES); + } String formattedAmount = loveAmount + "%"; if(loveAmount <= 30) formattedAmount += "... \uD83D\uDE22"; diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/LoveCalculatorCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/LoveCalculatorCommand.java index b99fafd..de2a09b 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/LoveCalculatorCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/LoveCalculatorCommand.java @@ -1,6 +1,5 @@ package wtf.beatrice.hidekobot.commands.message; -import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.IMentionable; import net.dv8tion.jda.api.entities.Mentions; @@ -9,12 +8,10 @@ import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.HidekoBot; import wtf.beatrice.hidekobot.commands.base.LoveCalculator; import wtf.beatrice.hidekobot.objects.commands.CommandCategory; import wtf.beatrice.hidekobot.objects.commands.MessageCommand; -import wtf.beatrice.hidekobot.util.RandomUtil; import java.util.Arrays; import java.util.LinkedList; @@ -88,7 +85,7 @@ public class LoveCalculatorCommand implements MessageCommand user2 = HidekoBot.getAPI().retrieveUserById(mentionedUserId).complete(); } - MessageEmbed embed = LoveCalculator.buildEmbed(event.getAuthor(), user1, user2); + MessageEmbed embed = LoveCalculator.buildEmbedAndCacheResult(event.getAuthor(), user1, user2); event.getChannel().sendMessageEmbeds(embed).queue(); } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/LoveCalculatorCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/LoveCalculatorCommand.java index e5df64a..9152145 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/LoveCalculatorCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/LoveCalculatorCommand.java @@ -55,7 +55,7 @@ public class LoveCalculatorCommand extends SlashCommandImpl secondUser = event.getUser(); } - MessageEmbed embed = LoveCalculator.buildEmbed(event.getUser(), firstUser, secondUser); + MessageEmbed embed = LoveCalculator.buildEmbedAndCacheResult(event.getUser(), firstUser, secondUser); event.replyEmbeds(embed).queue(); } }