Skip to content

Commit

Permalink
Merge pull request #7 from TosoxDev/development
Browse files Browse the repository at this point in the history
feat: german words for Hangman
  • Loading branch information
Tosox authored Jul 4, 2023
2 parents 25e43d4 + 0c32a9a commit de1f913
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import de.tosoxdev.tosoxjr.commands.cat.CatCmd;
import de.tosoxdev.tosoxjr.commands.csstats.CSStatsCmd;
import de.tosoxdev.tosoxjr.commands.hangman.HangmanCmd;
import de.tosoxdev.tosoxjr.commands.joke.JokeCmd;
import de.tosoxdev.tosoxjr.commands.quote.QuoteCmd;
import de.tosoxdev.tosoxjr.commands.say.SayCmd;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -18,6 +18,7 @@ public CommandManager() {
addCommand(new SayCmd());
addCommand(new CatCmd());
addCommand(new QuoteCmd());
addCommand(new JokeCmd());
addCommand(new CSStatsCmd());
addCommand(new HangmanCmd());
}
Expand All @@ -34,7 +35,6 @@ public void addCommand(CommandBase cmd) throws IllegalArgumentException {
commands.add(cmd);
}

@NotNull
public CommandBase getCommand(String name) {
return commands.stream()
.filter(cmd -> cmd.getName().equalsIgnoreCase(name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public CSStatsCmd() {

@Override
public void handle(SlashCommandInteractionEvent event) {
String user = ArgumentParser.getStringForced(event.getOption("user"));
String user = ArgumentParser.getString(event.getOption("user"), "");

// Check and get steamid64
String userid = csStats.getID64(user);
Expand All @@ -50,7 +50,7 @@ public void handle(SlashCommandInteractionEvent event) {
}

// Check if a statistic parameter is provided
String stat = ArgumentParser.getString(event.getOption("stat"));
String stat = ArgumentParser.getString(event.getOption("stat"), null);
if (stat != null) {
// Check if statistic exists
String statistic = csStats.getStatistic(userStats, stat);
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/de/tosoxdev/tosoxjr/commands/hangman/Hangman.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import org.json.JSONObject;

import java.awt.*;
import java.util.HashSet;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

Expand All @@ -39,7 +36,11 @@ public String getTitle() {
}

public class Hangman {
private static final String API_RANDOM_WORD = "https://random-word-api.vercel.app/api?words=1";
public static final HashMap<String, String> RANDOM_WORD_APIS = new HashMap<>(Map.of(
"en", "https://random-word-api.vercel.app/api?words=1",
"de", "https://alex-riedel.de/randV2.php?anz=1"
));

private static final String API_DICTIONARY = "https://www.dictionaryapi.com/api/v3/references/collegiate/json/%s?key=%s";
private static final int REGIONAL_INDICATOR_A_CP = 0x1F1E6;
private static final int REGIONAL_INDICATOR_Z_CP = 0x1F1FF;
Expand All @@ -51,17 +52,19 @@ public class Hangman {
private final MessageChannel channel;
private final String player;
private final boolean coop;
private final String language;

private Timer timer = new Timer();
private String embedMessageId;
private String word;
private String wordDefinition;
private int attempts;

public Hangman(String player, MessageChannel channel, boolean coop) {
public Hangman(String player, MessageChannel channel, boolean coop, String language) {
this.channel = channel;
this.player = player;
this.coop = coop;
this.language = language;
}

public boolean initialize() {
Expand Down Expand Up @@ -216,7 +219,8 @@ private void endGame(GameState state) {
}

private String generateWord() {
String response = APIRequest.getString(API_RANDOM_WORD);
String lang = RANDOM_WORD_APIS.getOrDefault(language.toLowerCase(), RANDOM_WORD_APIS.get("en"));
String response = APIRequest.getString(lang);
if (response == null) {
return null;
}
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/de/tosoxdev/tosoxjr/commands/hangman/HangmanCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,32 @@
import java.util.List;

public class HangmanCmd extends GameBase {
private final HashMap<String, Hangman> games = new HashMap<>();
private static final int MAX_GAMES = 10;
private static HangmanCmd instance;
private final HashMap<String, Hangman> games = new HashMap<>();
private final String languages;

public HangmanCmd() {
super("hangman", "Play a game of Hangman", List.of(
new OptionData(OptionType.STRING, "lang", "Decide the langauge of the word. Use 'list' to list all available ones", false),
new OptionData(OptionType.BOOLEAN, "coop", "Play Hangman with all your friends on the server", false)
));
instance = this;

StringBuilder sb = new StringBuilder();
Hangman.RANDOM_WORD_APIS.forEach((key, value) -> sb.append(String.format("- %s\n", key)));
languages = sb.toString();
}

@Override
public void handle(SlashCommandInteractionEvent event) {
String lang = ArgumentParser.getString(event.getOption("lang"), "");
if (lang.equalsIgnoreCase("list")) {
String msg = String.format("Available languages\n%s", languages);
event.reply(msg).queue();
return;
}

String user = event.getUser().getAsTag();
if (games.containsKey(user)) {
event.reply("You already started a game of Hangman").queue();
Expand All @@ -38,7 +51,7 @@ public void handle(SlashCommandInteractionEvent event) {
event.deferReply().queue(m -> m.deleteOriginal().queue());

boolean coop = ArgumentParser.getBoolean(event.getOption("coop"), false);
Hangman hangman = new Hangman(user, event.getChannel(), coop);
Hangman hangman = new Hangman(user, event.getChannel(), coop, lang);
if (hangman.initialize()) {
games.put(user, hangman);
}
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/de/tosoxdev/tosoxjr/commands/joke/Joke.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package de.tosoxdev.tosoxjr.commands.joke;

import de.tosoxdev.tosoxjr.utils.APIRequest;
import org.json.JSONArray;
import org.json.JSONObject;

public class Joke {
private final static String PUN_TEMPLATE = "%s\n... %s";

public static String getGeneral() {
JSONArray response = (JSONArray) APIRequest.getJson("https://official-joke-api.appspot.com/jokes/general/random");
if ((response == null) || (response.isEmpty())) {
return null;
}

String setup = response.getJSONObject(0).getString("setup");
String punchline = response.getJSONObject(0).getString("punchline");
return String.format(PUN_TEMPLATE, setup, punchline);
}

public static String getProgramming() {
JSONArray response = (JSONArray) APIRequest.getJson("https://official-joke-api.appspot.com/jokes/programming/random");
if ((response == null) || (response.isEmpty())) {
return null;
}

String setup = response.getJSONObject(0).getString("setup");
String punchline = response.getJSONObject(0).getString("punchline");
return String.format(PUN_TEMPLATE, setup, punchline);
}

public static String getChuckNorris() {
JSONObject response = (JSONObject) APIRequest.getJson("https://api.chucknorris.io/jokes/random");
if (response == null) {
return null;
}

return response.getString("value");
}
}
75 changes: 75 additions & 0 deletions src/main/java/de/tosoxdev/tosoxjr/commands/joke/JokeCmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package de.tosoxdev.tosoxjr.commands.joke;

import de.tosoxdev.tosoxjr.Main;
import de.tosoxdev.tosoxjr.commands.CommandBase;
import de.tosoxdev.tosoxjr.utils.ArgumentParser;
import de.tosoxdev.tosoxjr.utils.Utils;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ThreadLocalRandom;

public class JokeCmd extends CommandBase {
private final HashMap<String, Callable<String>> categories = new HashMap<>(Map.of(
"general", Joke::getGeneral,
"programming", Joke::getProgramming,
"chuck-norris", Joke::getChuckNorris
));
private final String categoriesList;

public JokeCmd() {
super("joke", "Show a random joke", List.of(
new OptionData(OptionType.STRING, "category", "List all available categories with 'list'", false)
));

StringBuilder sb = new StringBuilder();
categories.forEach((key, value) -> sb.append(String.format("- %s\n", key)));
categoriesList = sb.toString();
}

@Override
public void handle(SlashCommandInteractionEvent event) {
String category = ArgumentParser.getString(event.getOption("category"), null);
if (category == null) {
// Get random joke
int randomIdx = ThreadLocalRandom.current().nextInt(categories.size());
String randomCategory = (String) categories.keySet().toArray()[randomIdx];
Callable<String> callable = categories.get(randomCategory);

String joke = Utils.getStringFromCallable(callable);
if (joke == null) {
Main.getLogger().error("The callable didn't return a value when trying to run 'joke'");
return;
}

event.reply(joke).queue();
return;
}

if (category.equalsIgnoreCase("list")) {
String msg = String.format("Available categories\n%s", categoriesList);
event.reply(msg).queue();
return;
}

Callable<String> callable = categories.get(category);
if (callable == null) {
String msg = String.format("There are no jokes for '%s'", category);
event.reply(msg).queue();
return;
}

String joke = Utils.getStringFromCallable(callable);
if (joke == null) {
Main.getLogger().error("The callable didn't return a value when trying to run 'joke'");
return;
}

event.reply(joke).queue();
}
}
32 changes: 6 additions & 26 deletions src/main/java/de/tosoxdev/tosoxjr/commands/quote/Quote.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import de.tosoxdev.tosoxjr.utils.APIRequest;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.concurrent.Callable;

public class Quote {
private final static String QUOTE_TEMPLATE = "_%s_\n~ %s";

public static String getBreakingBad() {
JSONArray response = (JSONArray) APIRequest.getJson("https://api.breakingbadquotes.xyz/v1/quotes");
if ((response == null) || (response.isEmpty())) {
Expand All @@ -15,18 +14,7 @@ public static String getBreakingBad() {

String quote = response.getJSONObject(0).getString("quote");
String author = response.getJSONObject(0).getString("author");
return String.format("_%s_\n~ %s", quote, author);
}

public static String getJoke() {
JSONObject response = (JSONObject) APIRequest.getJson("https://official-joke-api.appspot.com/jokes/random");
if (response == null) {
return null;
}

String setup = response.getString("setup");
String punchline = response.getString("punchline");
return String.format("%s\n... %s", setup, punchline);
return String.format(QUOTE_TEMPLATE, quote, author);
}

public static String getFamous() {
Expand All @@ -37,7 +25,7 @@ public static String getFamous() {

String content = response.getJSONObject(0).getString("content");
String author = response.getJSONObject(0).getString("author");
return String.format("_%s_\n~ %s", content, author);
return String.format(QUOTE_TEMPLATE, content, author);
}

public static String getWisdom() {
Expand All @@ -48,7 +36,7 @@ public static String getWisdom() {

String content = response.getJSONObject(0).getString("content");
String author = response.getJSONObject(0).getString("author");
return String.format("_%s_\n~ %s", content, author);
return String.format(QUOTE_TEMPLATE, content, author);
}

public static String getInspirational() {
Expand All @@ -59,14 +47,6 @@ public static String getInspirational() {

String q = response.getJSONObject(0).getString("q");
String a = response.getJSONObject(0).getString("a");
return String.format("_%s_\n~ %s", q, a);
}

public static String getFromCallable(Callable<String> callable) {
try {
return callable.call();
} catch (Exception e) {
return null;
}
return String.format(QUOTE_TEMPLATE, q, a);
}
}
Loading

0 comments on commit de1f913

Please sign in to comment.