From 9e19e10b882c4596e4c413384ee57549f82142cd Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Sun, 3 Jul 2022 09:54:14 -0400 Subject: [PATCH] Fixed sorting on either int or time record. --- cogs/score/__init__.py | 6 ++--- highscores_config.json | 9 ++++--- utils/highscore_manage.py | 51 ++++++++++++++++---------------------- utils/highscore_message.py | 6 ++--- 4 files changed, 33 insertions(+), 39 deletions(-) diff --git a/cogs/score/__init__.py b/cogs/score/__init__.py index d4788fa..842ff48 100644 --- a/cogs/score/__init__.py +++ b/cogs/score/__init__.py @@ -15,7 +15,7 @@ class RegisterHighscoreChannel(commands.Cog, name="Score"): self.bot = bot @commands.command(name="score") - async def score(self, ctx: commands.Context, boss_name: str, category: str, score): + async def score(self, ctx: commands.Context, boss_name: str, category: str, score: str): """A command which calls a function to submit the score to highscores. Gathers required information from command and passes on. Usage: @@ -23,9 +23,7 @@ class RegisterHighscoreChannel(commands.Cog, name="Score"): ?score "Boss Name" "Category" "score" ``` """ - await highscore_manage.submit_score_int( - self, ctx, boss_name, category, ctx.author.display_name, score - ) + await highscore_manage.submit_score(self, ctx, boss_name, category, score) # This function will be called when this extension is loaded. diff --git a/highscores_config.json b/highscores_config.json index f7e2ea9..1f3cf9a 100755 --- a/highscores_config.json +++ b/highscores_config.json @@ -184,15 +184,18 @@ "categories": { "Kill Count": { "hardmode": true, - "ascending": false + "ascending": false, + "is_time_record": false }, "Kill Time": { "hardmode": true, - "ascending": true + "ascending": true, + "is_time_record": true }, "Spooned Log": { "hardmode": false, - "ascending": true + "ascending": true, + "is_time_record": false } }, "highscore_size": 3 diff --git a/utils/highscore_manage.py b/utils/highscore_manage.py index e1f7bec..a1f010d 100644 --- a/utils/highscore_manage.py +++ b/utils/highscore_manage.py @@ -9,9 +9,7 @@ from utils import highscore_message # pylint: disable=import-error from utils import data_storage # pylint: disable=import-error -async def submit_score_int( - self, ctx: commands.Context, boss_name: str, category: str, user: str, score: int -): +async def submit_score(self, ctx: commands.Context, boss_name: str, category: str, score: str): """ Adds a score to the high scores and sorts. Removes a score if larger than size limit. @param boss: the boss to add score to @@ -20,11 +18,29 @@ async def submit_score_int( @param score: score (int) to add """ # TODO update user submission to not have dupe users + + # create score tuple for either time or int + # define sort index + if highscores_config["categories"][category]["is_time_record"]: + score_list = score.split(":") + minutes = score_list[0] + seconds = score_list[1] + score_seconds = int(minutes) * 60 + int(seconds) + score_tuple = (ctx.author.display_name, score, score_seconds) + sort_index = 2 + + else: + score_tuple = (ctx.author.display_name, int(score)) + sort_index = 1 + scores = highscores_data[boss_name]["categories"][category] - score_tuple = (user, score) scores.append(score_tuple) - ascending = highscores_config["categories"][category] - scores.sort(key=lambda x: x[1], reverse=not ascending) + + # sort scores. + ascending = highscores_config["categories"][category]["ascending"] + scores.sort(key=lambda x: x[sort_index], reverse=not ascending) + + # enforce highscore size while len(scores) > highscores_config["highscore_size"]: scores.pop() highscores_data[boss_name]["categories"][category] = scores @@ -42,26 +58,3 @@ async def submit_score_int( # save data data_storage.save_highscores_data(highscores_data) - - -def submit_score_str(boss: str, category: str, user: str, score: str): - """ - Adds a score to the high scores and sorts. Removes a score if larger than size limit. - @param boss: the boss to add score to - @param category: the category to add score to - @param user: user who submitted score - @param score: score (string) to add - """ - # TODO update user submission to not have dupe users - # TODO sort the strings properly, update this function for string usage - scores = highscores_data[boss][category] - score_tuple = (user, score) - scores.append(score_tuple) - ascending = highscores_config["categories"][category] - scores.sort(key=lambda x: x[1], reverse=not ascending) - while len(scores) > highscores_config["highscore_size"]: - scores.pop() - highscores_data[boss][category] = scores - - # save data - data_storage.save_highscores_data(highscores_data) diff --git a/utils/highscore_message.py b/utils/highscore_message.py index 0c4fe79..a2c2adf 100644 --- a/utils/highscore_message.py +++ b/utils/highscore_message.py @@ -18,7 +18,7 @@ def format_highscore_message(boss_name: str): # Gather all categories and add to string for key, value in boss_data["categories"].items(): - ret_string += f"{key}".ljust(30, " ") + ret_string += f"{key}".ljust(50, " ") ret_string += "\n" highscore_size = highscores_config["highscore_size"] @@ -28,9 +28,9 @@ def format_highscore_message(boss_name: str): for key, value in boss_data["categories"].items(): if len(value) > i: score = value[i] - ret_string += f"{i+1}: {score[0]} - {score[1]}".ljust(30, " ") + ret_string += f"{i+1}: {score[0]} - {score[1]}".ljust(50, " ") else: - ret_string += f"{i+1}: submit your score".ljust(30, " ") + ret_string += f"{i+1}: submit your score".ljust(50, " ") ret_string += "\n" return ret_string