diff --git a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java index cefb5f79..058e4886 100644 --- a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java +++ b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java @@ -11,9 +11,8 @@ import org.jointheleague.features.examples.third_features.CatFactsApi; import org.jointheleague.features.examples.third_features.NewsApi; import org.jointheleague.features.examples.first_features.CurrentTime; -import org.jointheleague.features.examples.first_features.RandomNumber; import org.jointheleague.features.help_embed.HelpListener; -import org.jointheleague.features.student.first_feature.FeatureOne; +import org.jointheleague.features.student.first_feature.TriviaGame; public class DiscordBot { @@ -56,7 +55,7 @@ public void connect(boolean printInvite) throws InterruptedException { api.addEventListener(helpListener); //add features - addFeature(new FeatureOne(channelName)); + addFeature(new TriviaGame(channelName)); addFeature(new CurrentTime(channelName)); addFeature(new HighLowGame(channelName)); addFeature(new NewsApi(channelName)); diff --git a/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java b/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java deleted file mode 100644 index 8ab6ffc7..00000000 --- a/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.jointheleague.features.student.first_feature; - -import org.jointheleague.features.help_embed.plain_old_java_objects.help_embed.HelpEmbed; -import org.jointheleague.api_wrapper.ReceivedMessage; -import org.jointheleague.features.abstract_classes.Feature; -import org.jointheleague.features.templates.FeatureTemplate; - -import java.util.Random; - -public class FeatureOne extends FeatureTemplate { - public final String COMMAND = "!joke"; - - public FeatureOne(String channelName) { - super(channelName); - - //Create a help embed to describe feature when !help command is sent - helpEmbed = new HelpEmbed( - COMMAND, - "This command prints cheesy jokes." - ); - } -//IGNORE - @Override - public void handle(ReceivedMessage event) { - String messageContent = event.getMessageContent(); - if (messageContent.startsWith(COMMAND)) { - //respond to message here - Random ran = new Random(); - int ranInt = ran.nextInt(5); - - switch(ranInt){ - case 0:event.sendResponse("What’s the best thing about Switzerland? \n \n I don’t know, but the flag is a big plus."); - break; - case 1:event.sendResponse("I invented a new word! \n \n Plagiarism!"); - break; - case 2:event.sendResponse("Hear about the new restaurant called Karma? \n \n There’s no menu: You get what you deserve."); - break; - case 3:event.sendResponse("Did you hear about the claustrophobic astronaut? \n \n He just needed a little space."); - break; - case 4:event.sendResponse("Why don’t scientists trust atoms? \n \n Because they make up everything."); - break; - default:event.sendResponse("What’s the different between a cat and a comma? \n \n A cat has claws at the end of paws; A comma is a pause at the end of a clause. "); - break; - } - - } - } -} diff --git a/src/main/java/org/jointheleague/features/student/first_feature/TriviaGame.java b/src/main/java/org/jointheleague/features/student/first_feature/TriviaGame.java new file mode 100644 index 00000000..96fcaecb --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/TriviaGame.java @@ -0,0 +1,143 @@ +package org.jointheleague.features.student.first_feature; + +import org.jointheleague.features.examples.third_features.plain_old_java_objects.cat_facts_api.CatWrapper; +import org.jointheleague.features.help_embed.plain_old_java_objects.help_embed.HelpEmbed; +import org.jointheleague.api_wrapper.ReceivedMessage; +import org.jointheleague.features.templates.FeatureTemplate; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; + +import java.util.Random; + +public class TriviaGame extends FeatureTemplate { + public final String COMMAND = "!trivia"; + boolean questionActive = false; + + private WebClient webClient; + public static final String baseUrl = "https://opentdb.com/api.php?amount=10"; + + public TriviaGame(String channelName) { + super(channelName); + + //Create a help embed to describe feature when !help command is sent + helpEmbed = new HelpEmbed( + COMMAND, + "Play a trivia game, it'll be pretty chill." + ); + + this.webClient = WebClient + .builder() + .baseUrl(baseUrl) + .build(); + } +//IGNORE + @Override + public void handle(ReceivedMessage event) { + String messageContent = event.getMessageContent(); + // newline character might interfere, printed 1 but not 2. + messageContent = messageContent.trim(); + if (messageContent.startsWith(COMMAND)) { + System.out.println("1"); + if (messageContent.equals(COMMAND)){ + event.sendResponse("I will tell you a trivia question, and you will attempt to answer it using the command. Ex: \"!trivia George Washington\"\nDEBUG: " + getQuestion()); + boolean questionActive = true; + System.out.println("2"); + } + } + } + + public String getQuestion () { + + //Make the request, accepting the response as a plain old java object you created + Mono triviaWrapperMono = webClient.get() + .retrieve() + .bodyToMono(TriviaWrapper.class); + + //collect the response into a plain old java object + TriviaWrapper triviaWrapper = triviaWrapperMono.block(); + + //get the question from the response + //assert triviaWrapper != null; + + //send the message + return triviaWrapper.getData().get(0); + } + + public void setWebClient(WebClient webClient) { + this.webClient = webClient; + } +} + +// Chill Game here to use bits and pieces for logic. +/* +String messageContent = event.getMessageContent(); + if (messageContent.startsWith(COMMAND)) { + //respond to message here + if (messageContent.equals(COMMAND)){ + if (wordToGuess.isEmpty()){ + wordToGuess = Utilities.readRandomLineFromFile("src/main/java/org/jointheleague/features/student/first_feature/dictionary.txt"); + for (int i = 0; i < wordToGuess.length(); i++){ + currentDisplay += "-"; + } + event.sendResponse("There is a word you need to guess, guess a letter by doing the command, then a letter. Ex: \"!chillGame e\""); + System.out.println(wordToGuess); + } else { + event.sendResponse("You still haven't guessed the current word!"); + } + } else { + //check if the game has been started + if(wordToGuess.isEmpty()){ + //tell them to start the game first + event.sendResponse("Please start the game first using just the command"); + return; + } + + //parse the guess from the message + String guessMessage = messageContent.replaceAll(" ", "").replace(COMMAND, ""); + + //change the guess to a char + if (!guessMessage.equalsIgnoreCase(wordToGuess)){ + char guess = 0; + try{ + guess = guessMessage.charAt(0); + } + catch(IndexOutOfBoundsException e){ + //tell them to format their guess properly + event.sendResponse("Please format your guess like this: " + COMMAND + " e"); + return; + } + + if (wordToGuess.contains(guess + "") && !currentDisplay.contains(guess + "")){ + + char[] chars = currentDisplay.toCharArray(); + for (int i = 0; i < wordToGuess.length(); i++) { + if (wordToGuess.charAt(i) == guess){ + chars[i] = guess; + } + } + currentDisplay = new String(chars); + if (currentDisplay.contains("-")){ + event.sendResponse("Correct Guess!!\n" + currentDisplay); + } else { + event.sendResponse("Correct! The word I picked was " + wordToGuess); + wordToGuess = ""; + currentDisplay = ""; + } + + } else { + event.sendResponse("Incorrect Guess!!\n" + currentDisplay); + } + + } else { + //they got it correct + event.sendResponse("Correct! The word I picked was " + wordToGuess); + //set wordToGuess back to empty string + wordToGuess = ""; + currentDisplay = ""; + } + + + } + } + } + */ diff --git a/src/main/java/org/jointheleague/features/student/first_feature/TriviaWrapper.java b/src/main/java/org/jointheleague/features/student/first_feature/TriviaWrapper.java new file mode 100644 index 00000000..e8b7fdb5 --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/TriviaWrapper.java @@ -0,0 +1,11 @@ +package org.jointheleague.features.student.first_feature; + +import java.util.List; + +public class TriviaWrapper { + private List data = null; + + public List getData() { + return data; + } +} diff --git a/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java b/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java index 8c95c657..248894a1 100644 --- a/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java +++ b/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java @@ -21,7 +21,7 @@ public class FeatureOneTest { private final String testChannelName = "test"; - private final FeatureOne featureOne = new FeatureOne(testChannelName); + private final TriviaGame featureOne = new TriviaGame(testChannelName); private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final PrintStream originalOut = System.out;