From 6a1e4d6cafe046fd133a17fb1f4b4695a83edf5f Mon Sep 17 00:00:00 2001 From: Tanish Azad Date: Mon, 18 Mar 2024 19:56:58 +0530 Subject: [PATCH 1/2] added fixes to 2048 game --- app/build.gradle | 2 + .../tjplays/games/game2048/Game2048.java | 12 ++++-- .../listeners/commands/Game2048Command.java | 41 +++++++++++-------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 78ce6e1..f81a2e3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -51,6 +51,8 @@ dependencies { implementation 'org.apache.logging.log4j:log4j-core:2.23.0' runtimeOnly 'org.apache.logging.log4j:log4j-slf4j18-impl:2.18.0' + implementation 'com.github.ben-manes.caffeine:caffeine:3.1.8' + testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1' } diff --git a/app/src/main/java/com/togetherjava/tjplays/games/game2048/Game2048.java b/app/src/main/java/com/togetherjava/tjplays/games/game2048/Game2048.java index f2c5263..1e5f65f 100644 --- a/app/src/main/java/com/togetherjava/tjplays/games/game2048/Game2048.java +++ b/app/src/main/java/com/togetherjava/tjplays/games/game2048/Game2048.java @@ -8,13 +8,17 @@ public final class Game2048 { public static final int ROWS = 4; public static final int COLUMNS = 4; + private static final Random random = new Random(); - private static Random random = new Random(); - private int[][] board = new int[ROWS][COLUMNS]; - private GameState state = GameState.ONGOING; - private int score = 0; + private int[][] board; + private GameState state; + private int score; public Game2048() { + board = new int[ROWS][COLUMNS]; + state = GameState.ONGOING; + score = 0; + for (int i = 0; i < 2; i++) spawnNewBlock(); } diff --git a/app/src/main/java/com/togetherjava/tjplays/listeners/commands/Game2048Command.java b/app/src/main/java/com/togetherjava/tjplays/listeners/commands/Game2048Command.java index b76b233..4e85078 100644 --- a/app/src/main/java/com/togetherjava/tjplays/listeners/commands/Game2048Command.java +++ b/app/src/main/java/com/togetherjava/tjplays/listeners/commands/Game2048Command.java @@ -1,9 +1,9 @@ package com.togetherjava.tjplays.listeners.commands; -import java.util.HashMap; -import java.util.Map; import java.util.concurrent.TimeUnit; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; import com.togetherjava.tjplays.games.game2048.Game2048; import com.togetherjava.tjplays.games.game2048.GameState; import com.togetherjava.tjplays.games.game2048.Move; @@ -28,10 +28,14 @@ public final class Game2048Command extends SlashCommand { private static final Emoji DOWN_EMOJI = Emoji.fromUnicode("⬇️"); private static final Emoji RIGHT_EMOJI = Emoji.fromUnicode("➡️"); - private Map sessions = new HashMap<>(); + private final Cache sessionsCache; public Game2048Command() { super(Commands.slash(COMMAND_NAME, "Game 2048")); + + sessionsCache = Caffeine.newBuilder() + .expireAfterWrite(1, TimeUnit.DAYS) + .build(); } @Override @@ -39,10 +43,9 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { Renderer2048 gameRenderer = new Renderer2048(new Game2048()); event.reply(gameMessage(gameRenderer, event.getUser().getId())) - .queue(hook -> { - hook.retrieveOriginal().queue(message -> sessions.put(message.getId(), gameRenderer)); - hook.retrieveOriginal().queueAfter(10, TimeUnit.HOURS, message -> sessions.remove(message.getId())); - }); + .flatMap(hook -> hook.retrieveOriginal()) + .onSuccess(message -> sessionsCache.put(message.getId(), gameRenderer)) + .queue(); } @Override @@ -55,24 +58,26 @@ public void onButtonInteraction(ButtonInteractionEvent event) { return; } - if (buttonId.contains("reset")) - sessions.get(event.getMessageId()).setGame(new Game2048()); - else if (buttonId.contains("delete")) { - sessions.remove(event.getMessageId()); + if (buttonId.contains("delete")) { + sessionsCache.invalidate(event.getMessageId()); event.getMessage().delete().queue(); return; } - Move move = null; + Renderer2048 gameRenderer = sessionsCache.getIfPresent(event.getMessageId()); - if (buttonId.contains("up")) move = Move.UP; - else if (buttonId.contains("down")) move = Move.DOWN; - else if (buttonId.contains("left")) move = Move.LEFT; - else if (buttonId.contains("right")) move = Move.RIGHT; + if (buttonId.contains("reset")) { + gameRenderer.setGame(new Game2048()); + } else { + Move move = null; + + if (buttonId.contains("up")) move = Move.UP; + else if (buttonId.contains("down")) move = Move.DOWN; + else if (buttonId.contains("left")) move = Move.LEFT; + else if (buttonId.contains("right")) move = Move.RIGHT; - Renderer2048 gameRenderer = sessions.get(event.getMessageId()); - if (move != null) gameRenderer.getGame().move(move); + } event.editMessage(MessageEditData.fromCreateData(gameMessage(gameRenderer, event.getUser().getId()))).queue(); } From d5bc6e4e3566bae9601ca5eee3077ab0f197826b Mon Sep 17 00:00:00 2001 From: Tanish Azad Date: Mon, 18 Mar 2024 22:34:57 +0530 Subject: [PATCH 2/2] fixed deleting --- .../tjplays/listeners/commands/Game2048Command.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/togetherjava/tjplays/listeners/commands/Game2048Command.java b/app/src/main/java/com/togetherjava/tjplays/listeners/commands/Game2048Command.java index 4e85078..d045d8e 100644 --- a/app/src/main/java/com/togetherjava/tjplays/listeners/commands/Game2048Command.java +++ b/app/src/main/java/com/togetherjava/tjplays/listeners/commands/Game2048Command.java @@ -60,7 +60,8 @@ public void onButtonInteraction(ButtonInteractionEvent event) { if (buttonId.contains("delete")) { sessionsCache.invalidate(event.getMessageId()); - event.getMessage().delete().queue(); + event.deferEdit().queue(); + event.getHook().deleteOriginal().queue(); return; }