Skip to content

Commit

Permalink
feat: convert ranking and experience functions to async for improved …
Browse files Browse the repository at this point in the history
…performance
  • Loading branch information
gylfirst committed Dec 21, 2024
1 parent 6c61d0e commit 8ec46b5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion chouette/utils/hypixel_data.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand Down
48 changes: 30 additions & 18 deletions chouette/utils/ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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é.
Expand Down Expand Up @@ -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'
Expand All @@ -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(",", " ")
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8ec46b5

Please sign in to comment.