File size: 12,988 Bytes
5c2ed06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
 * @author mia-pi-git
 */

import { FS, Net, Utils } from '../../lib';

export const SEASONS_PER_YEAR = 4;
export const FORMATS_PER_SEASON = 4;
export const BADGE_THRESHOLDS: Record<string, number> = {
	gold: 3,
	silver: 30,
	bronze: 100,
};
export const FIXED_FORMATS = ['randombattle', 'ou'];
export const FORMAT_POOL = ['ubers', 'uu', 'ru', 'nu', 'pu', 'lc', 'doublesou', 'monotype'];
export const PUBLIC_PHASE_LENGTH = 3;

interface SeasonData {
	current: { period: number, year: number, formatsGeneratedAt: number, season: number };
	badgeholders: { [period: string]: { [format: string]: { [badgeType: string]: string[] } } };
	formatSchedule: Record<string, string[]>;
}

export let data: SeasonData;

try {
	data = JSON.parse(FS('config/chat-plugins/seasons.json').readSync());
} catch {
	data = {
		// force a reroll
		current: { season: null!, year: null!, formatsGeneratedAt: null!, period: null! },
		formatSchedule: {},
		badgeholders: {},
	};
}

export function getBadges(user: User, curFormat: string) {
	let userBadges: { type: string, format: string }[] = [];
	const season = data.current.season; // don't factor in old badges
	for (const format in data.badgeholders[season]) {
		const badges = data.badgeholders[season][format];
		for (const type in badges) {
			if (badges[type].includes(user.id)) {
				// ex badge-bronze-gen9ou-250-1-2024
				userBadges.push({ type, format });
			}
		}
	}
	// find which ones we should prioritize showing - badge of current tier/season, then top badges of other formats for this season
	let curFormatBadge;
	for (const [i, badge] of userBadges.entries()) {
		if (badge.format === curFormat) {
			userBadges.splice(i);
			curFormatBadge = badge;
		}
	}
	// now - sort by highest levels
	userBadges = Utils.sortBy(userBadges, x => Object.keys(BADGE_THRESHOLDS).indexOf(x.type))
		.slice(0, 2);
	if (curFormatBadge) userBadges.unshift(curFormatBadge);
	// format and return
	return userBadges;
}

function getUserHTML(user: User, format: string) {
	const buf = `<username>${user.name}</username>`;
	const badgeType = getBadges(user, format).find(x => x.format === format)?.type;
	if (badgeType) {
		let formatType = format.split(/gen\d+/)[1];
		if (!['ou', 'randombattle'].includes(formatType)) formatType = 'rotating';
		return `<img src="https://${Config.routes.client}/sprites/misc/${formatType}_${badgeType}.png" />` + buf;
	}
	return buf;
}

export function setFormatSchedule() {
	// guard heavily against this being overwritten
	if (data.current.formatsGeneratedAt === getYear()) return;
	data.current.formatsGeneratedAt = getYear();
	const formats = generateFormatSchedule();
	for (const [i, formatList] of formats.entries()) {
		data.formatSchedule[i + 1] = FIXED_FORMATS.concat(formatList.slice());
	}
	saveData();
}

class ScheduleGenerator {
	formats: string[][];
	items = new Map<string, number>();
	constructor() {
		this.formats = new Array(SEASONS_PER_YEAR).fill(null).map(() => [] as string[]);
		for (const format of FORMAT_POOL) this.items.set(format, 0);
	}
	generate() {
		for (let i = 0; i < this.formats.length; i++) {
			this.step([i, 0]);
		}
		for (let i = 1; i < SEASONS_PER_YEAR; i++) {
			this.step([0, i]);
		}
		return this.formats;
	}
	swap(x: number, y: number) {
		const item = this.formats[x][y];
		for (let i = 0; i < SEASONS_PER_YEAR; i++) {
			if (this.formats[i].includes(item)) continue;
			for (const [j, cur] of this.formats[i].entries()) {
				if (cur === item) continue;
				if (this.formats[x].includes(cur)) continue;
				this.formats[i][j] = item;
				return cur;
			}
		}
		throw new Error("Couldn't find swap target for " + item + ": " + JSON.stringify(this.formats));
	}
	select(x: number, y: number): string {
		const items = Array.from(this.items).filter(entry => entry[1] < 2);
		const item = Utils.randomElement(items);
		if (item[1] >= 2) {
			this.items.delete(item[0]);
			return this.select(x, y);
		}
		this.items.set(item[0], item[1] + 1);
		if (item[0] && this.formats[x].includes(item[0])) {
			this.formats[x][y] = item[0];
			return this.swap(x, y);
		}
		return item[0];
	}
	step(start: [number, number]) {
		let [x, y] = start;
		while (x < this.formats.length && y < FORMATS_PER_SEASON) {
			const item = this.select(x, y);
			this.formats[x][y] = item;
			x++;
			y++;
		}
	}
}

