diff --git a/.gitignore b/.gitignore index 19f55917..98ad63f6 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ src/main/java/org.jointheleague.Launcher.java .travis.yml run.sh appspec.yml +/.gradle/ +/build/ diff --git a/.gradle/7.3.3/checksums/checksums.lock b/.gradle/7.3.3/checksums/checksums.lock new file mode 100644 index 00000000..d3b400e9 Binary files /dev/null and b/.gradle/7.3.3/checksums/checksums.lock differ diff --git a/.gradle/7.3.3/checksums/md5-checksums.bin b/.gradle/7.3.3/checksums/md5-checksums.bin new file mode 100644 index 00000000..6db0e551 Binary files /dev/null and b/.gradle/7.3.3/checksums/md5-checksums.bin differ diff --git a/.gradle/7.3.3/checksums/sha1-checksums.bin b/.gradle/7.3.3/checksums/sha1-checksums.bin new file mode 100644 index 00000000..c401ea76 Binary files /dev/null and b/.gradle/7.3.3/checksums/sha1-checksums.bin differ diff --git a/.gradle/7.3.3/dependencies-accessors/dependencies-accessors.lock b/.gradle/7.3.3/dependencies-accessors/dependencies-accessors.lock new file mode 100644 index 00000000..164cfeae Binary files /dev/null and b/.gradle/7.3.3/dependencies-accessors/dependencies-accessors.lock differ diff --git a/.gradle/7.3.3/dependencies-accessors/gc.properties b/.gradle/7.3.3/dependencies-accessors/gc.properties new file mode 100644 index 00000000..e69de29b diff --git a/.gradle/7.3.3/fileChanges/last-build.bin b/.gradle/7.3.3/fileChanges/last-build.bin new file mode 100644 index 00000000..f76dd238 Binary files /dev/null and b/.gradle/7.3.3/fileChanges/last-build.bin differ diff --git a/.gradle/7.3.3/fileHashes/fileHashes.lock b/.gradle/7.3.3/fileHashes/fileHashes.lock new file mode 100644 index 00000000..e98bb258 Binary files /dev/null and b/.gradle/7.3.3/fileHashes/fileHashes.lock differ diff --git a/.gradle/7.3.3/gc.properties b/.gradle/7.3.3/gc.properties new file mode 100644 index 00000000..e69de29b diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock new file mode 100644 index 00000000..554cdac6 Binary files /dev/null and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/buildOutputCleanup/cache.properties b/.gradle/buildOutputCleanup/cache.properties new file mode 100644 index 00000000..262b54e7 --- /dev/null +++ b/.gradle/buildOutputCleanup/cache.properties @@ -0,0 +1,2 @@ +#Sun Feb 18 19:53:09 UTC 2024 +gradle.version=7.3.3 diff --git a/.gradle/vcs-1/gc.properties b/.gradle/vcs-1/gc.properties new file mode 100644 index 00000000..e69de29b diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 00000000..7eed456b --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,2 @@ +/main/ +/test/ diff --git a/build.gradle b/build.gradle index bb3a859e..546359da 100644 --- a/build.gradle +++ b/build.gradle @@ -26,7 +26,7 @@ dependencies { group = 'org.jointheleague' version = '2.0' description = 'discordbot' -sourceCompatibility = '1.8' +sourceCompatibility = '1.11' test { diff --git a/build/reports/tests/test/css/base-style.css b/build/reports/tests/test/css/base-style.css new file mode 100644 index 00000000..4afa73e3 --- /dev/null +++ b/build/reports/tests/test/css/base-style.css @@ -0,0 +1,179 @@ + +body { + margin: 0; + padding: 0; + font-family: sans-serif; + font-size: 12pt; +} + +body, a, a:visited { + color: #303030; +} + +#content { + padding-left: 50px; + padding-right: 50px; + padding-top: 30px; + padding-bottom: 30px; +} + +#content h1 { + font-size: 160%; + margin-bottom: 10px; +} + +#footer { + margin-top: 100px; + font-size: 80%; + white-space: nowrap; +} + +#footer, #footer a { + color: #a0a0a0; +} + +#line-wrapping-toggle { + vertical-align: middle; +} + +#label-for-line-wrapping-toggle { + vertical-align: middle; +} + +ul { + margin-left: 0; +} + +h1, h2, h3 { + white-space: nowrap; +} + +h2 { + font-size: 120%; +} + +ul.tabLinks { + padding-left: 0; + padding-top: 10px; + padding-bottom: 10px; + overflow: auto; + min-width: 800px; + width: auto !important; + width: 800px; +} + +ul.tabLinks li { + float: left; + height: 100%; + list-style: none; + padding-left: 10px; + padding-right: 10px; + padding-top: 5px; + padding-bottom: 5px; + margin-bottom: 0; + -moz-border-radius: 7px; + border-radius: 7px; + margin-right: 25px; + border: solid 1px #d4d4d4; + background-color: #f0f0f0; +} + +ul.tabLinks li:hover { + background-color: #fafafa; +} + +ul.tabLinks li.selected { + background-color: #c5f0f5; + border-color: #c5f0f5; +} + +ul.tabLinks a { + font-size: 120%; + display: block; + outline: none; + text-decoration: none; + margin: 0; + padding: 0; +} + +ul.tabLinks li h2 { + margin: 0; + padding: 0; +} + +div.tab { +} + +div.selected { + display: block; +} + +div.deselected { + display: none; +} + +div.tab table { + min-width: 350px; + width: auto !important; + width: 350px; + border-collapse: collapse; +} + +div.tab th, div.tab table { + border-bottom: solid #d0d0d0 1px; +} + +div.tab th { + text-align: left; + white-space: nowrap; + padding-left: 6em; +} + +div.tab th:first-child { + padding-left: 0; +} + +div.tab td { + white-space: nowrap; + padding-left: 6em; + padding-top: 5px; + padding-bottom: 5px; +} + +div.tab td:first-child { + padding-left: 0; +} + +div.tab td.numeric, div.tab th.numeric { + text-align: right; +} + +span.code { + display: inline-block; + margin-top: 0em; + margin-bottom: 1em; +} + +span.code pre { + font-size: 11pt; + padding-top: 10px; + padding-bottom: 10px; + padding-left: 10px; + padding-right: 10px; + margin: 0; + background-color: #f7f7f7; + border: solid 1px #d0d0d0; + min-width: 700px; + width: auto !important; + width: 700px; +} + +span.wrapped pre { + word-wrap: break-word; + white-space: pre-wrap; + word-break: break-all; +} + +label.hidden { + display: none; +} \ No newline at end of file diff --git a/build/reports/tests/test/css/style.css b/build/reports/tests/test/css/style.css new file mode 100644 index 00000000..3dc4913e --- /dev/null +++ b/build/reports/tests/test/css/style.css @@ -0,0 +1,84 @@ + +#summary { + margin-top: 30px; + margin-bottom: 40px; +} + +#summary table { + border-collapse: collapse; +} + +#summary td { + vertical-align: top; +} + +.breadcrumbs, .breadcrumbs a { + color: #606060; +} + +.infoBox { + width: 110px; + padding-top: 15px; + padding-bottom: 15px; + text-align: center; +} + +.infoBox p { + margin: 0; +} + +.counter, .percent { + font-size: 120%; + font-weight: bold; + margin-bottom: 8px; +} + +#duration { + width: 125px; +} + +#successRate, .summaryGroup { + border: solid 2px #d0d0d0; + -moz-border-radius: 10px; + border-radius: 10px; +} + +#successRate { + width: 140px; + margin-left: 35px; +} + +#successRate .percent { + font-size: 180%; +} + +.success, .success a { + color: #008000; +} + +div.success, #successRate.success { + background-color: #bbd9bb; + border-color: #008000; +} + +.failures, .failures a { + color: #b60808; +} + +.skipped, .skipped a { + color: #c09853; +} + +div.failures, #successRate.failures { + background-color: #ecdada; + border-color: #b60808; +} + +ul.linkList { + padding-left: 0; +} + +ul.linkList li { + list-style: none; + margin-bottom: 5px; +} diff --git a/build/reports/tests/test/index.html b/build/reports/tests/test/index.html new file mode 100644 index 00000000..159781f2 --- /dev/null +++ b/build/reports/tests/test/index.html @@ -0,0 +1,92 @@ + + + + + +Test results - Test Summary + + + + + +
+

