From e2db40943d384d0344dd5cf55c361443f6eb7119 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 3 Sep 2019 16:09:22 +0200 Subject: [PATCH] Track trivia scores --- royalnet/commands/royalgames/trivia.py | 11 +++++++-- royalnet/database/tables/__init__.py | 3 ++- royalnet/database/tables/triviascores.py | 29 ++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 royalnet/database/tables/triviascores.py diff --git a/royalnet/commands/royalgames/trivia.py b/royalnet/commands/royalgames/trivia.py index 4b861b3d..f6513492 100644 --- a/royalnet/commands/royalgames/trivia.py +++ b/royalnet/commands/royalgames/trivia.py @@ -7,7 +7,9 @@ from ..command import Command from ..commandargs import CommandArgs from ..commanddata import CommandData from ..commandinterface import CommandInterface +from ...utils import asyncify from ...error import * +from ...database.tables import TriviaScore class TriviaCommand(Command): @@ -15,13 +17,15 @@ class TriviaCommand(Command): description: str = "Manda una domanda dell'OpenTDB in chat." + require_alchemy_tables = {TriviaScore} + _letter_emojis = ["🇦", "🇧", "🇨", "🇩"] _correct_emoji = "✅" _wrong_emoji = "❌" - _answer_time = 15 + _answer_time = 18 def __init__(self, interface: CommandInterface): super().__init__(interface) @@ -88,8 +92,11 @@ class TriviaCommand(Command): for answerer in self._answerers[question_id]: if self._answerers[question_id][answerer]: results += self._correct_emoji + answerer.trivia_score.correct_answers += 1 else: results += self._wrong_emoji - results += f" {answerer}\n" + answerer.trivia_score.wrong_answers += 1 + results += f" {answerer} ({answerer.trivia_score.correct_answers}|{answerer.trivia_score.wrong_answers})\n" await data.reply(results) del self._answerers[question_id] + await asyncify(self.interface.session.commit) diff --git a/royalnet/database/tables/__init__.py b/royalnet/database/tables/__init__.py index f316c01b..e4995cc9 100644 --- a/royalnet/database/tables/__init__.py +++ b/royalnet/database/tables/__init__.py @@ -12,6 +12,7 @@ from .medals import Medal from .medalawards import MedalAward from .bios import Bio from .reminders import Reminder +from .triviascores import TriviaScore __all__ = ["Royal", "Telegram", "Diario", "Alias", "ActiveKvGroup", "Keyvalue", "Keygroup", "Discord", "WikiPage", - "WikiRevision", "Medal", "MedalAward", "Bio", "Reminder"] + "WikiRevision", "Medal", "MedalAward", "Bio", "Reminder", "TriviaScore"] diff --git a/royalnet/database/tables/triviascores.py b/royalnet/database/tables/triviascores.py new file mode 100644 index 00000000..5835623c --- /dev/null +++ b/royalnet/database/tables/triviascores.py @@ -0,0 +1,29 @@ +from sqlalchemy import Column, \ + Integer, \ + ForeignKey +from sqlalchemy.orm import relationship, backref +from sqlalchemy.ext.declarative import declared_attr +from .royals import Royal + + +class TriviaScore: + __tablename__ = "triviascores" + + @declared_attr + def royal_id(self): + return Column(Integer, ForeignKey("royals.uid"), primary_key=True) + + @declared_attr + def royal(self): + return relationship("Royal", backref=backref("trivia_score", uselist=False)) + + @declared_attr + def correct_answers(self): + return Column(Integer, nullable=False, default=0) + + @declared_attr + def wrong_answers(self): + return Column(Integer, nullable=False, default=0) + + def __repr__(self): + return f""