|
| 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 | +} |
0 commit comments