diff options
| author | 2021-10-18 22:53:54 -0400 | |
|---|---|---|
| committer | 2022-02-09 18:13:36 -0500 | |
| commit | e37115a620ff33d4f0aeb9bac3ced3e8578605e0 (patch) | |
| tree | 1fbe72cd5d08892fd311bae9d9106069f07e33a8 /bot/exts/events/trivianight/questions.py | |
| parent | question view shell finished (diff) | |
finished basic question interface
Diffstat (limited to 'bot/exts/events/trivianight/questions.py')
| -rw-r--r-- | bot/exts/events/trivianight/questions.py | 56 |
1 files changed, 50 insertions, 6 deletions
diff --git a/bot/exts/events/trivianight/questions.py b/bot/exts/events/trivianight/questions.py index 21277ce9..2c1dbc81 100644 --- a/bot/exts/events/trivianight/questions.py +++ b/bot/exts/events/trivianight/questions.py @@ -42,12 +42,10 @@ class QuestionButton(Button): class QuestionView(View): """View for the questions.""" - def __init__(self, scoreboard: Scoreboard): - self.scoreboard = scoreboard + def __init__(self): self.current_question = {} - self._users_picked = {} - def _create_current_question(self) -> Embed: + def create_current_question(self) -> Embed: """Helper function to create the embed for the current question.""" question_embed = Embed( title=f"Question {self.current_question['number']}", @@ -58,14 +56,60 @@ class QuestionView(View): question_embed.add_field(name=label, value=answer, inline=False) self.buttons = [QuestionButton(label) for label in ("A", "B", "C", "D")] + for button in self.buttons: + self.add_item(button) return question_embed - def end_question(self) -> dict: + def end_question(self) -> tuple[dict, Embed]: """Returns the dictionaries from the corresponding buttons for those who got it correct.""" labels = ("A", "B", "C", "D") label = labels[self.current_question["correct"].index(self.current_question["answers"])] return_dict = {} for button in self.buttons: return_dict.update(button.answer(label)) + self.remove_item(button) + + answer_embed = Embed( + title=f"The correct answer for Question {self.current_question['number']} was", + color=Colours.grass_green + ) + answer_embed.add_field( + name=label, + value=self.current_question["correct"].index(self.current_question["answers"]), + inline=False + ) + + return return_dict, answer_embed + + +class Questions: + """An interface to use from the TriviaNight cog for questions.""" + + def __init__(self, scoreboard: Scoreboard): + self.scoreboard = scoreboard + self.questions = [] + self._ptr = -1 + + def set_questions(self, questions: list) -> None: + """Setting `self.questions` dynamically via a function to set it.""" + self.questions = questions + + def next_question(self) -> None: + """Advances to the next question.""" + self._ptr += 1 + if self._ptr < len(self.questions): + self.questions[self._ptr]["visited"] = True + self.view.current_question = self.questions[self._ptr] + + def current_question(self) -> tuple[Embed, QuestionView]: + """Returns an embed entailing the current question as an embed with a view.""" + return self.view.create_current_question(), self.view + + def end_question(self) -> None: + """Terminates answering of the question and displays the correct answer.""" + scores, answer_embed = self.view.end_question() + for user, score in scores.items(): + self.scoreboard[f"points: {user}"] = score[1] + self.scoreboard[f"speed: {user}"] = score[2] - return return_dict + return answer_embed |