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

146 lines
4.4 KiB
Java
Raw 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.SQLException;
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
// 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 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;
static {
try (Connection connection = DriverManager.getConnection(dbName, dbUsername, dbPassword)) {
System.err.println("Database connected!");
}
catch (SQLException e) {
System.err.println("Could not connect to DB.");
e.printStackTrace();
}
//TODO: populate allQuestions
//TODO: populate categories
}
public static String[] getCategories() {
return categories;
}
public static Question getRandomQuestionForCategory(String category) {
//TODO: loop
//TODO: pick random question
//TODO: check if question is in category, return if it is
return null;
}
public static Question getNewRandomQuestionForCategory(String discordUserID, String category) {
//TODO: get list of previously displayed questions for that user
//TODO: retrieve list of answers that user has given to get the question IDs from there
//TODO: find a random question that isnt on said list
//TODO: add the question to said list and return it
return null;
}
public static int getScoreForUser(String discordUserID) {
int score = 0;
//TODO: iterate over all questions
//TODO: get achieved score for the user for the question
//TODO: add to score
return score;
}
public static class Question {
private String text;
private char[] validAnswers;
private char[] correctAnswers;
private int scoreValue;
private int ID;
private ArrayList<String> categories = new ArrayList<String>();
/**
* @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 arent
* @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<this.categories.size(); i++) {
if (this.categories.get(i).equals(category)) return true;
}
return false;
}
public void addCategory(String category) {
this.categories.add(category);
}
/**
* @return the full question text including multiple choice options with letters that correspond to getValidResponses()
*/
public String getQuestionTextWithOptions() {
return text;
}
/**
* @return a character array containing all the valid answers
*/
public char[] getValidResponses() {
return this.validAnswers;
}
/**
* @return a character array containing all the correct answers
*/
public char[] getCorrectResponses() {
return this.correctAnswers;
}
/**
* This is used to send the users reply back to the backend. Can be called
* multiple times to add answers for questions with multiple possible answers.
*
* @param discordUserID
* @param answer
*/
public void addAnswer(String discordUserID, char answer) {
//TODO: store question ID, user ID, and the chosen answer
}
/**
* Used to balance easy and difficult questions. Difficult questions will have a
* higher value so if someone gets a bunch of them they will get less questions
* overall.
*
* @return
*/
public int getQuestionScoreValue() {
return this.scoreValue;
}
public int getAchievedScore(String discordUserID) {
//TODO: retrieve given answers by user ID and question ID
//TODO: calculate score
return 0;
}
}
}