Make trivia functional
This commit is contained in:
parent
eb93362d16
commit
48fdb32e15
@ -5,6 +5,7 @@ import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
|||||||
import wtf.beatrice.hidekobot.commands.base.CoinFlip;
|
import wtf.beatrice.hidekobot.commands.base.CoinFlip;
|
||||||
import wtf.beatrice.hidekobot.commands.base.UrbanDictionary;
|
import wtf.beatrice.hidekobot.commands.base.UrbanDictionary;
|
||||||
import wtf.beatrice.hidekobot.util.CommandUtil;
|
import wtf.beatrice.hidekobot.util.CommandUtil;
|
||||||
|
import wtf.beatrice.hidekobot.util.TriviaUtil;
|
||||||
|
|
||||||
public class ButtonInteractionListener extends ListenerAdapter
|
public class ButtonInteractionListener extends ListenerAdapter
|
||||||
{
|
{
|
||||||
@ -25,6 +26,11 @@ public class ButtonInteractionListener extends ListenerAdapter
|
|||||||
case "urban_nextpage" -> UrbanDictionary.changePage(event, UrbanDictionary.ChangeType.NEXT);
|
case "urban_nextpage" -> UrbanDictionary.changePage(event, UrbanDictionary.ChangeType.NEXT);
|
||||||
case "urban_previouspage" -> UrbanDictionary.changePage(event, UrbanDictionary.ChangeType.PREVIOUS);
|
case "urban_previouspage" -> UrbanDictionary.changePage(event, UrbanDictionary.ChangeType.PREVIOUS);
|
||||||
|
|
||||||
|
// trivia
|
||||||
|
case "trivia_correct" -> TriviaUtil.handleAnswer(event, TriviaUtil.AnswerType.CORRECT);
|
||||||
|
case "trivia_wrong_1", "trivia_wrong_2", "trivia_wrong_3" ->
|
||||||
|
TriviaUtil.handleAnswer(event, TriviaUtil.AnswerType.WRONG);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,6 @@ import java.util.concurrent.ScheduledFuture;
|
|||||||
|
|
||||||
public class TriviaTask implements Runnable
|
public class TriviaTask implements Runnable
|
||||||
{
|
{
|
||||||
|
|
||||||
private final User author;
|
private final User author;
|
||||||
private final MessageChannel channel;
|
private final MessageChannel channel;
|
||||||
|
|
||||||
@ -55,14 +54,20 @@ public class TriviaTask implements Runnable
|
|||||||
// todo: we shouldn't use this method, since it messes with the database...
|
// todo: we shouldn't use this method, since it messes with the database...
|
||||||
CommandUtil.disableExpired(previousMessage.getId());
|
CommandUtil.disableExpired(previousMessage.getId());
|
||||||
String previousCorrectAnswer = questions.get(iteration-1).correctAnswer();
|
String previousCorrectAnswer = questions.get(iteration-1).correctAnswer();
|
||||||
previousMessage.reply("The correct answer was: **" + previousCorrectAnswer + "**!").queue();
|
|
||||||
|
// we need this to be thread-locking to avoid getting out of sync with the rest of the trivia features
|
||||||
|
previousMessage.reply("The correct answer was: **" + previousCorrectAnswer + "**!").complete();
|
||||||
// todo: maybe also add who replied correctly as a list
|
// todo: maybe also add who replied correctly as a list
|
||||||
|
|
||||||
|
// clean the list of people who answered, so they can answer again for the new question
|
||||||
|
TriviaUtil.channelAndWhoResponded.put(previousMessage.getChannel().getId(), new ArrayList<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
if(iteration >= questions.size())
|
if(iteration >= questions.size())
|
||||||
{
|
{
|
||||||
// todo: nicer-looking embed with stats
|
// todo: nicer-looking embed with stats
|
||||||
channel.sendMessage("Trivia session is over!").queue();
|
// we need this to be thread-locking to avoid getting out of sync with the rest of the trivia features
|
||||||
|
channel.sendMessage("Trivia session is over!").complete();
|
||||||
|
|
||||||
TriviaUtil.channelsRunningTrivia.remove(channel.getId());
|
TriviaUtil.channelsRunningTrivia.remove(channel.getId());
|
||||||
future.cancel(false);
|
future.cancel(false);
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package wtf.beatrice.hidekobot.util;
|
package wtf.beatrice.hidekobot.util;
|
||||||
|
|
||||||
|
import net.dv8tion.jda.api.entities.User;
|
||||||
|
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
||||||
|
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
|
||||||
import org.apache.commons.text.StringEscapeUtils;
|
import org.apache.commons.text.StringEscapeUtils;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.json.JSONString;
|
import wtf.beatrice.hidekobot.Cache;
|
||||||
import org.jsoup.Jsoup;
|
|
||||||
import wtf.beatrice.hidekobot.objects.TriviaQuestion;
|
import wtf.beatrice.hidekobot.objects.TriviaQuestion;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
@ -13,7 +15,9 @@ import java.io.InputStreamReader;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class TriviaUtil
|
public class TriviaUtil
|
||||||
{
|
{
|
||||||
@ -21,6 +25,9 @@ public class TriviaUtil
|
|||||||
|
|
||||||
public static List<String> channelsRunningTrivia = new ArrayList<>();
|
public static List<String> channelsRunningTrivia = new ArrayList<>();
|
||||||
|
|
||||||
|
// first string is the channelId, the list contain all users who responded there
|
||||||
|
public static HashMap<String, List<String>> channelAndWhoResponded = new HashMap<>();
|
||||||
|
|
||||||
public static JSONObject fetchTrivia()
|
public static JSONObject fetchTrivia()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@ -71,4 +78,48 @@ public class TriviaUtil
|
|||||||
return questions;
|
return questions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void handleAnswer(ButtonInteractionEvent event, AnswerType answerType)
|
||||||
|
{
|
||||||
|
if(trackResponse(event.getUser(), event.getChannel()))
|
||||||
|
{
|
||||||
|
if(answerType.equals(AnswerType.CORRECT))
|
||||||
|
{
|
||||||
|
event.reply(event.getUser().getAsMention() + " got it right!").queue();
|
||||||
|
} else {
|
||||||
|
event.reply(event.getUser().getAsMention() + ", that's not the right answer!").queue();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
event.reply(event.getUser().getAsMention() + ", you can't answer twice!")
|
||||||
|
.queue(message ->
|
||||||
|
Cache.getTaskScheduler().schedule(() ->
|
||||||
|
message.deleteOriginal().queue(), 5, TimeUnit.SECONDS));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean trackResponse(User user, MessageChannel channel)
|
||||||
|
{
|
||||||
|
String userId = user.getId();
|
||||||
|
String channelId = channel.getId();
|
||||||
|
|
||||||
|
List<String> responders = channelAndWhoResponded.get(channelId);
|
||||||
|
|
||||||
|
if(responders == null)
|
||||||
|
{
|
||||||
|
responders = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(responders.isEmpty() || !responders.contains(userId))
|
||||||
|
{
|
||||||
|
responders.add(userId);
|
||||||
|
channelAndWhoResponded.put(channelId, responders);
|
||||||
|
return true; // response was successfully tracked
|
||||||
|
} else {
|
||||||
|
return false; // response wasn't tracked because there already was an entry
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum AnswerType {
|
||||||
|
CORRECT, WRONG
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user