Hacky-Quizbot/src/main/java/linux/general/hackyquizbot/QuizBackend.java

88 lines
2.6 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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/quizbot"; //we can make this configurable later
private static Connection dbConnection;
private static Statement statement;
// temporary hack to get something working
private static boolean once = true;
// CREATE TABLE questions(id INT(32) AUTO_INCREMENT PRIMARY KEY, question TEXT, max_score INT(8));
// 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 ArrayList<String> categories;
static {
categories = new ArrayList<String>();
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.size(); i++) {
System.err.print(" " + categories.get(i));
}
System.err.println();
//TODO: populate allQuestions
}
catch (SQLException e) {
System.err.println("Error while interacting with DB.");
e.printStackTrace();
System.exit(1);
}
}
public static String[] getCategories() {
return (String[]) categories.toArray();
}
//TODO: user aware random question picker
//TODO: function to determine whether a user should get more questions
public static boolean shouldGetMoreQuestions(String discordUserID) {
if (once) {
once=false;
return true;
}
return false;
}
//TODO: function to get a users score
public static class Question {
//TODO: get full question text with multiple choice answers
//TODO: get possible answers (scrambled per user)
//TODO: get correct answers (scrambled per user)
//TODO: submit answer
}
}