From d08bdb85e0d886dbd75685f9096539833407c757 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Thu, 2 Feb 2023 01:19:28 +0100 Subject: [PATCH] refactoring: Outsourced time feature The time feature for the quiz has a seperate method now which returns the difference of the current time and the time you played the last time. If the difference is greater than 0 you can't play. --- .../java/org/bitbiome/commands/QuizCommand.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index e261831..81e7b3d 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -18,11 +18,9 @@ public class QuizCommand implements CommandAPI { String path = "src\\main\\resources\\quiz.json"; JSONObject quiz = JsonParser.readJSONFile(path); - long currentTime = System.currentTimeMillis(); - long minTime = Long.parseLong(quiz.get("lastPlayed").toString()) + (60 * 5 * 1000); - if (minTime >= currentTime) { - long diff = minTime - currentTime; - System.out.println("Du darfst erst in " + diff / 1000 / 60 + " minuten spielen."); + long diffTime = canPlayAgain(quiz.getLong("lastPlayed")); + if (diffTime > 0) { + print("Du darfst erst in " + diffTime / 1000 / 60 + " minuten spielen."); return; } @@ -87,6 +85,12 @@ public class QuizCommand implements CommandAPI { return gold; } + public static long canPlayAgain(long lastPlayedTime) { + long currentTime = System.currentTimeMillis(); + long minTime = lastPlayedTime + (60 * 5 * 1000); + return minTime - currentTime; + } + }