Skip to content

Commit

Permalink
Leaderboard fixes and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gc committed Mar 19, 2024
1 parent 4f0542b commit 71c2168
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
20 changes: 11 additions & 9 deletions src/lib/util/clLeaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@ export async function fetchCLLeaderboard({
const userEventMap = userEventsToMap(userEvents);
const userIds = Array.from(userEventMap.keys());
if (method === 'cl_array') {
const [specificUsers, generalUsers] = await prisma.$transaction([
prisma.$queryRawUnsafe<{ id: string; qty: number }[]>(`
const userIdsList = userIds.length > 0 ? userIds.map(i => `'${i}'`).join(', ') : 'NULL';
const specificUsers =
userIds.length > 0
? await prisma.$queryRawUnsafe<{ id: string; qty: number }[]>(`
SELECT user_id::text AS id, CARDINALITY(cl_array) - CARDINALITY(cl_array - array[${items
.map(i => `${i}`)
.join(', ')}]) AS qty
FROM user_stats
WHERE user_id::text in (${userIds.map(i => `'${i}'`).join(', ')})
`),
prisma.$queryRawUnsafe<{ id: string; qty: number }[]>(`
WHERE user_id::text IN (${userIdsList})
`)
: [];
const generalUsers = await prisma.$queryRawUnsafe<{ id: string; qty: number }[]>(`
SELECT user_id::text AS id, CARDINALITY(cl_array) - CARDINALITY(cl_array - array[${items
.map(i => `${i}`)
.join(', ')}]) AS qty
FROM user_stats
${ironmenOnly ? 'INNER JOIN "users" on "users"."id" = "user_stats"."user_id"::text' : ''}
WHERE (cl_array && array[${items.map(i => `${i}`).join(', ')}]
${ironmenOnly ? 'AND "users"."minion.ironman" = true' : ''})
AND user_id::text NOT IN (${userIds.map(i => `'${i}'`).join(', ')})
${userIds.length > 0 ? `AND user_id::text NOT IN (${userIdsList})` : ''}
ORDER BY qty DESC
LIMIT ${resultLimit}
`)
]);
`);

const users = [...specificUsers, ...generalUsers]
.sort((a, b) => {
Expand Down Expand Up @@ -75,7 +77,7 @@ SELECT id, (cardinality(u.cl_keys) - u.inverse_length) as qty
FROM users
WHERE ("collectionLogBank" ?| array[${items.map(i => `'${i}'`).join(', ')}]
${ironmenOnly ? 'AND "minion.ironman" = true' : ''})
OR user_id::text in (${userIds.map(i => `'${i}'`).join(', ')})
${userIds.length > 0 ? `OR id in (${userIds.map(i => `'${i}'`).join(', ')})` : ''}
) u
ORDER BY qty DESC
LIMIT ${resultLimit};
Expand Down
20 changes: 19 additions & 1 deletion src/mahoji/commands/testpotato.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
import getOSItem from '../../lib/util/getOSItem';
import { logError } from '../../lib/util/logError';
import { parseStringBank } from '../../lib/util/parseStringBank';
import { userEventToStr } from '../../lib/util/userEvents';
import { getPOH } from '../lib/abstracted_commands/pohCommand';
import { MAX_QP } from '../lib/abstracted_commands/questCommand';
import { allUsableItems } from '../lib/abstracted_commands/useCommand';
Expand Down Expand Up @@ -550,6 +551,12 @@ export const testPotatoCommand: OSBMahojiCommand | null = production
max_value: 1000
}
]
},
{
type: ApplicationCommandOptionType.Subcommand,
name: 'events',
description: 'See events',
options: []
}
],
run: async ({
Expand All @@ -571,13 +578,24 @@ export const testPotatoCommand: OSBMahojiCommand | null = production
check?: { monster_droprates?: string };
bingo_tools?: { start_bingo: string };
setslayertask?: { master: string; monster: string; quantity: number };
events?: {};
}>) => {
if (production) {
logError('Test command ran in production', { userID: userID.toString() });
return 'This will never happen...';
}
const user = await mUserFetch(userID.toString());

if (options.events) {
const events = await prisma.userEvent.findMany({
where: {
user_id: user.id
},
orderBy: {
date: 'desc'
}
});
return events.map(userEventToStr).join('\n');
}
if (options.bingo_tools) {
if (options.bingo_tools.start_bingo) {
const bingo = await prisma.bingo.findFirst({
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/misc.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { UserEvent } from '@prisma/client';
import { randArrItem } from 'e';
import { describe, expect, test } from 'vitest';

import { analyticsTick } from '../../src/lib/analytics';
import { allCollectionLogsFlat } from '../../src/lib/data/Collections';
import { fetchCLLeaderboard } from '../../src/lib/util/clLeaderboard';
import { minionStatsEmbed } from '../../src/lib/util/minionStatsEmbed';
import { mockClient } from './util';

Expand All @@ -13,4 +17,32 @@ describe('Integration Misc', () => {
await analyticsTick();
expect(await global.prisma!.analytic.count()).toBeGreaterThanOrEqual(1);
});
test('fetchCLLeaderboard', async () => {
for (const ironManOnly of [true, false]) {
for (const method of ['cl_array', 'raw_cl'] as const) {
for (const userEvents of [
[
{
id: 'asdf',
date: new Date(),
user_id: '123',
type: 'CLCompletion',
skill: null,
collection_log_name: 'giant mole'
} as UserEvent
],
null
]) {
await fetchCLLeaderboard({
ironmenOnly: ironManOnly,
method,
userEvents,
resultLimit: 100,
items: randArrItem(allCollectionLogsFlat).items
});
}
}
}
await Promise.all([fetchCLLeaderboard]);
});
});
6 changes: 6 additions & 0 deletions tests/integration/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ beforeEach(async () => {
afterEach(async () => {
await prisma.$disconnect();
});

async function init() {
await prisma.$queryRaw`CREATE EXTENSION IF NOT EXISTS intarray;`;
}

init();

0 comments on commit 71c2168

Please sign in to comment.