88 lines
2.6 KiB
Java
88 lines
2.6 KiB
Java
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 user’s 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
|
||
}
|
||
}
|