Test Summary

+
+ + + + + +
+
+ + + + + + + +
+
+
0
+

tests

+
+
+
+
0
+

failures

+
+
+
+
0
+

ignored

+
+
+
+
-
+

duration

+
+
+
+
+
+
-
+

successful

+
+
+
+
+ +
+

Classes

+ + + + + + + + + + + + +
ClassTestsFailuresIgnoredDurationSuccess rate
+
+
+ +
+ + diff --git a/build/reports/tests/test/js/report.js b/build/reports/tests/test/js/report.js new file mode 100644 index 00000000..83bab4a1 --- /dev/null +++ b/build/reports/tests/test/js/report.js @@ -0,0 +1,194 @@ +(function (window, document) { + "use strict"; + + var tabs = {}; + + function changeElementClass(element, classValue) { + if (element.getAttribute("className")) { + element.setAttribute("className", classValue); + } else { + element.setAttribute("class", classValue); + } + } + + function getClassAttribute(element) { + if (element.getAttribute("className")) { + return element.getAttribute("className"); + } else { + return element.getAttribute("class"); + } + } + + function addClass(element, classValue) { + changeElementClass(element, getClassAttribute(element) + " " + classValue); + } + + function removeClass(element, classValue) { + changeElementClass(element, getClassAttribute(element).replace(classValue, "")); + } + + function initTabs() { + var container = document.getElementById("tabs"); + + tabs.tabs = findTabs(container); + tabs.titles = findTitles(tabs.tabs); + tabs.headers = findHeaders(container); + tabs.select = select; + tabs.deselectAll = deselectAll; + tabs.select(0); + + return true; + } + + function getCheckBox() { + return document.getElementById("line-wrapping-toggle"); + } + + function getLabelForCheckBox() { + return document.getElementById("label-for-line-wrapping-toggle"); + } + + function findCodeBlocks() { + var spans = document.getElementById("tabs").getElementsByTagName("span"); + var codeBlocks = []; + for (var i = 0; i < spans.length; ++i) { + if (spans[i].className.indexOf("code") >= 0) { + codeBlocks.push(spans[i]); + } + } + return codeBlocks; + } + + function forAllCodeBlocks(operation) { + var codeBlocks = findCodeBlocks(); + + for (var i = 0; i < codeBlocks.length; ++i) { + operation(codeBlocks[i], "wrapped"); + } + } + + function toggleLineWrapping() { + var checkBox = getCheckBox(); + + if (checkBox.checked) { + forAllCodeBlocks(addClass); + } else { + forAllCodeBlocks(removeClass); + } + } + + function initControls() { + if (findCodeBlocks().length > 0) { + var checkBox = getCheckBox(); + var label = getLabelForCheckBox(); + + checkBox.onclick = toggleLineWrapping; + checkBox.checked = false; + + removeClass(label, "hidden"); + } + } + + function switchTab() { + var id = this.id.substr(1); + + for (var i = 0; i < tabs.tabs.length; i++) { + if (tabs.tabs[i].id === id) { + tabs.select(i); + break; + } + } + + return false; + } + + function select(i) { + this.deselectAll(); + + changeElementClass(this.tabs[i], "tab selected"); + changeElementClass(this.headers[i], "selected"); + + while (this.headers[i].firstChild) { + this.headers[i].removeChild(this.headers[i].firstChild); + } + + var h2 = document.createElement("H2"); + + h2.appendChild(document.createTextNode(this.titles[i])); + this.headers[i].appendChild(h2); + } + + function deselectAll() { + for (var i = 0; i < this.tabs.length; i++) { + changeElementClass(this.tabs[i], "tab deselected"); + changeElementClass(this.headers[i], "deselected"); + + while (this.headers[i].firstChild) { + this.headers[i].removeChild(this.headers[i].firstChild); + } + + var a = document.createElement("A"); + + a.setAttribute("id", "ltab" + i); + a.setAttribute("href", "#tab" + i); + a.onclick = switchTab; + a.appendChild(document.createTextNode(this.titles[i])); + + this.headers[i].appendChild(a); + } + } + + function findTabs(container) { + return findChildElements(container, "DIV", "tab"); + } + + function findHeaders(container) { + var owner = findChildElements(container, "UL", "tabLinks"); + return findChildElements(owner[0], "LI", null); + } + + function findTitles(tabs) { + var titles = []; + + for (var i = 0; i < tabs.length; i++) { + var tab = tabs[i]; + var header = findChildElements(tab, "H2", null)[0]; + + header.parentNode.removeChild(header); + + if (header.innerText) { + titles.push(header.innerText); + } else { + titles.push(header.textContent); + } + } + + return titles; + } + + function findChildElements(container, name, targetClass) { + var elements = []; + var children = container.childNodes; + + for (var i = 0; i < children.length; i++) { + var child = children.item(i); + + if (child.nodeType === 1 && child.nodeName === name) { + if (targetClass && child.className.indexOf(targetClass) < 0) { + continue; + } + + elements.push(child); + } + } + + return elements; + } + + // Entry point. + + window.onload = function() { + initTabs(); + initControls(); + }; +} (window, window.document)); \ No newline at end of file diff --git a/build/test-results/test/binary/output.bin b/build/test-results/test/binary/output.bin new file mode 100644 index 00000000..e69de29b diff --git a/build/test-results/test/binary/output.bin.idx b/build/test-results/test/binary/output.bin.idx new file mode 100644 index 00000000..f76dd238 Binary files /dev/null and b/build/test-results/test/binary/output.bin.idx differ diff --git a/build/test-results/test/binary/results.bin b/build/test-results/test/binary/results.bin new file mode 100644 index 00000000..e69de29b diff --git a/build/tmp/compileJava/previous-compilation-data.bin b/build/tmp/compileJava/previous-compilation-data.bin new file mode 100644 index 00000000..6cfea2aa Binary files /dev/null and b/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java index ae863505..dfc8582e 100644 --- a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java +++ b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java @@ -11,6 +11,8 @@ 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.second_feature.FeatureTwo; +import org.jointheleague.features.student.third_feature.FeatureThree; public class DiscordBot { @@ -29,7 +31,6 @@ public DiscordBot(String token, String channelName) { } public void connect(boolean printInvite) { - api = new DiscordApiBuilder().setToken(token).addIntents(Intent.MESSAGE_CONTENT).login().join(); //Print the URL to invite the bot @@ -45,11 +46,15 @@ public void connect(boolean printInvite) { api.addMessageCreateListener(helpListener); //add features - addFeature(new FeatureOne(channelName)); + addFeature(new RandomNumber(channelName)); addFeature(new CurrentTime(channelName)); addFeature(new HighLowGame(channelName)); addFeature(new NewsApi(channelName)); addFeature(new CatFactsApi(channelName)); + //MINE + addFeature(new FeatureOne(channelName)); + addFeature(new FeatureTwo(channelName)); + addFeature(new FeatureThree(channelName)); } private void addFeature(Feature feature){ diff --git a/src/main/java/org/jointheleague/features/examples/first_features/RandomNumber.java b/src/main/java/org/jointheleague/features/examples/first_features/RandomNumber.java index 13a59481..61ecc7b3 100644 --- a/src/main/java/org/jointheleague/features/examples/first_features/RandomNumber.java +++ b/src/main/java/org/jointheleague/features/examples/first_features/RandomNumber.java @@ -20,6 +20,7 @@ public void handle(MessageCreateEvent event) { String messageContent = event.getMessageContent(); if (messageContent.startsWith(COMMAND)) { Random r = new Random(); + System.out.println("test"); event.getChannel().sendMessage("Your random number is " + r.nextInt(1001)); } } diff --git a/src/main/java/org/jointheleague/features/examples/second_features/HighLowGame.java b/src/main/java/org/jointheleague/features/examples/second_features/HighLowGame.java index 8dee99bf..aef77b13 100644 --- a/src/main/java/org/jointheleague/features/examples/second_features/HighLowGame.java +++ b/src/main/java/org/jointheleague/features/examples/second_features/HighLowGame.java @@ -8,13 +8,13 @@ public class HighLowGame extends Feature { - public final String COMMAND = "!highLow"; + public final String COMMAND = "!HighLowGame"; private final Random random = new Random(); int numberToGuess; public HighLowGame(String channelName) { super(channelName); - helpEmbed = new HelpEmbed(COMMAND, "High low game. The computer picks a number when you start the game, and then tells you if your current guess is too low or too high"); + helpEmbed = new HelpEmbed(COMMAND, "Get as close to 21 as you can with your cards. 'Hit' means to ask for another card, stand means you are finished. Between us, whoever is closer to 21 wins!"); } @Override @@ -22,8 +22,8 @@ public void handle(MessageCreateEvent event){ String messageContent = event.getMessageContent(); //start the game with the command - if (messageContent.equals(COMMAND)) { - numberToGuess = random.nextInt(100) + 1; + if (messageContent.equals(COMMAND)) { + numberToGuess = random.nextInt(10) + 2; event.getChannel().sendMessage("I have picked a number between 1 and 100. Guess by using e.g. !highLow 5"); } //check a guess 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 index 9b303536..d18eca75 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java @@ -8,7 +8,7 @@ import java.util.Random; public class FeatureOne extends FeatureTemplate { - public final String COMMAND = "!joke"; + public final String COMMAND = "!introduce"; public FeatureOne(String channelName) { super(channelName); @@ -16,33 +16,18 @@ public FeatureOne(String channelName) { //Create a help embed to describe feature when !help command is sent helpEmbed = new HelpEmbed( COMMAND, - "This command prints cheesy jokes." + "I will tell you a little bit about who I am and what I can do!" ); } -//IGNORE + @Override public void handle(MessageCreateEvent 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.getChannel().sendMessage("What’s the best thing about Switzerland? \n \n I don’t know, but the flag is a big plus."); - break; - case 1:event.getChannel().sendMessage("I invented a new word! \n \n Plagiarism!"); - break; - case 2:event.getChannel().sendMessage("Hear about the new restaurant called Karma? \n \n There’s no menu: You get what you deserve."); - break; - case 3:event.getChannel().sendMessage("Did you hear about the claustrophobic astronaut? \n \n He just needed a little space."); - break; - case 4:event.getChannel().sendMessage("Why don’t scientists trust atoms? \n \n Because they make up everything."); - break; - default:event.getChannel().sendMessage("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; - } - + event.getChannel().sendMessage("I am NicholasBot, created by the one and only, Nicholas Falstad. I have features such as a blackjack game, and I can suggest an activity for you when you're bored."); } } -} + } + + \ No newline at end of file diff --git a/src/main/java/org/jointheleague/features/student/second_feature/FeatureTwo.java b/src/main/java/org/jointheleague/features/student/second_feature/FeatureTwo.java new file mode 100644 index 00000000..bd92713e --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/second_feature/FeatureTwo.java @@ -0,0 +1,142 @@ +package org.jointheleague.features.student.second_feature; + +import java.util.Random; + +import org.javacord.api.event.message.MessageCreateEvent; +import org.jointheleague.features.help_embed.plain_old_java_objects.help_embed.HelpEmbed; +import org.jointheleague.features.templates.FeatureTemplate; + +public class FeatureTwo extends FeatureTemplate { + public final String COMMAND = "!blackjack"; + private final Random random = new Random(); + int numberToGuess; + int yourValue = 0; + int botValue = 0; + + public FeatureTwo(String channelName) { + super(channelName); + helpEmbed = new HelpEmbed(COMMAND, "Get as close to 21 as you can with your cards. 'Hit' means to ask for another card, stand means you are finished. Between us, whoever is closer to 21 wins!"); + } + + @Override + public void handle(MessageCreateEvent event) { + String messageContent = event.getMessageContent(); + //divide into methods + + //start the game with the command + if (messageContent.equals(COMMAND)) { + String botCard = cardPicker(); + String yourCard = cardPicker(); + botValue += findValue(botCard); + yourValue += findValue(yourCard); + System.out.println("starting game"); + event.getChannel().sendMessage("My Card: " + botCard + "\n Your Card: " + yourCard + "\n Stand or hit?"); + } else if (messageContent.contains(COMMAND) && messageContent.contains("hit")) { + if(botValue == 0) { + event.getChannel().sendMessage("Please start the game before using this command."); + } else { + System.out.println("hitting"); + hit(event); + } + } else if(messageContent.contains(COMMAND) && messageContent.contains("stand")) { + if (botValue == 0) { + event.getChannel().sendMessage("Please start the game before using this command."); + } else { + stand(event); + } + } + } + public void hit(MessageCreateEvent event) { + String yourCard2 = cardPicker(); + yourValue += findValue(yourCard2); + event.getChannel().sendMessage("Your Next Card: " + yourCard2); + event.getChannel().sendMessage("Your Total Card Value: " + yourValue); + if(yourValue > 21) { + event.getChannel().sendMessage("Bot wins! You busted!"); + botValue = 0; + yourValue = 0; + } else { + event.getChannel().sendMessage("Hit or stand?"); + } + } + public void stand(MessageCreateEvent event) { + + while (botValue < 17) { + String botCard2 = cardPicker(); + botValue += findValue(botCard2); + } + + + if (yourValue > 21) { + event.getChannel().sendMessage("Bot wins! You busted!"); + } else if (botValue > 21) { + event.getChannel().sendMessage("You win! Bot busted!"); + } + if (botValue == yourValue) { + event.getChannel().sendMessage("Tie! You both scored " + yourValue); + } else if (botValue > yourValue) { + event.getChannel().sendMessage("Bot wins! Score: " + botValue); + } else if (botValue < yourValue) { + event.getChannel().sendMessage("You win! Score: " + botValue); + } + botValue = 0; + yourValue = 0; + } + public String cardPicker() { + String cardName = ""; + numberToGuess = random.nextInt(10) + 2; + if (numberToGuess == 10) { + numberToGuess = random.nextInt(4); + if (numberToGuess == 0) { + cardName += "10"; + } else if (numberToGuess == 1) { + cardName += "Jack"; + } else if (numberToGuess == 2) { + cardName += "Queen"; + } else if (numberToGuess == 3) { + cardName += "King"; + } + } else if (numberToGuess == 11) { + cardName += "Ace"; + } else { + cardName += (numberToGuess); + } + numberToGuess = random.nextInt(4); + if (numberToGuess == 0) { + cardName += " of Hearts"; + } else if (numberToGuess == 1) { + cardName += " of Diamonds"; + } else if (numberToGuess == 2) { + cardName += " of Spades"; + } else if (numberToGuess == 3) { + cardName += " of Clubs"; + } + return cardName; + } + + public int findValue(String card) { + if (card.substring(0, 1).equals("2")) { + return 2; + } else if (card.substring(0, 1).equals("3")) { + return 3; + } else if (card.substring(0, 1).equals("4")) { + return 4; + } else if (card.substring(0, 1).equals("5")) { + return 5; + } else if (card.substring(0, 1).equals("6")) { + return 6; + } else if (card.substring(0, 1).equals("7")) { + return 7; + } else if (card.substring(0, 1).equals("8")) { + return 8; + } else if (card.substring(0, 1).equals("9")) { + return 9; + } else if (card.substring(0, 1).equals("10")) { + return 10; + } else if (card.substring(0, 3).equals("Ace")) { + return 11; + } else { + return 10; + } + } +} diff --git a/src/main/java/org/jointheleague/features/student/third_feature/FeatureThree.java b/src/main/java/org/jointheleague/features/student/third_feature/FeatureThree.java new file mode 100644 index 00000000..c4386eef --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/third_feature/FeatureThree.java @@ -0,0 +1,54 @@ +package org.jointheleague.features.student.third_feature; + +import org.javacord.api.event.message.MessageCreateEvent; +import org.jointheleague.features.abstract_classes.Feature; +import org.jointheleague.features.help_embed.plain_old_java_objects.help_embed.HelpEmbed; +import org.jointheleague.features.student.third_features.plain_old_java_objects.bored_api.FeatureThreeWrapper; +import org.springframework.web.reactive.function.client.WebClient; + +import reactor.core.publisher.Mono; + +public class FeatureThree extends Feature { + + public final String COMMAND = "!bored"; + + private WebClient webClient; + private static final String baseUrl = "https://www.boredapi.com/api/activity"; + + public FeatureThree(String channelName) { + super(channelName); + helpEmbed = new HelpEmbed(COMMAND, "This will return an activity you can do when you're bored."); + + //build the WebClient + this.webClient = WebClient + .builder() + .baseUrl(baseUrl) + .build(); + } + + @Override + public void handle(MessageCreateEvent event) { + String messageContent = event.getMessageContent(); + if (messageContent.startsWith(COMMAND)) { + String activity = getActivity(); + event.getChannel().sendMessage(activity); + } + } + public String getActivity() { + + //Make the request, accepting the response as a plain old java object you created + Mono wrapperMono = webClient.get().retrieve().bodyToMono(FeatureThreeWrapper.class); + + //collect the response into a plain old java object + FeatureThreeWrapper wrapper = wrapperMono.block(); + //get the cat fact from the response + String message = wrapper.getActivity(); + //send the message + System.out.println(message); + return message; + } + + public void setWebClient(WebClient webClient) { + this.webClient = webClient; + } +} diff --git a/src/main/java/org/jointheleague/features/student/third_features/plain_old_java_objects/bored_api/FeatureThreeWrapper.java b/src/main/java/org/jointheleague/features/student/third_features/plain_old_java_objects/bored_api/FeatureThreeWrapper.java new file mode 100644 index 00000000..8baa3d48 --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/third_features/plain_old_java_objects/bored_api/FeatureThreeWrapper.java @@ -0,0 +1,31 @@ +package org.jointheleague.features.student.third_features.plain_old_java_objects.bored_api; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +import java.util.List; +public class FeatureThreeWrapper { + @SerializedName("data") + @Expose + private List data = null; + + public List getData() { + return data; + } + public void setData(List data) { + this.data = data; + } + + @SerializedName("activity") + @Expose + private String activity; + + public String getActivity() { + return activity; + } + + public void setActivity(String activity) { + this.activity = activity; + } + +} 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 a7915549..7341005d 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 @@ import static org.mockito.Mockito.never; public class FeatureOneTest { - private final String testChannelName = "test"; + private final String testChannelName = "FeatureOne"; private final FeatureOne featureOne = new FeatureOne(testChannelName); private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); diff --git a/src/test/java/org/jointheleague/features/student/second_feature/FeatureTwoTest.java b/src/test/java/org/jointheleague/features/student/second_feature/FeatureTwoTest.java new file mode 100644 index 00000000..680deff0 --- /dev/null +++ b/src/test/java/org/jointheleague/features/student/second_feature/FeatureTwoTest.java @@ -0,0 +1,170 @@ +package org.jointheleague.features.student.second_feature; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import org.javacord.api.entity.channel.TextChannel; +import org.javacord.api.event.message.MessageCreateEvent; +import org.jointheleague.features.examples.second_features.HighLowGame; +import org.jointheleague.features.help_embed.plain_old_java_objects.help_embed.HelpEmbed; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +public class FeatureTwoTest { + private final String testChannelName = "FeatureTwo"; + private final FeatureTwo featureTwo = new FeatureTwo(testChannelName); + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + + @Mock + private MessageCreateEvent messageCreateEvent; + + @Mock + private TextChannel textChannel; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + System.setOut(new PrintStream(outContent)); + } + + @AfterEach + public void itShouldNotPrintToSystemOut() { + String expected = ""; + String actual = outContent.toString(); + + assertEquals(expected, actual); + System.out.println(actual); + System.setOut(originalOut); + } + + @Test + void itShouldHaveACommand() { + //Given + + //When + String command = featureTwo.COMMAND; + + //Then + assertNotEquals("", command); + assertNotEquals("!command", command); + assertNotNull(command); + } + + @Test + void itShouldHandleMessagesWithCommand() { + //Given + HelpEmbed helpEmbed = new HelpEmbed(featureTwo.COMMAND, "test"); + when(messageCreateEvent.getMessageContent()).thenReturn(featureTwo.COMMAND); + when(messageCreateEvent.getChannel()).thenReturn((textChannel)); + + //When + featureTwo.handle(messageCreateEvent); + + //Then + verify(textChannel, times(1)).sendMessage(anyString()); + } + + @Test + void itShouldNotHandleMessagesWithoutCommand() { + //Given + String command = ""; + when(messageCreateEvent.getMessageContent()).thenReturn(command); + + //When + featureTwo.handle(messageCreateEvent); + + //Then + verify(textChannel, never()).sendMessage(""); + } + + @Test + void itShouldHaveAHelpEmbed() { + //Given + + //When + HelpEmbed actualHelpEmbed = featureTwo.getHelpEmbed(); + + //Then + assertNotNull(actualHelpEmbed); + } + + @Test + void itShouldHaveTheCommandAsTheTitleOfTheHelpEmbed() { + //Given + + //When + String helpEmbedTitle = featureTwo.getHelpEmbed().getTitle(); + String command = featureTwo.COMMAND; + + //Then + assertEquals(command, helpEmbedTitle); + } + +//FEATURE-SPECIFIC TESTS + + @Test + void itShouldNotAcceptGuessIfGameIsNotStarted() { + //Given + String command = "!Hit"; + when(messageCreateEvent.getMessageContent()).thenReturn(command); + when(messageCreateEvent.getChannel()).thenReturn((textChannel)); + + //When + featureTwo.handle(messageCreateEvent); + + //Then + verify(textChannel, times(1)).sendMessage("Please start the game before using this command."); + + } + + + + @Test + void itShouldTellTheUserIfTheyLose() { + //Given + String command = "!Hit"; + featureTwo.yourValue = 20; + featureTwo.botValue = 21; + when(messageCreateEvent.getMessageContent()).thenReturn(command); + when(messageCreateEvent.getChannel()).thenReturn((textChannel)); + + //When + featureTwo.handle(messageCreateEvent); + + //Then + verify(textChannel, times(1)).sendMessage("You lose! Score: " + featureTwo.botValue); + + } + + @Test + void itShouldTellTheUserIfTheyWin() { + //Given + String command = "!Hit"; + featureTwo.yourValue = 21; + featureTwo.botValue = 20; + when(messageCreateEvent.getMessageContent()).thenReturn(command); + when(messageCreateEvent.getChannel()).thenReturn((textChannel)); + + //When + featureTwo.handle(messageCreateEvent); + + //Then + verify(textChannel, times(1)).sendMessage("You win! Score: " + featureTwo.yourValue); + } + + +} diff --git a/src/test/java/org/jointheleague/features/student/third_feature/FeatureThreeTest.java b/src/test/java/org/jointheleague/features/student/third_feature/FeatureThreeTest.java new file mode 100644 index 00000000..43366b22 --- /dev/null +++ b/src/test/java/org/jointheleague/features/student/third_feature/FeatureThreeTest.java @@ -0,0 +1,158 @@ +package org.jointheleague.features.student.third_feature; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.Collections; +import java.util.List; + +import org.javacord.api.entity.channel.TextChannel; +import org.javacord.api.event.message.MessageCreateEvent; +import org.jointheleague.features.examples.third_features.CatFactsApi; +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.features.student.second_feature.FeatureTwo; +import org.jointheleague.features.student.third_features.plain_old_java_objects.bored_api.FeatureThreeWrapper; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.web.reactive.function.client.WebClient; + +import reactor.core.publisher.Mono; + +public class FeatureThreeTest { + + private final String testChannelName = "FeatureThree"; + private FeatureThree featureThree; + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + + @Mock + private MessageCreateEvent messageCreateEvent; + + @Mock + private TextChannel textChannel; + + @Mock + WebClient webClientMock; + + @Mock + WebClient.RequestHeadersUriSpec requestHeadersUriSpecMock; + + @Mock + WebClient.RequestHeadersSpec requestHeadersSpecMock; + + @Mock + WebClient.ResponseSpec responseSpecMock; + + @Mock + Mono featureThreeWrapperMonoMock; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + + featureThree = new FeatureThree(testChannelName); + featureThree.setWebClient(webClientMock); + System.setOut(new PrintStream(outContent)); + } + + @AfterEach + public void itShouldNotPrintToSystemOut() { + String expected = ""; + String actual = outContent.toString(); + + assertEquals(expected, actual); + System.setOut(originalOut); + } + + + @Test + void itShouldHaveACommand() { + //Given + + //When + String command = featureThree.COMMAND; + + //Then + assertNotEquals("", command); + assertNotEquals("!command", command); + assertNotNull(command); + } + + @Test + void itShouldHaveAHelpEmbed() { + //Given + + //When + HelpEmbed actualHelpEmbed = featureThree.getHelpEmbed(); + + //Then + assertNotNull(actualHelpEmbed); + } + + @Test + void itShouldHaveTheCommandAsTheTitleOfTheHelpEmbed() { + //Given + + //When + String helpEmbedTitle = featureThree.getHelpEmbed().getTitle(); + String command = featureThree.COMMAND; + + //Then + assertEquals(command, helpEmbedTitle); + } + + @Test + void givenMessageWithCommand_whenHandle_thenSendCatFact() { + //given + String activity = "Start a garden"; + List data = Collections.singletonList(activity); + + FeatureThreeWrapper featureThreeWrapper = new FeatureThreeWrapper(); + featureThreeWrapper.setData(data); + + when(messageCreateEvent.getMessageContent()).thenReturn(featureThree.COMMAND); + when(messageCreateEvent.getChannel()).thenReturn(textChannel); + + when(webClientMock.get()) + .thenReturn(requestHeadersUriSpecMock); + when(requestHeadersUriSpecMock.retrieve()) + .thenReturn(responseSpecMock); + when(responseSpecMock.bodyToMono(FeatureThreeWrapper.class)) + .thenReturn(featureThreeWrapperMonoMock); + when(featureThreeWrapperMonoMock.block()) + .thenReturn(featureThreeWrapper); + + //when + featureThree.handle(messageCreateEvent); + + //then + verify(webClientMock, times(1)).get(); + verify(textChannel, times(1)).sendMessage(activity); + } + + @Test + void givenMessageWithoutCommand_whenHandle_thenDoNothing() { + //Given + String command = ""; + when(messageCreateEvent.getMessageContent()).thenReturn(command); + + //When + featureThree.handle(messageCreateEvent); + + //Then + verify(textChannel, never()).sendMessage(""); + } + +}