export function generateFormatSchedule() {
	return new ScheduleGenerator().generate();
}

export async function getLadderTop(format: string) {
	try {
		const results = await Net(`https://${Config.routes.root}/ladder/?format=${toID(format)}&json`).get();
		const reply = JSON.parse(results);
		return reply.toplist;
	} catch (e) {
		Monitor.crashlog(e, "A season ladder request");
		return null;
	}
}

export async function updateBadgeholders() {
	rollSeason();
	const period = `${data.current.season}`;
	if (!data.badgeholders[period]) {
		data.badgeholders[period] = {};
	}
	for (const formatName of data.formatSchedule[findPeriod()]) {
		const formatid = `gen${Dex.gen}${formatName}`;
		const response = await getLadderTop(formatid);
		if (!response) continue; // ??
		const newHolders: Record<string, string[]> = {};
		for (const [i, row] of response.entries()) {
			let badgeType = null;
			for (const type in BADGE_THRESHOLDS) {
				if ((i + 1) <= BADGE_THRESHOLDS[type]) {
					badgeType = type;
					break;
				}
			}
			if (!badgeType) break;
			if (!newHolders[badgeType]) newHolders[badgeType] = [];
			newHolders[badgeType].push(row.userid);
		}
		data.badgeholders[period][formatid] = newHolders;
	}
	saveData();
}

function getYear() {
	return new Date().getFullYear();
}

function findPeriod(modifier = 0) {
	return Math.floor((new Date().getMonth() + modifier) / (SEASONS_PER_YEAR - 1)) + 1;
}

/** Are we in the last three days of the month (the public phase, where badged battles are public and the room is active?) */
function checkPublicPhase() {
	const daysInCurrentMonth = new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate();
	// last 3 days of the month, and next month is a new season
	return new Date().getDate() >= (daysInCurrentMonth - PUBLIC_PHASE_LENGTH) && findPeriod() !== findPeriod(1);
}

export function saveData() {
	FS('config/chat-plugins/seasons.json').writeUpdate(() => JSON.stringify(data));
}

export function rollSeason() {
	const year = getYear();
	if (data.current.year !== year) {
		data.current.year = year;
		setFormatSchedule();
	}
	if (findPeriod() !== data.current.period) {
		data.current.season++;
		data.badgeholders[data.current.season] = {};
		for (const k of data.formatSchedule[findPeriod()]) {
			data.badgeholders[data.current.season][`gen${Dex.gen}${k}`] = {};
		}
		data.current.period = findPeriod();
		saveData();
	}
}

export let updateTimeout: NodeJS.Timeout | true | null = null;

export function rollTimer() {
	if (updateTimeout === true) return;
	if (updateTimeout) {
		clearTimeout(updateTimeout);
	}
	updateTimeout = true;
	void updateBadgeholders();
	const time = Date.now();
	const next = new Date();
	next.setHours(next.getHours() + 1, 0, 0, 0);
	updateTimeout = setTimeout(() => rollTimer(), next.getTime() - time);

	const discussionRoom = Rooms.search('seasondiscussion');
	if (discussionRoom) {
		if (checkPublicPhase() && discussionRoom.settings.isPrivate) {
			discussionRoom.setPrivate(false);
			discussionRoom.settings.modchat = 'autoconfirmed';
			discussionRoom.add(
				`|html|<div class="broadcast-blue"><strong>The public phase of the month has now started!</strong>` +
				`<br /> Badged battles are now forced public, and this room is open for use.</div>`
			).update();
		} else if (!checkPublicPhase() && !discussionRoom.settings.isPrivate) {
			discussionRoom.setPrivate('unlisted');
			discussionRoom.add(
				`|html|<div class="broadcast-blue">The public phase of the month has ended.</div>`
			).update();
		}
	}
}

export function destroy() {
	if (updateTimeout && typeof updateTimeout !== 'boolean') {
		clearTimeout(updateTimeout);
	}
}

rollTimer();

export const commands: Chat.ChatCommands = {
	seasonschedule: 'seasons',
	seasons() {
		return this.parse(`/join view-seasonschedule`);
	},
};

