Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added fixes to 2048 game #25

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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.10.2'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -28,21 +28,24 @@ public final class Game2048Command extends SlashCommand {
private static final Emoji DOWN_EMOJI = Emoji.fromUnicode("⬇️");
private static final Emoji RIGHT_EMOJI = Emoji.fromUnicode("➡️");

private Map<String, Renderer2048> sessions = new HashMap<>();
private final Cache<String, Renderer2048> sessionsCache;

public Game2048Command() {
super(Commands.slash(COMMAND_NAME, "Game 2048"));

sessionsCache = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.DAYS)
.build();
}

@Override
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
Expand All @@ -55,24 +58,27 @@ 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());
event.getMessage().delete().queue();
if (buttonId.contains("delete")) {
sessionsCache.invalidate(event.getMessageId());
event.deferEdit().queue();
event.getHook().deleteOriginal().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();
}
Expand Down
Loading