Skip to content

Commit

Permalink
fix(skyblock): remove a redundant request with the second failing
Browse files Browse the repository at this point in the history
  • Loading branch information
Zalk0 committed Sep 25, 2024
1 parent cc2d549 commit c0c4d9a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
11 changes: 9 additions & 2 deletions chouette/utils/ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@

from chouette.utils.birthdays import month_to_str
from chouette.utils.hypixel_data import experience_to_level
from chouette.utils.skyblock import get_profile, get_stats, load_skyblock, save_skyblock
from chouette.utils.skyblock import (
get_hypixel_player,
get_profile,
get_stats,
load_skyblock,
save_skyblock,
)

SPACES = " " * 38

Expand Down Expand Up @@ -35,7 +41,8 @@ async def update_stats(api_key: str) -> str:
if not profile[0]:
raise Exception("Error while updating stats")
profile = profile[1]
new_data.get(uuid).update(await get_stats(session, api_key, pseudo, uuid, profile))
player = await get_hypixel_player(session, api_key, uuid)
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)
# TODO: handle comparison using old and new data (see issue #56)
Expand Down
23 changes: 9 additions & 14 deletions chouette/utils/skyblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,11 @@ async def minecraft_uuid(session: ClientSession, pseudo: str) -> tuple[bool, str
return True, json.get("id")


async def hypixel_discord(session: ClientSession, api_key: str, uuid: str) -> tuple[bool, str]:
async def hypixel_discord(player: dict) -> tuple[bool, str]:
"""Retourne le pseudo Discord lié à un compte Hypixel."""
async with session.get(
f"{HYPIXEL_API}player", params={"key": api_key, "uuid": uuid}
) as response:
json: dict = await response.json()
if response.status != 200:
return False, json.get("cause")
if not json.get("player").get("socialMedia", {}).get("links", {}).get("DISCORD", ""):
return False, "Vous n'avez pas associé votre compte Discord à Hypixel"
return True, json.get("player").get("socialMedia").get("links").get("DISCORD")
if not player.get("player").get("socialMedia", {}).get("links", {}).get("DISCORD", ""):
return False, "Vous n'avez pas associé votre compte Discord à Hypixel"
return True, player.get("player").get("socialMedia").get("links").get("DISCORD")


async def selected_profile(
Expand Down Expand Up @@ -109,12 +103,11 @@ async def get_networth(session: ClientSession, pseudo: str, profile_id: str) ->
return json.get("profiles").get(profile_id).get("data").get("networth").get("networth", 0)


async def get_stats(session, api_key, pseudo, uuid, profile) -> dict[str, float]:
async def get_stats(session, pseudo, uuid, hypixel_player, profile) -> dict[str, float]:
"""Retourne les statistiques d'un joueur Skyblock avec l'API."""
info = profile.get("members").get(uuid)
level: float = (info.get("leveling").get("experience")) / 100
networth = await get_networth(session, pseudo, profile.get("profile_id"))
hypixel_player = await get_hypixel_player(session, api_key, uuid)
skill = info.get("player_data").get("experience")
skills: tuple[float, float, float, float, float, float, float, float, float, float] = (
skill.get("SKILL_FISHING", 0),
Expand Down Expand Up @@ -163,7 +156,8 @@ async def pseudo_to_profile(

api_key = client.config.get("HYPIXEL_KEY")

discord = await hypixel_discord(session, api_key, uuid)
player = await get_hypixel_player(session, api_key, uuid)
discord = await hypixel_discord(player)
if not discord[0]:
# TODO: better handling
return discord[1]
Expand All @@ -183,7 +177,8 @@ async def pseudo_to_profile(
client.bot_logger.debug(f"Le profil {profile.get('cute_name')} a été trouvé")

info = {uuid: {"discord": discord, "pseudo": pseudo, "profile": profile.get("cute_name")}}
info.get(uuid).update(await get_stats(session, api_key, pseudo, uuid, profile))
info.get(uuid).update(await get_stats(session, pseudo, uuid, player, profile))
client.bot_logger.debug("Les stats ont bien été calculées")
file_content = await load_skyblock()
if file_content.get(uuid, {}).get("profile", "") != profile.get("profile_id"):
file_content.update(info)
Expand Down

0 comments on commit c0c4d9a

Please sign in to comment.