From e2b0c0ea89a2162ea62d7383243ebe7b9e6ea657 Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Fri, 29 Jul 2022 14:25:25 -0400 Subject: [PATCH 1/5] Enforcing python version because ordered dicts in 3.7 is required. --- highscores.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/highscores.py b/highscores.py index 6cc103a..edec65b 100644 --- a/highscores.py +++ b/highscores.py @@ -4,6 +4,7 @@ main file for pvm-highscores bot import logging import os +import sys import nextcord from nextcord.ext import commands @@ -32,6 +33,11 @@ submission_objects: dict = {} def main(): """Main function for bot.""" + min_python_version = (3, 7) + if sys.version_info < min_python_version: + print("ERROR: Python version must be >= 3.7") + sys.exit() + # Allows privledged intents for monitoring members joining, roles editing, and role assignments # These need to be enabled in the developer portal as well intents = nextcord.Intents.default() From 5a09beea31b248d0b44af564cdc1fa6d0d4016b5 Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Fri, 29 Jul 2022 15:56:37 -0400 Subject: [PATCH 2/5] Convert add_score to have scores as a dict. --- utilities/highscores_data.py | 37 +++++++----------------------------- 1 file changed, 7 insertions(+), 30 deletions(-) diff --git a/utilities/highscores_data.py b/utilities/highscores_data.py index 66ec10a..b97f900 100644 --- a/utilities/highscores_data.py +++ b/utilities/highscores_data.py @@ -66,40 +66,17 @@ class HighscoresData: minutes = score.split(":")[0] seconds = score.split(":")[1] score_seconds = int(minutes) * 60 + float(seconds) - score_tuple = (user, score, score_seconds) - sort_index = 2 - else: - score_tuple = (user, int(score)) + score_tuple = (score, score_seconds) sort_index = 1 + else: + score_tuple = int(score) + sort_index = 0 - # if user is in an existing tuple, delete it. - users_scores: "list[tuple]" = [score_tuple] - for tup in scores: - if user == tup[0]: - # Add tup to users_scores, remove from scores - users_scores.append(tup) + # Will not ensure new score is better than old one. Allows for correcting mistakes. - # sort users_scores - users_scores.sort( - key=lambda x: x[sort_index], - reverse=not ascending, - ) + scores[user] = score_tuple - for index, tup in enumerate(users_scores): - if index == 0: - # first score is the best score. - if tup not in scores: - scores.append(tup) - else: - # discard all other scores from scores list. - if tup in scores: - scores.remove(tup) - - # sort scores. - scores.sort( - key=lambda x: x[sort_index], - reverse=not ascending, - ) + scores = dict(sorted(scores.items(), key=lambda x: x[1][sort_index], reverse=not ascending)) self._data[str(guild_id)]["tables"][boss_name]["categories"][category] = scores save_highscores_data(self._data) From 9e9d6168143adb688aacd7de510b385273f32d3b Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Fri, 29 Jul 2022 16:16:16 -0400 Subject: [PATCH 3/5] Change tuple to list. Its stored as a list anyways. --- utilities/highscores_data.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utilities/highscores_data.py b/utilities/highscores_data.py index b97f900..88bd13e 100644 --- a/utilities/highscores_data.py +++ b/utilities/highscores_data.py @@ -66,15 +66,15 @@ class HighscoresData: minutes = score.split(":")[0] seconds = score.split(":")[1] score_seconds = int(minutes) * 60 + float(seconds) - score_tuple = (score, score_seconds) + score_list = [score, score_seconds] sort_index = 1 else: - score_tuple = int(score) + score_list = [int(score)] sort_index = 0 # Will not ensure new score is better than old one. Allows for correcting mistakes. - scores[user] = score_tuple + scores[user] = score_list scores = dict(sorted(scores.items(), key=lambda x: x[1][sort_index], reverse=not ascending)) From 306f983c8ed758379fdd0dd66caf6ea2205a427e Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Fri, 29 Jul 2022 16:35:45 -0400 Subject: [PATCH 4/5] Had to change access to scores. Somewhat weird due to them being dicts and dicts being difficult to access by index. --- utilities/utils.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/utilities/utils.py b/utilities/utils.py index 64df95e..e7bdd3f 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -17,27 +17,28 @@ def format_highscore_message(boss_name: str, guild_id: int): ret_string = f"**{boss_name}**\n```" # Gather all categories and add to string - for key, value in highscores_data.get_categories_data(guild_id, boss_name): - ret_string += f"{key}".ljust(26, " ") + for category_name in highscores_data.get_table_categories(guild_id, boss_name): + ret_string += f"{category_name}".ljust(26, " ") ret_string += "\n" highscore_size = highscores_data.get_config_highscore_size() # list rankings in a single line. Start with first place for each category and then continue for i in range(highscore_size): - for key, value in highscores_data.get_categories_data(guild_id, boss_name): - if len(value) > i: - score_pair = value[i] - + for category_name, category_data in highscores_data.get_categories_data( + guild_id, boss_name + ): + if len(category_data) > i: # pad username to max length. - username: str = score_pair[0].ljust( + username: str = list(category_data.keys())[i].ljust( highscores_data.get_guild_username_length(guild_id), " " ) - score: str = str(score_pair[1]) + # turns category data value into a string. Very janky. + score: str = str(list(category_data.values())[i][0]) # pad score to correct length # MM:SS.ms is 7 characters long - if highscores_data.get_config_time_record(key): + if highscores_data.get_config_time_record(category_name): # if there are single digit minutes, add a space for formatting. if score.find(":") and len(score.split(":", maxsplit=1)[0]) < 2: score = " " + score From f33dc8ce37df50c4e735ac838eacf668faaea90d Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Fri, 29 Jul 2022 16:47:30 -0400 Subject: [PATCH 5/5] Skeleton needs to have dict for category scores. --- utilities/highscores_data.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utilities/highscores_data.py b/utilities/highscores_data.py index 88bd13e..071245d 100644 --- a/utilities/highscores_data.py +++ b/utilities/highscores_data.py @@ -32,8 +32,8 @@ class HighscoresData: skeleton["tables"][boss] = {"message_id": 0} boss_categories = {} for category in categories: - rank_list = [] - boss_categories[category] = rank_list + scores_list = {} + boss_categories[category] = scores_list skeleton["tables"][boss]["categories"] = boss_categories self._data[str(guild_id)] = skeleton