From 56915d54b9fdb97391140c3f4ab25e89bd829132 Mon Sep 17 00:00:00 2001 From: ddorn Date: Tue, 12 May 2020 16:02:10 +0200 Subject: [PATCH] :sparkles: only one fractal at a time --- src/base_tirage.py | 13 ++++++++++-- src/cogs/misc.py | 51 +++++++++++++++++++++------------------------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/base_tirage.py b/src/base_tirage.py index 27d9508..bdea566 100644 --- a/src/base_tirage.py +++ b/src/base_tirage.py @@ -95,6 +95,16 @@ class BaseTirage(yaml.YAMLObject): self.poules: Dict[Poule, List[str]] = {} """A mapping between the poule and the list of teams in this poule.""" + def availaible(self, pb, poule): + pbs = [ + self.teams[team].accepted_problems[poule.rnd] for team in self.poules[poule] + ] + + if len(self.poules[poule]) < 5: + return pb not in pbs + else: + return pbs.count(pb) < 2 + async def event(self, event: Event): event.set() await self.queue.put(event) @@ -113,11 +123,10 @@ class BaseTirage(yaml.YAMLObject): else: return await self.warn_wrong_team(None, trigram) - other_pbs = [self.teams[team].accepted_problems[rnd] for team in teams] available = [ pb for pb in PROBLEMS - if pb not in team.accepted_problems and pb not in other_pbs + if pb not in team.accepted_problems and self.availaible(pb, poule) ] return await self.event(Event(trigram, random.choice(available))) diff --git a/src/cogs/misc.py b/src/cogs/misc.py index 076ecdd..832d575 100644 --- a/src/cogs/misc.py +++ b/src/cogs/misc.py @@ -51,7 +51,7 @@ class MiscCog(Cog, name="Divers"): self.bot = bot self.show_hidden = False self.verify_checks = True - self.last_fractal_time = 0.0 + self.computing = False @command( name="choose", @@ -103,35 +103,30 @@ class MiscCog(Cog, name="Divers"): @command(hidden=True) async def fractal(self, ctx: Context): - if time() > self.last_fractal_time + FRACTAL_COOLDOWN or has_role( - ctx.author, Role.DEV - ): - self.last_fractal_time = time() - else: - return await ctx.send( - f"Merci de ne pas générer plus d'une fractale " - f"toutes les {FRACTAL_COOLDOWN} secondes :wink: " - f"Il reste {round(FRACTAL_COOLDOWN - (time() - self.last_fractal_time), 1)}s." - ) + if self.computing: + return await ctx.send("Il y a déjà une fractale en cours de calcul...") - await ctx.message.add_reaction(Emoji.CHECK) - msg: discord.Message = ctx.message - seed = msg.content[len("!fractal ") :] - seed = seed or str(random.randint(0, 1_000_000_000)) - async with aiohttp.ClientSession() as session: - async with session.get( - FRACTAL_URL.format(seed=urllib.parse.quote(seed)) - ) as resp: - if resp.status != 200: - return await ctx.send( - "Il y a un problème pour calculer/télécharger l'image..." + try: + self.computing = True + + await ctx.message.add_reaction(Emoji.CHECK) + msg: discord.Message = ctx.message + seed = msg.content[len("!fractal ") :] + seed = seed or str(random.randint(0, 1_000_000_000)) + async with aiohttp.ClientSession() as session: + async with session.get( + FRACTAL_URL.format(seed=urllib.parse.quote(seed)) + ) as resp: + if resp.status != 200: + return await ctx.send( + "Il y a un problème pour calculer/télécharger l'image..." + ) + data = io.BytesIO(await resp.read()) + await ctx.send( + f"Seed: {seed}", file=discord.File(data, f"{seed}.png") ) - data = io.BytesIO(await resp.read()) - await ctx.send( - f"Seed: {seed}", file=discord.File(data, "cool_image.png") - ) - - self.last_fractal_time = time() + finally: + self.computing = False @command(hidden=True, aliases=["bang", "pan"]) async def pew(self, ctx):