added game timeout after 10 minutes with no action

This commit is contained in:
AffluentAvo
2020-08-10 14:52:04 +02:00
parent 5137d33e95
commit 38bc58ac0b
3 changed files with 41 additions and 10 deletions
@@ -3,6 +3,7 @@ package me.polymarsdev.sokobot;
import me.polymarsdev.sokobot.database.Database;
import me.polymarsdev.sokobot.listener.CommandListener;
import me.polymarsdev.sokobot.listener.GameListener;
import me.polymarsdev.sokobot.util.GameUtil;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
@@ -75,6 +76,7 @@ public class Bot {
builder.setActivity(Activity.playing("@Sokobot for info!"));
builder.addEventListeners(new GameListener(), new CommandListener());
shardManager = builder.build();
GameUtil.runGameTimer();
Thread consoleThread = new Thread(() -> {
Scanner s = new Scanner(System.in);
while (s.hasNextLine()) {
+16 -2
View File
@@ -4,6 +4,8 @@ import me.polymarsdev.sokobot.objects.Grid;
import me.polymarsdev.sokobot.util.GameUtil;
import net.dv8tion.jda.api.entities.*;
import java.util.concurrent.TimeUnit;
public class Game {
long gameMessageID;
long channelID;
@@ -13,6 +15,7 @@ public class Game {
public int level = 1;
int width = 9;
int height = 6;
public long lastAction;
Grid grid = new Grid(width, height, level, playerEmote);
public Game(User user) {
@@ -36,14 +39,25 @@ public class Game {
height = 6;
grid = new Grid(width, height, level, playerEmote);
gameActive = true;
lastAction = System.currentTimeMillis();
GameUtil.sendGameEmbed(channel, String.valueOf(level), grid.toString(), user);
}
}
public void stop() {
gameActive = false;
TextChannel textChannel = Bot.getShardManager().getTextChannelById(channelID);
if (textChannel != null) {
textChannel.retrieveMessageById(gameMessageID).queue(gameMessage -> gameMessage.delete().queue());
}
}
public void run(Guild guild, TextChannel channel, String userInput) {
lastAction = System.currentTimeMillis();
if (userInput.equals("stop") && gameActive) {
channel.sendMessage("Thanks for playing, " + user.getAsMention() + "!").queue();
gameActive = false;
stop();
channel.sendMessage("Thanks for playing, " + user.getAsMention() + "!")
.queue(msg -> msg.delete().queueAfter(10, TimeUnit.SECONDS));
}
if (userInput.equals("play") && !gameActive) {
newGame(channel);
@@ -9,6 +9,8 @@ import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.User;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
public class GameUtil {
@@ -34,10 +36,8 @@ public class GameUtil {
EmbedBuilder embed = new EmbedBuilder();
embed.setTitle("Sokobot | Level " + level);
embed.setDescription(game);
embed.addField(
"Enter direction (``up``, ``down``, ``left``, ``right``/``wasd``), ``r`` to reset or ``mr`` to "
+ "recreate the map",
"", false);
embed.addField("Enter direction (``up``, ``down``, ``left``, ``right``/``wasd``), ``r`` to reset or ``mr`` to "
+ "recreate the map", "", false);
embed.addField("Player", user.getAsMention(), false);
channel.sendMessage(embed.build()).queue();
}
@@ -46,10 +46,8 @@ public class GameUtil {
EmbedBuilder embed = new EmbedBuilder();
embed.setTitle("Sokobot | Level " + level);
embed.setDescription(game);
embed.addField(
"Enter direction (``up``, ``down``, ``left``, ``right``/``wasd``), ``r`` to reset or ``mr`` to "
+ "recreate the map",
"", false);
embed.addField("Enter direction (``up``, ``down``, ``left``, ``right``/``wasd``), ``r`` to reset or ``mr`` to "
+ "recreate the map", "", false);
embed.addField("Player", user.getAsMention(), false);
message.editMessage(embed.build()).queue();
}
@@ -63,4 +61,21 @@ public class GameUtil {
embed.setFooter("You can also press any reaction to continue.");
message.editMessage(embed.build()).queue();
}
public static void runGameTimer() {
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
long now = System.currentTimeMillis();
for (long playerId : games.keySet()) {
Game game = games.get(playerId);
long timeDifference = now - game.lastAction;
if (timeDifference > 10 * 60 * 1000) {
game.stop();
GameUtil.removeGame(playerId);
}
}
}
}, 10 * 60 * 1000, 60 * 1000);
}
}