monthly stats implemented

This commit is contained in:
2025-04-06 22:56:59 +02:00
parent 9bba99dd53
commit 6215d3ee80
3 changed files with 49 additions and 14 deletions

View File

@@ -1,13 +1,13 @@
import timeout from "./timeout";
import inventory from "./inventory";
import stats from "./stats";
import qbucks from "./qbucks";
import getloot from "./getloot";
import modme from "./modme";
import use from "./use";
import iteminfo from "./iteminfo"
import aliases from './itemAliases'
import admin from './admin'
import stats from "./stats"
import aliases from "./itemAliases"
import admin from "./admin"
export default [timeout, inventory, stats, qbucks, getloot, modme, use, iteminfo, ...aliases, ...admin]
export default [timeout, inventory, qbucks, getloot, modme, use, iteminfo, ...aliases, ...admin, ...stats]

View File

@@ -3,7 +3,36 @@ import api from "../lib/api";
import { getStats } from "../lib/userHelper";
import { HelixUser } from "@twurple/api";
export default createBotCommand('stats', async (params, { say, userName }) => {
const stats = createBotCommand('stats', async (params, { say, userName }) => {
let user: HelixUser | null
if (params.length !== 0) {
user = await api.users.getUserByName(params[0].replace(/[@]/g, ''))
} else user = await api.users.getUserByName(userName)
if (!user) {
await say(`User ${params[0]} not found`)
return
}
const monthdata = new Date().toISOString().slice(0, 7)
const data = params.length === 0 ? { me: true, stats: await getStats(user!, monthdata) } : { me: false, stats: await getStats(user!, monthdata) }
const KD = data.stats.shot.blaster / data.stats.hit.blaster
await say(
`
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).
Grenades lobbed: ${data.stats.used.grenade}
TNTs lit: ${data.stats.used.tnt},
Silver bullets fired: ${data.stats.shot.silverbullet},
Silver bullets taken: ${data.stats.hit.silverbullet}
`
)
})
const alltime = createBotCommand('alltime', async (params, { say, userName }) => {
let user: HelixUser | null
if (params.length !== 0) {
user = await api.users.getUserByName(params[0].replace(/[@]/g, ''))
@@ -19,7 +48,7 @@ export default createBotCommand('stats', async (params, { say, userName }) => {
await say(
`
Stats of ${data.me ? userName : params[0]}:
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).
Grenades lobbed: ${data.stats.used.grenade}
@@ -29,3 +58,5 @@ export default createBotCommand('stats', async (params, { say, userName }) => {
`
)
})
export default [stats, alltime]

View File

@@ -62,9 +62,11 @@ interface timeoutsGetResult {
const BLASTERS = ['blaster', 'grenade', 'tnt']
async function getTimeouts(userDBID: string): Promise<timeoutsGetResult> {
const hit = await pb.collection('timeouts').getFullList({ filter: `target="${userDBID}"` })
const shot = await pb.collection('timeouts').getFullList({ filter: `attacker="${userDBID}"` })
async function getTimeouts(userDBID: string, monthdata?: string): Promise<timeoutsGetResult> {
let monthquery = ''
if (monthdata) monthquery = ` && created~"${monthdata}"`
const hit = await pb.collection('timeouts').getFullList({ filter: `target="${userDBID}"${monthquery}` })
const shot = await pb.collection('timeouts').getFullList({ filter: `attacker="${userDBID}"${monthquery}` })
const blasterhit = hit.filter((item) => BLASTERS.includes(item.source)).length
const silverbullethit = hit.length - blasterhit
@@ -83,8 +85,10 @@ async function getTimeouts(userDBID: string): Promise<timeoutsGetResult> {
}
}
async function getItemUses(userDBID: string): Promise<inventory> {
const items = await pb.collection('itemuses').getFullList({ filter: `user="${userDBID}"` })
async function getItemUses(userDBID: string, monthdata?: string): Promise<inventory> {
let monthquery = ''
if (monthdata) monthquery = ` && created~"${monthdata}"`
const items = await pb.collection('itemuses').getFullList({ filter: `user="${userDBID}"${monthquery}` })
return {
version: 1,
blaster: items.filter((item) => item.name === 'blaster').length,
@@ -118,11 +122,11 @@ interface statsGetResult extends timeoutsGetResult {
used: inventory
}
export async function getStats(user: HelixUser): Promise<statsGetResult> {
export async function getStats(user: HelixUser, monthdata?: string): Promise<statsGetResult> {
await DBValidation(user)
const userDBID = await getDBID(user)
const { hit, shot } = await getTimeouts(userDBID)
const uses = await getItemUses(userDBID)
const { hit, shot } = await getTimeouts(userDBID, monthdata)
const uses = await getItemUses(userDBID, monthdata)
return { hit, shot, used: uses }
}