From d5201044ecd56bdfff80e3aa3c0007cfd18e581f Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Tue, 22 Feb 2022 17:54:56 +0100 Subject: [PATCH] start over --- .../general/hackyquizbot/QuizBackend.java | 148 +++++------------- 1 file changed, 40 insertions(+), 108 deletions(-) diff --git a/src/main/java/linux/general/hackyquizbot/QuizBackend.java b/src/main/java/linux/general/hackyquizbot/QuizBackend.java index 6cd5e89..6ffedcd 100644 --- a/src/main/java/linux/general/hackyquizbot/QuizBackend.java +++ b/src/main/java/linux/general/hackyquizbot/QuizBackend.java @@ -2,144 +2,76 @@ package linux.general.hackyquizbot; import java.sql.Connection; import java.sql.DriverManager; +import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.util.ArrayList; public class QuizBackend { private static final String dbUsername = Main.getCommandLineArgument(1); private static final String dbPassword = Main.getCommandLineArgument(2); - private static final String dbName = "jdbc:mariadb://localhost:3306/quizbot"; //we can make this configurable later + private static final String dbName = "jdbc:mariadb://localhost/quizbot"; //we can make this configurable later + private static Connection dbConnection; + private static Statement statement; // CREATE TABLE questions(id INT(32) AUTO_INCREMENT PRIMARY KEY, question TEXT, max_score INT(8)); - // CREATE TABLE answer_coices(id INT(32), answer VARCHAR(2048), correctness BOOLEAN); + // CREATE TABLE answer_choices(id INT(32), answer VARCHAR(2048), correctness BOOLEAN); // CREATE TABLE user_answers(id INT(32), given_answer VARCHAR(2048), discord_user VARCHAR(1024)); // CREATE TABLE categories(id INT(32), category VARCHAR(1024)); private static Question[] allQuestions; - private static String[] categories; + private static ArrayList categories; static { - try (Connection connection = DriverManager.getConnection(dbName, dbUsername, dbPassword)) { - System.err.println("Database connected!"); + + categories = new ArrayList(); + try { + dbConnection = DriverManager.getConnection(dbName, dbUsername, dbPassword); + System.err.println("Database connected."); + statement = dbConnection.createStatement(); + System.err.println("Statement object created."); + // populate categories + ResultSet resultSet = statement.executeQuery("SELECT category FROM categories;"); + while (resultSet.next()) { + String category = resultSet.getString("category"); + if (!categories.contains(category)) { + categories.add(category); + } + } + System.err.print("Loaded categories:"); + for (int i = 0; i categories = new ArrayList(); - - /** - * @param ID unique ID for the question - * @param question The string for the question text - * @param answers The multiple choice answers - * @param mask A mask to be laid over answers to decide which answers are correct and which aren’t - * @param scoreValue See getQuestionScoreValue() - */ - public Question(int ID, String question, String[] answers, boolean[] mask, int scoreValue) { - //TODO: build text, valid answers and correct answers - this.ID = ID; - this.scoreValue = scoreValue; - } - - //TODO: need a way to get back from the answer letter to the text answer and vice versa - - public boolean isInCategory(String category) { - for (int i=0; i