From 26f1c880ead28155a84c89ec4cd631d583ba0ba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Wed, 21 Dec 2022 20:27:56 +0100 Subject: [PATCH] Handle trivia edge cases without hanging --- .../hidekobot/runnables/TriviaTask.java | 17 +++++++++++------ .../wtf/beatrice/hidekobot/util/TriviaUtil.java | 7 ++++--- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/runnables/TriviaTask.java b/src/main/java/wtf/beatrice/hidekobot/runnables/TriviaTask.java index 156201f..7c0620f 100644 --- a/src/main/java/wtf/beatrice/hidekobot/runnables/TriviaTask.java +++ b/src/main/java/wtf/beatrice/hidekobot/runnables/TriviaTask.java @@ -51,6 +51,7 @@ public class TriviaTask implements Runnable @Override public void run() { + if(previousMessage != null) { // todo: we shouldn't use this method, since it messes with the database... look at coin reflip @@ -75,8 +76,9 @@ public class TriviaTask implements Runnable int topScore = 0; StringBuilder othersBuilder = new StringBuilder(); - LinkedList triviaScores = new LinkedList<>(TriviaUtil.channelAndScores.get(channel.getId())); - triviaScores.sort(new TriviaScoreComparator()); + LinkedList triviaScores = TriviaUtil.channelAndScores.get(channel.getId()); + if(triviaScores == null) triviaScores = new LinkedList<>(); + else triviaScores.sort(new TriviaScoreComparator()); int pos = 0; Integer previousScore = null; @@ -119,17 +121,19 @@ public class TriviaTask implements Runnable String winnersTitle = "\uD83D\uDCAB "; winnersTitle += winners.size() == 1 ? "Winner" : "Winners"; + String winnersString = winnersBuilder.toString(); + String othersString = othersBuilder.toString(); EmbedBuilder scoreboardBuilder = new EmbedBuilder(); scoreboardBuilder.setColor(Cache.getBotColor()); scoreboardBuilder.setTitle("\uD83C\uDF1F Trivia Scoreboard"); - scoreboardBuilder.addField(winnersTitle, winnersBuilder.toString(), false); - scoreboardBuilder.addField("☁️ Others", othersBuilder.toString(), false); + if(!winnersString.isEmpty()) scoreboardBuilder.addField(winnersTitle, winnersString, false); + else scoreboardBuilder.addField("\uD83D\uDE22 Sad Trivia", + "No one played \uD83D\uDE2D", false); + if(!othersString.isEmpty()) scoreboardBuilder.addField("☁️ Others", othersString, false); channel.sendMessage(scoreboardText).addEmbeds(scoreboardBuilder.build()).queue(); - - // remove all cached data TriviaUtil.channelsRunningTrivia.remove(channel.getId()); TriviaUtil.channelAndWhoResponded.remove(channel.getId()); @@ -185,6 +189,7 @@ public class TriviaTask implements Runnable .setActionRow(answerButtons) .complete(); + Cache.getDatabaseSource().trackRanCommandReply(previousMessage, author); // todo: ^ we should get rid of this tracking, since we don't need to know who started the trivia. // todo: however, for now, that's the only way to avoid a thread-locking scenario as some data is diff --git a/src/main/java/wtf/beatrice/hidekobot/util/TriviaUtil.java b/src/main/java/wtf/beatrice/hidekobot/util/TriviaUtil.java index bec479e..49fef41 100644 --- a/src/main/java/wtf/beatrice/hidekobot/util/TriviaUtil.java +++ b/src/main/java/wtf/beatrice/hidekobot/util/TriviaUtil.java @@ -22,6 +22,7 @@ import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -37,7 +38,7 @@ public class TriviaUtil public static HashMap> channelAndWhoResponded = new HashMap<>(); // first string is the channelId, the list contain all score records for that channel - public static HashMap> channelAndScores = new HashMap<>(); + public static HashMap> channelAndScores = new HashMap<>(); public static String getTriviaLink(int categoryId) {return triviaLink + categoryId; } public static String getCategoriesLink() {return categoriesLink; } @@ -116,8 +117,8 @@ public class TriviaUtil if(trackResponse(user, event.getChannel())) { - List scores = channelAndScores.get(channelId); - if(scores == null) scores = new ArrayList<>(); + LinkedList scores = channelAndScores.get(channelId); + if(scores == null) scores = new LinkedList<>(); TriviaScore currentUserScore = null; for(TriviaScore score : scores) {