From 73dff91f530d7125a7ff130e9a56b759b7fdbaab Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 00:35:57 +0100 Subject: Add constants parsing for multiple leaderboards I've added a unified approach to setting data for multiple leaderboards using environmental variables. Instead of setting separate variables for the three pieces of data we have, hoping that the position of each board matches up in the three fields, I now set one environmental variable, AOC_LEADERBOARDS, that holds delimited data. The data is in the format: board_id1,session1,join_code1::board_id2,session2,join_code2[::...] The staff leaderboards should be included as usual in this environmental variable. Another environment variable, AOC_STAFF_LEADERBOARD_ID, can be used to designate which leaderboard should be used as the staff board. I've also made some other constants configurable in this commit and added the role ID of the Events Lead role to allow the Events Lead to force a reload of the leaderboard cache. --- bot/constants.py | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index d24551f6..fc9929ec 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -2,7 +2,7 @@ import enum import logging from datetime import datetime from os import environ -from typing import NamedTuple +from typing import Dict, NamedTuple __all__ = ( "AdventOfCode", @@ -29,14 +29,42 @@ __all__ = ( log = logging.getLogger(__name__) +class AdventOfCodeLeaderboard(NamedTuple): + id: str + session: str + join_code: str + + +def _parse_aoc_leaderboard_env() -> Dict[str, AdventOfCodeLeaderboard]: + """ + Parse the environment variable containing leaderboard information. + + A leaderboard should be specified in the format `id,session,join_code`, + without the backticks. If more than leaderboard needs to be added to the + constants, separate the individual leaderboards with `::`. + + Example ENV: `id1,session1,join_code1::id2,session2,join_code2` + """ + raw_leaderboards = environ.get("AOC_LEADERBOARDS", "") + if not raw_leaderboards: + return {} + + leaderboards = {} + for leaderboard in raw_leaderboards.split("::"): + leaderboard_id, session, join_code = leaderboard.split(",") + leaderboards[leaderboard_id] = AdventOfCodeLeaderboard(leaderboard_id, session, join_code) + + return leaderboards + + class AdventOfCode: - leaderboard_cache_age_threshold_seconds = 3600 - leaderboard_public_ids = [645282] - leaderboard_staff_id = 957532 - # Public join codes in environment must be in the same order as AdventOfCode.leaderboard_public_ids - leaderboard_public_join_codes = environ.get("AOC_PUBLIC_JOIN_CODES", "").split(",") - leaderboard_staff_join_code = environ.get("AOC_STAFF_JOIN_CODE", "") - leaderboard_max_displayed_members = 10 + # Information for the several leaderboards we have + leaderboards = _parse_aoc_leaderboard_env() + staff_leaderboard_id = environ.get("AOC_STAFF_LEADERBOARD_ID", "") + + # Other Advent of Code constants + leaderboard_displayed_members = 10 + leaderboard_cache_expiry_seconds = 1800 year = int(environ.get("AOC_YEAR", datetime.utcnow().year)) role_id = int(environ.get("AOC_ROLE_ID", 518565788744024082)) @@ -197,9 +225,10 @@ class Roles(NamedTuple): muted = 277914926603829249 owner = 267627879762755584 verified = 352427296948486144 - helpers = 267630620367257601 + helpers = int(environ.get("ROLE_HELPERS", 267630620367257601)) rockstars = 458226413825294336 core_developers = 587606783669829632 + events_lead = 778361735739998228 class Tokens(NamedTuple): -- cgit v1.2.3