2019-11-11 08:56:08 +00:00
|
|
|
from sqlalchemy import Column, \
|
|
|
|
Integer, \
|
|
|
|
ForeignKey
|
|
|
|
from sqlalchemy.orm import relationship, backref
|
|
|
|
from sqlalchemy.ext.declarative import declared_attr
|
|
|
|
|
|
|
|
|
|
|
|
class TriviaScore:
|
|
|
|
__tablename__ = "triviascores"
|
|
|
|
|
|
|
|
@declared_attr
|
|
|
|
def royal_id(self):
|
|
|
|
return Column(Integer, ForeignKey("users.uid"), primary_key=True)
|
|
|
|
|
|
|
|
@declared_attr
|
|
|
|
def royal(self):
|
|
|
|
return relationship("User", 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)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def total_answers(self):
|
|
|
|
return self.correct_answers + self.wrong_answers
|
|
|
|
|
|
|
|
@property
|
|
|
|
def offset(self):
|
|
|
|
return self.correct_answers - self.wrong_answers
|
|
|
|
|
|
|
|
@property
|
|
|
|
def correct_rate(self):
|
|
|
|
return self.correct_answers / self.total_answers
|
|
|
|
|
2020-01-17 16:50:53 +00:00
|
|
|
@property
|
|
|
|
def score(self) -> float:
|
|
|
|
return (self.correct_answers + self.correct_answers * self.correct_rate) * 10
|
|
|
|
|
2019-11-11 08:56:08 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return f"<TriviaScore of {self.royal}: ({self.correct_answers}|{self.wrong_answers})>"
|