trivia add command

This commit is contained in:
nora 2021-01-21 21:23:26 +01:00
parent 080c79b34b
commit dac3b78f1c
11 changed files with 315 additions and 86 deletions

View file

@ -1,5 +1,5 @@
package com.github.nilstrieb.cofig;
public class Secrets {
public static final String TOKEN = "invite xp bot here: https://discord.com/api/oauth2/authorize?client_id=706935674800177193&permissions=8&scope=bot";
public static final String TOKEN = "ODAxMDE1MjU0MDIzNzk4ODI1.YAahlg.l8wbPw461HNWNr9D3ndoiIQeRlY";
}

View file

@ -4,7 +4,7 @@ import com.github.nilstrieb.cofig.Config;
import com.github.nilstrieb.commands.handler.Command;
import com.github.nilstrieb.sections.ChannelMessageEventManager;
import com.github.nilstrieb.sections.Section;
import com.github.nilstrieb.util.trivia.Arc;
import com.github.nilstrieb.util.ConsoleColors;
import com.github.nilstrieb.util.trivia.TriviaQuestion;
import com.github.nilstrieb.util.trivia.TriviaQuestionData;
import net.dv8tion.jda.api.EmbedBuilder;
@ -29,29 +29,41 @@ public class TriviaCommand extends Command {
@Override
public void called(MessageReceivedEvent event, String args) {
int arc = 0;
try {
arc = Integer.parseInt(args);
} catch (NumberFormatException ignored) {
}
TriviaQuestion question = TriviaQuestionData.getQuestion(arc);
StringBuilder answers = new StringBuilder();
for (int i = 0; i < question.getAnswers().length; i++) {
answers.append(i).append(". ").append(question.getAnswers()[i]).append("\n");
}
EmbedBuilder builder = Config.getDefaultEmbed(event)
.addField(question.getQuestion(), answers.toString(), false);
if (args.equals("dump") && event.getAuthor().getIdLong() == Config.NILS_ID) {
TriviaQuestionData.dump();
reply(event, "dumped");
} else if (args.startsWith("add")) {
reply(event, "Enter the Question");
new AddSection(event.getTextChannel().getIdLong(), event.getAuthor().getIdLong());
} else {
int arc = 0;
try {
arc = Integer.parseInt(args);
} catch (NumberFormatException ignored) {
}
reply(event, builder.build());
new TriviaSection(event.getTextChannel().getIdLong(), event.getAuthor().getIdLong(), question);
TriviaQuestion question = TriviaQuestionData.getQuestion(arc);
StringBuilder answers = new StringBuilder();
for (int i = 0; i < question.getAnswers().length; i++) {
answers.append(i).append(". ").append(question.getAnswers()[i]).append("\n");
}
EmbedBuilder builder = Config.getDefaultEmbed(event)
.addField(question.getQuestion(), answers.toString(), false);
reply(event, builder.build());
new TriviaSection(event.getTextChannel().getIdLong(), event.getAuthor().getIdLong(), question);
}
}
///
/// Trivia Section class
///
private static class TriviaSection extends Section {
private final TriviaQuestion question;
public TriviaSection(long textChannelID, long userID, TriviaQuestion question) {
private TriviaSection(long textChannelID, long userID, TriviaQuestion question) {
super(textChannelID, userID);
this.question = question;
}
@ -80,11 +92,47 @@ public class TriviaCommand extends Command {
.setTitle(answer)
.setThumbnail(null)
.addField("Correct answer", correctAnswer, false);
if (question.getArc() == Arc.EXAM) {
if (question.getArc() == TriviaQuestion.EXAM) {
builder.setFooter("Tip: Use " + Config.PREFIX + "help trivia for more questions.");
}
reply(event, builder.build());
ChannelMessageEventManager.removeListener(this);
dispose();
}
}
///
/// Add question section class
///
private static class AddSection extends Section {
private int status = 0;
private static final String[] messages = {"Enter all answers seperated by a ;", "Enter the correct answer index (starting at 0)",
"Enter the arc this question belongs to as a number (see " + Config.PREFIX + "help trivia for more info)"};
private String[] answers = new String[4];
private AddSection(long textChannelID, long userID) {
super(textChannelID, userID);
}
@Override
public void messageReceived(MessageReceivedEvent event) {
System.out.println(ConsoleColors.BLUE_BOLD + "[TriviaCommand.AddSection 121] Received Next Message: "
+ event.getMessage().getContentRaw() + " status: " + status + ConsoleColors.RESET);
answers[status] = event.getMessage().getContentRaw();
if (status >= 3) {
try {
TriviaQuestionData.addNew(new TriviaQuestion(answers));
reply(event, "Question successfully added for approval");
} catch (NumberFormatException e) {
reply(event, "Error: " + e.getMessage());
}
dispose();
} else {
reply(event, messages[status]);
}
status++;
}
}
}

View file

@ -1,13 +1,8 @@
package com.github.nilstrieb.commands.handler;
import com.github.nilstrieb.cofig.Config;
import com.github.nilstrieb.util.MultiPageEmbed;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import java.util.Timer;
import java.util.TimerTask;
public abstract class Command extends MessageSender{
private final String name;
private final String description;

View file

@ -7,9 +7,9 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import java.util.*;
public class ChannelMessageEventManager {
private static HashMap<Long, List<ChannelListener>> listeners = new HashMap<>();
private static List<ChannelListener> removeBuffer = new ArrayList<>();
private static Set<Long> removedChannels = new HashSet<>();
private static final HashMap<Long, List<ChannelListener>> listeners = new HashMap<>();
private static final List<ChannelListener> removeBuffer = new ArrayList<>();
private static final Set<Long> removedChannels = new HashSet<>();
public static void addListener(ChannelListener listener, long channel) {
if (!listeners.containsKey(channel)) {

View file

@ -18,6 +18,10 @@ public abstract class Section extends MessageSender implements ChannelListener{
this.userID = 0;
}
protected void dispose(){
ChannelMessageEventManager.removeListener(this);
}
@Override
public long getUserID() {
return userID;

View file

@ -1,15 +0,0 @@
package com.github.nilstrieb.util.trivia;
public enum Arc {
EXAM(0), ZOLDYCK_FAMILY(1), HEAVENS_ARENA(2), YORKNEW_CITY(3),
GREED_ISLAND(4), CHIMERA_ANT(5), ELECTION(6);
private final int number;
Arc(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
}

View file

@ -1,18 +1,36 @@
package com.github.nilstrieb.util.trivia;
public class TriviaQuestion {
private String question;
private String[] answers;
private int correctAnswer;
private Arc arc;
import java.util.Arrays;
public TriviaQuestion(String question, int correctAnswer, Arc arc, String ... answers) {
public class TriviaQuestion {
public static final int EXAM = 0;
public static final int ZOLDYCK_FAMILY = 1;
public static final int HEAVENS_ARENA = 2;
public static final int YORKNEW_CITY = 3;
public static final int GREED_ISLAND = 4;
public static final int CHIMERA_ANT = 5;
public static final int ELECTION = 6;
private final String question;
private final String[] answers;
private final int correctAnswer;
private final int arc;
public TriviaQuestion(String question, int correctAnswer, int arc, String ... answers) {
this.question = question;
this.answers = answers;
this.arc = arc;
this.correctAnswer = correctAnswer;
}
public TriviaQuestion(String[] answers) {
this.question = answers[0];
this.answers = answers[1].split(" *; *");
this.correctAnswer = Integer.parseInt(answers[2]);
this.arc = Integer.parseInt(answers[3]);
}
public String getQuestion() {
return question;
}
@ -25,7 +43,17 @@ public class TriviaQuestion {
return correctAnswer;
}
public Arc getArc() {
public int getArc() {
return arc;
}
@Override
public String toString() {
return "TriviaQuestion{" +
"question='" + question + '\'' +
", answers=" + Arrays.toString(answers) +
", correctAnswer=" + correctAnswer +
", arc=" + arc +
'}';
}
}

View file

@ -1,21 +1,16 @@
package com.github.nilstrieb.util.trivia;
import com.github.nilstrieb.util.ConsoleColors;
import com.google.gson.Gson;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/*
------------------------------------------------------------------------------
| |
| HXH SPOILER WARING |
| |
| CONTINUE READING AT YOUR ON RISK |
| |
------------------------------------------------------------------------------
*/
public class TriviaQuestionData {
static List<List<TriviaQuestion>> questions = new ArrayList<>();
private static final List<TriviaQuestion> newQuestions = new ArrayList<>();
private static final Random random = new Random();
static {
@ -26,38 +21,72 @@ public class TriviaQuestionData {
questions.add(new ArrayList<>());
questions.add(new ArrayList<>());
questions.add(new ArrayList<>());
add(new TriviaQuestion("What's the name of Gons aunt?", 1, Arc.EXAM,
"Kite", "Mito", "Ging", "Kurapika"));
add(new TriviaQuestion("What is Bisky's true form?", 1, Arc.GREED_ISLAND,
"Boy", "Big Woman", "Little Girl"));
add(new TriviaQuestion("Why does Leorio want money?", 0, Arc.EXAM,
"To become a doctor", "To send it to e-girls", "So that others don't get it", "To buy large quantities of drugs"));
add(new TriviaQuestion("Where did Meruem die?", 2, Arc.CHIMERA_ANT,
"Volcano", "In the desert with Netero", "In the basement of the palace", "In the womb of the queen"));
add(new TriviaQuestion("Who is Alluka?", 3, Arc.ELECTION,
"Gon's sister", "A Zodiac", "The 'dark' side of Nanika", "Killua's sister"));
add(new TriviaQuestion("How did Gon die on Greed Island?", 0, Arc.GREED_ISLAND,
"He didn't die on Greed Island", "Hisoka killed him with a card to the neck", "He lost too muich blood when Genthru blew his arm of", "He killed himself after Killuas death"));
add(new TriviaQuestion("How many doors are there at the Testing Gate of the Zoldyck Family?", 0, Arc.ZOLDYCK_FAMILY,
"7", "5", "12", "8"));
add(new TriviaQuestion("What was Gon's aim after Killua left him alone playing with the Chairman Netero?", 1, Arc.EXAM,
"Get the ball", "Make Netero use his right hand", "Make Netero use his left leg", "Let Netero drop the ball"));
add(new TriviaQuestion("What did Tonpa mix into the drink?", 2, Arc.EXAM,
"Sleeping pills", "Headache tabletsschla", "Laxative", "Rat poison"));
loadJSON();
}
private static void add(TriviaQuestion triviaQuestion) {
questions.get(triviaQuestion.getArc().getNumber()).add(triviaQuestion);
private static void loadJSON() {
StringBuilder json = new StringBuilder();
try (BufferedReader bufferedReader = new BufferedReader(
new FileReader("trivia_questions.json"))) {
String line = bufferedReader.readLine();
while (line != null) {
json.append(line);
line = bufferedReader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
Gson gson = new Gson();
TriviaQuestion[] array = gson.fromJson(json.toString(), TriviaQuestion[].class);
for (TriviaQuestion triviaQuestion : array) {
questions.get(triviaQuestion.getArc()).add(triviaQuestion);
}
}
private static void saveJSON(String path, TriviaQuestion[] array) {
Gson gson = new Gson();
try (BufferedWriter bufferedWriter = new BufferedWriter(
new FileWriter(path))) {
String json = gson.toJson(array);
bufferedWriter.write(json);
bufferedWriter.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void saveJSONFromAll(String path) {
TriviaQuestion[] questionsArray = new TriviaQuestion[getTotalQuestions()];
int i = 0;
for (List<TriviaQuestion> questionList : questions) {
for (TriviaQuestion triviaQuestion : questionList) {
questionsArray[i] = triviaQuestion;
i++;
}
}
saveJSON(path, questionsArray);
}
public static void add(TriviaQuestion triviaQuestion) {
questions.get(triviaQuestion.getArc()).add(triviaQuestion);
saveJSONFromAll("trivia_questions.json");
}
public static void addNew(TriviaQuestion triviaQuestion) {
newQuestions.add(triviaQuestion);
TriviaQuestion[] array = new TriviaQuestion[newQuestions.size()];
saveJSON("new_trivia_questions.json", newQuestions.toArray(array));
}
public static TriviaQuestion getQuestion(int toArc) {
int totalQuestions = 0;
for (int i = 0; i <= toArc; i++) {
totalQuestions += questions.get(i).size();
}
int totalQuestions = getTotalQuestions(toArc);
int randomQuestion = random.nextInt(totalQuestions);
@ -71,4 +100,28 @@ public class TriviaQuestionData {
throw new IndexOutOfBoundsException("No Question available for arc " + toArc);
}
private static int getTotalQuestions(int toArc) {
int totalQuestions = 0;
for (int i = 0; i <= toArc; i++) {
totalQuestions += questions.get(i).size();
}
return totalQuestions;
}
private static int getTotalQuestions() {
return getTotalQuestions(questions.size() - 1);
}
public static void dump() {
int i = 0;
for (List<TriviaQuestion> question : questions) {
System.out.println(ConsoleColors.BLUE_BACKGROUND + ConsoleColors.YELLOW +
"QUESTIONS ARC: " + i + ConsoleColors.RESET);
for (TriviaQuestion triviaQuestion : question) {
System.out.println(ConsoleColors.PURPLE_BOLD_BRIGHT + triviaQuestion + ConsoleColors.RESET);
}
i++;
}
}
}