Added wordgame

This commit is contained in:
2020-11-14 23:53:59 +01:00
parent e23ddf049b
commit 1d4acc40e0
3 changed files with 193 additions and 4 deletions

View File

@@ -10,6 +10,8 @@ class PlayerNotLinked(Exception):
pass
class QuestionNotFound(Exception):
pass
class WordNotFound(Exception):
pass
class PlayerNotFound(Exception):
pass
@@ -86,6 +88,67 @@ class QuizPlayersDB:
def close(self):
self.discorddbconn.close()
class WordGameDB:
def __init__(self):
self.tablename = "wordGame"
self.discorddbconn = DatabaseConnection(database="worldcraft_discord")
cursor = self.discorddbconn.get_cursor()
cursor.execute("create table IF NOT EXISTS {} (id int NOT NULL PRIMARY KEY AUTO_INCREMENT, word text NOT NULL, reward int NOT NULL, asked int DEFAULT 0)".format(self.tablename))
def add_word(self, word, reward):
cursor = self.discorddbconn.get_cursor()
sql = f"insert into {self.tablename} (word, reward) VALUES ('{word}', {str(reward)})"
try:
cursor.execute(sql)
self.discorddbconn.get_db().commit()
return cursor.lastrowid
except:
raise SQLInsertError()
def rm_word(self, id):
cursor = self.discorddbconn.get_cursor()
cursor.execute("select count(*) from {} where id={}".format(self.tablename, str(id)))
res = cursor.fetchone()
if res[0] == 1:
sql = f"DELETE FROM {self.tablename} WHERE id = {str(id)}"
cursor.execute(sql)
self.discorddbconn.get_db().commit()
else:
raise QuestionNotFound()
def get_words(self):
# word, used, reward
cursor = self.discorddbconn.get_cursor(dictionary=True)
sql = f"SELECT * FROM {self.tablename}"
cursor.execute(sql)
return list(cursor.fetchall())
def word_asked_increment(self, w_id):
cursor = self.discorddbconn.get_cursor()
sql = f"UPDATE {self.tablename} SET asked = asked + 1 WHERE id = {str(w_id)}"
cursor.execute(sql)
self.discorddbconn.get_db().commit()
def count_words(self):
cursor = self.discorddbconn.get_cursor()
cursor.execute("select count(*) from {}".format(self.tablename))
res = cursor.fetchone()
return res[0]
def get_random_word(self):
if self.count_words() != 0:
cursor = self.discorddbconn.get_cursor(dictionary=True)
sql = f"SELECT * FROM {self.tablename} ORDER BY RAND() LIMIT 1"
cursor.execute(sql)
return cursor.fetchone()
else:
raise WordNotFound()
def close(self):
self.discorddbconn.close()
class QuizDB:
def __init__(self):
self.tablename = "questions"