Started creating quiz reward system
This commit is contained in:
@@ -40,6 +40,30 @@ class DatabaseConnection:
|
||||
def close(self):
|
||||
self.mydb.close()
|
||||
|
||||
class QuizPlayersDB:
|
||||
def __init__(self):
|
||||
self.tablename = "quizplayers"
|
||||
self.discorddbconn = DatabaseConnection(database="worldcraft_discord")
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
cursor.execute("create table IF NOT EXISTS {} (discordid tinytext NOT NULL, wins int DEFAULT 0, rewards int DEFAULT 0, toclaim int DEFAULT 0)".format(self.tablename))
|
||||
|
||||
def player_exists(self, discordid):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
sql = f"SELECT count(*) FROM {self.tablename} WHERE discordid={str(discordid)}"
|
||||
cursor.execute(sql)
|
||||
res = cursor.fetchone()
|
||||
return res[0] == 1
|
||||
|
||||
def player_won(self, discordid, reward):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
if self.player_exists(discordid):
|
||||
sql = f"UPDATE {self.tablename} SET wins = wins + 1, rewards = rewards + {str(reward)}, toclaim = toclaim + {str(reward)} WHERE id = {str(q_id)}"
|
||||
cursor.execute(sql)
|
||||
else:
|
||||
sql = f"INSERT INTO {self.tablename} (discordid, wins, rewards, toclaim) VALUES ({discordid}, 1, {reward}, {reward})"
|
||||
cursor.execute(sql)
|
||||
self.discorddbconn.get_db().commit()
|
||||
|
||||
class QuizDB:
|
||||
def __init__(self):
|
||||
self.tablename = "questions"
|
||||
@@ -60,12 +84,12 @@ class QuizDB:
|
||||
|
||||
def rm_question(self, id):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
cursor.execute("select count(*) from {} where id='{}'".format(self.tablename, str(id)))
|
||||
cursor.execute("select count(*) from {} where id={}".format(self.tablename, str(id)))
|
||||
res = cursor.fetchone()
|
||||
if count_questions == 1:
|
||||
sql = f"DELETE FROM {self.tablename} WHERE id = '{str(id)}'"
|
||||
if res[0] == 1:
|
||||
sql = f"DELETE FROM {self.tablename} WHERE id = {str(id)}"
|
||||
cursor.execute(sql)
|
||||
res = self.discorddbconn.get_db().commit()
|
||||
self.discorddbconn.get_db().commit()
|
||||
else:
|
||||
raise QuestionNotFound()
|
||||
|
||||
@@ -81,8 +105,15 @@ class QuizDB:
|
||||
res = cursor.fetchone()
|
||||
return res[0]
|
||||
|
||||
def question_asked_increment(self, q_id):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
sql = f"UPDATE {self.tablename} SET asked = asked + 1 WHERE id = {str(q_id)}"
|
||||
cursor.execute(sql)
|
||||
self.discorddbconn.get_db().commit()
|
||||
|
||||
|
||||
def get_random_question(self):
|
||||
if self.count_questions != 0:
|
||||
if self.count_questions() != 0:
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
sql = f"SELECT * FROM {self.tablename} ORDER BY RAND() LIMIT 1"
|
||||
cursor.execute(sql)
|
||||
|
||||
Reference in New Issue
Block a user