From 306b2e858da85740017df2d25b95e4677b73216d Mon Sep 17 00:00:00 2001 From: victormylle Date: Wed, 18 Nov 2020 01:15:41 +0100 Subject: [PATCH] Created quizstreak db functions --- data/DatabaseConnection.py | 45 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/data/DatabaseConnection.py b/data/DatabaseConnection.py index 1f9e20a..f166804 100755 --- a/data/DatabaseConnection.py +++ b/data/DatabaseConnection.py @@ -60,7 +60,7 @@ class QuizPlayersDB: else: 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)) + cursor.execute("create table IF NOT EXISTS {} (discordid tinytext NOT NULL, wins int DEFAULT 0, rewards int DEFAULT 0, toclaim int DEFAULT 0, int cur_streak DEFAULT 0, int max_streak DEFAULT 0, int streak_count DEFAULT 0)".format(self.tablename)) def player_exists(self, discordid): cursor = self.discorddbconn.get_cursor() @@ -76,8 +76,28 @@ class QuizPlayersDB: sql = f"UPDATE {self.tablename} SET wins = wins + 1, rewards = rewards + {str(reward)}, toclaim = toclaim + {str(reward)} WHERE discordid = {str(discordid)}" cursor.execute(sql) else: - sql = f"INSERT INTO {self.tablename} (discordid, wins, rewards, toclaim) VALUES ({str(discordid)}, 1, {reward}, {reward})" + sql = f"INSERT INTO {self.tablename} (discordid, wins, rewards, toclaim) VALUES ({str(discordid)}, 1, {reward}, {reward}, 0, 0)" cursor.execute(sql) + + previousstreak_discordid = self.get_current_streak_discordid() + if previousstreak_discordid is not None and previousstreak_discordid != discordid: + self.reset_streak(previousstreak_discordid) + self.increment_cur_streak(discordid) + self.discorddbconn.get_db().commit() + + # get discord id of user with current streak + def get_current_streak_discordid(self): + cursor = self.discorddbconn.get_cursor(dictionary=True) + sql = f"SELECT discordid FROM {self.tablename} WHERE discordid={str(discordid)}" + cursor.execute(sql) + res = cursor.fetchone() + return None if res is None else res["discordid"] + + # set current streak to 0 from discorduser + def reset_streak(self, discordid): + cursor = self.discorddbconn.get_cursor(dictionary=True) + sql = f"UPDATE {self.tablename} SET cur_streak = 0 WHERE discordid = {str(discordid)}" + cursor.execute(sql) self.discorddbconn.get_db().commit() def get_top_players(self, limit = 20): @@ -86,6 +106,27 @@ class QuizPlayersDB: cursor.execute(sql) return list(cursor.fetchall()) + # increment the current streak of user with discordid (+ checks to increment max_streak and streak_count) + def increment_cur_streak(self, discordid): + cursor = self.discorddbconn.get_cursor(dictionary=True) + streaks = self.get_streaks() + if streaks["cur_streak"] == streaks["max_streak"]: + sql = f"UPDATE {self.tablename} SET cur_streak = cur_streak + 1, max_streak = max_streak + 1 WHERE discordid = {str(discordid)}" + else: + if streaks["cur_streak"] == 0: + sql = f"UPDATE {self.tablename} SET cur_streak = cur_streak + 1, streak_count = streak_count + 1 WHERE discordid = {str(discordid)}" + else: + sql = f"UPDATE {self.tablename} SET cur_streak = cur_streak + 1 WHERE discordid = {str(discordid)}" + cursor.execute(sql) + self.discorddbconn.get_db().commit() + + # get the streak information from user with discordid + def get_streaks(self, discordid): + cursor = self.discorddbconn.get_cursor(dictionary=True) + sql = f"SELECT cur_streak, max_streak, streak_count FROM {self.tablename} WHERE discordid={str(discordid)}" + cursor.execute(sql) + return cursor.fetchone() + def close(self): self.discorddbconn.close()