From 42594d75b0d2f76ead11fb05557ff0da256587a8 Mon Sep 17 00:00:00 2001 From: Simon Junod Date: Sun, 16 Oct 2022 00:34:11 +0200 Subject: [PATCH] Added a timeout for the 'force alternation' mode, so the same player can play again after some time --- cambot/settings_example.py | 1 + cambot/wordle.py | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cambot/settings_example.py b/cambot/settings_example.py index 232ecf2..f4234ed 100644 --- a/cambot/settings_example.py +++ b/cambot/settings_example.py @@ -17,6 +17,7 @@ WORDLE_SLOWMODE = 0 WORDLE_MINLENGTH = 8 WORDLE_MAXLENGTH = 10 WORDLE_FORCE_ALTERNATION = True +WORDLE_ALTERNATION_TIMEOUT = 300 HEARTBEAT = 60 EPHEMERIS = 0 diff --git a/cambot/wordle.py b/cambot/wordle.py index b892299..06ab6de 100644 --- a/cambot/wordle.py +++ b/cambot/wordle.py @@ -2,6 +2,8 @@ import random import re from unidecode import unidecode from collections import defaultdict +from datetime import datetime +from math import ceil from .settings import * with open(WORDLE_VALID_WORDS, "r") as f: @@ -43,7 +45,8 @@ class Game: self.tries = 0 self.last_player = None self.scores = None - self.tried = None + self.tried = None + self.last_datetime = None async def reset(self): output = "" @@ -55,6 +58,7 @@ class Game: self.last_player = None self.scores = defaultdict(int) self.tried = set() + self.last_datetime = None await self.channel.edit(slowmode_delay=WORDLE_SLOWMODE) output += f"Il y a un nouveau mot à deviner ! Il fait {len(self.target)} lettres." await self.channel.send(output) @@ -81,9 +85,11 @@ class Game: # check for errors and react accordingly error = False - if WORDLE_FORCE_ALTERNATION and message.author == self.last_player: + if WORDLE_FORCE_ALTERNATION and message.author == self.last_player and (elapsed := (datetime.now() - self.last_datetime).total_seconds()) < WORDLE_ALTERNATION_TIMEOUT: await message.add_reaction("\N{BUSTS IN SILHOUETTE}") - error = True + error = True + remaining = ceil(WORDLE_ALTERNATION_TIMEOUT - elapsed) + await self.channel.send(f"{message.author.mention} : tu pourras rejouer dans {remaining} seconde{'s' if remaining > 1 else ''}") if len(guess) != len(self.target): await message.add_reaction("\N{LEFT RIGHT ARROW}") error = True @@ -96,6 +102,7 @@ class Game: # everything is OK, validate the proposal self.tries += 1 self.last_player = message.author + self.last_datetime = datetime.now() result = validate(self.target, guess) points = 0 for idx, letter in enumerate(guess):