Add !wordbot leaderboard command
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -243,10 +243,33 @@ class Wordbot(Plugin):
|
|||||||
):
|
):
|
||||||
parts = line.strip().split()
|
parts = line.strip().split()
|
||||||
match parts:
|
match parts:
|
||||||
case ["!wordbot", "end_now"]:
|
# case ["!wordbot", "end_now"]:
|
||||||
async with self.__db_lock:
|
# async with self.__db_lock:
|
||||||
self.end_round(conn, channel)
|
# self.end_round(conn, channel)
|
||||||
self.start_round(channel, allow_early_end=True)
|
# 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 _:
|
case _:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -256,7 +279,6 @@ class Wordbot(Plugin):
|
|||||||
words = [word.strip() for word in fp]
|
words = [word.strip() for word in fp]
|
||||||
random.shuffle(words)
|
random.shuffle(words)
|
||||||
words = words[: self.words_per_round]
|
words = words[: self.words_per_round]
|
||||||
log.debug("%s", words)
|
|
||||||
self.db.start_round(channel, self.duration, words, allow_early_end)
|
self.db.start_round(channel, self.duration, words, allow_early_end)
|
||||||
|
|
||||||
def end_round(self, conn: IrcProtocol, channel: str):
|
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:")
|
self.send_to(conn, channel, "Game over. Here were the scores:")
|
||||||
for user, score in scores:
|
for user, score in scores:
|
||||||
self.send_to(conn, channel, f"{rankings[score] + 1}. {user}. {score}")
|
self.send_to(conn, channel, f"{rankings[score] + 1}. {user}. {score}")
|
||||||
|
|||||||
Reference in New Issue
Block a user