aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Sebastiaan Zeeff <[email protected]>2020-12-01 18:30:12 +0100
committerGravatar Sebastiaan Zeeff <[email protected]>2020-12-01 18:30:12 +0100
commit210c564e1b6823617cb4f64ff5e33031dc61d487 (patch)
tree80ce941d2a11cd143903ea57ab71794acfd50d84
parentClarify text of DM with Advent of Code join code (diff)
Add support for ignoring scores from specific days
I've added support for ignoring scores from specific days. A list of days to ignore can be provided using the environment variable `AOC_IGNORED_DAYS` as a comma-separated list. This example would ignore day 1 and day 23: AOC_IGNORED_DAYS=1,23 I've also added a helper function to sort the leaderboard not only on the achieved score, but also on the number of stars an individual has completed.
-rw-r--r--bot/constants.py1
-rw-r--r--bot/exts/christmas/advent_of_code/_helpers.py19
2 files changed, 17 insertions, 3 deletions
diff --git a/bot/constants.py b/bot/constants.py
index 292a242a..e313e086 100644
--- a/bot/constants.py
+++ b/bot/constants.py
@@ -63,6 +63,7 @@ class AdventOfCode:
staff_leaderboard_id = environ.get("AOC_STAFF_LEADERBOARD_ID", "")
# Other Advent of Code constants
+ ignored_days = [day for day in environ.get("AOC_IGNORED_DAYS", "").split(",")]
leaderboard_displayed_members = 10
leaderboard_cache_expiry_seconds = 1800
year = int(environ.get("AOC_YEAR", datetime.utcnow().year))
diff --git a/bot/exts/christmas/advent_of_code/_helpers.py b/bot/exts/christmas/advent_of_code/_helpers.py
index 72c1ce20..e84348cb 100644
--- a/bot/exts/christmas/advent_of_code/_helpers.py
+++ b/bot/exts/christmas/advent_of_code/_helpers.py
@@ -53,6 +53,17 @@ EST = pytz.timezone("EST")
_StarResult = collections.namedtuple("StarResult", "name completion_time")
+def leaderboard_sorting_function(entry: typing.Tuple[str, dict]) -> typing.Tuple[int, int]:
+ """
+ Provide a sorting value for our leaderboard.
+
+ The leaderboard is sorted primarily on the score someone has received and
+ secondary on the number of stars someone has completed.
+ """
+ result = entry[1]
+ return result["score"], result["star_2_count"] + result["star_1_count"]
+
+
def _parse_raw_leaderboard_data(raw_leaderboard_data: dict) -> dict:
"""
Parse the leaderboard data received from the AoC website.
@@ -98,13 +109,15 @@ def _parse_raw_leaderboard_data(raw_leaderboard_data: dict) -> dict:
# participants per star, we can compute the rank-based scores each participant
# should get for that star.
max_score = len(leaderboard)
- for star in star_results.values():
- for rank, star_result in enumerate(sorted(star, key=operator.itemgetter(1))):
+ for(day, _star), results in star_results.items():
+ if day in AdventOfCode.ignored_days:
+ continue
+ for rank, star_result in enumerate(sorted(results, key=operator.itemgetter(1))):
leaderboard[star_result.name]["score"] += max_score - rank
# Since dictionaries now retain insertion order, let's use that
sorted_leaderboard = dict(
- sorted(leaderboard.items(), key=lambda t: t[1]["score"], reverse=True)
+ sorted(leaderboard.items(), key=leaderboard_sorting_function, reverse=True)
)
# Create summary stats for the stars completed for each day of the event.