1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 21:44:21 +00:00
royalnet/royalpack/tables/triviascores.py

56 lines
1.5 KiB
Python
Raw Normal View History

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
2020-02-11 22:41:24 +00:00
def user_id(self):
2019-11-11 08:56:08 +00:00
return Column(Integer, ForeignKey("users.uid"), primary_key=True)
@declared_attr
2020-02-11 22:41:24 +00:00
def user(self):
2019-11-11 08:56:08 +00:00
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):
2020-01-17 16:55:18 +00:00
if self.total_answers == 0:
return 0.0
2019-11-11 08:56:08 +00:00
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
2020-06-19 15:39:46 +00:00
def json(self):
return {
"correct": self.correct_answers,
"wrong": self.wrong_answers,
"total": self.total_answers,
"rate": self.correct_rate,
"score": self.score
}
2019-11-11 08:56:08 +00:00
def __repr__(self):
2020-02-11 22:51:22 +00:00
return f"<TriviaScore of {self.user}: ({self.correct_answers}|{self.wrong_answers})>"