diff --git a/cogs/submission/verification_view.py b/cogs/submission/verification_view.py index eb14e23..fc6c077 100644 --- a/cogs/submission/verification_view.py +++ b/cogs/submission/verification_view.py @@ -1,6 +1,9 @@ """View for verification buttons""" import nextcord +from data.score import Score, ScoreTime # pylint: disable=import-error + +from highscores import highscores_data # pylint: disable=import-error from utilities.utils import submit_score # pylint: disable=import-error @@ -39,7 +42,21 @@ class VerificationView(nextcord.ui.View): user = field["value"] try: - await submit_score(interaction, user, boss_name, category, score) + submission_guild = highscores_data.get_guild(interaction.guild_id) + submission_table = submission_guild.get_table(boss_name) + submission_category = submission_table.get_category(category) + if submission_category.time_record: + submission_score: ScoreTime = ScoreTime(user, float(score)) + else: + submission_score: Score = Score(user, int(score)) + + await submit_score( + interaction, + submission_guild, + submission_table, + submission_category, + submission_score, + ) await interaction.message.edit(f"Approved by <@{interaction.user.id}>.", view=None) except UnboundLocalError: await interaction.send("Instruct user to submit with all fields.") diff --git a/utilities/utils.py b/utilities/utils.py index e7bdd3f..a0b532c 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -3,42 +3,47 @@ Contains various utlity functions for highscores bot """ import nextcord -from highscores import highscores_data # pylint: disable=import-error from highscores import highscores_message_map # pylint: disable=import-error +from data.score import Score # pylint: disable=import-error +from data.guild import Guild # pylint: disable=import-error +from data.category import Category # pylint: disable=import-error +from data.table import Table # pylint: disable=import-error -def format_highscore_message(boss_name: str, guild_id: int): +def format_highscore_message(guild: Guild, table: Table) -> str: """ - returns a formatted string for a boss's highscore - @param boss: the dictionary object for the boss - @param guild_id: the guild id the message is for - @return the formatted string + Creates a formatted string for a boss's highscore discord message. + This formatted string is in discord's code block to ensure monospace text. + Loops through each category to build the message one place at a time. + + Args: + guild (Guild): Guild object that is posting the message. + table (Table): Table object that contains the contents of the message. + + Returns: + str: Formatted string for discord message """ - ret_string = f"**{boss_name}**\n```" + ret_string = f"**{table.name}**\n```" # Gather all categories and add to string - for category_name in highscores_data.get_table_categories(guild_id, boss_name): - ret_string += f"{category_name}".ljust(26, " ") + for category in table.categories: + ret_string += f"{category.name}".ljust(26, " ") ret_string += "\n" - highscore_size = highscores_data.get_config_highscore_size() + highscore_size = guild.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 category_name, category_data in highscores_data.get_categories_data( - guild_id, boss_name - ): - if len(category_data) > i: + for category in table.categories: + if len(category.scores) > i: # pad username to max length. - username: str = list(category_data.keys())[i].ljust( - highscores_data.get_guild_username_length(guild_id), " " - ) - # turns category data value into a string. Very janky. - score: str = str(list(category_data.values())[i][0]) + username: str = category.scores[i].user.ljust(guild.username_length, " ") + + score: str = category.scores[i].get_str() # pad score to correct length # MM:SS.ms is 7 characters long - if highscores_data.get_config_time_record(category_name): + if category.time_record: # if there are single digit minutes, add a space for formatting. if score.find(":") and len(score.split(":", maxsplit=1)[0]) < 2: score = " " + score @@ -54,19 +59,23 @@ def format_highscore_message(boss_name: str, guild_id: int): async def send_highscore_message( - channel: nextcord.TextChannel, boss_name: str, guild_id: int + channel: nextcord.TextChannel, guild: Guild, table: Table ) -> 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_id: the guild id to send message to - @return: The nextcord message - """ - highscore_string = format_highscore_message(boss_name, guild_id) + Function takes care of checking if a message exists for a table. + Creates and sends/edits the message. - message_id = highscores_data.get_table_message_id(guild_id, boss_name) + Args: + channel (nextcord.TextChannel): Discord channel object where message is sent + guild (Guild): Guild object for Guild that is posting the message + table (Table): Table to post to the message + + Returns: + nextcord.Message: Message object that was posted with table contents + """ + highscore_string = format_highscore_message(guild, table) + + message_id = table.message_id try: message: nextcord.Message = await channel.fetch_message(message_id) await message.edit(content=highscore_string) @@ -74,38 +83,41 @@ async def send_highscore_message( # message doesn't exist. message: nextcord.Message = await channel.send(highscore_string) message_id = message.id - highscores_data.set_table_message_id(guild_id, boss_name, message_id) + table.message_id = 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. # check if guild is in message map already if not channel.guild.id in highscores_message_map: highscores_message_map[channel.guild.id] = {} - highscores_message_map[channel.guild.id][message_id] = boss_name + highscores_message_map[channel.guild.id][message_id] = table.name return message async def submit_score( - interaction: nextcord.Interaction, user: str, boss_name: str, category: str, score: str -): + interaction: nextcord.Interaction, guild: Guild, table: Table, category: Category, score: Score +) -> None: """ - 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 to add + Adds passed score to table. Then updates table message by calling send_highscore_message. + Ensures that a highscores channel exists. + + Args: + interaction (nextcord.Interaction): Interaction that caused the score submission + guild (Guild): Guild to submit score to + table (Table): Table to submit score to + category (Category): Category to submit score to + score (Score): Score to submit """ - highscores_data.add_score(interaction.guild_id, boss_name, category, user, score) + table.get_category(category).add_score(score) # edit message - highscore_channel_id = highscores_data.get_guild_highscores_channel(interaction.guild_id) - print(f"Editting message {highscore_channel_id}") - channel = interaction.guild.get_channel(highscore_channel_id) + print(f"Editting message {guild.highscore_channel}") + channel = interaction.guild.get_channel(guild.highscore_channel) error_response = "Registered Highscores channel does not exist or was never registered." 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, interaction.guild_id) + await send_highscore_message(channel, guild, table)