Skip to content

Commit

Permalink
Fixes some bugs in the user-event CL tracking (oldschoolgg#5801)
Browse files Browse the repository at this point in the history
* Fixes some bugs in the user-event CL tracking

* Fix test
  • Loading branch information
themrrobert authored Mar 27, 2024
1 parent 96e3e88 commit 07bdc50
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
27 changes: 23 additions & 4 deletions src/lib/data/Collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1286,20 +1286,37 @@ export function getPossibleOptions() {
return new AttachmentBuilder(Buffer.from(normalTable), { name: 'possible_logs.txt' });
}

export function getCollectionItems(collection: string, allItems = false, removeCoins = false): number[] {
export function getCollectionItems(
collection: string,
allItems: boolean,
removeCoins: boolean,
returnResolvedCl: boolean
): { resolvedCl: string; items: number[] };
export function getCollectionItems(collection: string, allItems?: boolean, removeCoins?: boolean): number[];
export function getCollectionItems(
collection: string,
allItems = false,
removeCoins = false,
returnResolvedCl?: boolean
): { resolvedCl: string; items: number[] } | number[] {
const returnValue = (clName: string, items: number[]) => {
return returnResolvedCl !== undefined ? { resolvedCl: clName.toLowerCase(), items } : items;
};
if (collection === 'overall+') {
return overallPlusItems;
return returnValue(collection, overallPlusItems);
}
if (['overall', 'all'].some(s => stringMatches(collection, s))) {
return allCLItemsFiltered;
return returnValue('overall', allCLItemsFiltered);
}

let _items: number[] = [];
let _clName: string = '';
loop: for (const [category, entries] of Object.entries(allCollectionLogs)) {
if (
stringMatches(category, collection) ||
(entries.alias && entries.alias.some(a => stringMatches(a, collection)))
) {
_clName = category;
_items = uniqueArr(
Object.values(entries.activities)
.map(e => [...new Set([...e.items, ...(allItems ? e.allItems ?? [] : [])])])
Expand All @@ -1313,6 +1330,7 @@ export function getCollectionItems(collection: string, allItems = false, removeC
stringMatches(activityName, collection) ||
(attributes.alias && attributes.alias.find(a => stringMatches(a, collection)))
) {
_clName = activityName;
_items = [
...new Set([...attributes.items, ...(allItems && attributes.allItems ? attributes.allItems : [])])
];
Expand All @@ -1326,11 +1344,12 @@ export function getCollectionItems(collection: string, allItems = false, removeC
[m.name, ...m.aliases].some(name => stringMatches(name, collection))
);
if (_monster) {
_clName = _monster.name;
_items = uniqueArr(Monsters.get(_monster!.id)!.allItems);
}
}
if (removeCoins && _items.includes(995)) _items = removeFromArr(_items, 995);
return _items;
return returnValue(_clName, _items);
}

function getUserClData(usarBank: ItemBank, clItems: number[]): [number, number] {
Expand Down
10 changes: 7 additions & 3 deletions src/lib/util/clLeaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export async function fetchCLLeaderboard({
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 (${userIdsList})
FROM user_stats s INNER JOIN users u ON s.user_id::text = u.id
WHERE ${ironmenOnly ? 'u."minion.ironman" = true AND' : ''} user_id::text IN (${userIdsList})
`)
: [];
const generalUsers = await prisma.$queryRawUnsafe<{ id: string; qty: number }[]>(`
Expand Down Expand Up @@ -77,7 +77,11 @@ 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' : ''})
${userIds.length > 0 ? `OR id in (${userIds.map(i => `'${i}'`).join(', ')})` : ''}
${
userIds.length > 0
? `OR (id in (${userIds.map(i => `'${i}'`).join(', ')})${ironmenOnly ? 'AND "minion.ironman" = true' : ''})`
: ''
}
) u
ORDER BY qty DESC
LIMIT ${resultLimit};
Expand Down
25 changes: 11 additions & 14 deletions src/mahoji/commands/leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ApplicationCommandOptionType, CommandRunOptions } from 'mahoji';

import { ClueTier, ClueTiers } from '../../lib/clues/clueTiers';
import { badges, badgesCache, Emoji, masteryKey, usernameCache } from '../../lib/constants';
import { allClNames, allCollectionLogsFlat, getCollectionItems } from '../../lib/data/Collections';
import { allClNames, getCollectionItems } from '../../lib/data/Collections';
import { effectiveMonsters } from '../../lib/minions/data/killableMonsters';
import { allOpenables } from '../../lib/openables';
import { Minigames } from '../../lib/settings/minigames';
Expand Down Expand Up @@ -270,23 +270,20 @@ async function clLb(
inputType: string,
ironmenOnly: boolean
) {
const items = getCollectionItems(inputType, false);
const { resolvedCl, items } = getCollectionItems(inputType, false, false, true);
if (!items || items.length === 0) {
return "That's not a valid collection log category. Check +cl for all possible logs.";
}

const clName = allCollectionLogsFlat.find(c => stringMatches(c.name, inputType))?.name;
const userEventOrders = clName
? await prisma.userEvent.findMany({
where: {
type: 'CLCompletion',
collection_log_name: clName.toLowerCase()
},
orderBy: {
date: 'asc'
}
})
: null;
const userEventOrders = await prisma.userEvent.findMany({
where: {
type: 'CLCompletion',
collection_log_name: resolvedCl.toLowerCase()
},
orderBy: {
date: 'asc'
}
});

const users = await fetchCLLeaderboard({ ironmenOnly, items, resultLimit: 200, userEvents: userEventOrders });
inputType = toTitleCase(inputType.toLowerCase());
Expand Down

0 comments on commit 07bdc50

Please sign in to comment.