diff options
author | 2021-11-19 18:06:52 +0100 | |
---|---|---|
committer | 2021-11-19 18:06:52 +0100 | |
commit | ad66899453b027f3a5522cc103f5c128456144c4 (patch) | |
tree | 2af226f0f4f3ca496dad6c2b02fb18b8014178ce | |
parent | Introduce command changes in the AoC Cog (diff) |
Update Caching logic in AoC helpers
- This commit adds a new set of leaderboard data to the cache so that it calculates the correct information if issued with either a `self_placement_name` or not.
- It also introduces code consistency upgrades
Co-Authored-By: Johannes Christ <[email protected]>
-rw-r--r-- | bot/exts/events/advent_of_code/_cog.py | 7 | ||||
-rw-r--r-- | bot/exts/events/advent_of_code/_helpers.py | 39 |
2 files changed, 40 insertions, 6 deletions
diff --git a/bot/exts/events/advent_of_code/_cog.py b/bot/exts/events/advent_of_code/_cog.py index 7f35c306..cd41e9ce 100644 --- a/bot/exts/events/advent_of_code/_cog.py +++ b/bot/exts/events/advent_of_code/_cog.py @@ -231,7 +231,7 @@ class AdventOfCode(commands.Cog): """ Get the current top scorers of the Python Discord Leaderboard. - Additionaly you can specify a `self_placement_name` + Additionally you can specify a `self_placement_name` that will append the specified profile's personal stats to the top of the leaderboard """ async with ctx.typing(): @@ -246,8 +246,9 @@ class AdventOfCode(commands.Cog): top_count = min(AocConfig.leaderboard_displayed_members, number_of_participants) self_placement_header = "(and your personal stats compared to the top 10)" if self_placement_name else "" header = f"Here's our current top {top_count}{self_placement_header}! {Emojis.christmas_tree * 3}" - - table = f"```\n{leaderboard['top_leaderboard']}\n```" + table = "```\n" \ + f"{leaderboard['placement_leaderboard'] if self_placement_name else leaderboard['top_leaderboard']}" \ + "\n```" info_embed = _helpers.get_summary_embed(leaderboard) await ctx.send(content=f"{header}\n\n{table}", embed=info_embed) diff --git a/bot/exts/events/advent_of_code/_helpers.py b/bot/exts/events/advent_of_code/_helpers.py index 58fc3054..35258544 100644 --- a/bot/exts/events/advent_of_code/_helpers.py +++ b/bot/exts/events/advent_of_code/_helpers.py @@ -71,6 +71,33 @@ class FetchingLeaderboardFailedError(Exception): """Raised when one or more leaderboards could not be fetched at all.""" +def _format_leaderboard_line(rank: int, data: dict[str, Any], *, is_author: bool) -> str: + """ + Build a string representing a line of the leaderboard. + + Parameters: + rank: + Rank in the leaderboard of this entry. + + data: + Mapping with entry information. + + Keyword arguments: + is_author: + Whether to address the name displayed in the returned line + personally. + + Returns: + A formatted line for the leaderboard. + """ + return AOC_TABLE_TEMPLATE.format( + rank=rank, + name=data['name'] if not is_author else f"(You) {data['name']}", + score=str(data['score']), + stars=f"({data['star_1']}, {data['star_2']})" + ) + + def leaderboard_sorting_function(entry: tuple[str, dict]) -> tuple[int, int]: """ Provide a sorting value for our leaderboard. @@ -287,7 +314,6 @@ async def fetch_leaderboard(invalidate_cache: bool = False, self_placement_name: miss, this function is locked to one call at a time using a decorator. """ cached_leaderboard = await _caches.leaderboard_cache.to_dict() - # Check if the cached leaderboard contains everything we expect it to. If it # does not, this probably means the cache has not been created yet or has # expired in Redis. This check also accounts for a malformed cache. @@ -301,11 +327,12 @@ async def fetch_leaderboard(invalidate_cache: bool = False, self_placement_name: leaderboard = parsed_leaderboard_data["leaderboard"] number_of_participants = len(leaderboard) - formatted_leaderboard = _format_leaderboard(leaderboard, self_placement_name) + formatted_leaderboard = _format_leaderboard(leaderboard) full_leaderboard_url = await _upload_leaderboard(formatted_leaderboard) leaderboard_fetched_at = datetime.datetime.utcnow().isoformat() cached_leaderboard = { + "placement_leaderboard": json.dumps(raw_leaderboard_data), "full_leaderboard": formatted_leaderboard, "top_leaderboard": _get_top_leaderboard(formatted_leaderboard), "full_leaderboard_url": full_leaderboard_url, @@ -324,7 +351,13 @@ async def fetch_leaderboard(invalidate_cache: bool = False, self_placement_name: _caches.leaderboard_cache.namespace, AdventOfCode.leaderboard_cache_expiry_seconds ) - + if self_placement_name: + formatted_placement_leaderboard = _parse_raw_leaderboard_data( + json.loads(cached_leaderboard["placement_leaderboard"]) + )["leaderboard"] + cached_leaderboard["placement_leaderboard"] = _get_top_leaderboard( + _format_leaderboard(formatted_placement_leaderboard, self_placement_name=self_placement_name) + ) return cached_leaderboard |