From 71611502cfd60bb4995800d7d9288fe4eb78f9d3 Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Tue, 28 Jun 2022 08:14:36 -0400 Subject: [PATCH] Filling out skeleton dict for highscores_data rather than let it be empty and filled at data creation. --- cogs/post_highscores/__init__.py | 22 ++++++++-------------- utils/data_storage.py | 27 +++++++++++++++++++++------ utils/highscore_message.py | 29 ++++++----------------------- 3 files changed, 35 insertions(+), 43 deletions(-) diff --git a/cogs/post_highscores/__init__.py b/cogs/post_highscores/__init__.py index a0c16f2..5461e0f 100644 --- a/cogs/post_highscores/__init__.py +++ b/cogs/post_highscores/__init__.py @@ -25,21 +25,15 @@ class PostHighscores(commands.Cog, name="Post Highscores"): """ # temporarily sending config data... channel_id = highscores.highscores_data["channel_id"] - if "channel_id" in highscores.highscores_data: - channel_id = highscores.highscores_data["channel_id"] - print(f"Printing out highscores information in channel {channel_id}") - channel = self.bot.get_channel(channel_id) - if channel is None: - response = 'Registered Highscores channel does not exist. Register again with "?register" command.' - await ctx.send(response) - return - for boss in highscores.highscores_config["bosses"]: - await highscore_message.send_highscore_message(channel, boss) - break - - else: - response = 'Highscores channel is not registered. Register with "?register" command.' + print(f"Printing out highscores information in channel {channel_id}") + channel = self.bot.get_channel(channel_id) + if channel is None: + response = 'Registered Highscores channel does not exist or was never registered. Register with "?register" command.' await ctx.send(response) + return + for boss in highscores.highscores_config["bosses"]: + await highscore_message.send_highscore_message(channel, boss) + break data_storage.save_highscores_data(highscores.highscores_data) diff --git a/utils/data_storage.py b/utils/data_storage.py index e492a35..97bce71 100644 --- a/utils/data_storage.py +++ b/utils/data_storage.py @@ -14,13 +14,28 @@ def open_highscores_data(): Opens a highscores_data.json file if one exists, otherwise creates a skeleton of the data. @return: dict of highscores data. """ - if (os.path.exists(HIGHSCORES_DATA_FILE)): - with open(HIGHSCORES_DATA_FILE, encoding='utf-8', mode="r") as file: + if os.path.exists(HIGHSCORES_DATA_FILE): + with open(HIGHSCORES_DATA_FILE, encoding="utf-8", mode="r") as file: data = json.load(file) else: # create the json object - data = {} - + data = {"channel_id": -1} + config = open_highscores_config() + size = config["highscore_size"] + for boss in config["bosses"]: + data[boss["boss"]] = {"message_id": -1} + for category in config["categories"]: + rank_list = [] + for i in range(size): # pylint: disable=unused-variable + rank = {"player": "", "score": -1} + rank_list.append(rank) + data[boss["boss"]][category["category"]] = rank_list + if boss["hardmode"] and category["hardmode"]: + rank_list = [] + for i in range(size): # pylint: disable=unused-variable + rank = {"player": "", "score": -1} + rank_list.append(rank) + data[boss["boss"]][category["category"] + "_hardmode"] = rank_list return data @@ -30,7 +45,7 @@ def open_highscores_config(): @return: dict of highscores config. """ # Load bot config - with open(HIGHSCORES_CONFIG_FILE, encoding='utf-8', mode='r') as file: + with open(HIGHSCORES_CONFIG_FILE, encoding="utf-8", mode="r") as file: return json.load(file) @@ -39,5 +54,5 @@ def save_highscores_data(data): Saves the highscores data dict into a json file. @param data: dict of highscores data. """ - with open(HIGHSCORES_DATA_FILE, encoding='utf-8', mode='w') as file: + with open(HIGHSCORES_DATA_FILE, encoding="utf-8", mode="w") as file: file.write(json.dumps(data, indent=4)) diff --git a/utils/highscore_message.py b/utils/highscore_message.py index 3387664..5d67bd6 100644 --- a/utils/highscore_message.py +++ b/utils/highscore_message.py @@ -3,7 +3,6 @@ Contains various functions for sending/editing the highscore messages. """ import nextcord -from nextcord.ext import commands import highscores # pylint: disable=import-error @@ -55,28 +54,12 @@ async def send_highscore_message(channel, boss): """ highscore_string = format_highscore_message(boss) - if (boss["boss"] in highscores.highscores_data) and ( - "message_id" in highscores.highscores_data[boss["boss"]] - ): - message_id = highscores.highscores_data[boss["boss"]]["message_id"] - try: - message = await channel.fetch_message(message_id) - await message.edit(highscore_string) - except nextcord.NotFound: - # message doesn't exist. - message = await channel.send(highscore_string) - message_id = message.id - # check if there is a dict for the boss in the highscores data - if not boss["boss"] in highscores.highscores_data: - highscores.highscores_data[boss["boss"]] = {} - - highscores.highscores_data[boss["boss"]]["message_id"] = message_id - else: - # make a new message + message_id = highscores.highscores_data[boss["boss"]]["message_id"] + try: + message = await channel.fetch_message(message_id) + await message.edit(highscore_string) + except nextcord.NotFound: + # message doesn't exist. message = await channel.send(highscore_string) message_id = message.id - # check if there is a dict for the boss in the highscores data - if not boss["boss"] in highscores.highscores_data: - highscores.highscores_data[boss["boss"]] = {} - highscores.highscores_data[boss["boss"]]["message_id"] = message_id