add leaderboard command

This commit is contained in:
2025-04-14 17:03:10 +02:00
parent fc8cc82478
commit adb0419aaf
4 changed files with 27 additions and 4 deletions

View File

@@ -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]

View File

@@ -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'] })

View File

@@ -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},

View File

@@ -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<timeoutsGetResult> {
export async function getTimeouts(userId: string, monthdata?: string): Promise<timeoutsGetResult> {
let monthquery = ''
if (monthdata) monthquery = ` && created~"${monthdata}"`
const hit = await pb.collection('timeouts').getFullList({ filter: `target="${userId}"${monthquery}` })