Skip to content

Commit

Permalink
Roles task fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gc committed Aug 2, 2024
1 parent 21a3778 commit 13836a0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"p-queue": "^6.6.2",
"piscina": "^4.6.1",
"random-js": "^2.1.0",
"remeda": "^2.7.0",
"simple-statistics": "^7.8.3",
"sonic-boom": "^4.0.1",
"zlib-sync": "^0.1.9",
Expand Down
48 changes: 27 additions & 21 deletions src/lib/rolesTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import { Minigames } from '../lib/settings/minigames';

import { Prisma } from '@prisma/client';
import PQueue from 'p-queue';
import Skills from '../lib/skilling/skills';
import { partition } from 'remeda';
import z from 'zod';
import { type CommandResponse, Stopwatch, convertXPtoLVL, getUsernameSync, returnStringOrFile } from '../lib/util';
import { ClueTiers } from './clues/clueTiers';
import { TeamLoot } from './simulation/TeamLoot';
import { SkillsArray } from './skilling/types';
import type { ItemBank } from './types';
import { fetchMultipleCLLeaderboards } from './util/clLeaderboard';

interface RoleResult {
roleID: string;
userID: string;
reason: string;
badge?: (typeof BadgesEnum)[keyof typeof BadgesEnum];
}
const RoleResultSchema = z.object({
roleID: z.string().min(17).max(19),
userID: z.string().min(17).max(19),
reason: z.string(),
badge: z.number().int().optional()
});
type RoleResult = z.infer<typeof RoleResultSchema>;

const minigames = Minigames.map(game => game.column).filter(i => i !== 'tithe_farm');

Expand All @@ -45,24 +48,24 @@ for (const cl of CLS_THAT_GET_ROLE) {

async function topSkillers() {
const results: RoleResult[] = [];
const skillVals = Object.values(Skills);

const [top200TotalXPUsers, ...top200ms] = await prisma.$transaction([
prisma.$queryRawUnsafe<any>(
`SELECT id, ${skillVals.map(s => `"skills.${s.id}"`)}, ${skillVals
.map(s => `"skills.${s.id}"::bigint`)
.join(' + ')} as totalxp FROM users ORDER BY totalxp DESC LIMIT 200;`
`SELECT id, ${SkillsArray.map(s => `"skills.${s}"`)}, ${SkillsArray.map(s => `"skills.${s}"::bigint`).join(
' + '
)} as totalxp FROM users ORDER BY totalxp DESC LIMIT 200;`
),
...skillVals.map(s =>
prisma.$queryRawUnsafe<{
...SkillsArray.map(s => {
const query = `SELECT id, "skills.${s}" AS xp, '${s}' AS skill FROM users ORDER BY xp DESC LIMIT 1;`;
return prisma.$queryRawUnsafe<{
id: string;
xp: string;
skill: string;
}>(`SELECT id, "skills.${s.id}" AS xp, '${s.id}' AS skill FROM users ORDER BY xp DESC LIMIT 1;`)
)
}>(query);
})
]);

for (const { id, skill } of top200ms) {
for (const { id, skill } of top200ms.flat()) {
results.push({
userID: id,
roleID: Roles.TopSkiller,
Expand All @@ -74,8 +77,8 @@ async function topSkillers() {
const rankOneTotal = top200TotalXPUsers
.map((u: any) => {
let totalLevel = 0;
for (const skill of skillVals) {
totalLevel += convertXPtoLVL(Number(u[`skills.${skill.id}` as keyof any]) as any);
for (const skill of SkillsArray) {
totalLevel += convertXPtoLVL(Number(u[`skills.${skill}` as keyof any]) as any);
}
return {
id: u.id,
Expand Down Expand Up @@ -196,7 +199,7 @@ LIMIT 1;`
)
);

for (const res of topClueHunters) {
for (const res of topClueHunters.flat()) {
results.push({
userID: res.user_id,
roleID: Roles.TopClueHunter,
Expand Down Expand Up @@ -364,7 +367,11 @@ export async function runRolesTask(dryRun: boolean): Promise<CommandResponse> {
const stopwatch = new Stopwatch();
const res = await fn();
console.log(`[RolesTask] Ran ${name} in ${stopwatch.stop()}`);
results.push(...res);
const [validResults, invalidResults] = partition(res, i => RoleResultSchema.safeParse(i).success);
results.push(...validResults);
if (invalidResults.length > 0) {
console.error(`[RolesTask] Invalid results for ${name}: ${JSON.stringify(invalidResults)}`);
}
});
}

Expand All @@ -381,7 +388,6 @@ export async function runRolesTask(dryRun: boolean): Promise<CommandResponse> {
if (!supportServerGuild) throw new Error('No support guild');

// Remove all top badges from all users (and add back later)

debugLog('Removing badges...');
const badgeIDs = `ARRAY[${allBadgeIDs.join(',')}]`;
await prisma.$queryRawUnsafe(`
Expand Down
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3972,6 +3972,15 @@ __metadata:
languageName: node
linkType: hard

"remeda@npm:^2.7.0":
version: 2.7.0
resolution: "remeda@npm:2.7.0"
dependencies:
type-fest: "npm:^4.21.0"
checksum: 10c0/4e7d0dc616f00961653244ea9df3f297720fc9346ac8ec7502abf4c434741af4a4750d5bd83ea9938ee406089b37e3a2270b8f022d48b345ba83218e47dd8918
languageName: node
linkType: hard

"require-directory@npm:^2.1.1":
version: 2.1.1
resolution: "require-directory@npm:2.1.1"
Expand Down Expand Up @@ -4148,6 +4157,7 @@ __metadata:
prettier: "npm:^3.3.2"
prisma: "npm:^5.17.0"
random-js: "npm:^2.1.0"
remeda: "npm:^2.7.0"
simple-statistics: "npm:^7.8.3"
sonic-boom: "npm:^4.0.1"
tsx: "npm:^4.16.2"
Expand Down Expand Up @@ -4613,6 +4623,13 @@ __metadata:
languageName: node
linkType: hard

"type-fest@npm:^4.21.0":
version: 4.23.0
resolution: "type-fest@npm:4.23.0"
checksum: 10c0/c42bb14e99329ab37983d1f188e307bf0cc705a23807d9b2268d8fb2ae781d610ac6e2058dde8f9ea2b1b8ddc77ceb578d157fa81f69f8f70aef1d42fb002996
languageName: node
linkType: hard

"typescript@npm:^5.2.2, typescript@npm:^5.5.3":
version: 5.5.3
resolution: "typescript@npm:5.5.3"
Expand Down

0 comments on commit 13836a0

Please sign in to comment.