export const pages: Chat.PageTable = {
	seasonschedule() {
		this.checkCan('globalban');
		let buf = `<div class="pad"><h2>Season schedule for ${getYear()}</h2><br />`;
		buf += `<div class="ladder pad"><table><tr><th>Season #</th><th>Formats</th></tr>`;
		for (const period in data.formatSchedule) {
			const match = findPeriod() === Number(period);
			const formatString = data.formatSchedule[period]
				.sort()
				.map(x => Dex.formats.get(x).name.replace(`[Gen ${Dex.gen}]`, ''))
				.join(', ');
			buf += `<tr><td>${match ? `<strong>${period}</strong>` : period}</td>`;
			buf += `<td>${match ? `<strong>${formatString}</strong>` : formatString}</td></tr>`;
		}
		buf += `</tr></table></div>`;
		return buf;
	},
	seasonladder(query, user) {
		const format = toID(query.shift());
		const season = toID(query.shift()) || `${data.current.season}`;
		if (!data.badgeholders[season]) {
			return this.errorReply(`Season ${season} not found.`);
		}
		this.title = `[Seasons]`;
		let buf = '<div class="pad">';
		if (!Object.keys(data.badgeholders[season]).includes(format)) {
			// fall back to the master list so that people can still access this easily from the ladder page of other formats
			this.title += ` All`;
			buf += `<h2>Season Records</h2>`;
			const seasonsDesc = Utils.sortBy(
				Object.keys(data.badgeholders),
				s => s.split('-').map(x => -Number(x))
			);
			for (const s of seasonsDesc) {
				buf += `<h3>Season ${s}</h3><hr />`;
				for (const f in data.badgeholders[s]) {
					buf += `<a class="button" name="send" target="replace" href="/view-seasonladder-${f}-${s}">${Dex.formats.get(f).name}</a>`;
				}
				buf += `<br />`;
			}
			return buf;
		}
		this.title += ` ${format} [Season ${season}]`;
		const uppercase = (str: string) => str.charAt(0).toUpperCase() + str.slice(1);
		let formatName = Dex.formats.get(format).name;
		// futureproofing for gen10/etc
		const room = Rooms.search(Utils.splitFirst(format, /\d+/)[1] || '');
		if (room) {
			formatName = `<a href="/${room.roomid}">${formatName}</a>`;
		}
		buf += `<h2>Season results for ${formatName} [${season}]</h2>`;
		buf += `<small><a target="replace" href="/view-seasonladder">View past seasons</a></small>`;
		let i = 0;
		for (const badgeType in data.badgeholders[season][format]) {
			buf += `<div class="ladder pad"><table>`;
			let formatType = format.split(/gen\d+/)[1];
			if (!['ou', 'randombattle'].includes(formatType)) formatType = 'rotating';
			buf += `<tr><h2><img src="https://${Config.routes.client}/sprites/misc/${formatType}_${badgeType}.png" /> ${uppercase(badgeType)}</h2></tr>`;
			for (const userid of data.badgeholders[season][format][badgeType]) {
				i++;
				buf += `<tr><td>${i}</td><td><a href="https://${Config.routes.root}/users/${userid}">${userid}</a></td></tr>`;
			}
			buf += `</table></div>`;
		}
		return buf;
	},
};

export const handlers: Chat.Handlers = {
	onBattleStart(user, room) {
		if (!room.battle) return; // should never happen, just sating TS
		// now first verify they have a badge
		const badges = getBadges(user, room.battle.format);
		if (!badges.length) return;
		const slot = room.battle.playerTable[user.id]?.slot;
		if (!slot) return; // not in battle fsr? wack
		for (const badge of badges) {
			room.add(`|badge|${slot}|${badge.type}|${badge.format}|${BADGE_THRESHOLDS[badge.type]}-${data.current.season}`);
		}

		if (
			checkPublicPhase() && !room.battle.forcedSettings.privacy &&
			badges.filter(x => x.format === room.battle!.format).length && room.battle.rated
		) {
			room.battle.forcedSettings.privacy = 'medal';
			room.add(
				`|html|<div class="broadcast-red"><strong>This battle is required to be public due to one or more player having a season medal.</strong><br />` +
				`During the public phase, you can discuss the state of the ladder <a href="/seasondiscussion">in a special chatroom.</a></div>`
			);
			room.setPrivate(false);
			const seasonRoom = Rooms.search('seasondiscussion');
			if (seasonRoom) {
				const p1html = getUserHTML(user, room.battle.format);
				const otherPlayer = user.id === room.battle.p1.id ? room.battle.p2 : room.battle.p1;
				const otherUser = otherPlayer.getUser();
				const p2html = otherUser ? getUserHTML(otherUser, room.battle.format) : `<username>${otherPlayer.name}</username>`;
				const formatName = Dex.formats.get(room.battle.format).name;
				seasonRoom.add(
					`|raw|<a href="/${room.roomid}" class="ilink">${formatName} battle started between ` +
					`${p1html} and ${p2html}. (rating: ${Math.floor(room.battle.rated)})</a>`
				).update();
			}
		}

		room.add(
			`|uhtml|medal-msg|<div class="broadcast-blue">Curious what those medals under the avatar are? PS now has Ladder Seasons!` +
			` For more information, check out the <a href="https://www.smogon.com/forums/threads/3740067/">thread on Smogon.</a></div>`
		);
		room.update();
	},
};