Created streaks

This commit is contained in:
2020-11-18 20:08:21 +01:00
parent 306b2e858d
commit acd7285695
18 changed files with 78 additions and 50 deletions

View File

@@ -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, int cur_streak DEFAULT 0, int max_streak DEFAULT 0, int streak_count 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, cur_streak int DEFAULT 0, max_streak int DEFAULT 0, streak_count int DEFAULT 0)".format(self.tablename))
def player_exists(self, discordid):
cursor = self.discorddbconn.get_cursor()
@@ -69,7 +69,7 @@ class QuizPlayersDB:
res = cursor.fetchone()
return res[0] == 1
# give the user with discordid his reward + start streak
def player_won(self, discordid, reward):
cursor = self.discorddbconn.get_cursor()
if self.player_exists(discordid):
@@ -79,19 +79,17 @@ class QuizPlayersDB:
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):
def get_current_streak(self):
cursor = self.discorddbconn.get_cursor(dictionary=True)
sql = f"SELECT discordid FROM {self.tablename} WHERE discordid={str(discordid)}"
sql = f"SELECT discordid, cur_streak FROM {self.tablename} WHERE cur_streak > 0"
cursor.execute(sql)
res = cursor.fetchone()
return None if res is None else res["discordid"]
return res
# set current streak to 0 from discorduser
def reset_streak(self, discordid):
@@ -109,14 +107,18 @@ class QuizPlayersDB:
# 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()
streaks = self.get_streaks(discordid)
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)}"
if streaks["cur_streak"] == 0:
sql = f"UPDATE {self.tablename} SET cur_streak = cur_streak + 1, max_streak = max_streak + 1, streak_count = streak_count + 1 WHERE discordid = {str(discordid)}"
else:
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()