From 415f550969555f6929cc2d0273818bf6db57cab0 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Tue, 24 Nov 2020 17:38:43 +0200 Subject: Update constants to match with new format of AoC that will run in 2020 --- bot/constants.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'bot') diff --git a/bot/constants.py b/bot/constants.py index 6999f321..841f2303 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -31,8 +31,10 @@ log = logging.getLogger(__name__) class AdventOfCode: leaderboard_cache_age_threshold_seconds = 3600 - leaderboard_id = 631135 - leaderboard_join_code = str(environ.get("AOC_JOIN_CODE", None)) + leaderboard_public_ids = [645282] + leaderboard_staff_id = 957532 + 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 year = int(environ.get("AOC_YEAR", datetime.utcnow().year)) role_id = int(environ.get("AOC_ROLE_ID", 518565788744024082)) @@ -44,7 +46,8 @@ class Branding: class Channels(NamedTuple): admins = 365960823622991872 - advent_of_code = int(environ.get("AOC_CHANNEL_ID", 517745814039166986)) + advent_of_code = int(environ.get("AOC_CHANNEL_ID", 780818162836439041)) + advent_of_code_staff = int(environ.get("AOC_STAFF_CHANNEL_ID", 778646502641500181)) announcements = int(environ.get("CHANNEL_ANNOUNCEMENTS", 354619224620138496)) big_brother_logs = 468507907357409333 bot = 267659945086812160 -- cgit v1.2.3 From e956170ab1be2ff2af803ce6ea46c6bcb2f91837 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Tue, 24 Nov 2020 17:39:42 +0200 Subject: Add staff AoC channel to whitelist --- bot/exts/christmas/adventofcode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/christmas/adventofcode.py b/bot/exts/christmas/adventofcode.py index b3fe0623..fdd84d5d 100644 --- a/bot/exts/christmas/adventofcode.py +++ b/bot/exts/christmas/adventofcode.py @@ -25,7 +25,7 @@ AOC_SESSION_COOKIE = {"session": Tokens.aoc_session_cookie} EST = timezone("EST") COUNTDOWN_STEP = 60 * 5 -AOC_WHITELIST = WHITELISTED_CHANNELS + (Channels.advent_of_code,) +AOC_WHITELIST = WHITELISTED_CHANNELS + (Channels.advent_of_code, Channels.advent_of_code_staff) def is_in_advent() -> bool: -- cgit v1.2.3 From 2f6236671f89eb1088e985284f87292695e376cf Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Tue, 24 Nov 2020 17:44:08 +0200 Subject: Add comments about AoC env config order and change cookies way --- bot/constants.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/constants.py b/bot/constants.py index 841f2303..bbfe7c3f 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -33,6 +33,7 @@ class AdventOfCode: leaderboard_cache_age_threshold_seconds = 3600 leaderboard_public_ids = [645282] leaderboard_staff_id = 957532 + # Public join codes in environment must be in same order than in 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 @@ -203,7 +204,9 @@ class Roles(NamedTuple): class Tokens(NamedTuple): giphy = environ.get("GIPHY_TOKEN") - aoc_session_cookie = environ.get("AOC_SESSION_COOKIE") + # Public AoC cookies in environment must be in same order than in AdventOfCode.leaderboard_public_ids + aoc_public_session_cookies = environ.get("AOC_PUBLIC_SESSION_COOKIES", "").split(",") + aoc_staff_session_cookie = environ.get("AOC_STAFF_SESSION_COOKIE") omdb = environ.get("OMDB_API_KEY") youtube = environ.get("YOUTUBE_API_KEY") tmdb = environ.get("TMDB_API_KEY") -- cgit v1.2.3 From 85ddb6d8c06722f09842339a43e91d0a88fd9946 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 25 Nov 2020 18:21:30 +0200 Subject: Implement different invitation codes for staff and public leaderboards --- bot/exts/christmas/adventofcode.py | 77 ++++++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 11 deletions(-) (limited to 'bot') diff --git a/bot/exts/christmas/adventofcode.py b/bot/exts/christmas/adventofcode.py index fdd84d5d..be1c733a 100644 --- a/bot/exts/christmas/adventofcode.py +++ b/bot/exts/christmas/adventofcode.py @@ -9,18 +9,19 @@ from typing import List, Tuple import aiohttp import discord +from async_rediscache import RedisCache from bs4 import BeautifulSoup from discord.ext import commands from pytz import timezone +from bot.bot import Bot from bot.constants import AdventOfCode as AocConfig, Channels, Colours, Emojis, Month, Tokens, WHITELISTED_CHANNELS from bot.utils import unlocked_role -from bot.utils.decorators import in_month, override_in_channel +from bot.utils.decorators import in_month, override_in_channel, seasonal_task log = logging.getLogger(__name__) AOC_REQUEST_HEADER = {"user-agent": "PythonDiscord AoC Event Bot"} -AOC_SESSION_COOKIE = {"session": Tokens.aoc_session_cookie} EST = timezone("EST") COUNTDOWN_STEP = 60 * 5 @@ -131,12 +132,19 @@ async def day_countdown(bot: commands.Bot) -> None: class AdventOfCode(commands.Cog): """Advent of Code festivities! Ho Ho Ho!""" - def __init__(self, bot: commands.Bot): + # Mapping for AoC PyDis community leaderboard IDs -> cached amount of members in leaderboard. + public_leaderboard_members = RedisCache() + + # We don't want that users join to multiple leaderboards, so return only 1 code to user. + # User ID -> AoC leaderboard ID + user_join_codes = RedisCache() + + def __init__(self, bot: Bot): self.bot = bot self._base_url = f"https://adventofcode.com/{AocConfig.year}" self.global_leaderboard_url = f"https://adventofcode.com/{AocConfig.year}/leaderboard" - self.private_leaderboard_url = f"{self._base_url}/leaderboard/private/view/{AocConfig.leaderboard_id}" + self.private_leaderboard_url = f"{self._base_url}/leaderboard/private/view/{AocConfig.leaderboard_staff_id}" self.about_aoc_filepath = Path("./bot/resources/advent_of_code/about.json") self.cached_about_aoc = self._build_about_embed() @@ -146,6 +154,7 @@ class AdventOfCode(commands.Cog): self.countdown_task = None self.status_task = None + self.leaderboard_member_update_task = self.bot.loop.create_task(self.leaderboard_members_updater()) countdown_coro = day_countdown(self.bot) self.countdown_task = self.bot.loop.create_task(countdown_coro) @@ -153,6 +162,32 @@ class AdventOfCode(commands.Cog): status_coro = countdown_status(self.bot) self.status_task = self.bot.loop.create_task(status_coro) + self.leaderboard_join_codes = { + aoc_id: join_code for aoc_id, join_code in zip( + AocConfig.leaderboard_public_ids, AocConfig.leaderboard_public_join_codes + ) + } + self.leaderboard_cookies = { + aoc_id: cookie for aoc_id, cookie in zip( + AocConfig.leaderboard_public_ids, Tokens.aoc_public_session_cookies + ) + } + + @seasonal_task(Month.DECEMBER, sleep_time=60 * 30) + async def leaderboard_members_updater(self) -> None: + """Updates public leaderboards cached member amounts in every 30 minutes.""" + # Whole December isn't advent + if not is_in_advent(): + return + + # Update every leaderboard for what we have session cookie + for aoc_id, cookie in self.leaderboard_cookies.items(): + leaderboard = await AocPrivateLeaderboard.from_url(aoc_id, cookie) + log.info(leaderboard.members) + # Update only when API return any members + if len(leaderboard.members) > 0: + await self.public_leaderboard_members.set(aoc_id, len(leaderboard.members)) + @in_month(Month.DECEMBER) @commands.group(name="adventofcode", aliases=("aoc",)) @override_in_channel(AOC_WHITELIST) @@ -234,9 +269,29 @@ class AdventOfCode(commands.Cog): author = ctx.message.author log.info(f"{author.name} ({author.id}) has requested the PyDis AoC leaderboard code") + if ctx.channel.id == Channels.advent_of_code_staff: + join_code = AocConfig.leaderboard_staff_join_code + log.info(f"{author.name} ({author.id}) ran command in staff AoC channel. Returning staff code.") + else: + # We want that user get only 1 code + if await self.user_join_codes.contains(ctx.author.id): + join_code = await self.user_join_codes.get(ctx.author.id) + log.info(f"{author.name} ({author.id}) have already cached AoC join code. Returning it.") + else: + least_id, least = 0, 200 + for aoc_id, amount in await self.public_leaderboard_members.items(): + log.info(amount, least) + if amount < least: + least, least_id = amount, aoc_id + + join_code = self.leaderboard_join_codes[least_id] + # Persist this code to Redis, so we can get it later again. + await self.user_join_codes.set(ctx.author.id, join_code) + log.info(f"{author.name} ({author.id}) got new join code. Persisted it to cache.") + info_str = ( "Head over to https://adventofcode.com/leaderboard/private " - f"with code `{AocConfig.leaderboard_join_code}` to join the PyDis private leaderboard!" + f"with code `{join_code}` to join the PyDis private leaderboard!" ) try: await author.send(info_str) @@ -580,8 +635,8 @@ class AocPrivateLeaderboard: @staticmethod async def json_from_url( - leaderboard_id: int = AocConfig.leaderboard_id, year: int = AocConfig.year - ) -> "AocPrivateLeaderboard": + leaderboard_id: int, cookie: str, year: int = AocConfig.year + ) -> dict: """ Request the API JSON from Advent of Code for leaderboard_id for the specified year's event. @@ -590,7 +645,7 @@ class AocPrivateLeaderboard: api_url = f"https://adventofcode.com/{year}/leaderboard/private/view/{leaderboard_id}.json" log.debug("Querying Advent of Code Private Leaderboard API") - async with aiohttp.ClientSession(cookies=AOC_SESSION_COOKIE, headers=AOC_REQUEST_HEADER) as session: + async with aiohttp.ClientSession(headers=AOC_REQUEST_HEADER, cookies={"session": cookie}) as session: async with session.get(api_url) as resp: if resp.status == 200: raw_dict = await resp.json() @@ -608,9 +663,9 @@ class AocPrivateLeaderboard: ) @classmethod - async def from_url(cls) -> "AocPrivateLeaderboard": + async def from_url(cls, leaderboard_id: int, cookie: str) -> "AocPrivateLeaderboard": """Helper wrapping of AocPrivateLeaderboard.json_from_url and AocPrivateLeaderboard.from_json.""" - api_json = await cls.json_from_url() + api_json = await cls.json_from_url(leaderboard_id, cookie) return cls.from_json(api_json) @staticmethod @@ -738,6 +793,6 @@ def _error_embed_helper(title: str, description: str) -> discord.Embed: return discord.Embed(title=title, description=description, colour=discord.Colour.red()) -def setup(bot: commands.Bot) -> None: +def setup(bot: Bot) -> None: """Advent of Code Cog load.""" bot.add_cog(AdventOfCode(bot)) -- cgit v1.2.3 From c3bbbbd114489d12b24a93ab3ab835fa85eb15e6 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 26 Nov 2020 20:01:59 +0200 Subject: Implement staff and public leaderboards --- bot/exts/christmas/adventofcode.py | 255 ++++++++++++++++++++++++++++--------- 1 file changed, 197 insertions(+), 58 deletions(-) (limited to 'bot') diff --git a/bot/exts/christmas/adventofcode.py b/bot/exts/christmas/adventofcode.py index be1c733a..9b7780ae 100644 --- a/bot/exts/christmas/adventofcode.py +++ b/bot/exts/christmas/adventofcode.py @@ -136,21 +136,24 @@ class AdventOfCode(commands.Cog): public_leaderboard_members = RedisCache() # We don't want that users join to multiple leaderboards, so return only 1 code to user. - # User ID -> AoC leaderboard ID + # User ID -> Join code user_join_codes = RedisCache() + # We must keep track when user got (and what) stars, because we have multiple leaderboards. + # Format: User ID -> AoCCachedMember (pickle) + public_user_data = RedisCache() + def __init__(self, bot: Bot): self.bot = bot self._base_url = f"https://adventofcode.com/{AocConfig.year}" self.global_leaderboard_url = f"https://adventofcode.com/{AocConfig.year}/leaderboard" - self.private_leaderboard_url = f"{self._base_url}/leaderboard/private/view/{AocConfig.leaderboard_staff_id}" self.about_aoc_filepath = Path("./bot/resources/advent_of_code/about.json") self.cached_about_aoc = self._build_about_embed() self.cached_global_leaderboard = None - self.cached_private_leaderboard = None + self.cached_staff_leaderboard = None self.countdown_task = None self.status_task = None @@ -173,6 +176,11 @@ class AdventOfCode(commands.Cog): ) } + self.last_updated = None + self.staff_last_updated = None + self.refresh_lock = asyncio.Lock() + self.staff_refresh_lock = asyncio.Lock() + @seasonal_task(Month.DECEMBER, sleep_time=60 * 30) async def leaderboard_members_updater(self) -> None: """Updates public leaderboards cached member amounts in every 30 minutes.""" @@ -183,11 +191,129 @@ class AdventOfCode(commands.Cog): # Update every leaderboard for what we have session cookie for aoc_id, cookie in self.leaderboard_cookies.items(): leaderboard = await AocPrivateLeaderboard.from_url(aoc_id, cookie) - log.info(leaderboard.members) # Update only when API return any members if len(leaderboard.members) > 0: await self.public_leaderboard_members.set(aoc_id, len(leaderboard.members)) + async def refresh_leaderboard(self) -> None: + """Updates public PyDis leaderboard scores based on dates.""" + self.last_updated = datetime.utcnow() + leaderboard_users = {} + leaderboards = [ + await AocPrivateLeaderboard.json_from_url(aoc_id, cookie) + for aoc_id, cookie in self.leaderboard_cookies.items() + ] + + for leaderboard in leaderboards: + for member_id, data in leaderboard["members"].items(): + leaderboard_users[int(member_id)] = { + "name": data.get("name", "Anonymous User"), + "aoc_id": int(member_id), + "days": { + day: { + "star_one": "1" in stars, + "star_two": "2" in stars, + "star_one_earned": int(stars["1"]["get_star_ts"]) if "1" in stars else None, + "star_two_earned": int(stars["2"]["get_star_ts"]) if "2" in stars else None, + } for day, stars in data.get("completion_day_level", {}).items() + } + } + + # Iterate over every advent day + for day in range(1, 26): + day = str(day) + star_one_users = [] + star_two_users = [] + + for user, user_data in leaderboard_users.items(): + if day in user_data["days"]: + if user_data["days"][day]["star_one"]: + star_one_users.append({ + "id": user, + "earned": datetime.fromtimestamp(user_data["days"][day]["star_one_earned"]), + }) + + if user_data["days"][day]["star_two"]: + star_two_users.append({ + "id": user, + "earned": datetime.fromtimestamp(user_data["days"][day]["star_two_earned"]), + }) + + # Sort these lists based on user star earning time + star_one_users = sorted(star_one_users, key=lambda k: k["earned"])[:100] + star_two_users = sorted(star_two_users, key=lambda k: k["earned"])[:100] + + points = 100 + for star_user_one in star_one_users: + if "score" in leaderboard_users[star_user_one["id"]]: + leaderboard_users[star_user_one["id"]]["score"] += points + else: + leaderboard_users[star_user_one["id"]]["score"] = points + points -= 1 + + points = 100 + for star_user_two in star_two_users: + if "score" in leaderboard_users[star_user_two["id"]]: + leaderboard_users[star_user_two["id"]]["score"] += points + else: + leaderboard_users[star_user_two["id"]]["score"] = points + points -= 1 + + # Put completions also in to make building easier later. + for user, user_data in leaderboard_users.items(): + completions_star_one = sum([1 for day in user_data["days"].values() if day["star_one"]]) + completions_star_two = sum([1 for day in user_data["days"].values() if day["star_two"]]) + + leaderboard_users[user]["star_one_completions"] = completions_star_one + leaderboard_users[user]["star_two_completions"] = completions_star_two + + # Finally clear old cache and persist everything to Redis + await self.public_user_data.clear() + [await self.public_user_data.set(user, json.dumps(user_data)) for user, user_data in leaderboard_users.items()] + + async def check_leaderboard(self) -> None: + """Checks should be public leaderboard refreshed and refresh when required.""" + async with self.refresh_lock: + secs = AocConfig.leaderboard_cache_age_threshold_seconds + if self.last_updated is None or self.last_updated < datetime.utcnow() - timedelta(seconds=secs): + await self.refresh_leaderboard() + + async def check_staff_leaderboard(self) -> None: + """Checks should be staff leaderboard refreshed and refresh when required.""" + async with self.staff_refresh_lock: + secs = AocConfig.leaderboard_cache_age_threshold_seconds + if self.staff_last_updated is None or self.staff_last_updated < datetime.utcnow() - timedelta(seconds=secs): + self.staff_last_updated = datetime.utcnow() + self.cached_staff_leaderboard = await AocPrivateLeaderboard.from_url( + AocConfig.leaderboard_staff_id, + Tokens.aoc_staff_session_cookie + ) + + async def get_leaderboard(self, members_amount: int) -> str: + """Generates leaderboard based on Redis data.""" + await self.check_leaderboard() + leaderboard_members = sorted( + [json.loads(data) for user, data in await self.public_user_data.items()], key=lambda k: k["score"] + )[:members_amount] + + stargroup = f"{Emojis.star}, {Emojis.star * 2}" + header = f"{' ' * 3}{'Score'} {'Name':^25} {stargroup:^7}\n{'-' * 44}" + table = "" + for i, member in enumerate(leaderboard_members): + if member["name"] == "Anonymous User": + name = f"{member['name']} #{member['aoc_id']}" + else: + name = member["name"] + + table += ( + f"{i + 1:2}) {member['score']:4} {name:25.25} " + f"({member['star_one_completions']:2}, {member['star_two_completions']:2})\n" + ) + else: + table = f"```{header}\n{table}```" + + return table + @in_month(Month.DECEMBER) @commands.group(name="adventofcode", aliases=("aoc",)) @override_in_channel(AOC_WHITELIST) @@ -316,26 +442,32 @@ class AdventOfCode(commands.Cog): limit will default to this maximum and provide feedback to the user. """ async with ctx.typing(): - await self._check_leaderboard_cache(ctx) - - if not self.cached_private_leaderboard: - # Feedback on issues with leaderboard caching are sent by _check_leaderboard_cache() - # Short circuit here if there's an issue - return - + staff = ctx.channel.id == Channels.advent_of_code_staff number_of_people_to_display = await self._check_n_entries(ctx, number_of_people_to_display) - # Generate leaderboard table for embed - members_to_print = self.cached_private_leaderboard.top_n(number_of_people_to_display) - table = AocPrivateLeaderboard.build_leaderboard_embed(members_to_print) + if staff: + await self.check_staff_leaderboard() + members_to_print = self.cached_staff_leaderboard.top_n(number_of_people_to_display) + table = AocPrivateLeaderboard.build_leaderboard_embed(members_to_print) + else: + table = await self.get_leaderboard(number_of_people_to_display) # Build embed aoc_embed = discord.Embed( - description=f"Total members: {len(self.cached_private_leaderboard.members)}", + description=( + "Total members: " + f"{len(self.cached_staff_leaderboard.members) if staff else await self.public_user_data.length()}" + ), colour=Colours.soft_green, - timestamp=self.cached_private_leaderboard.last_updated + timestamp=self.staff_last_updated if staff else self.last_updated ) - aoc_embed.set_author(name="Advent of Code", url=self.private_leaderboard_url) + if ctx.channel.id == Channels.advent_of_code_staff: + aoc_embed.set_author( + name="Advent of Code", + url=f"{self._base_url}/leaderboard/private/view/{AocConfig.leaderboard_staff_id}" + ) + else: + aoc_embed.set_author(name="Advent of Code") aoc_embed.set_footer(text="Last Updated") await ctx.send( @@ -356,29 +488,54 @@ class AdventOfCode(commands.Cog): Embed will display the total members and the number of users who have completed each day's puzzle """ async with ctx.typing(): - await self._check_leaderboard_cache(ctx) - - if not self.cached_private_leaderboard: - # Feedback on issues with leaderboard caching are sent by _check_leaderboard_cache() - # Short circuit here if there's an issue - return + is_staff = ctx.channel.id == Channels.advent_of_code_staff + if is_staff: + await self.check_staff_leaderboard() + else: + await self.check_leaderboard() # Build ASCII table - total_members = len(self.cached_private_leaderboard.members) + if is_staff: + total_members = len(self.cached_staff_leaderboard.members) + else: + total_members = await self.public_user_data.length() + _star = Emojis.star header = f"{'Day':4}{_star:^8}{_star*2:^4}{'% ' + _star:^8}{'% ' + _star*2:^4}\n{'='*35}" table = "" - for day, completions in enumerate(self.cached_private_leaderboard.daily_completion_summary): - per_one_star = f"{(completions[0]/total_members)*100:.2f}" - per_two_star = f"{(completions[1]/total_members)*100:.2f}" + if is_staff: + for day, completions in enumerate(self.cached_staff_leaderboard.daily_completion_summary): + per_one_star = f"{(completions[0]/total_members)*100:.2f}" + per_two_star = f"{(completions[1]/total_members)*100:.2f}" - table += f"{day+1:3}){completions[0]:^8}{completions[1]:^6}{per_one_star:^10}{per_two_star:^6}\n" + table += f"{day+1:3}){completions[0]:^8}{completions[1]:^6}{per_one_star:^10}{per_two_star:^6}\n" + else: + completions = {} + # Build data for completion rates + for _, user_data in await self.public_user_data.items(): + user_data = json.loads(user_data) + for day, stars in user_data["days"].items(): + day = int(day) + if day not in completions: + completions[day] = [0, 0] + + if stars["star_one"]: + completions[day][0] += 1 + if stars["star_two"]: + completions[day][1] += 1 + + for day, completion in completions.items(): + per_one_star = f"{(completion[0]/total_members)*100:.2f}" + per_two_star = f"{(completion[1] / total_members) * 100:.2f}" + + table += f"{day:3}){completion[0]:^8}{completion[1]:^6}{per_one_star:^10}{per_two_star:^6}\n" table = f"```\n{header}\n{table}```" # Build embed daily_stats_embed = discord.Embed( - colour=Colours.soft_green, timestamp=self.cached_private_leaderboard.last_updated + colour=Colours.soft_green, + timestamp=self.staff_last_updated if is_staff else self.last_updated ) daily_stats_embed.set_author(name="Advent of Code", url=self._base_url) daily_stats_embed.set_footer(text="Last Updated") @@ -402,7 +559,7 @@ class AdventOfCode(commands.Cog): limit will default to this maximum and provide feedback to the user. """ async with ctx.typing(): - await self._check_leaderboard_cache(ctx, global_board=True) + await self._check_leaderboard_cache(ctx) if not self.cached_global_leaderboard: # Feedback on issues with leaderboard caching are sent by _check_leaderboard_cache() @@ -425,41 +582,31 @@ class AdventOfCode(commands.Cog): embed=aoc_embed, ) - async def _check_leaderboard_cache(self, ctx: commands.Context, global_board: bool = False) -> None: + async def _check_leaderboard_cache(self, ctx: commands.Context) -> None: """ Check age of current leaderboard & pull a new one if the board is too old. global_board is a boolean to toggle between the global board and the Pydis private board """ - # Toggle between global & private leaderboards - if global_board: - log.debug("Checking global leaderboard cache") - leaderboard_str = "cached_global_leaderboard" - _shortstr = "global" - else: - log.debug("Checking private leaderboard cache") - leaderboard_str = "cached_private_leaderboard" - _shortstr = "private" - - leaderboard = getattr(self, leaderboard_str) + leaderboard = self.cached_global_leaderboard if not leaderboard: - log.debug(f"No cached {_shortstr} leaderboard found") - await self._boardgetter(global_board) + log.debug("No cached global leaderboard found") + self.cached_global_leaderboard = await AocGlobalLeaderboard.from_url() else: leaderboard_age = datetime.utcnow() - leaderboard.last_updated age_seconds = leaderboard_age.total_seconds() if age_seconds < AocConfig.leaderboard_cache_age_threshold_seconds: - log.debug(f"Cached {_shortstr} leaderboard age less than threshold ({age_seconds} seconds old)") + log.debug(f"Cached global leaderboard age less than threshold ({age_seconds} seconds old)") else: - log.debug(f"Cached {_shortstr} leaderboard age greater than threshold ({age_seconds} seconds old)") - await self._boardgetter(global_board) + log.debug(f"Cached global leaderboard age greater than threshold ({age_seconds} seconds old)") + self.cached_global_leaderboard = await AocGlobalLeaderboard.from_url() - leaderboard = getattr(self, leaderboard_str) + leaderboard = self.cached_global_leaderboard if not leaderboard: await ctx.send( "", embed=_error_embed_helper( - title=f"Something's gone wrong and there's no cached {_shortstr} leaderboard!", + title="Something's gone wrong and there's no cached global leaderboard!", description="Please check in with a staff member.", ), ) @@ -475,8 +622,7 @@ class AdventOfCode(commands.Cog): ) await ctx.send( f":x: {author.mention}, number of entries to display must be a positive " - f"integer less than or equal to {max_entries}\n\n" - f"Head to {self.private_leaderboard_url} to view the entire leaderboard" + f"integer less than or equal to {max_entries}" ) number_of_people_to_display = max_entries @@ -496,13 +642,6 @@ class AdventOfCode(commands.Cog): return about_embed - async def _boardgetter(self, global_board: bool) -> None: - """Invoke the proper leaderboard getter based on the global_board boolean.""" - if global_board: - self.cached_global_leaderboard = await AocGlobalLeaderboard.from_url() - else: - self.cached_private_leaderboard = await AocPrivateLeaderboard.from_url() - def cog_unload(self) -> None: """Cancel season-related tasks on cog unload.""" log.debug("Unloading the cog and canceling the background task.") -- cgit v1.2.3 From eddb38e9c091e8e0f3cb4529ec501091f71d901b Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Thu, 26 Nov 2020 20:05:29 +0200 Subject: Store AoC leaderboard IDs instead join codes for users mapping --- bot/exts/christmas/adventofcode.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'bot') diff --git a/bot/exts/christmas/adventofcode.py b/bot/exts/christmas/adventofcode.py index 9b7780ae..70bdc50a 100644 --- a/bot/exts/christmas/adventofcode.py +++ b/bot/exts/christmas/adventofcode.py @@ -136,8 +136,8 @@ class AdventOfCode(commands.Cog): public_leaderboard_members = RedisCache() # We don't want that users join to multiple leaderboards, so return only 1 code to user. - # User ID -> Join code - user_join_codes = RedisCache() + # User ID -> AoC Leaderboard ID + user_leaderboards = RedisCache() # We must keep track when user got (and what) stars, because we have multiple leaderboards. # Format: User ID -> AoCCachedMember (pickle) @@ -400,8 +400,8 @@ class AdventOfCode(commands.Cog): log.info(f"{author.name} ({author.id}) ran command in staff AoC channel. Returning staff code.") else: # We want that user get only 1 code - if await self.user_join_codes.contains(ctx.author.id): - join_code = await self.user_join_codes.get(ctx.author.id) + if await self.user_leaderboards.contains(ctx.author.id): + join_code = self.leaderboard_join_codes[await self.user_leaderboards.get(ctx.author.id)] log.info(f"{author.name} ({author.id}) have already cached AoC join code. Returning it.") else: least_id, least = 0, 200 @@ -412,7 +412,7 @@ class AdventOfCode(commands.Cog): join_code = self.leaderboard_join_codes[least_id] # Persist this code to Redis, so we can get it later again. - await self.user_join_codes.set(ctx.author.id, join_code) + await self.user_leaderboards.set(ctx.author.id, least_id) log.info(f"{author.name} ({author.id}) got new join code. Persisted it to cache.") info_str = ( @@ -466,6 +466,11 @@ class AdventOfCode(commands.Cog): name="Advent of Code", url=f"{self._base_url}/leaderboard/private/view/{AocConfig.leaderboard_staff_id}" ) + elif await self.user_leaderboards.contains(ctx.author.id): + aoc_embed.set_author( + name="Advent of Code", + url=f"{self._base_url}/leaderboard/private/view/{await self.user_leaderboards.get(ctx.author.id)}" + ) else: aoc_embed.set_author(name="Advent of Code") aoc_embed.set_footer(text="Last Updated") -- cgit v1.2.3 From 40461802eca56b9d92e627e275b402bdcd3e6824 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Fri, 27 Nov 2020 17:31:05 +0200 Subject: Add comment about choosing leaderboard for user --- bot/exts/christmas/adventofcode.py | 1 + 1 file changed, 1 insertion(+) (limited to 'bot') diff --git a/bot/exts/christmas/adventofcode.py b/bot/exts/christmas/adventofcode.py index 70bdc50a..2b8ac7bf 100644 --- a/bot/exts/christmas/adventofcode.py +++ b/bot/exts/christmas/adventofcode.py @@ -404,6 +404,7 @@ class AdventOfCode(commands.Cog): join_code = self.leaderboard_join_codes[await self.user_leaderboards.get(ctx.author.id)] log.info(f"{author.name} ({author.id}) have already cached AoC join code. Returning it.") else: + # Find leaderboard that have least members inside (based on cache) least_id, least = 0, 200 for aoc_id, amount in await self.public_leaderboard_members.items(): log.info(amount, least) -- cgit v1.2.3 From ab28341bfdcf91d0ebf6ce1e4bc492b748067330 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Fri, 27 Nov 2020 17:32:03 +0200 Subject: Remove unnecessary check for members in leaderboard updater task --- bot/exts/christmas/adventofcode.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'bot') diff --git a/bot/exts/christmas/adventofcode.py b/bot/exts/christmas/adventofcode.py index 2b8ac7bf..0d73eb3f 100644 --- a/bot/exts/christmas/adventofcode.py +++ b/bot/exts/christmas/adventofcode.py @@ -191,9 +191,7 @@ class AdventOfCode(commands.Cog): # Update every leaderboard for what we have session cookie for aoc_id, cookie in self.leaderboard_cookies.items(): leaderboard = await AocPrivateLeaderboard.from_url(aoc_id, cookie) - # Update only when API return any members - if len(leaderboard.members) > 0: - await self.public_leaderboard_members.set(aoc_id, len(leaderboard.members)) + await self.public_leaderboard_members.set(aoc_id, len(leaderboard.members)) async def refresh_leaderboard(self) -> None: """Updates public PyDis leaderboard scores based on dates.""" -- cgit v1.2.3 From bede07a6983a1d15461cd65914f41df624244dc3 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Fri, 27 Nov 2020 18:00:35 +0200 Subject: Handle leaderboard cache create/update fail --- bot/exts/christmas/adventofcode.py | 64 +++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 12 deletions(-) (limited to 'bot') diff --git a/bot/exts/christmas/adventofcode.py b/bot/exts/christmas/adventofcode.py index 0d73eb3f..9d7289c2 100644 --- a/bot/exts/christmas/adventofcode.py +++ b/bot/exts/christmas/adventofcode.py @@ -3,6 +3,7 @@ import json import logging import math import re +import typing from datetime import datetime, timedelta from pathlib import Path from typing import List, Tuple @@ -202,6 +203,11 @@ class AdventOfCode(commands.Cog): for aoc_id, cookie in self.leaderboard_cookies.items() ] + # Check does this have any failed requests + if False in leaderboards: + log.warning("Unable to get one or more of the public leaderboards. Not updating cache.") + return + for leaderboard in leaderboards: for member_id, data in leaderboard["members"].items(): leaderboard_users[int(member_id)] = { @@ -281,15 +287,24 @@ class AdventOfCode(commands.Cog): async with self.staff_refresh_lock: secs = AocConfig.leaderboard_cache_age_threshold_seconds if self.staff_last_updated is None or self.staff_last_updated < datetime.utcnow() - timedelta(seconds=secs): - self.staff_last_updated = datetime.utcnow() - self.cached_staff_leaderboard = await AocPrivateLeaderboard.from_url( + leaderboard = await AocPrivateLeaderboard.from_url( AocConfig.leaderboard_staff_id, Tokens.aoc_staff_session_cookie ) - async def get_leaderboard(self, members_amount: int) -> str: + if leaderboard is not None: + self.staff_last_updated = datetime.utcnow() + self.cached_staff_leaderboard = leaderboard + else: + log.warning("Can't update staff leaderboard. Got unexpected response.") + + async def get_leaderboard(self, members_amount: int, context: commands.Context) -> typing.Union[str, bool]: """Generates leaderboard based on Redis data.""" - await self.check_leaderboard() + # When we don't have users in cache, log warning and return False. + if await self.public_user_data.length() == 0: + log.warning("Don't have cache for displaying AoC public leaderboard.") + return False + leaderboard_members = sorted( [json.loads(data) for user, data in await self.public_user_data.items()], key=lambda k: k["score"] )[:members_amount] @@ -446,10 +461,22 @@ class AdventOfCode(commands.Cog): if staff: await self.check_staff_leaderboard() - members_to_print = self.cached_staff_leaderboard.top_n(number_of_people_to_display) - table = AocPrivateLeaderboard.build_leaderboard_embed(members_to_print) + if self.cached_staff_leaderboard is not None: + members_to_print = self.cached_staff_leaderboard.top_n(number_of_people_to_display) + table = AocPrivateLeaderboard.build_leaderboard_embed(members_to_print) + else: + log.warning("Missing AoC staff leaderboard cache.") + table = False else: - table = await self.get_leaderboard(number_of_people_to_display) + await self.check_leaderboard() + table = await self.get_leaderboard(number_of_people_to_display, ctx) + + # When we don't have cache, show it to user. + if table is False: + await ctx.send( + ":x: Sorry, we can't get our leaderboard cache. Please let staff know about it." + ) + return # Build embed aoc_embed = discord.Embed( @@ -495,6 +522,9 @@ class AdventOfCode(commands.Cog): is_staff = ctx.channel.id == Channels.advent_of_code_staff if is_staff: await self.check_staff_leaderboard() + if self.cached_staff_leaderboard is None: + await ctx.send(":x: Missing leaderboard cache.") + return else: await self.check_leaderboard() @@ -503,6 +533,9 @@ class AdventOfCode(commands.Cog): total_members = len(self.cached_staff_leaderboard.members) else: total_members = await self.public_user_data.length() + if total_members == 0: + await ctx.send(":x: Missing leaderboard cache. Please notify staff about it.") + return _star = Emojis.star header = f"{'Day':4}{_star:^8}{_star*2:^4}{'% ' + _star:^8}{'% ' + _star*2:^4}\n{'='*35}" @@ -779,7 +812,7 @@ class AocPrivateLeaderboard: @staticmethod async def json_from_url( leaderboard_id: int, cookie: str, year: int = AocConfig.year - ) -> dict: + ) -> typing.Union[dict, bool]: """ Request the API JSON from Advent of Code for leaderboard_id for the specified year's event. @@ -791,10 +824,14 @@ class AocPrivateLeaderboard: async with aiohttp.ClientSession(headers=AOC_REQUEST_HEADER, cookies={"session": cookie}) as session: async with session.get(api_url) as resp: if resp.status == 200: - raw_dict = await resp.json() + try: + raw_dict = await resp.json() + except aiohttp.ContentTypeError: + log.warning(f"Got invalid response type from AoC API. Leaderboard ID {leaderboard_id}") + return False else: log.warning(f"Bad response received from AoC ({resp.status}), check session cookie") - resp.raise_for_status() + return False return raw_dict @@ -806,10 +843,13 @@ class AocPrivateLeaderboard: ) @classmethod - async def from_url(cls, leaderboard_id: int, cookie: str) -> "AocPrivateLeaderboard": + async def from_url(cls, leaderboard_id: int, cookie: str) -> typing.Union["AocPrivateLeaderboard", None]: """Helper wrapping of AocPrivateLeaderboard.json_from_url and AocPrivateLeaderboard.from_json.""" api_json = await cls.json_from_url(leaderboard_id, cookie) - return cls.from_json(api_json) + if api_json is not False: + return cls.from_json(api_json) + else: + return None @staticmethod def _sorted_members(injson: dict) -> list: -- cgit v1.2.3 From 6be2a47e32e5e07ab23d2796b5e551be1ec7b0b6 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Fri, 27 Nov 2020 18:02:09 +0200 Subject: Use default 0 for score and reverse leaderboard members --- bot/exts/christmas/adventofcode.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/christmas/adventofcode.py b/bot/exts/christmas/adventofcode.py index 9d7289c2..feff09aa 100644 --- a/bot/exts/christmas/adventofcode.py +++ b/bot/exts/christmas/adventofcode.py @@ -306,7 +306,9 @@ class AdventOfCode(commands.Cog): return False leaderboard_members = sorted( - [json.loads(data) for user, data in await self.public_user_data.items()], key=lambda k: k["score"] + [json.loads(data) for user, data in await self.public_user_data.items()], + key=lambda k: k.get("score", 0), + reverse=True )[:members_amount] stargroup = f"{Emojis.star}, {Emojis.star * 2}" -- cgit v1.2.3 From ffa3619a19515c147013669339f51d36f83a5f29 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Fri, 27 Nov 2020 19:14:27 +0200 Subject: Fix adventofcode extension (and constants) grammar Co-authored-by: Joe Banks --- bot/constants.py | 4 ++-- bot/exts/christmas/adventofcode.py | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'bot') diff --git a/bot/constants.py b/bot/constants.py index bbfe7c3f..d24551f6 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -33,7 +33,7 @@ class AdventOfCode: leaderboard_cache_age_threshold_seconds = 3600 leaderboard_public_ids = [645282] leaderboard_staff_id = 957532 - # Public join codes in environment must be in same order than in AdventOfCode.leaderboard_public_ids + # 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 @@ -204,7 +204,7 @@ class Roles(NamedTuple): class Tokens(NamedTuple): giphy = environ.get("GIPHY_TOKEN") - # Public AoC cookies in environment must be in same order than in AdventOfCode.leaderboard_public_ids + # Public AoC cookies in environment must be in the same order as AdventOfCode.leaderboard_public_ids aoc_public_session_cookies = environ.get("AOC_PUBLIC_SESSION_COOKIES", "").split(",") aoc_staff_session_cookie = environ.get("AOC_STAFF_SESSION_COOKIE") omdb = environ.get("OMDB_API_KEY") diff --git a/bot/exts/christmas/adventofcode.py b/bot/exts/christmas/adventofcode.py index feff09aa..b188059e 100644 --- a/bot/exts/christmas/adventofcode.py +++ b/bot/exts/christmas/adventofcode.py @@ -185,11 +185,11 @@ class AdventOfCode(commands.Cog): @seasonal_task(Month.DECEMBER, sleep_time=60 * 30) async def leaderboard_members_updater(self) -> None: """Updates public leaderboards cached member amounts in every 30 minutes.""" - # Whole December isn't advent + # Check whether we are in the 25 days of advent if not is_in_advent(): return - # Update every leaderboard for what we have session cookie + # Update every leaderboard with our session cookies for aoc_id, cookie in self.leaderboard_cookies.items(): leaderboard = await AocPrivateLeaderboard.from_url(aoc_id, cookie) await self.public_leaderboard_members.set(aoc_id, len(leaderboard.members)) @@ -203,7 +203,7 @@ class AdventOfCode(commands.Cog): for aoc_id, cookie in self.leaderboard_cookies.items() ] - # Check does this have any failed requests + # Check if any requests failed if False in leaderboards: log.warning("Unable to get one or more of the public leaderboards. Not updating cache.") return @@ -243,7 +243,7 @@ class AdventOfCode(commands.Cog): "earned": datetime.fromtimestamp(user_data["days"][day]["star_two_earned"]), }) - # Sort these lists based on user star earning time + # Sort these lists based on the time a user earnt a star star_one_users = sorted(star_one_users, key=lambda k: k["earned"])[:100] star_two_users = sorted(star_two_users, key=lambda k: k["earned"])[:100] @@ -263,7 +263,7 @@ class AdventOfCode(commands.Cog): leaderboard_users[star_user_two["id"]]["score"] = points points -= 1 - # Put completions also in to make building easier later. + # Attach star completions for building the response later for user, user_data in leaderboard_users.items(): completions_star_one = sum([1 for day in user_data["days"].values() if day["star_one"]]) completions_star_two = sum([1 for day in user_data["days"].values() if day["star_two"]]) @@ -271,7 +271,7 @@ class AdventOfCode(commands.Cog): leaderboard_users[user]["star_one_completions"] = completions_star_one leaderboard_users[user]["star_two_completions"] = completions_star_two - # Finally clear old cache and persist everything to Redis + # Finally, clear old cache and persist everything to Redis await self.public_user_data.clear() [await self.public_user_data.set(user, json.dumps(user_data)) for user, user_data in leaderboard_users.items()] @@ -300,7 +300,7 @@ class AdventOfCode(commands.Cog): async def get_leaderboard(self, members_amount: int, context: commands.Context) -> typing.Union[str, bool]: """Generates leaderboard based on Redis data.""" - # When we don't have users in cache, log warning and return False. + # When we don't have users in cache, warn and return False. if await self.public_user_data.length() == 0: log.warning("Don't have cache for displaying AoC public leaderboard.") return False @@ -414,12 +414,12 @@ class AdventOfCode(commands.Cog): join_code = AocConfig.leaderboard_staff_join_code log.info(f"{author.name} ({author.id}) ran command in staff AoC channel. Returning staff code.") else: - # We want that user get only 1 code + # Ensure we use the same leaderboard code for the same user if await self.user_leaderboards.contains(ctx.author.id): join_code = self.leaderboard_join_codes[await self.user_leaderboards.get(ctx.author.id)] - log.info(f"{author.name} ({author.id}) have already cached AoC join code. Returning it.") + log.info(f"{author.name} ({author.id}) has a cached AoC join code, returning it.") else: - # Find leaderboard that have least members inside (based on cache) + # Find the leaderboard that has the least members inside from cache least_id, least = 0, 200 for aoc_id, amount in await self.public_leaderboard_members.items(): log.info(amount, least) -- cgit v1.2.3 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') 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 From 0c79e90c8f9637fd6136047fecdec5813d2ccca3 Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 00:42:55 +0100 Subject: Remove adventofcode.py in favour of subpackage As the Advent of Code file was getting massive, I've removed the old single-file based extension as I'm going to replace it with a partially rewritten subpackage-based extension. --- bot/exts/christmas/adventofcode.py | 983 ------------------------------------- 1 file changed, 983 deletions(-) delete mode 100644 bot/exts/christmas/adventofcode.py (limited to 'bot') diff --git a/bot/exts/christmas/adventofcode.py b/bot/exts/christmas/adventofcode.py deleted file mode 100644 index b188059e..00000000 --- a/bot/exts/christmas/adventofcode.py +++ /dev/null @@ -1,983 +0,0 @@ -import asyncio -import json -import logging -import math -import re -import typing -from datetime import datetime, timedelta -from pathlib import Path -from typing import List, Tuple - -import aiohttp -import discord -from async_rediscache import RedisCache -from bs4 import BeautifulSoup -from discord.ext import commands -from pytz import timezone - -from bot.bot import Bot -from bot.constants import AdventOfCode as AocConfig, Channels, Colours, Emojis, Month, Tokens, WHITELISTED_CHANNELS -from bot.utils import unlocked_role -from bot.utils.decorators import in_month, override_in_channel, seasonal_task - -log = logging.getLogger(__name__) - -AOC_REQUEST_HEADER = {"user-agent": "PythonDiscord AoC Event Bot"} - -EST = timezone("EST") -COUNTDOWN_STEP = 60 * 5 - -AOC_WHITELIST = WHITELISTED_CHANNELS + (Channels.advent_of_code, Channels.advent_of_code_staff) - - -def is_in_advent() -> bool: - """Utility function to check if we are between December 1st and December 25th.""" - # Run the code from the 1st to the 24th - return datetime.now(EST).day in range(1, 25) and datetime.now(EST).month == 12 - - -def time_left_to_aoc_midnight() -> Tuple[datetime, timedelta]: - """Calculates the amount of time left until midnight in UTC-5 (Advent of Code maintainer timezone).""" - # Change all time properties back to 00:00 - todays_midnight = datetime.now(EST).replace(microsecond=0, - second=0, - minute=0, - hour=0) - - # We want tomorrow so add a day on - tomorrow = todays_midnight + timedelta(days=1) - - # Calculate the timedelta between the current time and midnight - return tomorrow, tomorrow - datetime.now(EST) - - -async def countdown_status(bot: commands.Bot) -> None: - """Set the playing status of the bot to the minutes & hours left until the next day's challenge.""" - while is_in_advent(): - _, time_left = time_left_to_aoc_midnight() - - aligned_seconds = int(math.ceil(time_left.seconds / COUNTDOWN_STEP)) * COUNTDOWN_STEP - hours, minutes = aligned_seconds // 3600, aligned_seconds // 60 % 60 - - if aligned_seconds == 0: - playing = "right now!" - elif aligned_seconds == COUNTDOWN_STEP: - playing = f"in less than {minutes} minutes" - elif hours == 0: - playing = f"in {minutes} minutes" - elif hours == 23: - playing = f"since {60 - minutes} minutes ago" - else: - playing = f"in {hours} hours and {minutes} minutes" - - # Status will look like "Playing in 5 hours and 30 minutes" - await bot.change_presence(activity=discord.Game(playing)) - - # Sleep until next aligned time or a full step if already aligned - delay = time_left.seconds % COUNTDOWN_STEP or COUNTDOWN_STEP - await asyncio.sleep(delay) - - -async def day_countdown(bot: commands.Bot) -> None: - """ - Calculate the number of seconds left until the next day of Advent. - - Once we have calculated this we should then sleep that number and when the time is reached, ping - the Advent of Code role notifying them that the new challenge is ready. - """ - while is_in_advent(): - tomorrow, time_left = time_left_to_aoc_midnight() - - # Correct `time_left.seconds` for the sleep we have after unlocking the role (-5) and adding - # a second (+1) as the bot is consistently ~0.5 seconds early in announcing the puzzles. - await asyncio.sleep(time_left.seconds - 4) - - channel = bot.get_channel(Channels.advent_of_code) - - if not channel: - log.error("Could not find the AoC channel to send notification in") - break - - aoc_role = channel.guild.get_role(AocConfig.role_id) - if not aoc_role: - log.error("Could not find the AoC role to announce the daily puzzle") - break - - async with unlocked_role(aoc_role, delay=5): - puzzle_url = f"https://adventofcode.com/{AocConfig.year}/day/{tomorrow.day}" - - # Check if the puzzle is already available to prevent our members from spamming - # the puzzle page before it's available by making a small HEAD request. - for retry in range(1, 5): - log.debug(f"Checking if the puzzle is already available (attempt {retry}/4)") - async with bot.http_session.head(puzzle_url, raise_for_status=False) as resp: - if resp.status == 200: - log.debug("Puzzle is available; let's send an announcement message.") - break - log.debug(f"The puzzle is not yet available (status={resp.status})") - await asyncio.sleep(10) - else: - log.error("The puzzle does does not appear to be available at this time, canceling announcement") - break - - await channel.send( - f"{aoc_role.mention} Good morning! Day {tomorrow.day} is ready to be attempted. " - f"View it online now at {puzzle_url}. Good luck!" - ) - - # Wait a couple minutes so that if our sleep didn't sleep enough - # time we don't end up announcing twice. - await asyncio.sleep(120) - - -class AdventOfCode(commands.Cog): - """Advent of Code festivities! Ho Ho Ho!""" - - # Mapping for AoC PyDis community leaderboard IDs -> cached amount of members in leaderboard. - public_leaderboard_members = RedisCache() - - # We don't want that users join to multiple leaderboards, so return only 1 code to user. - # User ID -> AoC Leaderboard ID - user_leaderboards = RedisCache() - - # We must keep track when user got (and what) stars, because we have multiple leaderboards. - # Format: User ID -> AoCCachedMember (pickle) - public_user_data = RedisCache() - - def __init__(self, bot: Bot): - self.bot = bot - - self._base_url = f"https://adventofcode.com/{AocConfig.year}" - self.global_leaderboard_url = f"https://adventofcode.com/{AocConfig.year}/leaderboard" - - self.about_aoc_filepath = Path("./bot/resources/advent_of_code/about.json") - self.cached_about_aoc = self._build_about_embed() - - self.cached_global_leaderboard = None - self.cached_staff_leaderboard = None - - self.countdown_task = None - self.status_task = None - self.leaderboard_member_update_task = self.bot.loop.create_task(self.leaderboard_members_updater()) - - countdown_coro = day_countdown(self.bot) - self.countdown_task = self.bot.loop.create_task(countdown_coro) - - status_coro = countdown_status(self.bot) - self.status_task = self.bot.loop.create_task(status_coro) - - self.leaderboard_join_codes = { - aoc_id: join_code for aoc_id, join_code in zip( - AocConfig.leaderboard_public_ids, AocConfig.leaderboard_public_join_codes - ) - } - self.leaderboard_cookies = { - aoc_id: cookie for aoc_id, cookie in zip( - AocConfig.leaderboard_public_ids, Tokens.aoc_public_session_cookies - ) - } - - self.last_updated = None - self.staff_last_updated = None - self.refresh_lock = asyncio.Lock() - self.staff_refresh_lock = asyncio.Lock() - - @seasonal_task(Month.DECEMBER, sleep_time=60 * 30) - async def leaderboard_members_updater(self) -> None: - """Updates public leaderboards cached member amounts in every 30 minutes.""" - # Check whether we are in the 25 days of advent - if not is_in_advent(): - return - - # Update every leaderboard with our session cookies - for aoc_id, cookie in self.leaderboard_cookies.items(): - leaderboard = await AocPrivateLeaderboard.from_url(aoc_id, cookie) - await self.public_leaderboard_members.set(aoc_id, len(leaderboard.members)) - - async def refresh_leaderboard(self) -> None: - """Updates public PyDis leaderboard scores based on dates.""" - self.last_updated = datetime.utcnow() - leaderboard_users = {} - leaderboards = [ - await AocPrivateLeaderboard.json_from_url(aoc_id, cookie) - for aoc_id, cookie in self.leaderboard_cookies.items() - ] - - # Check if any requests failed - if False in leaderboards: - log.warning("Unable to get one or more of the public leaderboards. Not updating cache.") - return - - for leaderboard in leaderboards: - for member_id, data in leaderboard["members"].items(): - leaderboard_users[int(member_id)] = { - "name": data.get("name", "Anonymous User"), - "aoc_id": int(member_id), - "days": { - day: { - "star_one": "1" in stars, - "star_two": "2" in stars, - "star_one_earned": int(stars["1"]["get_star_ts"]) if "1" in stars else None, - "star_two_earned": int(stars["2"]["get_star_ts"]) if "2" in stars else None, - } for day, stars in data.get("completion_day_level", {}).items() - } - } - - # Iterate over every advent day - for day in range(1, 26): - day = str(day) - star_one_users = [] - star_two_users = [] - - for user, user_data in leaderboard_users.items(): - if day in user_data["days"]: - if user_data["days"][day]["star_one"]: - star_one_users.append({ - "id": user, - "earned": datetime.fromtimestamp(user_data["days"][day]["star_one_earned"]), - }) - - if user_data["days"][day]["star_two"]: - star_two_users.append({ - "id": user, - "earned": datetime.fromtimestamp(user_data["days"][day]["star_two_earned"]), - }) - - # Sort these lists based on the time a user earnt a star - star_one_users = sorted(star_one_users, key=lambda k: k["earned"])[:100] - star_two_users = sorted(star_two_users, key=lambda k: k["earned"])[:100] - - points = 100 - for star_user_one in star_one_users: - if "score" in leaderboard_users[star_user_one["id"]]: - leaderboard_users[star_user_one["id"]]["score"] += points - else: - leaderboard_users[star_user_one["id"]]["score"] = points - points -= 1 - - points = 100 - for star_user_two in star_two_users: - if "score" in leaderboard_users[star_user_two["id"]]: - leaderboard_users[star_user_two["id"]]["score"] += points - else: - leaderboard_users[star_user_two["id"]]["score"] = points - points -= 1 - - # Attach star completions for building the response later - for user, user_data in leaderboard_users.items(): - completions_star_one = sum([1 for day in user_data["days"].values() if day["star_one"]]) - completions_star_two = sum([1 for day in user_data["days"].values() if day["star_two"]]) - - leaderboard_users[user]["star_one_completions"] = completions_star_one - leaderboard_users[user]["star_two_completions"] = completions_star_two - - # Finally, clear old cache and persist everything to Redis - await self.public_user_data.clear() - [await self.public_user_data.set(user, json.dumps(user_data)) for user, user_data in leaderboard_users.items()] - - async def check_leaderboard(self) -> None: - """Checks should be public leaderboard refreshed and refresh when required.""" - async with self.refresh_lock: - secs = AocConfig.leaderboard_cache_age_threshold_seconds - if self.last_updated is None or self.last_updated < datetime.utcnow() - timedelta(seconds=secs): - await self.refresh_leaderboard() - - async def check_staff_leaderboard(self) -> None: - """Checks should be staff leaderboard refreshed and refresh when required.""" - async with self.staff_refresh_lock: - secs = AocConfig.leaderboard_cache_age_threshold_seconds - if self.staff_last_updated is None or self.staff_last_updated < datetime.utcnow() - timedelta(seconds=secs): - leaderboard = await AocPrivateLeaderboard.from_url( - AocConfig.leaderboard_staff_id, - Tokens.aoc_staff_session_cookie - ) - - if leaderboard is not None: - self.staff_last_updated = datetime.utcnow() - self.cached_staff_leaderboard = leaderboard - else: - log.warning("Can't update staff leaderboard. Got unexpected response.") - - async def get_leaderboard(self, members_amount: int, context: commands.Context) -> typing.Union[str, bool]: - """Generates leaderboard based on Redis data.""" - # When we don't have users in cache, warn and return False. - if await self.public_user_data.length() == 0: - log.warning("Don't have cache for displaying AoC public leaderboard.") - return False - - leaderboard_members = sorted( - [json.loads(data) for user, data in await self.public_user_data.items()], - key=lambda k: k.get("score", 0), - reverse=True - )[:members_amount] - - stargroup = f"{Emojis.star}, {Emojis.star * 2}" - header = f"{' ' * 3}{'Score'} {'Name':^25} {stargroup:^7}\n{'-' * 44}" - table = "" - for i, member in enumerate(leaderboard_members): - if member["name"] == "Anonymous User": - name = f"{member['name']} #{member['aoc_id']}" - else: - name = member["name"] - - table += ( - f"{i + 1:2}) {member['score']:4} {name:25.25} " - f"({member['star_one_completions']:2}, {member['star_two_completions']:2})\n" - ) - else: - table = f"```{header}\n{table}```" - - return table - - @in_month(Month.DECEMBER) - @commands.group(name="adventofcode", aliases=("aoc",)) - @override_in_channel(AOC_WHITELIST) - async def adventofcode_group(self, ctx: commands.Context) -> None: - """All of the Advent of Code commands.""" - if not ctx.invoked_subcommand: - await ctx.send_help(ctx.command) - - @adventofcode_group.command( - name="subscribe", - aliases=("sub", "notifications", "notify", "notifs"), - brief="Notifications for new days" - ) - @override_in_channel(AOC_WHITELIST) - async def aoc_subscribe(self, ctx: commands.Context) -> None: - """Assign the role for notifications about new days being ready.""" - role = ctx.guild.get_role(AocConfig.role_id) - unsubscribe_command = f"{ctx.prefix}{ctx.command.root_parent} unsubscribe" - - if role not in ctx.author.roles: - await ctx.author.add_roles(role) - await ctx.send("Okay! You have been __subscribed__ to notifications about new Advent of Code tasks. " - f"You can run `{unsubscribe_command}` to disable them again for you.") - else: - await ctx.send("Hey, you already are receiving notifications about new Advent of Code tasks. " - f"If you don't want them any more, run `{unsubscribe_command}` instead.") - - @adventofcode_group.command(name="unsubscribe", aliases=("unsub",), brief="Notifications for new days") - @override_in_channel(AOC_WHITELIST) - async def aoc_unsubscribe(self, ctx: commands.Context) -> None: - """Remove the role for notifications about new days being ready.""" - role = ctx.guild.get_role(AocConfig.role_id) - - if role in ctx.author.roles: - await ctx.author.remove_roles(role) - await ctx.send("Okay! You have been __unsubscribed__ from notifications about new Advent of Code tasks.") - else: - await ctx.send("Hey, you don't even get any notifications about new Advent of Code tasks currently anyway.") - - @adventofcode_group.command(name="countdown", aliases=("count", "c"), brief="Return time left until next day") - @override_in_channel(AOC_WHITELIST) - async def aoc_countdown(self, ctx: commands.Context) -> None: - """Return time left until next day.""" - if not is_in_advent(): - datetime_now = datetime.now(EST) - - # Calculate the delta to this & next year's December 1st to see which one is closest and not in the past - this_year = datetime(datetime_now.year, 12, 1, tzinfo=EST) - next_year = datetime(datetime_now.year + 1, 12, 1, tzinfo=EST) - deltas = (dec_first - datetime_now for dec_first in (this_year, next_year)) - delta = min(delta for delta in deltas if delta >= timedelta()) # timedelta() gives 0 duration delta - - # Add a finer timedelta if there's less than a day left - if delta.days == 0: - delta_str = f"approximately {delta.seconds // 3600} hours" - else: - delta_str = f"{delta.days} days" - - await ctx.send(f"The Advent of Code event is not currently running. " - f"The next event will start in {delta_str}.") - return - - tomorrow, time_left = time_left_to_aoc_midnight() - - hours, minutes = time_left.seconds // 3600, time_left.seconds // 60 % 60 - - await ctx.send(f"There are {hours} hours and {minutes} minutes left until day {tomorrow.day}.") - - @adventofcode_group.command(name="about", aliases=("ab", "info"), brief="Learn about Advent of Code") - @override_in_channel(AOC_WHITELIST) - async def about_aoc(self, ctx: commands.Context) -> None: - """Respond with an explanation of all things Advent of Code.""" - await ctx.send("", embed=self.cached_about_aoc) - - @adventofcode_group.command(name="join", aliases=("j",), brief="Learn how to join the leaderboard (via DM)") - @override_in_channel(AOC_WHITELIST) - async def join_leaderboard(self, ctx: commands.Context) -> None: - """DM the user the information for joining the PyDis AoC private leaderboard.""" - author = ctx.message.author - log.info(f"{author.name} ({author.id}) has requested the PyDis AoC leaderboard code") - - if ctx.channel.id == Channels.advent_of_code_staff: - join_code = AocConfig.leaderboard_staff_join_code - log.info(f"{author.name} ({author.id}) ran command in staff AoC channel. Returning staff code.") - else: - # Ensure we use the same leaderboard code for the same user - if await self.user_leaderboards.contains(ctx.author.id): - join_code = self.leaderboard_join_codes[await self.user_leaderboards.get(ctx.author.id)] - log.info(f"{author.name} ({author.id}) has a cached AoC join code, returning it.") - else: - # Find the leaderboard that has the least members inside from cache - least_id, least = 0, 200 - for aoc_id, amount in await self.public_leaderboard_members.items(): - log.info(amount, least) - if amount < least: - least, least_id = amount, aoc_id - - join_code = self.leaderboard_join_codes[least_id] - # Persist this code to Redis, so we can get it later again. - await self.user_leaderboards.set(ctx.author.id, least_id) - log.info(f"{author.name} ({author.id}) got new join code. Persisted it to cache.") - - info_str = ( - "Head over to https://adventofcode.com/leaderboard/private " - f"with code `{join_code}` to join the PyDis private leaderboard!" - ) - try: - await author.send(info_str) - except discord.errors.Forbidden: - log.debug(f"{author.name} ({author.id}) has disabled DMs from server members") - await ctx.send(f":x: {author.mention}, please (temporarily) enable DMs to receive the join code") - else: - await ctx.message.add_reaction(Emojis.envelope) - - @adventofcode_group.command( - name="leaderboard", - aliases=("board", "lb"), - brief="Get a snapshot of the PyDis private AoC leaderboard", - ) - @override_in_channel(AOC_WHITELIST) - async def aoc_leaderboard(self, ctx: commands.Context, number_of_people_to_display: int = 10) -> None: - """ - Pull the top number_of_people_to_display members from the PyDis leaderboard and post an embed. - - For readability, number_of_people_to_display defaults to 10. A maximum value is configured in the - Advent of Code section of the bot constants. number_of_people_to_display values greater than this - limit will default to this maximum and provide feedback to the user. - """ - async with ctx.typing(): - staff = ctx.channel.id == Channels.advent_of_code_staff - number_of_people_to_display = await self._check_n_entries(ctx, number_of_people_to_display) - - if staff: - await self.check_staff_leaderboard() - if self.cached_staff_leaderboard is not None: - members_to_print = self.cached_staff_leaderboard.top_n(number_of_people_to_display) - table = AocPrivateLeaderboard.build_leaderboard_embed(members_to_print) - else: - log.warning("Missing AoC staff leaderboard cache.") - table = False - else: - await self.check_leaderboard() - table = await self.get_leaderboard(number_of_people_to_display, ctx) - - # When we don't have cache, show it to user. - if table is False: - await ctx.send( - ":x: Sorry, we can't get our leaderboard cache. Please let staff know about it." - ) - return - - # Build embed - aoc_embed = discord.Embed( - description=( - "Total members: " - f"{len(self.cached_staff_leaderboard.members) if staff else await self.public_user_data.length()}" - ), - colour=Colours.soft_green, - timestamp=self.staff_last_updated if staff else self.last_updated - ) - if ctx.channel.id == Channels.advent_of_code_staff: - aoc_embed.set_author( - name="Advent of Code", - url=f"{self._base_url}/leaderboard/private/view/{AocConfig.leaderboard_staff_id}" - ) - elif await self.user_leaderboards.contains(ctx.author.id): - aoc_embed.set_author( - name="Advent of Code", - url=f"{self._base_url}/leaderboard/private/view/{await self.user_leaderboards.get(ctx.author.id)}" - ) - else: - aoc_embed.set_author(name="Advent of Code") - aoc_embed.set_footer(text="Last Updated") - - await ctx.send( - content=f"Here's the current Top {number_of_people_to_display}! {Emojis.christmas_tree*3}\n\n{table}", - embed=aoc_embed, - ) - - @adventofcode_group.command( - name="stats", - aliases=("dailystats", "ds"), - brief="Get daily statistics for the PyDis private leaderboard" - ) - @override_in_channel(AOC_WHITELIST) - async def private_leaderboard_daily_stats(self, ctx: commands.Context) -> None: - """ - Respond with a table of the daily completion statistics for the PyDis private leaderboard. - - Embed will display the total members and the number of users who have completed each day's puzzle - """ - async with ctx.typing(): - is_staff = ctx.channel.id == Channels.advent_of_code_staff - if is_staff: - await self.check_staff_leaderboard() - if self.cached_staff_leaderboard is None: - await ctx.send(":x: Missing leaderboard cache.") - return - else: - await self.check_leaderboard() - - # Build ASCII table - if is_staff: - total_members = len(self.cached_staff_leaderboard.members) - else: - total_members = await self.public_user_data.length() - if total_members == 0: - await ctx.send(":x: Missing leaderboard cache. Please notify staff about it.") - return - - _star = Emojis.star - header = f"{'Day':4}{_star:^8}{_star*2:^4}{'% ' + _star:^8}{'% ' + _star*2:^4}\n{'='*35}" - table = "" - if is_staff: - for day, completions in enumerate(self.cached_staff_leaderboard.daily_completion_summary): - per_one_star = f"{(completions[0]/total_members)*100:.2f}" - per_two_star = f"{(completions[1]/total_members)*100:.2f}" - - table += f"{day+1:3}){completions[0]:^8}{completions[1]:^6}{per_one_star:^10}{per_two_star:^6}\n" - else: - completions = {} - # Build data for completion rates - for _, user_data in await self.public_user_data.items(): - user_data = json.loads(user_data) - for day, stars in user_data["days"].items(): - day = int(day) - if day not in completions: - completions[day] = [0, 0] - - if stars["star_one"]: - completions[day][0] += 1 - if stars["star_two"]: - completions[day][1] += 1 - - for day, completion in completions.items(): - per_one_star = f"{(completion[0]/total_members)*100:.2f}" - per_two_star = f"{(completion[1] / total_members) * 100:.2f}" - - table += f"{day:3}){completion[0]:^8}{completion[1]:^6}{per_one_star:^10}{per_two_star:^6}\n" - - table = f"```\n{header}\n{table}```" - - # Build embed - daily_stats_embed = discord.Embed( - colour=Colours.soft_green, - timestamp=self.staff_last_updated if is_staff else self.last_updated - ) - daily_stats_embed.set_author(name="Advent of Code", url=self._base_url) - daily_stats_embed.set_footer(text="Last Updated") - - await ctx.send( - content=f"Here's the current daily statistics!\n\n{table}", embed=daily_stats_embed - ) - - @adventofcode_group.command( - name="global", - aliases=("globalboard", "gb"), - brief="Get a snapshot of the global AoC leaderboard", - ) - @override_in_channel(AOC_WHITELIST) - async def global_leaderboard(self, ctx: commands.Context, number_of_people_to_display: int = 10) -> None: - """ - Pull the top number_of_people_to_display members from the global AoC leaderboard and post an embed. - - For readability, number_of_people_to_display defaults to 10. A maximum value is configured in the - Advent of Code section of the bot constants. number_of_people_to_display values greater than this - limit will default to this maximum and provide feedback to the user. - """ - async with ctx.typing(): - await self._check_leaderboard_cache(ctx) - - if not self.cached_global_leaderboard: - # Feedback on issues with leaderboard caching are sent by _check_leaderboard_cache() - # Short circuit here if there's an issue - return - - number_of_people_to_display = await self._check_n_entries(ctx, number_of_people_to_display) - - # Generate leaderboard table for embed - members_to_print = self.cached_global_leaderboard.top_n(number_of_people_to_display) - table = AocGlobalLeaderboard.build_leaderboard_embed(members_to_print) - - # Build embed - aoc_embed = discord.Embed(colour=Colours.soft_green, timestamp=self.cached_global_leaderboard.last_updated) - aoc_embed.set_author(name="Advent of Code", url=self._base_url) - aoc_embed.set_footer(text="Last Updated") - - await ctx.send( - f"Here's the current global Top {number_of_people_to_display}! {Emojis.christmas_tree*3}\n\n{table}", - embed=aoc_embed, - ) - - async def _check_leaderboard_cache(self, ctx: commands.Context) -> None: - """ - Check age of current leaderboard & pull a new one if the board is too old. - - global_board is a boolean to toggle between the global board and the Pydis private board - """ - leaderboard = self.cached_global_leaderboard - if not leaderboard: - log.debug("No cached global leaderboard found") - self.cached_global_leaderboard = await AocGlobalLeaderboard.from_url() - else: - leaderboard_age = datetime.utcnow() - leaderboard.last_updated - age_seconds = leaderboard_age.total_seconds() - if age_seconds < AocConfig.leaderboard_cache_age_threshold_seconds: - log.debug(f"Cached global leaderboard age less than threshold ({age_seconds} seconds old)") - else: - log.debug(f"Cached global leaderboard age greater than threshold ({age_seconds} seconds old)") - self.cached_global_leaderboard = await AocGlobalLeaderboard.from_url() - - leaderboard = self.cached_global_leaderboard - if not leaderboard: - await ctx.send( - "", - embed=_error_embed_helper( - title="Something's gone wrong and there's no cached global leaderboard!", - description="Please check in with a staff member.", - ), - ) - - async def _check_n_entries(self, ctx: commands.Context, number_of_people_to_display: int) -> int: - """Check for n > max_entries and n <= 0.""" - max_entries = AocConfig.leaderboard_max_displayed_members - author = ctx.message.author - if not 0 <= number_of_people_to_display <= max_entries: - log.debug( - f"{author.name} ({author.id}) attempted to fetch an invalid number " - f" of entries from the AoC leaderboard ({number_of_people_to_display})" - ) - await ctx.send( - f":x: {author.mention}, number of entries to display must be a positive " - f"integer less than or equal to {max_entries}" - ) - number_of_people_to_display = max_entries - - return number_of_people_to_display - - def _build_about_embed(self) -> discord.Embed: - """Build and return the informational "About AoC" embed from the resources file.""" - with self.about_aoc_filepath.open("r", encoding="utf8") as f: - embed_fields = json.load(f) - - about_embed = discord.Embed(title=self._base_url, colour=Colours.soft_green, url=self._base_url) - about_embed.set_author(name="Advent of Code", url=self._base_url) - for field in embed_fields: - about_embed.add_field(**field) - - about_embed.set_footer(text=f"Last Updated (UTC): {datetime.utcnow()}") - - return about_embed - - def cog_unload(self) -> None: - """Cancel season-related tasks on cog unload.""" - log.debug("Unloading the cog and canceling the background task.") - self.countdown_task.cancel() - self.status_task.cancel() - - -class AocMember: - """Object representing the Advent of Code user.""" - - def __init__(self, name: str, aoc_id: int, stars: int, starboard: list, local_score: int, global_score: int): - self.name = name - self.aoc_id = aoc_id - self.stars = stars - self.starboard = starboard - self.local_score = local_score - self.global_score = global_score - self.completions = self._completions_from_starboard(self.starboard) - - def __repr__(self): - """Generate a user-friendly representation of the AocMember & their score.""" - return f"<{self.name} ({self.aoc_id}): {self.local_score}>" - - @classmethod - def member_from_json(cls, injson: dict) -> "AocMember": - """ - Generate an AocMember from AoC's private leaderboard API JSON. - - injson is expected to be the dict contained in: - - AoC_APIjson['members'][:str] - - Returns an AocMember object - """ - return cls( - name=injson["name"] if injson["name"] else "Anonymous User", - aoc_id=int(injson["id"]), - stars=injson["stars"], - starboard=cls._starboard_from_json(injson["completion_day_level"]), - local_score=injson["local_score"], - global_score=injson["global_score"], - ) - - @staticmethod - def _starboard_from_json(injson: dict) -> list: - """ - Generate starboard from AoC's private leaderboard API JSON. - - injson is expected to be the dict contained in: - - AoC_APIjson['members'][:str]['completion_day_level'] - - Returns a list of 25 lists, where each nested list contains a pair of booleans representing - the code challenge completion status for that day - """ - # Basic input validation - if not isinstance(injson, dict): - raise ValueError - - # Initialize starboard - starboard = [] - for _i in range(25): - starboard.append([False, False]) - - # Iterate over days, which are the keys of injson (as str) - for day in injson: - idx = int(day) - 1 - # If there is a second star, the first star must be completed - if "2" in injson[day].keys(): - starboard[idx] = [True, True] - # If the day exists in injson, then at least the first star is completed - else: - starboard[idx] = [True, False] - - return starboard - - @staticmethod - def _completions_from_starboard(starboard: list) -> tuple: - """Return days completed, as a (1 star, 2 star) tuple, from starboard.""" - completions = [0, 0] - for day in starboard: - if day[0]: - completions[0] += 1 - if day[1]: - completions[1] += 1 - - return tuple(completions) - - -class AocPrivateLeaderboard: - """Object representing the Advent of Code private leaderboard.""" - - def __init__(self, members: list, owner_id: int, event_year: int): - self.members = members - self._owner_id = owner_id - self._event_year = event_year - self.last_updated = datetime.utcnow() - - self.daily_completion_summary = self.calculate_daily_completion() - - def top_n(self, n: int = 10) -> dict: - """ - Return the top n participants on the leaderboard. - - If n is not specified, default to the top 10 - """ - return self.members[:n] - - def calculate_daily_completion(self) -> List[tuple]: - """ - Calculate member completion rates by day. - - Return a list of tuples for each day containing the number of users who completed each part - of the challenge - """ - daily_member_completions = [] - for day in range(25): - one_star_count = 0 - two_star_count = 0 - for member in self.members: - if member.starboard[day][1]: - one_star_count += 1 - two_star_count += 1 - elif member.starboard[day][0]: - one_star_count += 1 - else: - daily_member_completions.append((one_star_count, two_star_count)) - - return(daily_member_completions) - - @staticmethod - async def json_from_url( - leaderboard_id: int, cookie: str, year: int = AocConfig.year - ) -> typing.Union[dict, bool]: - """ - Request the API JSON from Advent of Code for leaderboard_id for the specified year's event. - - If no year is input, year defaults to the current year - """ - api_url = f"https://adventofcode.com/{year}/leaderboard/private/view/{leaderboard_id}.json" - - log.debug("Querying Advent of Code Private Leaderboard API") - async with aiohttp.ClientSession(headers=AOC_REQUEST_HEADER, cookies={"session": cookie}) as session: - async with session.get(api_url) as resp: - if resp.status == 200: - try: - raw_dict = await resp.json() - except aiohttp.ContentTypeError: - log.warning(f"Got invalid response type from AoC API. Leaderboard ID {leaderboard_id}") - return False - else: - log.warning(f"Bad response received from AoC ({resp.status}), check session cookie") - return False - - return raw_dict - - @classmethod - def from_json(cls, injson: dict) -> "AocPrivateLeaderboard": - """Generate an AocPrivateLeaderboard object from AoC's private leaderboard API JSON.""" - return cls( - members=cls._sorted_members(injson["members"]), owner_id=injson["owner_id"], event_year=injson["event"] - ) - - @classmethod - async def from_url(cls, leaderboard_id: int, cookie: str) -> typing.Union["AocPrivateLeaderboard", None]: - """Helper wrapping of AocPrivateLeaderboard.json_from_url and AocPrivateLeaderboard.from_json.""" - api_json = await cls.json_from_url(leaderboard_id, cookie) - if api_json is not False: - return cls.from_json(api_json) - else: - return None - - @staticmethod - def _sorted_members(injson: dict) -> list: - """ - Generate a sorted list of AocMember objects from AoC's private leaderboard API JSON. - - Output list is sorted based on the AocMember.local_score - """ - members = [AocMember.member_from_json(injson[member]) for member in injson] - members.sort(key=lambda x: x.local_score, reverse=True) - - return members - - @staticmethod - def build_leaderboard_embed(members_to_print: List[AocMember]) -> str: - """ - Build a text table from members_to_print, a list of AocMember objects. - - Returns a string to be used as the content of the bot's leaderboard response - """ - stargroup = f"{Emojis.star}, {Emojis.star*2}" - header = f"{' '*3}{'Score'} {'Name':^25} {stargroup:^7}\n{'-'*44}" - table = "" - for i, member in enumerate(members_to_print): - if member.name == "Anonymous User": - name = f"{member.name} #{member.aoc_id}" - else: - name = member.name - - table += ( - f"{i+1:2}) {member.local_score:4} {name:25.25} " - f"({member.completions[0]:2}, {member.completions[1]:2})\n" - ) - else: - table = f"```{header}\n{table}```" - - return table - - -class AocGlobalLeaderboard: - """Object representing the Advent of Code global leaderboard.""" - - def __init__(self, members: List[tuple]): - self.members = members - self.last_updated = datetime.utcnow() - - def top_n(self, n: int = 10) -> dict: - """ - Return the top n participants on the leaderboard. - - If n is not specified, default to the top 10 - """ - return self.members[:n] - - @classmethod - async def from_url(cls) -> "AocGlobalLeaderboard": - """ - Generate an list of tuples for the entries on AoC's global leaderboard. - - Because there is no API for this, web scraping needs to be used - """ - aoc_url = f"https://adventofcode.com/{AocConfig.year}/leaderboard" - - async with aiohttp.ClientSession(headers=AOC_REQUEST_HEADER) as session: - async with session.get(aoc_url) as resp: - if resp.status == 200: - raw_html = await resp.text() - else: - log.warning(f"Bad response received from AoC ({resp.status}), check session cookie") - resp.raise_for_status() - - soup = BeautifulSoup(raw_html, "html.parser") - ele = soup.find_all("div", class_="leaderboard-entry") - - exp = r"(?:[ ]{,2}(\d+)\))?[ ]+(\d+)\s+([\w\(\)\#\@\-\d ]+)" - - lb_list = [] - for entry in ele: - # Strip off the AoC++ decorator - raw_str = entry.text.replace("(AoC++)", "").rstrip() - - # Use a regex to extract the info from the string to unify formatting - # Group 1: Rank - # Group 2: Global Score - # Group 3: Member string - r = re.match(exp, raw_str) - - rank = int(r.group(1)) if r.group(1) else None - global_score = int(r.group(2)) - - member = r.group(3) - if member.lower().startswith("(anonymous"): - # Normalize anonymous user string by stripping () and title casing - member = re.sub(r"[\(\)]", "", member).title() - - lb_list.append((rank, global_score, member)) - - return cls(lb_list) - - @staticmethod - def build_leaderboard_embed(members_to_print: List[tuple]) -> str: - """ - Build a text table from members_to_print, a list of tuples. - - Returns a string to be used as the content of the bot's leaderboard response - """ - header = f"{' '*4}{'Score'} {'Name':^25}\n{'-'*36}" - table = "" - for member in members_to_print: - # In the event of a tie, rank is None - if member[0]: - rank = f"{member[0]:3})" - else: - rank = f"{' ':4}" - table += f"{rank} {member[1]:4} {member[2]:25.25}\n" - else: - table = f"```{header}\n{table}```" - - return table - - -def _error_embed_helper(title: str, description: str) -> discord.Embed: - """Return a red-colored Embed with the given title and description.""" - return discord.Embed(title=title, description=description, colour=discord.Colour.red()) - - -def setup(bot: Bot) -> None: - """Advent of Code Cog load.""" - bot.add_cog(AdventOfCode(bot)) -- cgit v1.2.3 From 1444c81f41703a224a527eaa45381a1cf073c549 Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 01:06:18 +0100 Subject: Rewrite Advent of Code leaderboard logic I've rewritten the Advent of Code leaderboard logic. Unfortunately, nearly all of the changes made are interrelated, meaning that they've ended up in the same commit. To add a bit of structure to the extension, I've chosen for a subpackage structure instead of a single file structure. The biggest changes: - Whether or not you get a join code for the staff leaderboard will now be determined by looking for the Helpers-role. - The Python Discord Leaderboard now includes all boards, including the staff leaderboard. This is one event. - Redis is now used to set a cache expiry period. This means that our code does not have to check for cache staleness; Redis will do that for us. - The period "fetching" task has been removed. We now fetch solely when the data is needed to prevent putting unnecessary stress on the Advent of Code website. - The option to display the Global Leaderboard within Discord has been removed. Rather, we now link to the website. This simplified the code for now, although we could add it back later. - An additional command, `.aoc refresh`, has been added to allow Admins and the Events Lead to force the cache to be invalidated. This should be done sparingly to not overburden the AoC website. - I've also made sure that the daily notification task actually pings the notification role by setting the `allowed_mentions` kwarg. --- bot/exts/christmas/advent_of_code/__init__.py | 10 + bot/exts/christmas/advent_of_code/_caches.py | 5 + bot/exts/christmas/advent_of_code/_cog.py | 353 ++++++++++++++++++++++++++ bot/exts/christmas/advent_of_code/_helpers.py | 312 +++++++++++++++++++++++ 4 files changed, 680 insertions(+) create mode 100644 bot/exts/christmas/advent_of_code/__init__.py create mode 100644 bot/exts/christmas/advent_of_code/_caches.py create mode 100644 bot/exts/christmas/advent_of_code/_cog.py create mode 100644 bot/exts/christmas/advent_of_code/_helpers.py (limited to 'bot') diff --git a/bot/exts/christmas/advent_of_code/__init__.py b/bot/exts/christmas/advent_of_code/__init__.py new file mode 100644 index 00000000..20ac5ab9 --- /dev/null +++ b/bot/exts/christmas/advent_of_code/__init__.py @@ -0,0 +1,10 @@ +from bot.bot import Bot + + +def setup(bot: Bot) -> None: + """Advent of Code Cog load.""" + # Import the Cog at runtime to prevent side effects like defining + # RedisCache instances too early. + from ._cog import AdventOfCode + + bot.add_cog(AdventOfCode(bot)) diff --git a/bot/exts/christmas/advent_of_code/_caches.py b/bot/exts/christmas/advent_of_code/_caches.py new file mode 100644 index 00000000..32d5394f --- /dev/null +++ b/bot/exts/christmas/advent_of_code/_caches.py @@ -0,0 +1,5 @@ +import async_rediscache + +leaderboard_counts = async_rediscache.RedisCache(namespace="AOC_leaderboard_counts") +leaderboard_cache = async_rediscache.RedisCache(namespace="AOC_leaderboard_cache") +assigned_leaderboard = async_rediscache.RedisCache(namespace="AOC_assigned_leaderboard") diff --git a/bot/exts/christmas/advent_of_code/_cog.py b/bot/exts/christmas/advent_of_code/_cog.py new file mode 100644 index 00000000..0df645bd --- /dev/null +++ b/bot/exts/christmas/advent_of_code/_cog.py @@ -0,0 +1,353 @@ +import asyncio +import json +import logging +import math +from datetime import datetime, timedelta +from pathlib import Path +from typing import Tuple + +import discord +from discord.ext import commands +from pytz import timezone + +from bot.bot import Bot +from bot.constants import ( + AdventOfCode as AocConfig, Channels, Colours, Emojis, Month, Roles, WHITELISTED_CHANNELS, +) +from bot.exts.christmas.advent_of_code import _helpers +from bot.utils.decorators import in_month, override_in_channel, with_role + +log = logging.getLogger(__name__) + +AOC_REQUEST_HEADER = {"user-agent": "PythonDiscord AoC Event Bot"} + +EST = timezone("EST") +COUNTDOWN_STEP = 60 * 5 + +AOC_WHITELIST = WHITELISTED_CHANNELS + (Channels.advent_of_code, Channels.advent_of_code_staff) + + +def is_in_advent() -> bool: + """Utility function to check if we are between December 1st and December 25th.""" + # Run the code from the 1st to the 24th + return datetime.now(EST).day in range(1, 25) and datetime.now(EST).month == 12 + + +def time_left_to_aoc_midnight() -> Tuple[datetime, timedelta]: + """Calculates the amount of time left until midnight in UTC-5 (Advent of Code maintainer timezone).""" + # Change all time properties back to 00:00 + todays_midnight = datetime.now(EST).replace( + microsecond=0, + second=0, + minute=0, + hour=0 + ) + + # We want tomorrow so add a day on + tomorrow = todays_midnight + timedelta(days=1) + + # Calculate the timedelta between the current time and midnight + return tomorrow, tomorrow - datetime.now(EST) + + +async def countdown_status(bot: commands.Bot) -> None: + """Set the playing status of the bot to the minutes & hours left until the next day's challenge.""" + while is_in_advent(): + _, time_left = time_left_to_aoc_midnight() + + aligned_seconds = int(math.ceil(time_left.seconds / COUNTDOWN_STEP)) * COUNTDOWN_STEP + hours, minutes = aligned_seconds // 3600, aligned_seconds // 60 % 60 + + if aligned_seconds == 0: + playing = "right now!" + elif aligned_seconds == COUNTDOWN_STEP: + playing = f"in less than {minutes} minutes" + elif hours == 0: + playing = f"in {minutes} minutes" + elif hours == 23: + playing = f"since {60 - minutes} minutes ago" + else: + playing = f"in {hours} hours and {minutes} minutes" + + # Status will look like "Playing in 5 hours and 30 minutes" + await bot.change_presence(activity=discord.Game(playing)) + + # Sleep until next aligned time or a full step if already aligned + delay = time_left.seconds % COUNTDOWN_STEP or COUNTDOWN_STEP + await asyncio.sleep(delay) + + +async def day_countdown(bot: commands.Bot) -> None: + """ + Calculate the number of seconds left until the next day of Advent. + + Once we have calculated this we should then sleep that number and when the time is reached, ping + the Advent of Code role notifying them that the new challenge is ready. + """ + while is_in_advent(): + tomorrow, time_left = time_left_to_aoc_midnight() + + # Prevent bot from being slightly too early in trying to announce today's puzzle + await asyncio.sleep(time_left.seconds + 1) + + channel = bot.get_channel(Channels.advent_of_code) + + if not channel: + log.error("Could not find the AoC channel to send notification in") + break + + aoc_role = channel.guild.get_role(AocConfig.role_id) + if not aoc_role: + log.error("Could not find the AoC role to announce the daily puzzle") + break + + puzzle_url = f"https://adventofcode.com/{AocConfig.year}/day/{tomorrow.day}" + + # Check if the puzzle is already available to prevent our members from spamming + # the puzzle page before it's available by making a small HEAD request. + for retry in range(1, 5): + log.debug(f"Checking if the puzzle is already available (attempt {retry}/4)") + async with bot.http_session.head(puzzle_url, raise_for_status=False) as resp: + if resp.status == 200: + log.debug("Puzzle is available; let's send an announcement message.") + break + log.debug(f"The puzzle is not yet available (status={resp.status})") + await asyncio.sleep(10) + else: + log.error("The puzzle does does not appear to be available at this time, canceling announcement") + break + + await channel.send( + f"{aoc_role.mention} Good morning! Day {tomorrow.day} is ready to be attempted. " + f"View it online now at {puzzle_url}. Good luck!", + allowed_mentions=discord.AllowedMentions( + everyone=False, + users=False, + roles=[discord.Object(AocConfig.role_id)], + ) + ) + + # Wait a couple minutes so that if our sleep didn't sleep enough + # time we don't end up announcing twice. + await asyncio.sleep(120) + + +class AdventOfCode(commands.Cog): + """Advent of Code festivities! Ho Ho Ho!""" + + def __init__(self, bot: Bot) -> None: + self.bot = bot + + self._base_url = f"https://adventofcode.com/{AocConfig.year}" + self.global_leaderboard_url = f"https://adventofcode.com/{AocConfig.year}/leaderboard" + + self.about_aoc_filepath = Path("./bot/resources/advent_of_code/about.json") + self.cached_about_aoc = self._build_about_embed() + + self.countdown_task = None + self.status_task = None + + countdown_coro = day_countdown(self.bot) + self.countdown_task = self.bot.loop.create_task(countdown_coro) + + status_coro = countdown_status(self.bot) + self.status_task = self.bot.loop.create_task(status_coro) + + @in_month(Month.DECEMBER) + @commands.group(name="adventofcode", aliases=("aoc",)) + @override_in_channel(AOC_WHITELIST) + async def adventofcode_group(self, ctx: commands.Context) -> None: + """All of the Advent of Code commands.""" + if not ctx.invoked_subcommand: + await ctx.send_help(ctx.command) + + @adventofcode_group.command( + name="subscribe", + aliases=("sub", "notifications", "notify", "notifs"), + brief="Notifications for new days" + ) + @override_in_channel(AOC_WHITELIST) + async def aoc_subscribe(self, ctx: commands.Context) -> None: + """Assign the role for notifications about new days being ready.""" + role = ctx.guild.get_role(AocConfig.role_id) + unsubscribe_command = f"{ctx.prefix}{ctx.command.root_parent} unsubscribe" + + if role not in ctx.author.roles: + await ctx.author.add_roles(role) + await ctx.send("Okay! You have been __subscribed__ to notifications about new Advent of Code tasks. " + f"You can run `{unsubscribe_command}` to disable them again for you.") + else: + await ctx.send("Hey, you already are receiving notifications about new Advent of Code tasks. " + f"If you don't want them any more, run `{unsubscribe_command}` instead.") + + @adventofcode_group.command(name="unsubscribe", aliases=("unsub",), brief="Notifications for new days") + @override_in_channel(AOC_WHITELIST) + async def aoc_unsubscribe(self, ctx: commands.Context) -> None: + """Remove the role for notifications about new days being ready.""" + role = ctx.guild.get_role(AocConfig.role_id) + + if role in ctx.author.roles: + await ctx.author.remove_roles(role) + await ctx.send("Okay! You have been __unsubscribed__ from notifications about new Advent of Code tasks.") + else: + await ctx.send("Hey, you don't even get any notifications about new Advent of Code tasks currently anyway.") + + @adventofcode_group.command(name="countdown", aliases=("count", "c"), brief="Return time left until next day") + @override_in_channel(AOC_WHITELIST) + async def aoc_countdown(self, ctx: commands.Context) -> None: + """Return time left until next day.""" + if not is_in_advent(): + datetime_now = datetime.now(EST) + + # Calculate the delta to this & next year's December 1st to see which one is closest and not in the past + this_year = datetime(datetime_now.year, 12, 1, tzinfo=EST) + next_year = datetime(datetime_now.year + 1, 12, 1, tzinfo=EST) + deltas = (dec_first - datetime_now for dec_first in (this_year, next_year)) + delta = min(delta for delta in deltas if delta >= timedelta()) # timedelta() gives 0 duration delta + + # Add a finer timedelta if there's less than a day left + if delta.days == 0: + delta_str = f"approximately {delta.seconds // 3600} hours" + else: + delta_str = f"{delta.days} days" + + await ctx.send(f"The Advent of Code event is not currently running. " + f"The next event will start in {delta_str}.") + return + + tomorrow, time_left = time_left_to_aoc_midnight() + + hours, minutes = time_left.seconds // 3600, time_left.seconds // 60 % 60 + + await ctx.send(f"There are {hours} hours and {minutes} minutes left until day {tomorrow.day}.") + + @adventofcode_group.command(name="about", aliases=("ab", "info"), brief="Learn about Advent of Code") + @override_in_channel(AOC_WHITELIST) + async def about_aoc(self, ctx: commands.Context) -> None: + """Respond with an explanation of all things Advent of Code.""" + await ctx.send("", embed=self.cached_about_aoc) + + @adventofcode_group.command(name="join", aliases=("j",), brief="Learn how to join the leaderboard (via DM)") + @override_in_channel(AOC_WHITELIST) + async def join_leaderboard(self, ctx: commands.Context) -> None: + """DM the user the information for joining the PyDis AoC private leaderboard.""" + author = ctx.message.author + log.info(f"{author.name} ({author.id}) has requested a PyDis AoC leaderboard code") + + if AocConfig.staff_leaderboard_id and any(r.id == Roles.helpers for r in author.roles): + join_code = AocConfig.leaderboards[AocConfig.staff_leaderboard_id].join_code + else: + join_code = await _helpers.get_public_join_code(author) + + if not join_code: + log.error(f"Failed to get a join code for user {author} ({author.id})") + error_embed = _error_embed_helper( + title="Unable to get join code", + description="Failed to get a join code to one of our boards. Please notify staff." + ) + await ctx.send(embed=error_embed) + return + + info_str = ( + "Head over to https://adventofcode.com/leaderboard/private " + f"with code `{join_code}` to join the Python Discord leaderboard!" + ) + try: + await author.send(info_str) + except discord.errors.Forbidden: + log.debug(f"{author.name} ({author.id}) has disabled DMs from server members") + await ctx.send(f":x: {author.mention}, please (temporarily) enable DMs to receive the join code") + else: + await ctx.message.add_reaction(Emojis.envelope) + + @adventofcode_group.command( + name="leaderboard", + aliases=("board", "lb"), + brief="Get a snapshot of the PyDis private AoC leaderboard", + ) + @override_in_channel(AOC_WHITELIST) + async def aoc_leaderboard(self, ctx: commands.Context) -> None: + """Get the current top scorers of the Python Discord Leaderboard.""" + async with ctx.typing(): + leaderboard = await _helpers.fetch_leaderboard() + number_of_participants = leaderboard["number_of_participants"] + + top_count = min(AocConfig.leaderboard_displayed_members, number_of_participants) + header = f"Here's our current top {top_count}! {Emojis.christmas_tree * 3}" + + table = f"```\n{leaderboard['top_leaderboard']}\n```" + info_embed = _helpers.get_summary_embed(leaderboard) + + await ctx.send(content=f"{header}\n\n{table}", embed=info_embed) + + @adventofcode_group.command( + name="stats", + aliases=("dailystats", "ds"), + brief="Get daily statistics for the PyDis private leaderboard" + ) + @override_in_channel(AOC_WHITELIST) + async def private_leaderboard_daily_stats(self, ctx: commands.Context) -> None: + """Send an embed with daily completion statistics for the Python Discord leaderboard.""" + leaderboard = await _helpers.fetch_leaderboard() + + # The daily stats are serialized as JSON as they have to be cached in Redis + daily_stats = json.loads(leaderboard["daily_stats"]) + async with ctx.typing(): + lines = ["Day ⭐ ⭐⭐ | %⭐ %⭐⭐\n================================"] + for day, stars in daily_stats.items(): + star_one = stars["star_one"] + star_two = stars["star_two"] + p_star_one = star_one / leaderboard["number_of_participants"] + p_star_two = star_two / leaderboard["number_of_participants"] + lines.append( + f"{day:>2}) {star_one:>4} {star_two:>4} | {p_star_one:>7.2%} {p_star_two:>7.2%}" + ) + table = "\n".join(lines) + info_embed = _helpers.get_summary_embed(leaderboard) + await ctx.send(f"```\n{table}\n```", embed=info_embed) + + @with_role(Roles.admin, Roles.events_lead) + @adventofcode_group.command( + name="refresh", + aliases=("fetch",), + brief="Force a refresh of the leaderboard cache.", + ) + async def refresh_leaderboard(self, ctx: commands.Context) -> None: + """ + Force a refresh of the leaderboard cache. + + Note: This should be used sparingly, as we want to prevent sending too + many requests to the Advent of Code server. + """ + async with ctx.typing(): + await _helpers.fetch_leaderboard(invalidate_cache=True) + await ctx.send("\N{OK Hand Sign} Refreshed leaderboard cache!") + + def cog_unload(self) -> None: + """Cancel season-related tasks on cog unload.""" + log.debug("Unloading the cog and canceling the background task.") + self.countdown_task.cancel() + self.status_task.cancel() + + def _build_about_embed(self) -> discord.Embed: + """Build and return the informational "About AoC" embed from the resources file.""" + with self.about_aoc_filepath.open("r", encoding="utf8") as f: + embed_fields = json.load(f) + + about_embed = discord.Embed( + title=self._base_url, + colour=Colours.soft_green, + url=self._base_url, + timestamp=datetime.utcnow() + ) + about_embed.set_author(name="Advent of Code", url=self._base_url) + for field in embed_fields: + about_embed.add_field(**field) + + about_embed.set_footer(text="Last Updated") + return about_embed + + +def _error_embed_helper(title: str, description: str) -> discord.Embed: + """Return a red-colored Embed with the given title and description.""" + return discord.Embed(title=title, description=description, colour=discord.Colour.red()) diff --git a/bot/exts/christmas/advent_of_code/_helpers.py b/bot/exts/christmas/advent_of_code/_helpers.py new file mode 100644 index 00000000..8b85bf5d --- /dev/null +++ b/bot/exts/christmas/advent_of_code/_helpers.py @@ -0,0 +1,312 @@ +import collections +import datetime +import json +import logging +import operator +import typing + +import aiohttp +import discord + +from bot.constants import AdventOfCode, Colours +from bot.exts.christmas.advent_of_code import _caches + +log = logging.getLogger(__name__) + +PASTE_URL = "https://paste.pythondiscord.com/documents" +RAW_PASTE_URL_TEMPLATE = "https://paste.pythondiscord.com/raw/{key}" + +# Base API URL for Advent of Code Private Leaderboards +AOC_API_URL = "https://adventofcode.com/{year}/leaderboard/private/view/{leaderboard_id}.json" +AOC_REQUEST_HEADER = {"user-agent": "PythonDiscord AoC Event Bot"} + +# Leaderboard Line Template +AOC_TABLE_TEMPLATE = "{rank: >4} | {name:25.25} | {score: >5} | {stars}" +HEADER = AOC_TABLE_TEMPLATE.format(rank="", name="Name", score="Score", stars="⭐, ⭐⭐") +HEADER = f"{HEADER}\n{'-' * (len(HEADER) + 2)}" +HEADER_LINES = len(HEADER.splitlines()) +TOP_LEADERBOARD_LINES = HEADER_LINES + AdventOfCode.leaderboard_displayed_members + +# Keys that need to be set for a cached leaderboard +REQUIRED_CACHE_KEYS = ( + "full_leaderboard", + "top_leaderboard", + "full_leaderboard_url", + "leaderboard_fetched_at", + "number_of_participants", + "daily_stats", +) + +AOC_EMBED_THUMBNAIL = ( + "https://raw.githubusercontent.com/python-discord" + "/branding/master/seasonal/christmas/server_icons/festive_256.gif" +) + +# Create namedtuple that combines a participant's name and their completion +# time for a specific star. We're going to use this later to order the results +# for each star to compute the rank score. +_StarResult = collections.namedtuple("StarResult", "name completion_time") + + +def _parse_raw_leaderboard_data(raw_leaderboard_data: dict) -> dict: + """ + Parse the leaderboard data received from the AoC website. + + The data we receive from AoC is structured by member, not by day/star. This + means that we need to "transpose" the data to a per star structure in order + to calculate the rank scores each individual should get. + + As we need our data both "per participant" as well as "per day", we return + the parsed and analyzed data in both formats. + """ + # We need to get an aggregate of completion times for each star of each day, + # instead of per participant to compute the rank scores. This dictionary will + # provide such a transposed dataset. + star_results = collections.defaultdict(list) + + # As we're already iterating over the participants, we can record the number of + # first stars and second stars they've achieved right here and now. This means + # we won't have to iterate over the participants again later. + leaderboard = {} + + # The data we get from the AoC website is structured by member, not by day/star, + # which means we need to iterate over the members to transpose the data to a per + # star view. We need that per star view to compute rank scores per star. + for member in raw_leaderboard_data.values(): + name = member["name"] if member["name"] else f"Anonymous #{member['id']}" + leaderboard[name] = {"score": 0, "star_1_count": 0, "star_2_count": 0} + + # Iterate over all days for this participant + for day, stars in member["completion_day_level"].items(): + # Iterate over the complete stars for this day for this participant + for star, data in stars.items(): + # Record completion of this star for this individual + leaderboard[name][f"star_{star}_count"] += 1 + + # Record completion datetime for this participant for this day/star + completion_time = datetime.datetime.fromtimestamp(int(data['get_star_ts'])) + star_results[(day, star)].append( + _StarResult(name=name, completion_time=completion_time) + ) + + # Now that we have a transposed dataset that holds the completion time of all + # 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))): + 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) + ) + + daily_stats = {} + for day in range(1, 26): + star_one = len(star_results.get((day, 1), [])) + star_two = len(star_results.get((day, 1), [])) + daily_stats[day] = {"star_one": star_one, "star_two": star_two} + + return {"daily_stats": daily_stats, "leaderboard": sorted_leaderboard} + + +def _format_leaderboard(leaderboard: typing.Dict[str, int]) -> str: + """Format the leaderboard using the AOC_TABLE_TEMPLATE.""" + leaderboard_lines = [HEADER] + for rank, (name, results) in enumerate(leaderboard.items(), start=1): + leaderboard_lines.append( + AOC_TABLE_TEMPLATE.format( + rank=rank, + name=name, + score=str(results["score"]), + stars=f"({results['star_1_count']}, {results['star_2_count']})" + ) + ) + + return "\n".join(leaderboard_lines) + + +async def _fetch_leaderboard_data() -> typing.Dict[str, typing.Any]: + """Fetch data for all leaderboards and return a pooled result.""" + year = AdventOfCode.year + + # We'll make our requests one at a time to not flood the AoC website with + # up to six simultaneous requests. This may take a little longer, but it + # does avoid putting unnecessary stress on the Advent of Code website. + + # Container to store the raw data of each leaderboard + participants = {} + for leaderboard in AdventOfCode.leaderboards.values(): + leaderboard_url = AOC_API_URL.format(year=year, leaderboard_id=leaderboard.id) + cookies = {"session": leaderboard.session} + + # We don't need to create a session if we're going to throw it away after each request + async with aiohttp.request( + "GET", leaderboard_url, headers=AOC_REQUEST_HEADER, cookies=cookies + ) as resp: + if resp.status == 200: + raw_data = await resp.json() + + # Get the participants and store their current count + board_participants = raw_data["members"] + await _caches.leaderboard_counts.set(leaderboard.id, len(board_participants)) + participants.update(board_participants) + else: + log.warning(f"Fetching data failed for leaderboard `{leaderboard.id}`") + resp.raise_for_status() + + log.info(f"Fetched leaderboard information for {len(participants)} participants") + return participants + + +async def _upload_leaderboard(leaderboard: str) -> str: + """Upload the full leaderboard to our paste service and return the URL.""" + async with aiohttp.request("POST", PASTE_URL, data=leaderboard) as resp: + try: + resp_json = await resp.json() + except Exception: + log.exception("Failed to upload full leaderboard to paste service") + return "" + + if "key" in resp_json: + return RAW_PASTE_URL_TEMPLATE.format(key=resp_json["key"]) + + log.error(f"Unexpected response from paste service while uploading leaderboard {resp_json}") + return "" + + +def _get_top_leaderboard(full_leaderboard: str) -> str: + """Get the leaderboard up to the maximum specified entries.""" + return "\n".join(full_leaderboard.splitlines()[:TOP_LEADERBOARD_LINES]) + + +@_caches.leaderboard_cache.atomic_transaction +async def fetch_leaderboard(invalidate_cache: bool = False) -> dict: + """ + Get the current Python Discord combined leaderboard. + + The leaderboard is cached and only fetched from the API if the current data + is older than the lifetime set in the constants. To prevent multiple calls + to this function fetching new leaderboard information in case of a cache + 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. + if invalidate_cache or any(key not in cached_leaderboard for key in REQUIRED_CACHE_KEYS): + log.info("No leaderboard cache available, fetching leaderboards...") + # Fetch the raw data + raw_leaderboard_data = await _fetch_leaderboard_data() + + # Parse it to extract "per star, per day" data and participant scores + parsed_leaderboard_data = _parse_raw_leaderboard_data(raw_leaderboard_data) + + leaderboard = parsed_leaderboard_data["leaderboard"] + number_of_participants = len(leaderboard) + formatted_leaderboard = _format_leaderboard(leaderboard) + full_leaderboard_url = await _upload_leaderboard(formatted_leaderboard) + leaderboard_fetched_at = datetime.datetime.utcnow().isoformat() + + cached_leaderboard = { + "full_leaderboard": formatted_leaderboard, + "top_leaderboard": _get_top_leaderboard(formatted_leaderboard), + "full_leaderboard_url": full_leaderboard_url, + "leaderboard_fetched_at": leaderboard_fetched_at, + "number_of_participants": number_of_participants, + "daily_stats": json.dumps(parsed_leaderboard_data["daily_stats"]), + } + + # Store the new values in Redis + await _caches.leaderboard_cache.update(cached_leaderboard) + + # Set an expiry on the leaderboard RedisCache + with await _caches.leaderboard_cache._get_pool_connection() as connection: + await connection.expire( + _caches.leaderboard_cache.namespace, + AdventOfCode.leaderboard_cache_expiry_seconds + ) + + return cached_leaderboard + + +def get_summary_embed(leaderboard: dict) -> discord.Embed: + """Get an embed with the current summary stats of the leaderboard.""" + leaderboard_url = leaderboard['full_leaderboard_url'] + + aoc_embed = discord.Embed( + colour=Colours.soft_green, + timestamp=datetime.datetime.fromisoformat(leaderboard["leaderboard_fetched_at"]), + ) + aoc_embed.add_field( + name="Number of Participants", + value=leaderboard["number_of_participants"], + inline=True, + ) + if leaderboard_url: + aoc_embed.add_field( + name="Full Leaderboard", + value=f"[Python Discord Leaderboard]({leaderboard_url})", + inline=True, + ) + aoc_embed.set_author(name="Advent of Code", url=leaderboard_url) + aoc_embed.set_footer(text="Last Updated") + aoc_embed.set_thumbnail(url=AOC_EMBED_THUMBNAIL) + + return aoc_embed + + +async def get_public_join_code(author: discord.Member) -> typing.Optional[str]: + """ + Get the join code for one of the non-staff leaderboards. + + If a user has previously requested a join code and their assigned board + hasn't filled up yet, we'll return the same join code to prevent them from + getting join codes for multiple boards. + """ + # Make sure to fetch new leaderboard information if the cache is older than + # 30 minutes. While this still means that there could be a discrepancy + # between the current leaderboard state and the numbers we have here, this + # should work fairly well given the buffer of slots that we have. + await fetch_leaderboard() + previously_assigned_board = await _caches.assigned_leaderboard.get(author.id) + current_board_counts = await _caches.leaderboard_counts.to_dict() + + # Remove the staff board from the current board counts as it should be ignored. + current_board_counts.pop(AdventOfCode.staff_leaderboard_id, None) + + # If this user has already received a join code, we'll give them the + # exact same one to prevent them from joining multiple boards and taking + # up multiple slots. + if previously_assigned_board: + # Check if their previously assigned board still has room for them + if current_board_counts.get(previously_assigned_board, 0) < 200: + log.info(f"{author} ({author.id}) was already assigned to a board with open slots.") + return AdventOfCode.leaderboards[previously_assigned_board].join_code + + log.info( + f"User {author} ({author.id}) previously received the join code for " + f"board `{previously_assigned_board}`, but that board's now full. " + "Assigning another board to this user." + ) + + # If we don't have the current board counts cached, let's force fetching a new cache + if not current_board_counts: + log.warning("Leaderboard counts were missing from the cache unexpectedly!") + await fetch_leaderboard(invalidate_cache=True) + current_board_counts = await _caches.leaderboard_counts.to_dict() + + # Find the board with the current lowest participant count. As we can't + best_board, _count = min(current_board_counts.items(), key=operator.itemgetter(1)) + + if current_board_counts.get(best_board, 0) >= 200: + log.warning(f"User {author} `{author.id}` requested a join code, but all boards are full!") + return + + log.info(f"Assigning user {author} ({author.id}) to board `{best_board}`") + await _caches.assigned_leaderboard.set(author.id, best_board) + + # Return the join code for this board + return AdventOfCode.leaderboards[best_board].join_code -- cgit v1.2.3 From 159c2ebdbbdb2c2ebdffe9e857c9abad5f36511f Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 01:09:37 +0100 Subject: Remove redundant _error_embed_helper function This helper function was only being used in one spot and did not factor out any logic. I've removed the helper function to just create the embed where it's needed. --- bot/exts/christmas/advent_of_code/_cog.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'bot') diff --git a/bot/exts/christmas/advent_of_code/_cog.py b/bot/exts/christmas/advent_of_code/_cog.py index 0df645bd..1a6715bb 100644 --- a/bot/exts/christmas/advent_of_code/_cog.py +++ b/bot/exts/christmas/advent_of_code/_cog.py @@ -241,9 +241,10 @@ class AdventOfCode(commands.Cog): if not join_code: log.error(f"Failed to get a join code for user {author} ({author.id})") - error_embed = _error_embed_helper( + error_embed = discord.Embed( title="Unable to get join code", - description="Failed to get a join code to one of our boards. Please notify staff." + description="Failed to get a join code to one of our boards. Please notify staff.", + colour=discord.Colour.red(), ) await ctx.send(embed=error_embed) return @@ -346,8 +347,3 @@ class AdventOfCode(commands.Cog): about_embed.set_footer(text="Last Updated") return about_embed - - -def _error_embed_helper(title: str, description: str) -> discord.Embed: - """Return a red-colored Embed with the given title and description.""" - return discord.Embed(title=title, description=description, colour=discord.Colour.red()) -- cgit v1.2.3 From b13667cfc261ec43daad6fd592d5b952f73997fe Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 01:18:40 +0100 Subject: Remove now redundant advent_of_code_staff constant We're no longer going to use a two-channel setup for this event, as we don't want to split the event community into two, staff and non-staff. --- bot/constants.py | 1 - bot/exts/christmas/advent_of_code/_cog.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/constants.py b/bot/constants.py index fc9929ec..e459ed21 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -76,7 +76,6 @@ class Branding: class Channels(NamedTuple): admins = 365960823622991872 advent_of_code = int(environ.get("AOC_CHANNEL_ID", 780818162836439041)) - advent_of_code_staff = int(environ.get("AOC_STAFF_CHANNEL_ID", 778646502641500181)) announcements = int(environ.get("CHANNEL_ANNOUNCEMENTS", 354619224620138496)) big_brother_logs = 468507907357409333 bot = 267659945086812160 diff --git a/bot/exts/christmas/advent_of_code/_cog.py b/bot/exts/christmas/advent_of_code/_cog.py index 1a6715bb..388d0592 100644 --- a/bot/exts/christmas/advent_of_code/_cog.py +++ b/bot/exts/christmas/advent_of_code/_cog.py @@ -24,7 +24,7 @@ AOC_REQUEST_HEADER = {"user-agent": "PythonDiscord AoC Event Bot"} EST = timezone("EST") COUNTDOWN_STEP = 60 * 5 -AOC_WHITELIST = WHITELISTED_CHANNELS + (Channels.advent_of_code, Channels.advent_of_code_staff) +AOC_WHITELIST = WHITELISTED_CHANNELS + (Channels.advent_of_code,) def is_in_advent() -> bool: -- cgit v1.2.3 From 5ce12af9d9f48042b0ebb7ef215e3253bcc418bd Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 01:20:19 +0100 Subject: Set correct channel ID as default for AoC channel --- bot/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/constants.py b/bot/constants.py index e459ed21..00d75a3f 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -75,7 +75,7 @@ class Branding: class Channels(NamedTuple): admins = 365960823622991872 - advent_of_code = int(environ.get("AOC_CHANNEL_ID", 780818162836439041)) + advent_of_code = int(environ.get("AOC_CHANNEL_ID", 782715290437943306)) announcements = int(environ.get("CHANNEL_ANNOUNCEMENTS", 354619224620138496)) big_brother_logs = 468507907357409333 bot = 267659945086812160 -- cgit v1.2.3 From c352c80cf97620e715ee55184b54d1609312c76b Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 01:38:11 +0100 Subject: Fix docstrings and add a few explanatory comments --- bot/constants.py | 4 ++-- bot/exts/christmas/advent_of_code/__init__.py | 2 +- bot/exts/christmas/advent_of_code/_cog.py | 4 ++-- bot/exts/christmas/advent_of_code/_helpers.py | 3 +++ 4 files changed, 8 insertions(+), 5 deletions(-) (limited to 'bot') diff --git a/bot/constants.py b/bot/constants.py index 00d75a3f..cb5a91cc 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -40,8 +40,8 @@ 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 `::`. + without the backticks. If more than one leaderboard needs to be added to + the constant, separate the individual leaderboards with `::`. Example ENV: `id1,session1,join_code1::id2,session2,join_code2` """ diff --git a/bot/exts/christmas/advent_of_code/__init__.py b/bot/exts/christmas/advent_of_code/__init__.py index 20ac5ab9..3c521168 100644 --- a/bot/exts/christmas/advent_of_code/__init__.py +++ b/bot/exts/christmas/advent_of_code/__init__.py @@ -2,7 +2,7 @@ from bot.bot import Bot def setup(bot: Bot) -> None: - """Advent of Code Cog load.""" + """Set up the Advent of Code extension.""" # Import the Cog at runtime to prevent side effects like defining # RedisCache instances too early. from ._cog import AdventOfCode diff --git a/bot/exts/christmas/advent_of_code/_cog.py b/bot/exts/christmas/advent_of_code/_cog.py index 388d0592..19baca93 100644 --- a/bot/exts/christmas/advent_of_code/_cog.py +++ b/bot/exts/christmas/advent_of_code/_cog.py @@ -230,7 +230,7 @@ class AdventOfCode(commands.Cog): @adventofcode_group.command(name="join", aliases=("j",), brief="Learn how to join the leaderboard (via DM)") @override_in_channel(AOC_WHITELIST) async def join_leaderboard(self, ctx: commands.Context) -> None: - """DM the user the information for joining the PyDis AoC private leaderboard.""" + """DM the user the information for joining the Python Discord leaderboard.""" author = ctx.message.author log.info(f"{author.name} ({author.id}) has requested a PyDis AoC leaderboard code") @@ -284,7 +284,7 @@ class AdventOfCode(commands.Cog): @adventofcode_group.command( name="stats", aliases=("dailystats", "ds"), - brief="Get daily statistics for the PyDis private leaderboard" + brief="Get daily statistics for the Python Discord leaderboard" ) @override_in_channel(AOC_WHITELIST) async def private_leaderboard_daily_stats(self, ctx: commands.Context) -> None: diff --git a/bot/exts/christmas/advent_of_code/_helpers.py b/bot/exts/christmas/advent_of_code/_helpers.py index 8b85bf5d..57aad54d 100644 --- a/bot/exts/christmas/advent_of_code/_helpers.py +++ b/bot/exts/christmas/advent_of_code/_helpers.py @@ -102,10 +102,13 @@ def _parse_raw_leaderboard_data(raw_leaderboard_data: dict) -> dict: sorted(leaderboard.items(), key=lambda t: t[1]["score"], reverse=True) ) + # Create summary stats for the stars completed for each day of the event. daily_stats = {} for day in range(1, 26): star_one = len(star_results.get((day, 1), [])) star_two = len(star_results.get((day, 1), [])) + # By using a dictionary instead of namedtuple here, we can serialize + # this data to JSON in order to cache it in Redis. daily_stats[day] = {"star_one": star_one, "star_two": star_two} return {"daily_stats": daily_stats, "leaderboard": sorted_leaderboard} -- cgit v1.2.3 From c6b89c21dc7d73e4286cf67df421772c1e6df77d Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 01:52:08 +0100 Subject: Add global leaderboard command back I accidentally removed the global leaderboard command. I've added it back! --- bot/exts/christmas/advent_of_code/_cog.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'bot') diff --git a/bot/exts/christmas/advent_of_code/_cog.py b/bot/exts/christmas/advent_of_code/_cog.py index 19baca93..3d1d268f 100644 --- a/bot/exts/christmas/advent_of_code/_cog.py +++ b/bot/exts/christmas/advent_of_code/_cog.py @@ -281,6 +281,22 @@ class AdventOfCode(commands.Cog): await ctx.send(content=f"{header}\n\n{table}", embed=info_embed) + @adventofcode_group.command( + name="global", + aliases=("globalboard", "gb"), + brief="Get a link to the global leaderboard", + ) + @override_in_channel(AOC_WHITELIST) + async def aoc_global_leaderboard(self, ctx: commands.Context) -> None: + """Get a link to the global Advent of Code leaderboard.""" + url = self.global_leaderboard_url + global_leaderboard = discord.Embed( + title="Advent of Code — Global Leaderboard", + description=f"You can find the global leaderboard [here]({url})." + ) + global_leaderboard.set_thumbnail(url=_helpers.AOC_EMBED_THUMBNAIL) + await ctx.send(embed=global_leaderboard) + @adventofcode_group.command( name="stats", aliases=("dailystats", "ds"), -- cgit v1.2.3 From 60ace6a69804041bb47bd4fa1e70699aa2bd6633 Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 02:19:35 +0100 Subject: Update information for the .aoc about embed I've updated the information for the about embed: - Added information on the scoring of private leaderboards - Changed the text of "join our private leaderboard", as we no longer have to introduce private leaderboards there. - I've also streamlined the section on Auth providers. --- bot/resources/advent_of_code/about.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'bot') diff --git a/bot/resources/advent_of_code/about.json b/bot/resources/advent_of_code/about.json index 91ae6813..dd0fe59a 100644 --- a/bot/resources/advent_of_code/about.json +++ b/bot/resources/advent_of_code/about.json @@ -6,22 +6,22 @@ }, { "name": "How do I sign up?", - "value": "AoC utilizes the following services' OAuth:", + "value": "Sign up with one of these services:", "inline": true }, { - "name": "Service", + "name": "Auth Services", "value": "GitHub\nGoogle\nTwitter\nReddit", "inline": true }, { "name": "How does scoring work?", - "value": "Getting a star first is worth 100 points, second is 99, and so on down to 1 point at 100th place.\n\nCheck out AoC's [global leaderboard](https://adventofcode.com/leaderboard) to see who's leading this year's event!", + "value": "For the [global leaderboard](https://adventofcode.com/leaderboard), the first person to get a star first gets 100 points, the second person gets 99 points, and so on down to 1 point at 100th place.\n\nFor private leaderboards, the first person to get a star gets N points, where N is the number of people on the leaderboard. The second person to get the star gets N-1 points and so on and so forth.", "inline": false }, { "name": "Join our private leaderboard!", - "value": "In addition to the global leaderboard, AoC also offers private leaderboards, where you can compete against a smaller group of friends!\n\nGet the join code using `.aoc join` and head over to AoC's [private leaderboard page](https://adventofcode.com/leaderboard/private) to join the PyDis private leaderboard!", + "value": "Come join the Python Discord private leaderboard and compete against other people in the community! Get the join code using `.aoc join` and visit the [private leaderboard page](https://adventofcode.com/leaderboard/private) to join our leaderboard.", "inline": false } ] -- cgit v1.2.3 From b0e30c21f0638a0b45096a01a00327acee51b46d Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 13:50:34 +0100 Subject: Remove constants replaced by AOC_LEADERBOARDS There were still two constants left over that were no longer used after the rewrite. I've removed them. --- bot/constants.py | 3 --- 1 file changed, 3 deletions(-) (limited to 'bot') diff --git a/bot/constants.py b/bot/constants.py index cb5a91cc..292a242a 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -232,9 +232,6 @@ class Roles(NamedTuple): class Tokens(NamedTuple): giphy = environ.get("GIPHY_TOKEN") - # Public AoC cookies in environment must be in the same order as AdventOfCode.leaderboard_public_ids - aoc_public_session_cookies = environ.get("AOC_PUBLIC_SESSION_COOKIES", "").split(",") - aoc_staff_session_cookie = environ.get("AOC_STAFF_SESSION_COOKIE") omdb = environ.get("OMDB_API_KEY") youtube = environ.get("YOUTUBE_API_KEY") tmdb = environ.get("TMDB_API_KEY") -- cgit v1.2.3 From c0685c73ebbffb185bddc8e0b1e6a8e09b9f289d Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 14:01:32 +0100 Subject: Move helper functions to the ._helpers module I've moved the helper functions to the _helpers.py module and clarified the docstring of the `is_in_advent` helper function. --- bot/exts/christmas/advent_of_code/_cog.py | 44 ++++++--------------------- bot/exts/christmas/advent_of_code/_helpers.py | 33 ++++++++++++++++++++ 2 files changed, 42 insertions(+), 35 deletions(-) (limited to 'bot') diff --git a/bot/exts/christmas/advent_of_code/_cog.py b/bot/exts/christmas/advent_of_code/_cog.py index 3d1d268f..bc2a4724 100644 --- a/bot/exts/christmas/advent_of_code/_cog.py +++ b/bot/exts/christmas/advent_of_code/_cog.py @@ -4,11 +4,9 @@ import logging import math from datetime import datetime, timedelta from pathlib import Path -from typing import Tuple import discord from discord.ext import commands -from pytz import timezone from bot.bot import Bot from bot.constants import ( @@ -21,39 +19,15 @@ log = logging.getLogger(__name__) AOC_REQUEST_HEADER = {"user-agent": "PythonDiscord AoC Event Bot"} -EST = timezone("EST") COUNTDOWN_STEP = 60 * 5 AOC_WHITELIST = WHITELISTED_CHANNELS + (Channels.advent_of_code,) -def is_in_advent() -> bool: - """Utility function to check if we are between December 1st and December 25th.""" - # Run the code from the 1st to the 24th - return datetime.now(EST).day in range(1, 25) and datetime.now(EST).month == 12 - - -def time_left_to_aoc_midnight() -> Tuple[datetime, timedelta]: - """Calculates the amount of time left until midnight in UTC-5 (Advent of Code maintainer timezone).""" - # Change all time properties back to 00:00 - todays_midnight = datetime.now(EST).replace( - microsecond=0, - second=0, - minute=0, - hour=0 - ) - - # We want tomorrow so add a day on - tomorrow = todays_midnight + timedelta(days=1) - - # Calculate the timedelta between the current time and midnight - return tomorrow, tomorrow - datetime.now(EST) - - async def countdown_status(bot: commands.Bot) -> None: """Set the playing status of the bot to the minutes & hours left until the next day's challenge.""" - while is_in_advent(): - _, time_left = time_left_to_aoc_midnight() + while _helpers.is_in_advent(): + _, time_left = _helpers.time_left_to_aoc_midnight() aligned_seconds = int(math.ceil(time_left.seconds / COUNTDOWN_STEP)) * COUNTDOWN_STEP hours, minutes = aligned_seconds // 3600, aligned_seconds // 60 % 60 @@ -84,8 +58,8 @@ async def day_countdown(bot: commands.Bot) -> None: Once we have calculated this we should then sleep that number and when the time is reached, ping the Advent of Code role notifying them that the new challenge is ready. """ - while is_in_advent(): - tomorrow, time_left = time_left_to_aoc_midnight() + while _helpers.is_in_advent(): + tomorrow, time_left = _helpers.time_left_to_aoc_midnight() # Prevent bot from being slightly too early in trying to announce today's puzzle await asyncio.sleep(time_left.seconds + 1) @@ -196,12 +170,12 @@ class AdventOfCode(commands.Cog): @override_in_channel(AOC_WHITELIST) async def aoc_countdown(self, ctx: commands.Context) -> None: """Return time left until next day.""" - if not is_in_advent(): - datetime_now = datetime.now(EST) + if not _helpers.is_in_advent(): + datetime_now = datetime.now(_helpers.EST) # Calculate the delta to this & next year's December 1st to see which one is closest and not in the past - this_year = datetime(datetime_now.year, 12, 1, tzinfo=EST) - next_year = datetime(datetime_now.year + 1, 12, 1, tzinfo=EST) + this_year = datetime(datetime_now.year, 12, 1, tzinfo=_helpers.EST) + next_year = datetime(datetime_now.year + 1, 12, 1, tzinfo=_helpers.EST) deltas = (dec_first - datetime_now for dec_first in (this_year, next_year)) delta = min(delta for delta in deltas if delta >= timedelta()) # timedelta() gives 0 duration delta @@ -215,7 +189,7 @@ class AdventOfCode(commands.Cog): f"The next event will start in {delta_str}.") return - tomorrow, time_left = time_left_to_aoc_midnight() + tomorrow, time_left = _helpers.time_left_to_aoc_midnight() hours, minutes = time_left.seconds // 3600, time_left.seconds // 60 % 60 diff --git a/bot/exts/christmas/advent_of_code/_helpers.py b/bot/exts/christmas/advent_of_code/_helpers.py index 57aad54d..7ac54322 100644 --- a/bot/exts/christmas/advent_of_code/_helpers.py +++ b/bot/exts/christmas/advent_of_code/_helpers.py @@ -4,9 +4,11 @@ import json import logging import operator import typing +from typing import Tuple import aiohttp import discord +import pytz from bot.constants import AdventOfCode, Colours from bot.exts.christmas.advent_of_code import _caches @@ -42,6 +44,9 @@ AOC_EMBED_THUMBNAIL = ( "/branding/master/seasonal/christmas/server_icons/festive_256.gif" ) +# Create an easy constant for the EST timezone +EST = pytz.timezone("EST") + # Create namedtuple that combines a participant's name and their completion # time for a specific star. We're going to use this later to order the results # for each star to compute the rank score. @@ -313,3 +318,31 @@ async def get_public_join_code(author: discord.Member) -> typing.Optional[str]: # Return the join code for this board return AdventOfCode.leaderboards[best_board].join_code + + +def is_in_advent() -> bool: + """ + Check if we're currently on an Advent of Code day, excluding 25 December. + + This helper function is used to check whether or not a feature that prepares + something for the next Advent of Code challenge should run. As the puzzle + published on the 25th is the last puzzle, this check excludes that date. + """ + return datetime.datetime.now(EST).day in range(1, 25) and datetime.datetime.now(EST).month == 12 + + +def time_left_to_aoc_midnight() -> Tuple[datetime.datetime, datetime.timedelta]: + """Calculates the amount of time left until midnight in UTC-5 (Advent of Code maintainer timezone).""" + # Change all time properties back to 00:00 + todays_midnight = datetime.datetime.now(EST).replace( + microsecond=0, + second=0, + minute=0, + hour=0 + ) + + # We want tomorrow so add a day on + tomorrow = todays_midnight + datetime.timedelta(days=1) + + # Calculate the timedelta between the current time and midnight + return tomorrow, tomorrow - datetime.datetime.now(EST) -- cgit v1.2.3 From 809da330f5ec60c6186dfefaf8bb4bb0128f8442 Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 14:20:21 +0100 Subject: Set character encoding for logging to utf-8 --- bot/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/__init__.py b/bot/__init__.py index a9a0865e..bdb18666 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -37,7 +37,8 @@ os.makedirs(log_dir, exist_ok=True) # File handler rotates logs every 5 MB file_handler = logging.handlers.RotatingFileHandler( - log_file, maxBytes=5 * (2**20), backupCount=10) + log_file, maxBytes=5 * (2**20), backupCount=10, encoding="utf-8", +) file_handler.setLevel(logging.TRACE if Client.debug else logging.DEBUG) # Console handler prints to terminal @@ -61,7 +62,7 @@ logging.basicConfig( format='%(asctime)s - %(name)s %(levelname)s: %(message)s', datefmt="%D %H:%M:%S", level=logging.TRACE if Client.debug else logging.DEBUG, - handlers=[console_handler, file_handler] + handlers=[console_handler, file_handler], ) logging.getLogger().info('Logging initialization complete') -- cgit v1.2.3