Browse Source

Added time feature for quiz

You have to wait a min time after you can play the quiz again. Currently 5 minutes
remotes/origin/quiz
Friederike von Gruben 2 years ago
parent
commit
b4cc87b7ca
  1. 14
      src/main/java/org/bitbiome/commands/QuizCommand.java

14
src/main/java/org/bitbiome/commands/QuizCommand.java

@ -5,6 +5,7 @@ import org.bitbiome.classes.JsonParser;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.Date;
import java.util.Random; import java.util.Random;
import java.util.Scanner; import java.util.Scanner;
@ -17,6 +18,14 @@ public class QuizCommand implements CommandAPI {
String path = "src\\main\\resources\\quiz.json"; String path = "src\\main\\resources\\quiz.json";
JSONObject quiz = JsonParser.readJSONFile(path); 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"); JSONArray fragen = quiz.getJSONArray("Quiz");
JSONObject frage = fragen.getJSONObject(random(fragen.length())); JSONObject frage = fragen.getJSONObject(random(fragen.length()));
@ -39,6 +48,11 @@ public class QuizCommand implements CommandAPI {
} }
print("Das Quiz ist vorbei."); 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) { public static boolean answerIsCorrect(int picked, String answer, JSONArray answers) {

Loading…
Cancel
Save