/* This file is part of the ZeroFusion MapleStory Server Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc> Matthias Butz <matze@odinms.de> Jan Christian Meyer <vimes@odinms.de> ZeroFusion organized by "RMZero213" <RMZero213@hotmail.com> This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation. You may not use, modify or distribute this program under any other version of the GNU Affero General Public License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package server.events; import database.DatabaseConnection; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import server.Randomizer; import tools.Pair; public class MapleOxQuizFactory { private final Map<Pair<Integer, Integer>, MapleOxQuizEntry> questionCache = new HashMap<>(); private static final MapleOxQuizFactory instance = new MapleOxQuizFactory(); public MapleOxQuizFactory() { initialize(); } public static MapleOxQuizFactory getInstance() { return instance; } public Entry<Pair<Integer, Integer>, MapleOxQuizEntry> grabRandomQuestion() { final int size = questionCache.size(); while(true) { for (Entry<Pair<Integer, Integer>, MapleOxQuizEntry> oxquiz : questionCache.entrySet()) { if (Randomizer.nextInt(size) == 0) { return oxquiz; } } } } private void initialize() { try { Connection con = DatabaseConnection.getConnection(); try (PreparedStatement ps = con.prepareStatement("SELECT * FROM wz_oxdata"); ResultSet rs = ps.executeQuery()) { while (rs.next()) { questionCache.put(new Pair<>(rs.getInt("questionset"), rs.getInt("questionid")), get(rs)); } } } catch (Exception e) { } } private MapleOxQuizEntry get(ResultSet rs) throws SQLException { return new MapleOxQuizEntry(rs.getString("question"), rs.getString("display"), getAnswerByText(rs.getString("answer")), rs.getInt("questionset"), rs.getInt("questionid")); } private int getAnswerByText(String text) { if (text.equalsIgnoreCase("x")) { return 0; } else if (text.equalsIgnoreCase("o")) { return 1; } else { return -1; } } public static class MapleOxQuizEntry { private String question, answerText; private int answer, questionset, questionid; public MapleOxQuizEntry(String question, String answerText, int answer, int questionset, int questionid) { this.question = question; this.answerText = answerText; this.answer = answer; this.questionset = questionset; this.questionid = questionid; } public String getQuestion() { return question; } public String getAnswerText() { return answerText; } public int getAnswer() { return answer; } public int getQuestionSet() { return questionset; } public int getQuestionId() { return questionid; } } }