diff options
| author | 2019-04-21 20:03:00 +1000 | |
|---|---|---|
| committer | 2019-04-21 20:03:00 +1000 | |
| commit | 079d107c35ef11a3d55ff9a88aa8e5dc34f99dbb (patch) | |
| tree | ed8064ed37dffee4bd215c070ca24e27f0821311 | |
| parent | Make team assignment deterministic for event distribution. (diff) | |
Ensure super egg drops.
| -rw-r--r-- | bot/seasons/easter/egg_hunt/cog.py | 41 | ||||
| -rw-r--r-- | bot/seasons/easter/egg_hunt/constants.py | 2 |
2 files changed, 25 insertions, 18 deletions
diff --git a/bot/seasons/easter/egg_hunt/cog.py b/bot/seasons/easter/egg_hunt/cog.py index 7d592bba..56d4e71d 100644 --- a/bot/seasons/easter/egg_hunt/cog.py +++ b/bot/seasons/easter/egg_hunt/cog.py @@ -295,11 +295,11 @@ class EggHunt(commands.Cog): self.task = asyncio.create_task(self.super_egg()) self.task.add_done_callback(self.task_cleanup) - @staticmethod - def task_cleanup(task): - """Returns a task result. Used as a done callback to show raised exceptions.""" + def task_cleanup(self, task): + """Returns task result and restarts. Used as a done callback to show raised exceptions.""" task.result() + self.task = asyncio.create_task(self.super_egg()) @staticmethod def current_timestamp() -> int: @@ -323,26 +323,33 @@ class EggHunt(commands.Cog): await asyncio.sleep(remaining) log.debug(f"Hunt started.") - current_window = EggHuntSettings.start_time - next_window = 0 - for window in EggHuntSettings.windows: - window = int(window) - if window < now: - current_window = window + + db = sqlite3.connect(DB_PATH) + c = db.cursor() + + current_window = None + next_window = None + windows = EggHuntSettings.windows.copy() + windows.insert(0, EggHuntSettings.start_time) + for i, window in enumerate(windows): + c.execute(f"SELECT COUNT(*) FROM super_eggs WHERE window={window}") + alread_dropped = c.fetchone()[0] + if alread_dropped: continue - if not next_window: - next_window = window else: + current_window = window + next_window = window[i+1] break - log.debug(f"Current Window: {current_window}. Next Window {next_window}") - - db = sqlite3.connect(DB_PATH) - c = db.cursor() - c.execute(f"SELECT COUNT(*) FROM super_eggs WHERE window={current_window}") count = c.fetchone()[0] db.close() + if not current_window: + log.debug(f"Suitable window not found.") + break + + log.debug(f"Current Window: {current_window}. Next Window {next_window}") + if not count: next_drop = random.randrange(now, next_window) log.debug(f"Sleeping until next super egg drop: {next_drop}.") @@ -370,7 +377,7 @@ class EggHunt(commands.Cog): await SuperEggMessage(msg, egg, current_window).start() log.debug("Sleeping until next window.") - next_loop = max(next_window - self.current_timestamp(), 0) + next_loop = max(next_window - self.current_timestamp(), 60*60) await asyncio.sleep(next_loop) @commands.Cog.listener() diff --git a/bot/seasons/easter/egg_hunt/constants.py b/bot/seasons/easter/egg_hunt/constants.py index 3f3c3bbe..c7d9818b 100644 --- a/bot/seasons/easter/egg_hunt/constants.py +++ b/bot/seasons/easter/egg_hunt/constants.py @@ -11,7 +11,7 @@ GUILD = bot.get_guild(Client.guild) class EggHuntSettings: start_time = int(os.environ["HUNT_START"]) end_time = start_time + 172800 # 48 hrs later - windows = os.environ.get("HUNT_WINDOWS").split(',') or [] + windows = [int(w) for w in os.environ.get("HUNT_WINDOWS").split(',')] or [] allowed_channels = [ Channels.seasonalbot_chat, Channels.off_topic_0, |