From 8a6b44a54ed40c278c2716198bbac2780c3c528f Mon Sep 17 00:00:00 2001 From: Zalk0 <88582103+zalk0@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:07:48 +0200 Subject: [PATCH] fix(ranking): sort level_cap while sorting xp --- chouette/utils/ranking.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/chouette/utils/ranking.py b/chouette/utils/ranking.py index 64dd693..b6733f3 100644 --- a/chouette/utils/ranking.py +++ b/chouette/utils/ranking.py @@ -86,18 +86,17 @@ def parse_data(data: dict) -> tuple[dict, list]: # Sorting the nested dictionaries by value for category in ranking: if isinstance(ranking[category], dict): + unsorted = ranking[category] ranking[category] = dict( sorted(ranking[category].items(), key=lambda item: item[1], reverse=True) ) - elif category == "skills" or category == "slayers": - for subcategory in ranking[category]: - ranking[category][subcategory] = dict( - sorted( - ranking[category][subcategory].items(), - key=lambda item: item[1], - reverse=True, - ) - ) + # In the case of farming we need sort the level_cap too + if category == "farming": + level_cap[0] = [ + level_cap[0][list(unsorted.keys()).index(key)] for key in ranking[category] + ] + else: + raise ValueError(f"Unknown category while sorting: {category}") return ranking, level_cap @@ -147,7 +146,7 @@ def generate_ranking_message(data, category, level_cap): else: value = experience_to_level(type_xp="slayer_web", xp_amount=value) else: - raise ValueError(f"Unknown category: {category}") + raise ValueError(f"Unknown category in the ranking: {category}") value = f"{value:.2f}" elif category == "networth": value = format_number(value)