Skip to content

Commit

Permalink
Merge pull request #6 from TosoxDev/development
Browse files Browse the repository at this point in the history
fix: hangman joker bug
  • Loading branch information
Tosox authored Jul 4, 2023
2 parents 46a5506 + 9f9c775 commit 25e43d4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/main/java/de/tosoxdev/tosoxjr/commands/hangman/Hangman.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.awt.*;
Expand Down Expand Up @@ -179,13 +180,15 @@ private String getWordDefinition() {
return "_Unknown_";
}

// Check if one definition
if (response.length() == 1) {
return response.getString(0);
// Check if word exists in dictionary
JSONObject objDefinition;
try {
objDefinition = response.getJSONObject(0);
} catch (JSONException e) {
return "_Unknown_";
}

// If multiple definitions
JSONObject objDefinition = response.getJSONObject(0);
JSONArray shortDefinitions = objDefinition.getJSONArray("shortdef");
if ((shortDefinitions == null) || (shortDefinitions.isEmpty())) {
return "_Unknown_";
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/de/tosoxdev/tosoxjr/commands/hangman/HangmanCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@

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

public HangmanCmd() {
super("hangman", "Play a game of hangman", List.of(
new OptionData(OptionType.BOOLEAN, "coop", "Play hangman with all your friends on the server", false)
super("hangman", "Play a game of Hangman", List.of(
new OptionData(OptionType.BOOLEAN, "coop", "Play Hangman with all your friends on the server", false)
));
instance = this;
}
Expand All @@ -25,7 +26,12 @@ public HangmanCmd() {
public void handle(SlashCommandInteractionEvent event) {
String user = event.getUser().getAsTag();
if (games.containsKey(user)) {
event.reply("An instance of 'hangman' is already running").queue();
event.reply("You already started a game of Hangman").queue();
return;
}

if (games.size() > MAX_GAMES) {
event.reply("Sorry, there are to many games of Hangman already").queue();
return;
}

Expand All @@ -41,7 +47,7 @@ public void handle(SlashCommandInteractionEvent event) {
@Override
public void handleEvent(Event event) {
for (Hangman hangman : games.values()) {
hangman.handleEvent(event);
new Thread(() -> hangman.handleEvent(event)).start();
}
}

Expand Down

0 comments on commit 25e43d4

Please sign in to comment.