Skip to content

Commit 1b533ec

Browse files
committed
feat: Add scoring for Hindi tours
1 parent e34894b commit 1b533ec

3 files changed

Lines changed: 67 additions & 0 deletions

File tree

src/ps/handlers/tours.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { PSRoomConfigs } from '@/cache';
2+
import { bulkAddPoints } from '@/database/points';
3+
import getSecretFunction from '@/secrets/functions';
4+
import { errorLog } from '@/utils/logger';
5+
6+
import type { Client } from 'ps-client';
7+
8+
export function tourHandler(this: Client, roomId: string, line: string, isIntro?: boolean): void {
9+
if (isIntro) return;
10+
11+
const [tourEventType] = line.lazySplit('|', 1);
12+
const room = this.getRoom(roomId);
13+
14+
if (roomId === 'hindi') {
15+
if (!tourEventType) return;
16+
if (tourEventType === 'battlestart') {
17+
const tourData = line.lazySplit('|', 3);
18+
this.joinRoom(tourData[3]).then(() => this.getRoom(tourData[3]).send("G'luck!\n/part"));
19+
} else if (tourEventType === 'update') {
20+
try {
21+
const tourData = line.lazySplit('|', 1);
22+
const json = JSON.parse(tourData[1]);
23+
if (json.generator !== 'Single Elimination') return;
24+
if (!json.bracketData) return;
25+
if (json.bracketData.type !== 'tree') return;
26+
if (!json.bracketData.rootNode) return;
27+
if (json.bracketData.rootNode.state === 'inprogress') {
28+
room.send(`/wall Tour finals! <<${json.bracketData.rootNode.room}>>`);
29+
}
30+
} catch (e) {
31+
if (e instanceof Error) errorLog(e);
32+
}
33+
} else if (tourEventType === 'end') {
34+
try {
35+
const [_tourEventType, tourBracketTree] = line.lazySplit('|', 1);
36+
const json = JSON.parse(tourBracketTree);
37+
if (/casual|ignore|no ?points/i.test(json.format || '')) return;
38+
39+
// The actual algorithm is secret
40+
// Nice try, though
41+
const scoringAlgo = getSecretFunction<(tourBracket: string) => Record<string, number> | null>(
42+
'hindiTourPointsAlgo',
43+
() => null
44+
);
45+
const pointsToAdd = scoringAlgo(tourBracketTree);
46+
47+
if (!pointsToAdd) return;
48+
49+
const pointsType = PSRoomConfigs[roomId]?.points?.priority[0];
50+
if (!pointsType) throw new Error(`AAAAAA someone ping PartMan for ${roomId}`);
51+
bulkAddPoints(
52+
Object.fromEntries(
53+
Object.entries(pointsToAdd).map(([user, points]) => [user, { id: user, points: { [pointsType]: points } }])
54+
),
55+
roomId
56+
).then(() => {
57+
// TODO: Run leaderboard
58+
});
59+
} catch (e) {
60+
if (e instanceof Error) errorLog(e);
61+
}
62+
}
63+
}
64+
}

src/ps/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ PS.on('name', (...args) => LivePS.nickHandler.call(PS, ...args));
2323
PS.on('leave', (...args) => LivePS.leaveHandler.call(PS, ...args));
2424
PS.on('notify', (...args) => LivePS.notifyHandler.call(PS, ...args));
2525
PS.on('raw', (...args) => LivePS.rawHandler.call(PS, ...args));
26+
PS.on('tour', (...args) => LivePS.tourHandler.call(PS, ...args));
2627

2728
if (IS_ENABLED.PS) startPSCron.bind(PS)();
2829

src/sentinel/live.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { joinHandler, leaveHandler, nickHandler } from '@/ps/handlers/joins';
1111
import { notifyHandler } from '@/ps/handlers/notifications';
1212
import { pageHandler } from '@/ps/handlers/page';
1313
import { rawHandler } from '@/ps/handlers/raw';
14+
import { tourHandler } from '@/ps/handlers/tours';
1415

1516
/**
1617
* Exports the 'live' versions of functions from Sentinel.
@@ -41,4 +42,5 @@ export const LivePS = {
4142
notifyHandler,
4243
pageHandler,
4344
rawHandler,
45+
tourHandler,
4446
};

0 commit comments

Comments
 (0)