Handle trivia edge cases without hanging

This commit is contained in:
Bea 2022-12-21 20:27:56 +01:00
parent 09c53c07a1
commit 26f1c880ea
2 changed files with 15 additions and 9 deletions

View File

@ -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<TriviaScore> triviaScores = new LinkedList<>(TriviaUtil.channelAndScores.get(channel.getId()));
triviaScores.sort(new TriviaScoreComparator());
LinkedList<TriviaScore> 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

View File

@ -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<String, List<String>> channelAndWhoResponded = new HashMap<>();
// first string is the channelId, the list contain all score records for that channel
public static HashMap<String, List<TriviaScore>> channelAndScores = new HashMap<>();
public static HashMap<String, LinkedList<TriviaScore>> 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<TriviaScore> scores = channelAndScores.get(channelId);
if(scores == null) scores = new ArrayList<>();
LinkedList<TriviaScore> scores = channelAndScores.get(channelId);
if(scores == null) scores = new LinkedList<>();
TriviaScore currentUserScore = null;
for(TriviaScore score : scores)
{