From adb0419aaf210eca3540e1e62877690087abc2f1 Mon Sep 17 00:00:00 2001 From: qwerinope Date: Mon, 14 Apr 2025 17:03:10 +0200 Subject: [PATCH] add leaderboard command --- src/commands/index.ts | 3 ++- src/commands/leaderboard.ts | 22 ++++++++++++++++++++++ src/commands/stats.ts | 4 ++-- src/lib/userHelper.ts | 2 +- 4 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 src/commands/leaderboard.ts diff --git a/src/commands/index.ts b/src/commands/index.ts index 61682a1..6ae3bc1 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -5,9 +5,10 @@ import getloot from "./getloot"; import modme from "./modme"; import use from "./use"; import iteminfo from "./iteminfo" +import leaderboard from "./leaderboard" import stats from "./stats" import aliases from "./itemAliases" import admin from "./admin" -export default [timeout, inventory, qbucks, getloot, modme, use, iteminfo, ...aliases, ...admin, ...stats] +export default [timeout, inventory, qbucks, getloot, modme, use, iteminfo, leaderboard, ...aliases, ...admin, ...stats] diff --git a/src/commands/leaderboard.ts b/src/commands/leaderboard.ts new file mode 100644 index 0000000..46e735d --- /dev/null +++ b/src/commands/leaderboard.ts @@ -0,0 +1,22 @@ +import { createBotCommand } from "@twurple/easy-bot" +import pb, { User } from "../lib/pocketbase" +import { getTimeouts } from "../lib/userHelper" + +type KDData = { user: User, KD: number } + +export default createBotCommand('leaderboard', async (_params, { say }) => { + const users = await pb.collection('users').getFullList() + let userKDs: KDData[] = [] + for (const user of users) { + const data = await getTimeouts(user.id) + if (data.hit.blaster < 5) continue + const KD = data.shot.blaster / data.hit.blaster + const obj: KDData = { user, KD } + userKDs.push(obj) + } + if (userKDs.length === 0) { await say('No users on leaderboard yet!'); return } + userKDs.sort((data1, data2) => data2.KD - data1.KD) + const textlist: string[] = [] + for (let i = 0; i < (userKDs.length < 5 ? userKDs.length : 5); i++) textlist.push(`${i + 1}. ${userKDs.at(i)!.user.firstname}: ${userKDs.at(i)!.KD.toFixed(2)}`) + await say(`${textlist.join(' | ')}`) +}, { aliases: ['kdleaderboard'] }) diff --git a/src/commands/stats.ts b/src/commands/stats.ts index 5eb3de3..60f5e3e 100644 --- a/src/commands/stats.ts +++ b/src/commands/stats.ts @@ -23,7 +23,7 @@ const stats = createBotCommand('stats', async (params, { say, userName }) => { ` THIS MONTH: Stats of ${data.me ? userName : params[0]}: Users blasted: ${data.stats.shot.blaster}, - Blasted by others: ${data.stats.hit.blaster} (${isNaN(KD) ? 0 : KD.toFixed(3)} K/D). + Blasted by others: ${data.stats.hit.blaster} (${isNaN(KD) ? 0 : KD.toFixed(2)} K/D). Grenades lobbed: ${data.stats.used.grenade} TNTs lit: ${data.stats.used.tnt}, Silver bullets fired: ${data.stats.shot.silverbullet}, @@ -50,7 +50,7 @@ const alltime = createBotCommand('alltime', async (params, { say, userName }) => ` ALLTIME: Stats of ${data.me ? userName : params[0]}: Users blasted: ${data.stats.shot.blaster}, - Blasted by others: ${data.stats.hit.blaster} (${isNaN(KD) ? 0 : KD.toFixed(3)} K/D). + Blasted by others: ${data.stats.hit.blaster} (${isNaN(KD) ? 0 : KD.toFixed(2)} K/D). Grenades lobbed: ${data.stats.used.grenade} TNTs lit: ${data.stats.used.tnt}, Silver bullets fired: ${data.stats.shot.silverbullet}, diff --git a/src/lib/userHelper.ts b/src/lib/userHelper.ts index 9e57b3e..b838105 100644 --- a/src/lib/userHelper.ts +++ b/src/lib/userHelper.ts @@ -54,7 +54,7 @@ const BLASTERS = ['blaster', 'grenade', 'tnt'] /** Get the amount of times the user has (been) shot (by) another user * The 'blaster' data is all timeouts excluding silver bullets */ -async function getTimeouts(userId: string, monthdata?: string): Promise { +export async function getTimeouts(userId: string, monthdata?: string): Promise { let monthquery = '' if (monthdata) monthquery = ` && created~"${monthdata}"` const hit = await pb.collection('timeouts').getFullList({ filter: `target="${userId}"${monthquery}` })