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

88 lines
2.6 KiB
Java
Raw Permalink Normal View History

package linux.general.hackyquizbot;
2022-02-21 12:35:35 +01:00
import java.sql.Connection;
import java.sql.DriverManager;
2022-02-22 17:54:56 +01:00
import java.sql.ResultSet;
2022-02-21 12:35:35 +01:00
import java.sql.SQLException;
2022-02-22 17:54:56 +01:00
import java.sql.Statement;
import java.util.ArrayList;
public class QuizBackend {
2022-02-21 12:35:35 +01:00
private static final String dbUsername = Main.getCommandLineArgument(1);
private static final String dbPassword = Main.getCommandLineArgument(2);
2022-02-22 17:54:56 +01:00
private static final String dbName = "jdbc:mariadb://localhost/quizbot"; //we can make this configurable later
private static Connection dbConnection;
private static Statement statement;
2022-02-22 19:45:04 +01:00
// temporary hack to get something working
private static boolean once = true;
2022-02-21 12:35:35 +01:00
// CREATE TABLE questions(id INT(32) AUTO_INCREMENT PRIMARY KEY, question TEXT, max_score INT(8));
2022-02-22 17:54:56 +01:00
// 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;
2022-02-22 17:54:56 +01:00
private static ArrayList<String> categories;
static {
2022-02-22 17:54:56 +01:00
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
2022-02-21 12:35:35 +01:00
}
catch (SQLException e) {
2022-02-22 17:54:56 +01:00
System.err.println("Error while interacting with DB.");
2022-02-21 12:35:35 +01:00
e.printStackTrace();
2022-02-22 17:54:56 +01:00
System.exit(1);
2022-02-21 12:35:35 +01:00
}
}
public static String[] getCategories() {
2022-02-22 17:54:56 +01:00
return (String[]) categories.toArray();
}
2022-02-22 17:54:56 +01:00
//TODO: user aware random question picker
//TODO: function to determine whether a user should get more questions
2022-02-22 18:50:19 +01:00
public static boolean shouldGetMoreQuestions(String discordUserID) {
2022-02-22 19:45:04 +01:00
if (once) {
once=false;
return true;
}
2022-02-22 18:50:19 +01:00
return false;
}
2022-02-22 17:54:56 +01:00
//TODO: function to get a users score
public static class Question {
2022-02-20 19:37:48 +01:00
2022-02-22 17:54:56 +01:00
//TODO: get full question text with multiple choice answers
2022-02-22 17:54:56 +01:00
//TODO: get possible answers (scrambled per user)
2022-02-20 19:37:48 +01:00
2022-02-22 17:54:56 +01:00
//TODO: get correct answers (scrambled per user)
2022-02-22 17:54:56 +01:00
//TODO: submit answer
}
}