Skip to content

Commit

Permalink
Merge branch 'master' into bso
Browse files Browse the repository at this point in the history
  • Loading branch information
gc committed Feb 26, 2024
2 parents 1414e66 + 894281d commit d3fe14d
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 48 deletions.
6 changes: 3 additions & 3 deletions src/lib/addXP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { sendToChannelID } from './util/webhook';

const skillsVals = Object.values(Skills);
const maxFilter = skillsVals.map(s => `"skills.${s.id}" >= ${LEVEL_120_XP}`).join(' AND ');
const makeQuery = (ironman: boolean) => `SELECT count(id)
const makeQuery = (ironman: boolean) => `SELECT count(id)::int
FROM users
WHERE ${maxFilter}
${ironman ? 'AND "minion.ironman" = true' : ''};`;
Expand Down Expand Up @@ -323,15 +323,15 @@ export async function addXP(user: MUser, params: AddXpParams): Promise<string> {
{
count: string;
}[]
>(`SELECT COUNT(*) FROM users WHERE "skills.${params.skillName}" >= ${queryValue};`);
>(`SELECT COUNT(*)::int FROM users WHERE "skills.${params.skillName}" >= ${queryValue};`);
resultStr = resultStr.replace('{nthUser}', formatOrdinal(Number(nthUser.count) + 1));
if (user.isIronman) {
const [nthIron] = await prisma.$queryRawUnsafe<
{
count: string;
}[]
>(
`SELECT COUNT(*) FROM users WHERE "minion.ironman" = true AND "skills.${params.skillName}" >= ${queryValue};`
`SELECT COUNT(*)::int FROM users WHERE "minion.ironman" = true AND "skills.${params.skillName}" >= ${queryValue};`
);
resultStr = resultStr.replace('{nthIron}', formatOrdinal(Number(nthIron.count) + 1));
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ export async function analyticsTick() {
const [numberOfMinions, totalSacrificed, numberOfIronmen, totalGP] = (
await Promise.all(
[
'SELECT COUNT(*) FROM users WHERE "minion.hasBought" = true;',
'SELECT SUM ("sacrificedValue") AS count FROM users;',
'SELECT COUNT(*) FROM users WHERE "minion.ironman" = true;',
'SELECT SUM ("GP") AS count FROM users;'
'SELECT COUNT(*)::int FROM users WHERE "minion.hasBought" = true;',
'SELECT SUM("sacrificedValue")::int AS count FROM users;',
'SELECT COUNT(*)::int FROM users WHERE "minion.ironman" = true;',
'SELECT SUM("GP") AS count FROM users;'
].map(query => prisma.$queryRawUnsafe(query))
)
).map((result: any) => parseInt(result[0].count)) as number[];
Expand Down
2 changes: 1 addition & 1 deletion src/lib/roboChimp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export async function roboChimpUserFetch(userID: string) {

export async function calculateOwnCLRanking(userID: string) {
const clPercentRank = (
await roboChimpClient.$queryRaw<{ count: number }[]>`SELECT COUNT(*)
await roboChimpClient.$queryRaw<{ count: number }[]>`SELECT COUNT(*)::int
FROM public.user
WHERE osb_cl_percent >= (SELECT osb_cl_percent FROM public.user WHERE id = ${BigInt(userID)});`
)[0].count;
Expand Down
12 changes: 7 additions & 5 deletions src/lib/rolesTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ LIMIT 1;`;
const mostSlayerTasksDoneQuery = `SELECT user_id::text as id, 'Most Tasks' as desc
FROM slayer_tasks
GROUP BY user_id
ORDER BY count(user_id) DESC
ORDER BY count(user_id)::int DESC
LIMIT 1;`;

async function addRoles({
Expand Down Expand Up @@ -290,12 +290,12 @@ SELECT id, (cardinality(u.cl_keys) - u.inverse_length) as qty
addToUserMap(userMap, mostValueIronman[0].id, 'Rank 1 Ironman Sacrificed Value');

const mostUniques = await q<any[]>(`SELECT u.id, u.sacbanklength FROM (
SELECT (SELECT COUNT(*) FROM JSONB_OBJECT_KEYS("sacrificed_bank")) sacbanklength, user_id::text as id FROM user_stats
SELECT (SELECT COUNT(*)::int FROM JSONB_OBJECT_KEYS("sacrificed_bank")) sacbanklength, user_id::text as id FROM user_stats
) u
ORDER BY u.sacbanklength DESC LIMIT 1;`);

const mostUniquesIron = await q<any[]>(`SELECT u.id, u.sacbanklength FROM (
SELECT (SELECT COUNT(*) FROM JSONB_OBJECT_KEYS("sacrificed_bank")) sacbanklength, user_id::text as id FROM user_stats
SELECT (SELECT COUNT(*)::int FROM JSONB_OBJECT_KEYS("sacrificed_bank")) sacbanklength, user_id::text as id FROM user_stats
INNER JOIN users ON "user_stats"."user_id"::text = "users"."id"
WHERE "users"."minion.ironman" = true
) u
Expand Down Expand Up @@ -391,7 +391,7 @@ LIMIT 2;`,
FROM activity
WHERE type = 'Farming'
GROUP BY user_id
ORDER BY count(user_id) DESC
ORDER BY count(user_id)::int DESC
LIMIT 2;`,
`SELECT user_id::text as id, 'Top 2 Tithe Farm' as desc
FROM user_stats
Expand Down Expand Up @@ -448,7 +448,9 @@ LIMIT 2;`
'982989775399174184',
'346304390858145792'
];
const res = await prisma.$queryRaw<{ user_id: string; qty: number }[]>`SELECT user_id, COUNT(user_id) AS qty
const res = await prisma.$queryRaw<
{ user_id: string; qty: number }[]
>`SELECT user_id, COUNT(user_id)::int AS qty
FROM giveaway
WHERE channel_id IN (${Prisma.join(GIVEAWAY_CHANNELS)})
AND user_id NOT IN ('157797566833098752')
Expand Down
2 changes: 1 addition & 1 deletion src/lib/settings/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function convertStoredActivityToFlatActivity(activity: Activity): Activit
* ⚠️ Uses queryRawUnsafe
*/
export async function countUsersWithItemInCl(itemID: number, ironmenOnly: boolean) {
const query = `SELECT COUNT(id)
const query = `SELECT COUNT(id)::int
FROM users
WHERE ("collectionLogBank"->>'${itemID}') IS NOT NULL
AND ("collectionLogBank"->>'${itemID}')::int >= 1
Expand Down
2 changes: 1 addition & 1 deletion src/lib/slayer/slayerUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ export function filterLootReplace(myBank: Bank, myLoot: Bank) {

export async function getSlayerTaskStats(userID: string) {
const result: { monster_id: number; total_quantity: number; qty: number }[] =
await prisma.$queryRaw`SELECT monster_id, SUM(quantity) AS total_quantity, COUNT(monster_id) AS qty
await prisma.$queryRaw`SELECT monster_id, SUM(quantity)::int AS total_quantity, COUNT(monster_id)::int AS qty
FROM slayer_tasks
WHERE user_id = ${userID}
AND quantity_remaining = 0
Expand Down
16 changes: 8 additions & 8 deletions src/mahoji/commands/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ async function unsafeEval({ userID, code }: { userID: string; code: string }) {
async function allEquippedPets() {
const pets = await prisma.$queryRawUnsafe<
{ pet: number; qty: number }[]
>(`SELECT "minion.equippedPet" AS pet, COUNT("minion.equippedPet") AS qty
>(`SELECT "minion.equippedPet" AS pet, COUNT("minion.equippedPet")::int AS qty
FROM users
WHERE "minion.equippedPet" IS NOT NULL
GROUP BY "minion.equippedPet"
Expand Down Expand Up @@ -322,7 +322,7 @@ AND ("gear.melee" IS NOT NULL OR
>(`SELECT (blowpipe->>'scales')::int AS scales, (blowpipe->>'dartID')::int AS dart, (blowpipe->>'dartQuantity')::int AS qty
FROM users
WHERE blowpipe iS NOT NULL and (blowpipe->>'dartQuantity')::int != 0;`),
prisma.$queryRawUnsafe<{ sum: number }[]>('SELECT SUM("GP") FROM users;'),
prisma.$queryRawUnsafe<{ sum: number }[]>('SELECT SUM("GP")::int FROM users;'),
prisma.$queryRawUnsafe<{ banks: ItemBank }[]>(`SELECT
json_object_agg(itemID, itemQTY)::jsonb as banks
from (
Expand Down Expand Up @@ -362,7 +362,7 @@ WHERE blowpipe iS NOT NULL and (blowpipe->>'dartQuantity')::int != 0;`),
name: 'Most Active',
run: async () => {
const res = await prisma.$queryRawUnsafe<{ num: number; username: string }[]>(`
SELECT sum(duration) as num, "new_user"."username", user_id
SELECT sum(duration)::int as num, "new_user"."username", user_id
FROM activity
INNER JOIN "new_users" "new_user" on "new_user"."id" = "activity"."user_id"::text
WHERE start_date > now() - interval '2 days'
Expand Down Expand Up @@ -442,11 +442,11 @@ The next buy limit reset is at: ${buyLimitInterval.nextResetStr}, it resets ever
run: async () => {
const result = await prisma.$queryRawUnsafe<{ item_id: string; total_gp_spent: number }[]>(`SELECT
key AS item_id,
sum((cost_gp / total_items) * value::integer) AS total_gp_spent
sum((cost_gp / total_items) * value::integer)::int AS total_gp_spent
FROM
buy_command_transaction,
json_each_text(loot_bank),
(SELECT id, sum(value::integer) as total_items FROM buy_command_transaction, json_each_text(loot_bank) GROUP BY id) subquery
(SELECT id, sum(value::integer)::int as total_items FROM buy_command_transaction, json_each_text(loot_bank) GROUP BY id) subquery
WHERE
buy_command_transaction.id = subquery.id
GROUP BY
Expand Down Expand Up @@ -474,7 +474,7 @@ LIMIT
run: async () => {
const result = await prisma.$queryRawUnsafe<
{ item_id: number; gp: number }[]
>(`select item_id, sum(gp_received) as gp
>(`select item_id, sum(gp_received)::int as gp
from bot_item_sell
group by item_id
order by gp desc
Expand All @@ -483,7 +483,7 @@ limit 80;

const totalGPGivenOut = await prisma.$queryRawUnsafe<
{ total_gp_given_out: number }[]
>(`select sum(gp_received) as total_gp_given_out
>(`select sum(gp_received)::int as total_gp_given_out
from bot_item_sell;`);

return {
Expand Down Expand Up @@ -512,7 +512,7 @@ from bot_item_sell;`);
name: 'Max G.E Slot users',
run: async () => {
const res = await prisma.$queryRawUnsafe<{ user_id: string; slots_used: number }[]>(`
SELECT user_id, COUNT(*) AS slots_used
SELECT user_id, COUNT(*)::int AS slots_used
FROM ge_listing
WHERE cancelled_at IS NULL AND fulfilled_at IS NULL
GROUP BY user_id
Expand Down
2 changes: 1 addition & 1 deletion src/mahoji/commands/ge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ The next buy limit reset is at: ${GrandExchange.getInterval().nextResetStr}, it
>(`SELECT
DATE_TRUNC('week', sellTransactions.created_at) AS week,
AVG(sellTransactions.price_per_item_before_tax) AS average_price_per_item_before_tax,
SUM(sellTransactions.quantity_bought) AS total_quantity_bought
SUM(sellTransactions.quantity_bought)::int AS total_quantity_bought
FROM
ge_listing
INNER JOIN
Expand Down
2 changes: 1 addition & 1 deletion src/mahoji/commands/leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ async function sacrificeLb(
const mostUniques: { id: string; sacbanklength: number }[] = await prisma.$queryRawUnsafe(
`SELECT u.user_id::text AS id, u.sacbanklength
FROM (
SELECT (SELECT COUNT(*) FROM JSONB_OBJECT_KEYS(sacrificed_bank)) sacbanklength, user_id FROM user_stats
SELECT (SELECT COUNT(*)::int FROM JSONB_OBJECT_KEYS(sacrificed_bank)) sacbanklength, user_id FROM user_stats
${ironmanOnly ? 'INNER JOIN users ON users.id::bigint = user_stats.user_id WHERE "minion.ironman" = true' : ''}
) u
ORDER BY u.sacbanklength DESC LIMIT 10;
Expand Down
2 changes: 1 addition & 1 deletion src/mahoji/commands/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ GROUP BY type
ORDER BY qty DESC
LIMIT 15;`),
prisma.$queryRawUnsafe(`
SELECT sum(duration)
SELECT sum(duration)::int
FROM activity
WHERE user_id = ${id}
${whereInMassClause(id)};`)
Expand Down
2 changes: 1 addition & 1 deletion src/mahoji/lib/abstracted_commands/infernoCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function baseDuration(_attempts: number, isEmergedZuk: boolean) {
async function timesMadeToZuk(userID: string) {
const timesMadeToZuk = Number(
(
await prisma.$queryRawUnsafe<any>(`SELECT COUNT(*)
await prisma.$queryRawUnsafe<any>(`SELECT COUNT(*)::int
FROM activity
WHERE type = 'Inferno'
AND user_id = ${userID}
Expand Down
Loading

0 comments on commit d3fe14d

Please sign in to comment.