Skip to content

Commit

Permalink
Merge pull request #10 from TosoxDev/development
Browse files Browse the repository at this point in the history
fix: Scramble and Hangman
  • Loading branch information
Tosox authored Jul 13, 2023
2 parents bba7131 + a4dc6c7 commit 362c6b5
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private String createDescription() {

private EmbedBuilder createGameEmbed(GameState state) {
EmbedBuilder gameEmbed = new EmbedBuilder();
gameEmbed.setTitle((coop ? "[CO-OP] " : "") + state.getTitle());
gameEmbed.setTitle(String.format("%s[%s] %s", coop ? "[CO-OP]" : "", language.isBlank() ? "EN" : language.toUpperCase(), state.getTitle()));
gameEmbed.setColor(Color.BLUE);
gameEmbed.setDescription(createDescription());
gameEmbed.addField("Word", "```" + (state == GameState.ONGOING ? showWord() : revealWord()) + "```", false);
Expand All @@ -267,6 +267,7 @@ private EmbedBuilder createGameEmbed(GameState state) {
- React with the stop sign (🛑) to end the game
""", false);
}
gameEmbed.setFooter("Request made by @" + player, null);
return gameEmbed;
}
}
4 changes: 2 additions & 2 deletions src/main/java/de/tosoxdev/tosoxjr/commands/joke/JokeCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void handle(SlashCommandInteractionEvent event) {
String randomCategory = (String) categories.keySet().toArray()[randomIdx];
Callable<String> callable = categories.get(randomCategory);

String joke = Utils.getStringFromCallable(callable);
String joke = Utils.getFromCallable(callable);
if (joke == null) {
Main.getLogger().error("The callable didn't return a value when trying to run 'joke'");
return;
Expand All @@ -63,7 +63,7 @@ public void handle(SlashCommandInteractionEvent event) {
return;
}

String joke = Utils.getStringFromCallable(callable);
String joke = Utils.getFromCallable(callable);
if (joke == null) {
Main.getLogger().error("The callable didn't return a value when trying to run 'joke'");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void handle(SlashCommandInteractionEvent event) {
String randomCategory = (String) categories.keySet().toArray()[randomIdx];
Callable<String> callable = categories.get(randomCategory);

String quote = Utils.getStringFromCallable(callable);
String quote = Utils.getFromCallable(callable);
if (quote == null) {
Main.getLogger().error("The callable didn't return a value when trying to run 'quote'");
return;
Expand All @@ -64,7 +64,7 @@ public void handle(SlashCommandInteractionEvent event) {
return;
}

String quote = Utils.getStringFromCallable(callable);
String quote = Utils.getFromCallable(callable);
if (quote == null) {
Main.getLogger().error("The callable didn't return a value when trying to run 'quote'");
return;
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/de/tosoxdev/tosoxjr/commands/scramble/Scramble.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Scramble {
}
}

private static final int TIMEOUT_MS = 2 * 60 * 1000;
private static final int TIMEOUT_MS = 10 * 60 * 1000;
private static final int STOP_SIGN_CP = 0x1F6D1;

private final MessageChannel channel;
Expand All @@ -40,7 +40,6 @@ public class Scramble {
private Timer tmTimeout = new Timer();
private String embedMessageId;
private long timer;
private int attempts;
private String word;
private String scrambledWord;

Expand All @@ -58,8 +57,15 @@ public boolean initialize() {
return false;
}

// Bugfix for the words with spaces ("Ansicht")
word = word.replaceAll("\\s+","");

// Shuffle word
List<String> chars = new ArrayList<>(word.chars().mapToObj(c -> String.valueOf((char) c)).toList());
List<String> chars = new ArrayList<>(word
.toLowerCase()
.chars()
.mapToObj(c -> String.valueOf((char) c))
.toList());
Collections.shuffle(chars);
scrambledWord = String.join("", chars);

Expand Down Expand Up @@ -113,8 +119,6 @@ private void handleMessageReceivedEvent(MessageReceivedEvent event) {
// Reset timeout timer
resetTimer();

attempts++;

if (event.getMessage().getContentDisplay().equalsIgnoreCase(word)) {
endGame(GameState.WIN, sender.getName());
}
Expand Down Expand Up @@ -146,14 +150,14 @@ private String generateWord() {

private EmbedBuilder createGameEmbed(GameState state, String sender) {
EmbedBuilder gameEmbed = new EmbedBuilder();
gameEmbed.setTitle((coop ? "[CO-OP] " : "") + state.getTitle());
gameEmbed.setTitle(String.format("%s[%s] %s", coop ? "[CO-OP]" : "", language.isBlank() ? "EN" : language.toUpperCase(), state.getTitle()));
gameEmbed.setColor(Color.CYAN);
gameEmbed.addField(state == GameState.ONGOING ? "Word" : "The word was", state == GameState.ONGOING ? scrambledWord : word, false);
if (state == GameState.WIN) {
double time = (double)(System.currentTimeMillis() - timer) / 1000;
String results = coop
? String.format("%s guessed the word first after %.2fs", sender, time)
: String.format("You guessed the word after %.2fs and %d attempts", time, attempts);
: String.format("You guessed the word after %.2fs", time);
gameEmbed.addField("Results", results, false);
}
if (state == GameState.ONGOING) {
Expand All @@ -165,6 +169,7 @@ private EmbedBuilder createGameEmbed(GameState state, String sender) {
- React with the stop sign (🛑) to end the game
""", false);
}
gameEmbed.setFooter("Request made by @" + player, null);
return gameEmbed;
}
}
16 changes: 7 additions & 9 deletions src/main/java/de/tosoxdev/tosoxjr/utils/APIRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import de.tosoxdev.tosoxjr.Main;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
Expand Down Expand Up @@ -49,15 +48,14 @@ public static Object getJson(String query) {
return null;
}

try {
char fistChar = body.charAt(0);
if (fistChar == '{') {
return new JSONObject(body);
} catch (JSONException e) {
try {
return new JSONArray(body);
} catch (JSONException f) {
Main.getLogger().error("The json body for request '{}' is malformed", query);
return null;
}
} else if (fistChar == '[')
return new JSONArray(body);
else {
Main.getLogger().error("The json body for request '{}' is malformed", query);
return null;
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/de/tosoxdev/tosoxjr/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.concurrent.Callable;

public class Utils {
public static String getStringFromCallable(Callable<String> callable) {
public static <T> T getFromCallable(Callable<T> callable) {
try {
return callable.call();
} catch (Exception e) {
Expand Down

0 comments on commit 362c6b5

Please sign in to comment.