1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-24 03:54:20 +00:00

Use scores for trivia

This commit is contained in:
Steffo 2020-01-17 17:50:53 +01:00
parent 803aab7dc8
commit 38c9d1f019
2 changed files with 10 additions and 3 deletions

View file

@ -46,10 +46,10 @@ class TriviaCommand(rc.Command):
elif arg == "scores": elif arg == "scores":
trivia_scores = await ru.asyncify(data.session.query(self.alchemy.get(TriviaScore)).all) trivia_scores = await ru.asyncify(data.session.query(self.alchemy.get(TriviaScore)).all)
strings = ["🏆 [b]Trivia Leaderboards[/b]\n"] strings = ["🏆 [b]Trivia Leaderboards[/b]\n"]
for index, ts in enumerate(sorted(trivia_scores, key=lambda ts: -ts.correct_rate)): for index, ts in enumerate(sorted(trivia_scores, key=lambda ts: -ts.score)):
if index > 3: if index > 3:
index = 3 index = 3
strings.append(f"{self._medal_emojis[index]} {ts.royal.username}" strings.append(f"{self._medal_emojis[index]} {ts.royal.username}: [b]{ts.score:.0f}p[/b]"
f" ({ts.correct_answers}/{ts.total_answers})") f" ({ts.correct_answers}/{ts.total_answers})")
await data.reply("\n".join(strings)) await data.reply("\n".join(strings))
return return
@ -131,13 +131,16 @@ class TriviaCommand(rc.Command):
ts = self.interface.alchemy.get(TriviaScore)(royal=answerer) ts = self.interface.alchemy.get(TriviaScore)(royal=answerer)
data.session.add(ts) data.session.add(ts)
await ru.asyncify(data.session.commit) await ru.asyncify(data.session.commit)
previous_score = answerer.trivia_score.score
if self._answerers[question_id][answerer_id]: if self._answerers[question_id][answerer_id]:
results += self._correct_emoji results += self._correct_emoji
answerer.trivia_score.correct_answers += 1 answerer.trivia_score.correct_answers += 1
else: else:
results += self._wrong_emoji results += self._wrong_emoji
answerer.trivia_score.wrong_answers += 1 answerer.trivia_score.wrong_answers += 1
results += f" {answerer} ({answerer.trivia_score.correct_answers}/{answerer.trivia_score.total_answers})\n" current_score = answerer.trivia_score.score
score_difference = current_score - previous_score
results += f" {answerer}: [b]{current_score:.0f}p[/b] ({score_difference:+.0f}p)\n"
await data.reply(results) await data.reply(results)
del self._answerers[question_id] del self._answerers[question_id]
await ru.asyncify(data.session.commit) await ru.asyncify(data.session.commit)

View file

@ -36,5 +36,9 @@ class TriviaScore:
def correct_rate(self): def correct_rate(self):
return self.correct_answers / self.total_answers return self.correct_answers / self.total_answers
@property
def score(self) -> float:
return (self.correct_answers + self.correct_answers * self.correct_rate) * 10
def __repr__(self): def __repr__(self):
return f"<TriviaScore of {self.royal}: ({self.correct_answers}|{self.wrong_answers})>" return f"<TriviaScore of {self.royal}: ({self.correct_answers}|{self.wrong_answers})>"