Compare commits

...

3 Commits

Author SHA1 Message Date
BodgeMaster 113323da00 reworked how users are handled 2022-02-22 18:09:37 +01:00
BodgeMaster d5201044ec start over 2022-02-22 17:54:56 +01:00
BodgeMaster 5a37675753 fixed typo 2022-02-21 12:54:01 +01:00
3 changed files with 53 additions and 124 deletions

View File

@ -4,6 +4,7 @@ import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
@ -20,21 +21,24 @@ public class Main {
CheckDeploy check = new CheckDeploy(7500, "/var/www/deployment/Hacky-Quizbot/id.txt");
check.start();
//TODO: remove, this is just a way to force loading of the QuizBackend class while it isnt being loaded automatically
//TODO: remove, this is just a way of force loading of the QuizBackend class while it isnt being loaded automatically
QuizBackend backend = new QuizBackend();
DiscordApi api = new DiscordApiBuilder().setToken(commandLineArguments[0]).login().join();
//Trivia management
Member member = new Member(api);
//user management
ArrayList<QuizHandler> QuizHandlers = new ArrayList<QuizHandler>();
// small status command
// commands
api.addMessageCreateListener(event -> {
if (event.getMessageContent().equalsIgnoreCase("!ping")) {
event.getChannel().sendMessage("Pong!\nHacky the quiz bot has been running since "+startupTime);
}
if (event.getMessageContent().equalsIgnoreCase("!trivia")) {
QuizHandlers.add(new QuizHandler(api, event.getMessageAuthor().asUser().get()));
}
});
}
public static String getCommandLineArgument(int index) {

View File

@ -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<String> categories;
static {
try (Connection connection = DriverManager.getConnection(dbName, dbUsername, dbPassword)) {
System.err.println("Database connected!");
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("Could not connect to DB.");
System.err.println("Error while interacting with DB.");
e.printStackTrace();
System.exit(1);
}
//TODO: populate allQuestions
//TODO: populate categories
}
public static String[] getCategories() {
return categories;
return (String[]) categories.toArray();
}
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;
}
//TODO: user aware random question picker
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;
}
//TODO: function to determine whether a user should get more questions
//TODO: function to get a users 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);
}
//TODO: get full question text with multiple choice answers
/**
* @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;
}
//TODO: get possible answers (scrambled per user)
/**
* @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;
}
//TODO: get correct answers (scrambled per user)
public int getAchievedScore(String discordUserID) {
//TODO: retrieve given answers by user ID and question ID
//TODO: calculate score
return 0;
}
//TODO: submit answer
}
}

View File

@ -5,21 +5,14 @@ import org.javacord.api.entity.user.User;
import org.javacord.api.entity.message.embed.EmbedBuilder;
//TODO: find a better name for this class
public class Member {
public class QuizHandler {
private DiscordApi api;
private User user;
public Member(DiscordApi api) {
public QuizHandler(DiscordApi api, User user) {
this.api = api;
this.api.addMessageCreateListener(event ->{
if(event.getMessageContent().equalsIgnoreCase("!trivia")) {
if(event.getMessageAuthor().asUser().isPresent()) {
this.user = event.getMessageAuthor().asUser().get();
this.user.sendMessage("Hi! I'm Hacky! **Fuck you**! :D");
}
}
});
this.user = user;
user.sendMessage("Hi! Im Hacky.\nThis is a test.");
}
public void sendMessage(String question, String[] options) {