From b4cc87b7ca622d978321c1e4a2b9ae03fdd8370f Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Thu, 2 Feb 2023 01:12:43 +0100 Subject: [PATCH] Added time feature for quiz You have to wait a min time after you can play the quiz again. Currently 5 minutes --- .../java/org/bitbiome/commands/QuizCommand.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index 658a898..e261831 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -5,6 +5,7 @@ import org.bitbiome.classes.JsonParser; import org.json.JSONArray; import org.json.JSONObject; +import java.util.Date; import java.util.Random; import java.util.Scanner; @@ -17,6 +18,14 @@ 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."); + return; + } + JSONArray fragen = quiz.getJSONArray("Quiz"); JSONObject frage = fragen.getJSONObject(random(fragen.length())); @@ -39,6 +48,11 @@ public class QuizCommand implements CommandAPI { } print("Das Quiz ist vorbei."); + + Date d = new Date(); + long lastPlayed = d.getTime(); + quiz.put("lastPlayed", lastPlayed); + JsonParser.writeObject(path, quiz); } public static boolean answerIsCorrect(int picked, String answer, JSONArray answers) {