diff --git a/cogs/post_highscores/__init__.py b/cogs/post_highscores/__init__.py index b9d65c2..229a9e1 100644 --- a/cogs/post_highscores/__init__.py +++ b/cogs/post_highscores/__init__.py @@ -43,6 +43,7 @@ class PostHighscores(commands.Cog, name="Post Highscores"): message: nextcord.Message = await send_highscore_message(channel, boss) await message.edit(view=SubmissionCreateButton(self.bot)) + # Saving in case new message was sent. save_highscores_data(highscores_data) diff --git a/cogs/score/__init__.py b/cogs/score/__init__.py index 85ecd7b..4f41dc3 100644 --- a/cogs/score/__init__.py +++ b/cogs/score/__init__.py @@ -5,7 +5,6 @@ Bot command to submit a score from nextcord.ext import commands from highscores import highscores_data # pylint: disable=import-error -from utilities.utils import submit_score # pylint: disable=import-error from .verification_view import VerificationView diff --git a/cogs/submission/submission_views.py b/cogs/submission/submission_views.py index 79e81df..08a9ad8 100644 --- a/cogs/submission/submission_views.py +++ b/cogs/submission/submission_views.py @@ -7,9 +7,9 @@ import nextcord from nextcord import Embed from nextcord.ext import commands -from utilities.data_storage import open_message_map # pylint: disable=import-error from highscores import highscores_data # pylint: disable=import-error from highscores import submission_objects # pylint: disable=import-error +from highscores import highscores_message_map # pylint: disable=import-error from .verification_view import VerificationView @@ -55,7 +55,7 @@ def get_submission_embed(userid: int) -> nextcord.Embed: def category_select_options(boss_name: str) -> "list[nextcord.SelectOption]": """Returns list of categories for a given boss.""" options: "list[nextcord.SelectOption]" = [] - boss_dict = highscores_data[boss_name] + boss_dict = highscores_data["tables"][boss_name] for category in boss_dict["categories"].items(): options.append(nextcord.SelectOption(label=category[0])) @@ -186,11 +186,7 @@ class SubmissionCreateButton(nextcord.ui.View): self, button: nextcord.ui.Button, interaction: nextcord.Interaction ): # pylint: disable=unused-argument """Button for score submission.""" - highscores_message_map = open_message_map() - boss_name = highscores_message_map[str(interaction.message.id)] - # embed = nextcord.Embed(title="Highscores Submission") - # embed.add_field(name="Boss", value=boss_name, inline=False) - # embed.add_field(name="User", value=interaction.user.display_name, inline=False) + boss_name = highscores_message_map[interaction.message.id] channel = self._bot.get_channel(highscores_data["submission_channel_id"]) # a user should only have 1 submission active at once. diff --git a/highscores.py b/highscores.py index f868b0f..493e0bb 100644 --- a/highscores.py +++ b/highscores.py @@ -11,7 +11,7 @@ from nextcord.ext import commands import config from utilities.data_storage import open_highscores_config from utilities.data_storage import open_highscores_data -from utilities.data_storage import open_message_map +from utilities.data_storage import create_message_map logger = logging.getLogger("nextcord") logger.setLevel(logging.DEBUG) @@ -22,7 +22,7 @@ logger.addHandler(handler) highscores_config = open_highscores_config() highscores_data = open_highscores_data() -highscores_message_map = open_message_map() +highscores_message_map = create_message_map(highscores_data) submission_objects: dict = {} diff --git a/utilities/data_storage.py b/utilities/data_storage.py index d29ea49..ec9e096 100644 --- a/utilities/data_storage.py +++ b/utilities/data_storage.py @@ -5,7 +5,6 @@ import json HIGHSCORES_CONFIG_FILE = "highscores_config.json" HIGHSCORES_DATA_FILE = "highscores_data.json" -HIGHSCORES_MESSAGE_MAP_FILE = "highscores_message_map.json" def open_highscores_config(): @@ -31,26 +30,28 @@ def open_highscores_data(): data = {"highscore_channel_id": -1, "verification_channel_id": -1, "username_length": 12} config: dict = open_highscores_config() for boss, categories in config["highscore_table"].items(): - data[boss] = {"message_id": 0} + data["tables"][boss] = {"message_id": 0} boss_categories = {} for category in categories: # pylint: disable=unused-variable rank_list = [] boss_categories[category] = rank_list - data[boss]["categories"] = boss_categories + data["tables"][boss]["categories"] = boss_categories return data -def open_message_map(): +def create_message_map(data: dict): """ - Opens a highscore_message_map.json file if one exists. Otherwise returns empty dict. + Creates a message map from input highscores data. + + @param data: highscores_data dict to create map from. @return: dict message_map of message to boss. """ - if os.path.exists(HIGHSCORES_MESSAGE_MAP_FILE): - with open(HIGHSCORES_MESSAGE_MAP_FILE, encoding="utf-8", mode="r") as file: - message_map = json.load(file) - else: - # create the json object - message_map = {} + + message_map = {} + + for boss, info in data["tables"].items(): + message_map[info["message_id"]] = boss + return message_map @@ -61,12 +62,3 @@ def save_highscores_data(data): """ with open(HIGHSCORES_DATA_FILE, encoding="utf-8", mode="w") as file: file.write(json.dumps(data, indent=4)) - - -def save_message_map(message_map): - """ - Saves the highscores message mao data dict into a json file. - @param map: dict map of message to boss. - """ - with open(HIGHSCORES_MESSAGE_MAP_FILE, encoding="utf-8", mode="w") as file: - file.write(json.dumps(message_map, indent=4)) diff --git a/utilities/utils.py b/utilities/utils.py index 4eb1cb6..6f58685 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -7,7 +7,6 @@ from highscores import highscores_config # pylint: disable=import-error from highscores import highscores_data # pylint: disable=import-error from highscores import highscores_message_map # pylint: disable=import-error from utilities.data_storage import save_highscores_data # pylint: disable=import-error -from utilities.data_storage import save_message_map # pylint: disable=import-error def format_highscore_message(boss_name: str): @@ -17,7 +16,7 @@ def format_highscore_message(boss_name: str): @return the formatted string """ ret_string = f"**{boss_name}**\n```" - boss_data = highscores_data[boss_name] + boss_data = highscores_data["tables"][boss_name] # Gather all categories and add to string for key, value in boss_data["categories"].items(): @@ -59,7 +58,7 @@ async def send_highscore_message(channel, boss_name: str) -> nextcord.Message: """ highscore_string = format_highscore_message(boss_name) - message_id = highscores_data[boss_name]["message_id"] + message_id = highscores_data["tables"][boss_name]["message_id"] try: message: nextcord.Message = await channel.fetch_message(message_id) await message.edit(content=highscore_string) @@ -67,10 +66,9 @@ async def send_highscore_message(channel, boss_name: str) -> nextcord.Message: # message doesn't exist. message: nextcord.Message = await channel.send(highscore_string) message_id = message.id - highscores_data[boss_name]["message_id"] = message_id + highscores_data["tables"][boss_name]["message_id"] = message_id # save message -> boss_name in message_map highscores_message_map[str(message_id)] = boss_name - save_message_map(highscores_message_map) return message @@ -104,7 +102,7 @@ async def submit_score( score_tuple = (user, int(score)) sort_index = 1 - scores: "list[tuple]" = highscores_data[boss_name]["categories"][category] + scores: "list[tuple]" = highscores_data["tables"][boss_name]["categories"][category] # if user is in an existing tuple, delete it. users_scores: "list[tuple]" = [score_tuple] @@ -138,7 +136,7 @@ async def submit_score( # enforce highscore size while len(scores) > highscores_config["highscore_size"]: scores.pop() - highscores_data[boss_name]["categories"][category] = scores + highscores_data["tables"][boss_name]["categories"][category] = scores # edit message highscore_channel_id = highscores_data["highscore_channel_id"]