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
@@ -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);
}
}