Fill out HighscoreData class with access functions for utils.py.

This commit is contained in:
alwayscompiling
2022-07-27 22:15:06 -04:00
parent 4b15fef30c
commit 5b31314f02
2 changed files with 132 additions and 81 deletions
+17 -70
View File
@@ -3,10 +3,8 @@ Contains various utlity functions for highscores bot
"""
import nextcord
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.highscores_data import save_highscores_data # pylint: disable=import-error
def create_message_map(data: dict):
@@ -28,36 +26,37 @@ def create_message_map(data: dict):
return message_map
def format_highscore_message(boss_name: str, guild_data: dict):
def format_highscore_message(boss_name: str, guild_id: int):
"""
returns a formatted string for a boss's highscore
@param boss: the dictionary object for the boss
@param guild_data: the guild's highscore data
@param guild_id: the guild id the message is for
@return the formatted string
"""
ret_string = f"**{boss_name}**\n```"
boss_data = guild_data["tables"][boss_name]
# Gather all categories and add to string
for key, value in boss_data["categories"].items():
for key, value in highscores_data.get_categories_data(guild_id, boss_name):
ret_string += f"{key}".ljust(26, " ")
ret_string += "\n"
highscore_size = highscores_config["highscore_size"]
highscore_size = highscores_data.get_config_highscore_size()
# list rankings in a single line. Start with first place for each category and then continue
for i in range(highscore_size):
for key, value in boss_data["categories"].items():
for key, value in highscores_data.get_categories_data(guild_id, boss_name):
if len(value) > i:
score_pair = value[i]
# pad username to max length.
username: str = score_pair[0].ljust(guild_data["username_length"], " ")
username: str = score_pair[0].ljust(
highscores_data.get_guild_username_length(guild_id), " "
)
score: str = str(score_pair[1])
# pad score to correct length
# MM:SS.ms is 7 characters long
if highscores_config["categories"][key]["is_time_record"]:
if highscores_data.get_config_is_category_time_record(key):
# if there are single digit minutes, add a space for formatting.
if score.find(":") and len(score.split(":", maxsplit=1)[0]) < 2:
score = " " + score
@@ -73,19 +72,19 @@ def format_highscore_message(boss_name: str, guild_data: dict):
async def send_highscore_message(
channel: nextcord.TextChannel, boss_name: str, guild_data: dict
channel: nextcord.TextChannel, boss_name: str, guild_id: int
) -> nextcord.Message:
"""
Function takes care of checking if a message exists for a boss, then
creating and sending/editing the message
@param channel: the channel to send the message in/search for message id
@param boss: the boss to send the message about
@param guild_data: the guild's highscore data
@param guild_id: the guild id to send message to
@return: The nextcord message
"""
highscore_string = format_highscore_message(boss_name, guild_data)
highscore_string = format_highscore_message(boss_name, guild_id)
message_id = guild_data["tables"][boss_name]["message_id"]
message_id = highscores_data.get_table_message_id(guild_id, boss_name)
try:
message: nextcord.Message = await channel.fetch_message(message_id)
await message.edit(content=highscore_string)
@@ -93,7 +92,7 @@ async def send_highscore_message(
# message doesn't exist.
message: nextcord.Message = await channel.send(highscore_string)
message_id = message.id
guild_data["tables"][boss_name]["message_id"] = message_id
highscores_data.set_table_message_id(guild_id, boss_name, message_id)
# save message -> boss_name in message_map
# get guild id from channel
# channel should come from the guild that is requesting this highscores message anyways.
@@ -116,58 +115,10 @@ async def submit_score(
@param score: score to add
"""
boss_category_config = highscores_config["categories"][category]
guild_data = highscores_data[str(interaction.guild.id)]
# create score tuple for either time or int
# define sort index
if boss_category_config["is_time_record"]:
if score.find(":") == -1:
score_seconds = float(score)
else:
minutes = score.split(":")[0]
seconds = score.split(":")[1]
score_seconds = int(minutes) * 60 + float(seconds)
score_tuple = (user, score, score_seconds)
sort_index = 2
else:
score_tuple = (user, int(score))
sort_index = 1
scores: "list[tuple]" = guild_data["tables"][boss_name]["categories"][category]
# if user is in an existing tuple, delete it.
users_scores: "list[tuple]" = [score_tuple]
for tup in scores:
if user == tup[0]:
# Add tup to users_scores, remove from scores
users_scores.append(tup)
# sort users_scores
users_scores.sort(
key=lambda x: x[sort_index],
reverse=not boss_category_config["ascending"],
)
for index, tup in enumerate(users_scores):
if index == 0:
# first score is the best score.
if tup not in scores:
scores.append(tup)
else:
# discard all other scores from scores list.
if tup in scores:
scores.remove(tup)
# sort scores.
scores.sort(
key=lambda x: x[sort_index],
reverse=not boss_category_config["ascending"],
)
highscores_data.add_score(interaction.guild_id, boss_name, category, user, score)
# edit message
highscore_channel_id = guild_data["highscore_channel_id"]
highscore_channel_id = highscores_data.get_guild_channel_id(interaction.guild_id)
print(f"Editting message {highscore_channel_id}")
channel = interaction.guild.get_channel(highscore_channel_id)
@@ -175,8 +126,4 @@ async def submit_score(
error_response += 'Register with "?register" command.'
assert channel is not None, await interaction.send(error_response, ephemeral=True)
await send_highscore_message(channel, boss_name, guild_data)
# save data
highscores_data[str(interaction.guild.id)] = guild_data
save_highscores_data(highscores_data)
await send_highscore_message(channel, boss_name, interaction.guild_id)