From f204be8d52a98490c2b8dcf11cd5918223b18550 Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Wed, 29 Jun 2022 19:52:19 -0400 Subject: [PATCH] Add score submission command. Add highscore management function. Change how config file and data file are defined. --- cogs/post_highscores/__init__.py | 1 - cogs/score/__init__.py | 33 ++++++++++++++++++++ highscores_config.json | 13 +++----- utils/highscore_manage.py | 52 ++++++++++++++++++++++++++++++++ utils/highscore_message.py | 3 +- 5 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 cogs/score/__init__.py create mode 100644 utils/highscore_manage.py diff --git a/cogs/post_highscores/__init__.py b/cogs/post_highscores/__init__.py index 406eb88..19ecb06 100644 --- a/cogs/post_highscores/__init__.py +++ b/cogs/post_highscores/__init__.py @@ -36,7 +36,6 @@ class PostHighscores(commands.Cog, name="Post Highscores"): return for boss in highscores_config["bosses"]: await highscore_message.send_highscore_message(channel, boss) - break data_storage.save_highscores_data(highscores_data) diff --git a/cogs/score/__init__.py b/cogs/score/__init__.py new file mode 100644 index 0000000..19e7a2a --- /dev/null +++ b/cogs/score/__init__.py @@ -0,0 +1,33 @@ +""" +Bot command to submit a score +""" + +from nextcord.ext import commands +from utils import highscore_manage # pylint: disable=import-error + +# from utils import highscore_manage # pylint: disable=import-error + + +class RegisterHighscoreChannel(commands.Cog, name="Score"): + """Actions of bot command""" + + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.command(name="score") + async def score(self, ctx: commands.Context, boss: str, category: str, score): + """A command which calls a function to submit the score to highscores. Gathers required + information from command and passes on. + Usage: + ``` + ?score "Boss Name" "Category" "score" + ``` + """ + highscore_manage.submit_score_int(boss, category, ctx.author.display_name, score) + + +# This function will be called when this extension is loaded. +# It is necessary to add these functions to the bot. +def setup(bot: commands.Bot): + """Adds functon to bot""" + bot.add_cog(RegisterHighscoreChannel(bot)) diff --git a/highscores_config.json b/highscores_config.json index 4b223b0..f7e2ea9 100755 --- a/highscores_config.json +++ b/highscores_config.json @@ -181,22 +181,19 @@ "hardmode": false } ], - "categories": [ - { - "category": "Kill Count", + "categories": { + "Kill Count": { "hardmode": true, "ascending": false }, - { - "category": "Kill Time", + "Kill Time": { "hardmode": true, "ascending": true }, - { - "category": "Spooned Log", + "Spooned Log": { "hardmode": false, "ascending": true } - ], + }, "highscore_size": 3 } \ No newline at end of file diff --git a/utils/highscore_manage.py b/utils/highscore_manage.py new file mode 100644 index 0000000..06f94af --- /dev/null +++ b/utils/highscore_manage.py @@ -0,0 +1,52 @@ +""" +Contains various functions for managing the highscores data +""" + +from highscores import highscores_config # pylint: disable=import-error +from highscores import highscores_data # pylint: disable=import-error +from utils import data_storage # pylint: disable=import-error + + +def submit_score_int(boss: str, category: str, user: str, score: int): + """ + 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 (int) to add + """ + # TODO update user submission to not have dupe users + 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) + + +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 5ee6630..eb42dd8 100644 --- a/utils/highscore_message.py +++ b/utils/highscore_message.py @@ -56,8 +56,7 @@ def format_highscore_message(boss): boss_category_scores = boss_data[f"{hardmode_category}_hardmode"] if len(boss_category_scores) > i: score = boss_category_scores[i] - for key, value in score.items(): - ret_string += f"{i+1}: {key} - {value}".ljust(30, " ") + ret_string += f"{i+1}: {score[0]} - {score[1]}".ljust(30, " ") else: ret_string += f"{i+1}: submit your score".ljust(30, " ") ret_string += "\n"