diff --git a/src/commands/index.ts b/src/commands/index.ts index 1a8edf7..61682a1 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -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] diff --git a/src/commands/stats.ts b/src/commands/stats.ts index 72b4d08..5eb3de3 100644 --- a/src/commands/stats.ts +++ b/src/commands/stats.ts @@ -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] diff --git a/src/lib/userHelper.ts b/src/lib/userHelper.ts index cb51400..399ff38 100644 --- a/src/lib/userHelper.ts +++ b/src/lib/userHelper.ts @@ -62,9 +62,11 @@ interface timeoutsGetResult { const BLASTERS = ['blaster', 'grenade', 'tnt'] -async function getTimeouts(userDBID: string): Promise { - 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 { + 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 { } } -async function getItemUses(userDBID: string): Promise { - const items = await pb.collection('itemuses').getFullList({ filter: `user="${userDBID}"` }) +async function getItemUses(userDBID: string, monthdata?: string): Promise { + 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 { +export async function getStats(user: HelixUser, monthdata?: string): Promise { 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 } }