mirror of
https://gitlab.com/qwerinope/qweribot.git
synced 2026-02-04 10:56:57 +01:00
add monthly and alltime stat commands
This commit is contained in:
28
src/commands/alltimestats.ts
Normal file
28
src/commands/alltimestats.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { Command, sendMessage } from "commands";
|
||||||
|
import { getTimeoutStats, getItemStats } from "lib/getStats";
|
||||||
|
import parseCommandArgs from "lib/parseCommandArgs";
|
||||||
|
import User from "user";
|
||||||
|
|
||||||
|
export default new Command('alltimestats', ['alltime', 'alltimestats'], 'chatter', async (msg, user) => {
|
||||||
|
const args = parseCommandArgs(msg.messageText);
|
||||||
|
let target: User | null = user;
|
||||||
|
if (args[0]) {
|
||||||
|
target = await User.initUsername(args[0]);
|
||||||
|
if (!target) { await sendMessage(`User ${args[0]} doesn't exist!`, msg.messageId); return; };
|
||||||
|
};
|
||||||
|
|
||||||
|
const [timeout, item] = await Promise.all([getTimeoutStats(target, false), getItemStats(target, false)]);
|
||||||
|
if (!timeout || !item) { await sendMessage(`ERROR: Something went wrong!`, msg.messageId); return; };
|
||||||
|
|
||||||
|
const KD = timeout.shot.blaster / timeout.hit.blaster;
|
||||||
|
|
||||||
|
await sendMessage(`
|
||||||
|
Alltime: stats of ${target.displayName}:
|
||||||
|
Users blasted: ${timeout.shot.blaster},
|
||||||
|
Blasted by others: ${timeout.hit.blaster} (${isNaN(KD) ? 0 : KD.toFixed(2)} K/D).
|
||||||
|
Grenades lobbed: ${item.grenade},
|
||||||
|
TNT exploded: ${item.tnt}.
|
||||||
|
Silver bullets fired: ${timeout.shot.silverbullet},
|
||||||
|
Silver bullets taken: ${timeout.hit.silverbullet}.
|
||||||
|
`, msg.messageId);
|
||||||
|
});
|
||||||
28
src/commands/monthlystats.ts
Normal file
28
src/commands/monthlystats.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { Command, sendMessage } from "commands";
|
||||||
|
import { getTimeoutStats, getItemStats } from "lib/getStats";
|
||||||
|
import parseCommandArgs from "lib/parseCommandArgs";
|
||||||
|
import User from "user";
|
||||||
|
|
||||||
|
export default new Command('monthlystats', ['stats', 'monthlystats'], 'chatter', async (msg, user) => {
|
||||||
|
const args = parseCommandArgs(msg.messageText);
|
||||||
|
let target: User | null = user;
|
||||||
|
if (args[0]) {
|
||||||
|
target = await User.initUsername(args[0]);
|
||||||
|
if (!target) { await sendMessage(`User ${args[0]} doesn't exist!`, msg.messageId); return; };
|
||||||
|
};
|
||||||
|
|
||||||
|
const [timeout, item] = await Promise.all([getTimeoutStats(target, true), getItemStats(target, true)]);
|
||||||
|
if (!timeout || !item) { await sendMessage(`ERROR: Something went wrong!`, msg.messageId); return; };
|
||||||
|
|
||||||
|
const KD = timeout.shot.blaster / timeout.hit.blaster;
|
||||||
|
|
||||||
|
await sendMessage(`
|
||||||
|
This month: stats of ${target.displayName}:
|
||||||
|
Users blasted: ${timeout.shot.blaster},
|
||||||
|
Blasted by others: ${timeout.hit.blaster} (${isNaN(KD) ? 0 : KD.toFixed(2)} K/D).
|
||||||
|
Grenades lobbed: ${item.grenade},
|
||||||
|
TNT exploded: ${item.tnt}.
|
||||||
|
Silver bullets fired: ${timeout.shot.silverbullet},
|
||||||
|
Silver bullets taken: ${timeout.hit.silverbullet}.
|
||||||
|
`, msg.messageId);
|
||||||
|
});
|
||||||
@@ -11,3 +11,29 @@ export async function createTimeoutRecord(user: User, target: User, item: string
|
|||||||
logger.err(err as string);
|
logger.err(err as string);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export async function getTimeoutsAsUser(user: User, monthData?: string) {
|
||||||
|
try {
|
||||||
|
const monthquery = monthData ? ` && created~"${monthData}"` : '';
|
||||||
|
const data = await pb.getFullList({
|
||||||
|
filter: `user="${user.id}"${monthquery}`
|
||||||
|
});
|
||||||
|
return data;
|
||||||
|
} catch (e) {
|
||||||
|
logger.err(`Failed to get timeouts as user: ${user.id}, month: ${monthData}`);
|
||||||
|
logger.err(e as string);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function getTimeoutsAsTarget(user: User, monthData?: string) {
|
||||||
|
try {
|
||||||
|
const monthquery = monthData ? ` && created~"${monthData}"` : '';
|
||||||
|
const data = await pb.getFullList({
|
||||||
|
filter: `target="${user.id}"${monthquery}`
|
||||||
|
});
|
||||||
|
return data;
|
||||||
|
} catch (e) {
|
||||||
|
logger.err(`Failed to get timeouts as target: ${user.id}, month: ${monthData}`);
|
||||||
|
logger.err(e as string);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|||||||
@@ -11,3 +11,17 @@ export async function createUsedItemRecord(user: User, item: string): Promise<vo
|
|||||||
logger.err(err as string);
|
logger.err(err as string);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export async function getItemsUsed(user: User, monthData?: string) {
|
||||||
|
try {
|
||||||
|
const monthquery = monthData ? ` && created~"${monthData}"` : '';
|
||||||
|
const data = await pb.getFullList({
|
||||||
|
filter: `user="${user.id}"${monthquery}`
|
||||||
|
});
|
||||||
|
return data;
|
||||||
|
} catch (e) {
|
||||||
|
logger.err(`Failed to get items used for user: ${user.id}, month: ${monthData}`);
|
||||||
|
logger.err(e as string);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|||||||
45
src/lib/getStats.ts
Normal file
45
src/lib/getStats.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import { getTimeoutsAsTarget, getTimeoutsAsUser } from "db/dbTimeouts";
|
||||||
|
import { getItemsUsed } from "db/dbUsedItems";
|
||||||
|
import type { inventory } from "items";
|
||||||
|
import type User from "user";
|
||||||
|
|
||||||
|
export async function getTimeoutStats(target: User, thismonth: boolean) {
|
||||||
|
const monthdata = thismonth ? new Date().toISOString().slice(0, 7) : undefined;
|
||||||
|
|
||||||
|
const [shot, hit] = await Promise.all([
|
||||||
|
getTimeoutsAsUser(target, monthdata),
|
||||||
|
getTimeoutsAsTarget(target, monthdata)
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!shot || !hit) return;
|
||||||
|
|
||||||
|
const blasterhit = hit.filter(item => item.item !== 'silverbullet').length;
|
||||||
|
const silverbullethit = hit.length - blasterhit;
|
||||||
|
const blastershot = shot.filter(item => item.item !== 'silverbullet').length;
|
||||||
|
const silverbulletshot = shot.length - blastershot;
|
||||||
|
|
||||||
|
return {
|
||||||
|
hit: {
|
||||||
|
blaster: blasterhit,
|
||||||
|
silverbullet: silverbullethit
|
||||||
|
},
|
||||||
|
shot: {
|
||||||
|
blaster: blastershot,
|
||||||
|
silverbullet: silverbulletshot
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function getItemStats(target: User, thismonth: boolean) {
|
||||||
|
const monthdata = thismonth ? new Date().toISOString().slice(0, 7) : undefined;
|
||||||
|
|
||||||
|
const data = await getItemsUsed(target, monthdata);
|
||||||
|
if (!data) return;
|
||||||
|
|
||||||
|
const returnObj: inventory = {
|
||||||
|
grenade: data.filter(use => use.item === 'grenade').length,
|
||||||
|
tnt: data.filter(use => use.item === 'tnt').length
|
||||||
|
};
|
||||||
|
|
||||||
|
return returnObj;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user