diff --git a/chouette/utils/hypixel_data.py b/chouette/utils/hypixel_data.py index 486b6b7..00ef07f 100644 --- a/chouette/utils/hypixel_data.py +++ b/chouette/utils/hypixel_data.py @@ -1,7 +1,7 @@ from typing import Optional -def experience_to_level( +async def experience_to_level( type_xp: str, xp_amount: float, max_level: Optional[int] = None ) -> (float, float): """ diff --git a/chouette/utils/ranking.py b/chouette/utils/ranking.py index 473aada..6075278 100644 --- a/chouette/utils/ranking.py +++ b/chouette/utils/ranking.py @@ -19,7 +19,7 @@ SPACES = " " * 38 -def format_number(number) -> str: +async def format_number(number) -> str: """Permet de formater un nombre en K, M ou B.""" if number >= 1_000_000_000: return f"{number / 1_000_000_000:.2f}B" @@ -30,7 +30,7 @@ def format_number(number) -> str: return str(number) -def format_ranking_message(player: str, value: str, i: int, position: int) -> str: +async def format_ranking_message(player: str, value: str, i: int, position: int) -> str: """Formate le message pour les données du classement de la guilde sur Hypixel Skyblock.""" # Codes emoji custom pour les positions (discord developer portal app) if position == 1: @@ -73,11 +73,11 @@ async def update_stats(api_key: str) -> (str, dict): new_data.get(uuid).update(await get_stats(session, pseudo, uuid, player, profile)) msg += f"\n{SPACES}- {pseudo} sur le profil {profile_name}" await save_skyblock(new_data) - old_data = parse_data(old_data) + old_data = await parse_data(old_data) return msg, old_data -def parse_data(data: dict) -> dict: +async def parse_data(data: dict) -> dict: """ Parse les données de la guilde sur Hypixel Skyblock. Calcule les niveaux et overflows des joueurs. @@ -145,7 +145,7 @@ def parse_data(data: dict) -> dict: category = "slayers" skill_list = slayers - level, overflow = experience_to_level( + level, overflow = await experience_to_level( type_xp, data[player][category][skill_list.index(skill)], max_level ) ranking[skill]["level"][data[player]["pseudo"]] = level @@ -177,7 +177,7 @@ def parse_data(data: dict) -> dict: return sorted_ranking -def generate_ranking_message(data, category, old_data) -> list[str]: +async def generate_ranking_message(data, category, old_data) -> list[str]: """ Génère les messages pour les données du classement de la guilde sur Hypixel Skyblock. On compare les anciennes données avec les nouvelles pour déterminer si un joueur a monté, descendu ou n'a pas bougé. @@ -209,16 +209,22 @@ def generate_ranking_message(data, category, old_data) -> list[str]: if category == "level": for i, (player, value) in enumerate(data[category].items()): value = f"{value:.2f}" - message = format_ranking_message( - player, value, i, calculate_player_position(old_data, data, category, player) + message = await format_ranking_message( + player, + value, + i, + position=await calculate_player_position(old_data, data, category, player), ) messages.append(message) # 'Networth' if category == "networth": for i, (player, value) in enumerate(data[category].items()): - value = format_number(value) - message = format_ranking_message( - player, value, i, calculate_player_position(old_data, data, category, player) + value = await format_number(value) + message = await format_ranking_message( + player, + value, + i, + position=await calculate_player_position(old_data, data, category, player), ) messages.append(message) # 'Skills' et 'slayers' @@ -230,8 +236,11 @@ def generate_ranking_message(data, category, old_data) -> list[str]: overflow = math.floor(overflow) else: value = f"{value:.2f}" - message = format_ranking_message( - player, value, i, calculate_player_position(old_data, data, category, player) + message = await format_ranking_message( + player, + value, + i, + position=await calculate_player_position(old_data, data, category, player), ) if overflow: message += f" (*{overflow:,}*)".replace(",", " ") @@ -264,14 +273,17 @@ def generate_ranking_message(data, category, old_data) -> list[str]: ) for i, (player, value) in enumerate(data[category].items()): value = f"{value:.2f}" - message = format_ranking_message( - player, value, i, calculate_player_position(old_data, data, category, player) + message = await format_ranking_message( + player, + value, + i, + position=await calculate_player_position(old_data, data, category, player), ) messages.append(message) return messages -def calculate_player_position(old_data, new_data, category, player) -> int: +async def calculate_player_position(old_data, new_data, category, player) -> int: """ Calcule la position d'un joueur dans le classement. @@ -317,12 +329,12 @@ async def display_ranking(img: str, old_ranking: dict) -> list[discord.Embed]: ) ranking.set_thumbnail(url=img) ranking.set_footer(text="\N{WHITE HEAVY CHECK MARK} Mis à jour le 1er de chaque mois à 8h00") - new_ranking_data = parse_data(await load_skyblock()) + new_ranking_data = await parse_data(await load_skyblock()) # On génère les messages pour chaque catégorie for category in new_ranking_data: if isinstance(new_ranking_data[category], dict): - messages = generate_ranking_message(new_ranking_data, category, old_ranking) + messages = await generate_ranking_message(new_ranking_data, category, old_ranking) field_value = "\n".join(messages) # La limite des embeds est de 6000 caractères # on split avant de dépasser cette limite pour éviter une coupure dans les catégories