From dba6d5aa19adc359837a565776651b2cdf7d61a6 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 25 Oct 2019 13:19:32 +0200 Subject: [PATCH] Fix and improve trivia --- royalnet/bots/telegram.py | 14 +++++++++----- royalnet/packs/royal/commands/trivia.py | 24 ++++++++++++++++-------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/royalnet/bots/telegram.py b/royalnet/bots/telegram.py index 4d5f8a8a..d75a1d8b 100644 --- a/royalnet/bots/telegram.py +++ b/royalnet/bots/telegram.py @@ -163,8 +163,8 @@ class TelegramBot(GenericBot): return # Prepare data data = self._Data(interface=command.interface, update=update) - # Run the command try: + # Run the command await command.run(CommandArgs(parameters), data) except InvalidInputError as e: await data.reply(f"โš ๏ธ {e.message}\n" @@ -178,8 +178,9 @@ class TelegramBot(GenericBot): error_message = f"๐Ÿฆ€ [b]{e.__class__.__name__}[/b] ๐Ÿฆ€\n" error_message += '\n'.join(e.args) await data.reply(error_message) - # Close the data session - await data.session_close() + finally: + # Close the data session + await data.session_close() async def _handle_callback_query(self, update: telegram.Update): query: telegram.CallbackQuery = update.callback_query @@ -196,10 +197,13 @@ class TelegramBot(GenericBot): return try: response = await callback(data=self._Data(interface=command.interface, update=update)) - except KeyboardExpiredError: + except KeyboardExpiredError as e: # FIXME: May cause a memory leak, as keys are not deleted after use await self.safe_api_call(source.edit_reply_markup, reply_markup=None) - await self.safe_api_call(query.answer, text="โ›”๏ธ This keyboard has expired.") + if len(e.args) > 0: + await self.safe_api_call(query.answer, text=f"โ›”๏ธ {e.args[0]}") + else: + await self.safe_api_call(query.answer, text="โ›”๏ธ This keyboard has expired.") return except Exception as e: error_text = f"โ›”๏ธ {e.__class__.__name__}\n" diff --git a/royalnet/packs/royal/commands/trivia.py b/royalnet/packs/royal/commands/trivia.py index 0a82bce5..7f61dda7 100644 --- a/royalnet/packs/royal/commands/trivia.py +++ b/royalnet/packs/royal/commands/trivia.py @@ -24,13 +24,19 @@ class TriviaCommand(Command): _wrong_emoji = "โŒ" - _answer_time = 18 + _answer_time = 14 + + _question_lock: bool = False def __init__(self, interface: CommandInterface): super().__init__(interface) - self._answerers: typing.Dict[uuid.UUID, typing.Dict[..., bool]] = {} + self._answerers: typing.Dict[uuid.UUID, typing.Dict[str, bool]] = {} async def run(self, args: CommandArgs, data: CommandData) -> None: + if self._question_lock: + raise CommandError("C'รจ giร  un'altra domanda attiva!") + self._question_lock = True + await data.delete_invoking() # Fetch the question async with aiohttp.ClientSession() as session: async with session.get("https://opentdb.com/api.php?amount=1") as response: @@ -70,17 +76,17 @@ class TriviaCommand(Command): async def correct(data: CommandData): answerer_ = await data.get_author(error_if_none=True) try: - self._answerers[question_id][answerer_] = True + self._answerers[question_id][answerer_.uid] = True except KeyError: - raise KeyboardExpiredError("Question time ran out.") + raise KeyboardExpiredError("Tempo scaduto!") return "๐Ÿ†— Hai risposto alla domanda. Ora aspetta un attimo per i risultati!" async def wrong(data: CommandData): answerer_ = await data.get_author(error_if_none=True) try: - self._answerers[question_id][answerer_] = False + self._answerers[question_id][answerer_.uid] = False except KeyError: - raise KeyboardExpiredError("Question time ran out.") + raise KeyboardExpiredError("Tempo scaduto!") return "๐Ÿ†— Hai risposto alla domanda. Ora aspetta un attimo per i risultati!" # Add question @@ -94,12 +100,13 @@ class TriviaCommand(Command): await asyncio.sleep(self._answer_time) results = f"โ—๏ธ Tempo scaduto!\n" \ f"La risposta corretta era [b]{answers[correct_index]}[/b]!\n\n" - for answerer in self._answerers[question_id]: + for answerer_id in self._answerers[question_id]: + answerer = data.session.query(self.alchemy.User).get(answerer_id) if answerer.trivia_score is None: ts = self.interface.alchemy.TriviaScore(royal=answerer) data.session.add(ts) data.session.commit() - if self._answerers[question_id][answerer]: + if self._answerers[question_id][answerer_id]: results += self._correct_emoji answerer.trivia_score.correct_answers += 1 else: @@ -109,3 +116,4 @@ class TriviaCommand(Command): await data.reply(results) del self._answerers[question_id] await asyncify(data.session.commit) + self._question_lock = False