Add !wordbot leaderboard command

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-05-30 17:33:37 -07:00
parent 1b4084322b
commit de2b1c1761

View File

@@ -243,10 +243,33 @@ class Wordbot(Plugin):
):
parts = line.strip().split()
match parts:
case ["!wordbot", "end_now"]:
async with self.__db_lock:
self.end_round(conn, channel)
self.start_round(channel, allow_early_end=True)
# case ["!wordbot", "end_now"]:
# async with self.__db_lock:
# self.end_round(conn, channel)
# self.start_round(channel, allow_early_end=True)
case ["!wordbot", "leaderboard", *args]:
if args:
user = args[0]
else:
user = who.nick
leaderboard = sorted(
self.db.leaderboard(channel).items(), key=lambda value: -value[1]
)
# Only print out the top 5
for rank, (nick, score) in enumerate(leaderboard[:5]):
self.send_to(conn, channel, f"{rank + 1}. {nick}. {score}")
# If the user isn't in the top 5, get their rank
leaderboard_users = [user for user, _ in leaderboard]
if user not in leaderboard_users[:5] and user in leaderboard_users:
rankings = {
nick: (rank, score)
for rank, (nick, score) in enumerate(leaderboard)
}
rank, score = rankings[user]
self.send_to(conn, channel, "...")
self.send_to(conn, channel, f"{rank + 1}. {user}. {score}")
case _:
pass
@@ -256,7 +279,6 @@ class Wordbot(Plugin):
words = [word.strip() for word in fp]
random.shuffle(words)
words = words[: self.words_per_round]
log.debug("%s", words)
self.db.start_round(channel, self.duration, words, allow_early_end)
def end_round(self, conn: IrcProtocol, channel: str):
@@ -271,8 +293,6 @@ class Wordbot(Plugin):
)
)
}
log.debug("%r", rankings)
log.debug("%r", scores)
self.send_to(conn, channel, "Game over. Here were the scores:")
for user, score in scores:
self.send_to(conn, channel, f"{rankings[score] + 1}. {user}. {score}")