Updated data structure to have categories be lists of key value pairs. This will make sorting easier.

This commit is contained in:
alwayscompiling
2022-06-28 22:27:31 -04:00
parent b134a8107d
commit 2b5c49a896
3 changed files with 27 additions and 14 deletions
+5 -2
View File
@@ -17,7 +17,8 @@ class PostHighscores(commands.Cog, name="Post Highscores"):
@commands.command(name="post-scores")
async def post_highscores(self, ctx: commands.Context):
"""
A command which posts the highscore in the designated channel. Sends warning if no channel has been registered.
A command which posts the highscore in the designated channel. Sends warning if no channel
has been registered.
Usage:
```
?post-scores
@@ -28,11 +29,13 @@ class PostHighscores(commands.Cog, name="Post Highscores"):
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.'
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)
+1 -8
View File
@@ -21,20 +21,13 @@ def open_highscores_data():
# create the json object
data = {"channel_id": -1}
config = open_highscores_config()
size = config["highscore_size"]
for boss in config["bosses"]:
data[boss["boss"]] = {"message_id": -1}
data[boss["boss"]] = {"message_id": 0}
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
+21 -4
View File
@@ -8,12 +8,13 @@ import highscores # pylint: disable=import-error
def format_highscore_message(boss):
"""
returns a formattedd string for a boss's highscore
returns a formatted string for a boss's highscore
@param boss: the dictionary object for the boss
@return the formatted string
"""
boss_name = boss["boss"]
ret_string = f"**{boss_name}**\n"
boss_data = highscores.highscores_data[boss_name]
# Gather all categories and add to string
for category in highscores.highscores_config["categories"]:
@@ -26,7 +27,16 @@ def format_highscore_message(boss):
# list rankings
for i in range(highscore_size):
for category in highscores.highscores_config["categories"]:
ret_string += f"{i+1}: submit your score".ljust(30, " ")
boss_category_scores = boss_data[category["category"]]
if len(boss_category_scores) > i:
score = boss_category_scores[i]
# this is the dumbest solution to a problem iv seen in a while...
# I just needed the key and this is the best way to get it without
# getting a list of 1 key and parsing that string.
for key, value in score.items():
ret_string += f"{i+1}: {key} - {value}".ljust(30, " ")
else:
ret_string += f"{i+1}: submit your score".ljust(30, " ")
ret_string += "\n"
# if boss is hardmode, repeat above for hardmode applicable categories
@@ -34,14 +44,21 @@ def format_highscore_message(boss):
for category in highscores.highscores_config["categories"]:
if category["hardmode"]:
category_name = category["category"]
ret_string += f"{category_name}".ljust(30, " ")
ret_string += f"Hardmode {category_name}".ljust(30, " ")
ret_string += "\n"
for i in range(highscore_size):
for category in highscores.highscores_config["categories"]:
if category["hardmode"]:
ret_string += f"{i+1}: (name here)".ljust(30, " ")
hardmode_category = category["category"]
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, " ")
else:
ret_string += f"{i+1}: submit your score".ljust(30, " ")
ret_string += "\n"
return ret_string