11import { pokedex } from 'ps-client/data' ;
2+ import { uploadToPastie } from 'ps-client/tools' ;
23
3- import { PSCommands } from '@/cache' ;
4+ import { PSCommands , PSRoomConfigs } from '@/cache' ;
5+ import { queryPoints , resetPoints } from '@/database/points' ;
6+ import { HINDI_LOGS } from '@/discord/constants/servers/hindi' ;
7+ import { getChannel } from '@/discord/loaders/channels' ;
48import { i18n } from '@/i18n' ;
59import { TimeZone } from '@/ps/handlers/cron/constants' ;
10+ import { Logger } from '@/utils/logger' ;
611import { sample } from '@/utils/random' ;
712import { sleep } from '@/utils/sleep' ;
813
@@ -12,6 +17,15 @@ import type { RecursivePartial } from '@/types/common';
1217import type { PSMessage } from '@/types/ps' ;
1318import type { Client , User } from 'ps-client' ;
1419
20+ function hasPoints < Points extends number [ ] > ( user : Points , targetPoints : Points ) : boolean {
21+ for ( const [ index , targetPoint ] of targetPoints . entries ( ) ) {
22+ if ( user [ index ] > targetPoint ) return true ;
23+ if ( user [ index ] === targetPoint ) continue ;
24+ return false ;
25+ }
26+ return true ;
27+ }
28+
1529function _preGGSSEvent ( this : Client , eventType : string ) {
1630 const hindiRoom = this . getRoom ( 'hindi' ) ;
1731 const ggssRoom = this . getRoom ( 'galligallisimsim' ) ;
@@ -85,6 +99,45 @@ export function register(this: Client, Jobs: PSCronJobManager): void {
8599 room ?. send ( '/automodchat off' ) ;
86100 } ) ;
87101
102+ // Midnight on the 1st of every month
103+ Jobs . register ( 'hindi-monthly-lb-reset' , '0 0 1 * *' , TimeZone . IST , async ( ) => {
104+ const room = this . getRoom ( 'hindi' ) ;
105+ if ( ! room ) return ;
106+
107+ const roomConfig = PSRoomConfigs [ 'hindi' ] ?. points ;
108+ if ( ! roomConfig ) return ;
109+
110+ const leaderboard = await queryPoints ( 'hindi' , roomConfig . priority , Infinity ) ;
111+ if ( ! leaderboard ?. length ) return ;
112+
113+ const topThreeScore = roomConfig . priority . map ( type => leaderboard [ 2 ] . points [ type ] ) ;
114+ const hasSomePoints = topThreeScore . some ( v => v > 0 ) ;
115+ if ( ! hasSomePoints ) return ;
116+
117+ const topUsers = leaderboard . filter ( user =>
118+ hasPoints (
119+ roomConfig . priority . map ( type => user . points [ type ] ) ,
120+ topThreeScore
121+ )
122+ ) ;
123+
124+ room . send ( `/wall Congratulations to ${ topUsers . map ( u => u . name ) . list ( ) } for topping this month's leaderboard!` ) ;
125+
126+ const backupURL = await uploadToPastie ( JSON . stringify ( leaderboard , null , 2 ) ) ;
127+ Logger . log ( `Monthly leaderboard backup for hindi: ${ backupURL } ` ) ;
128+ room . send ( `/modnote Monthly leaderboard backup: ${ backupURL } ` ) ;
129+
130+ const channel = getChannel ( HINDI_LOGS ) ;
131+ if ( ! channel ) return ;
132+ channel . send (
133+ `Top users: ${ topUsers
134+ . map ( user => `${ user . name } (${ roomConfig . priority . map ( type => user . points [ type ] ) . join ( ', ' ) } )` )
135+ . list ( ) } . Monthly leaderboard backup: ${ backupURL } .`
136+ ) ;
137+
138+ await resetPoints ( 'hindi' , true ) ;
139+ } ) ;
140+
88141 // TODO: RkR, DkR
89142
90143 // Galli Galli Sim Sim
0 commit comments