pokemon-showdown/server/room-minor-activity.ts
Guangcong Luo 78439b4a02
Update to ESLint 9 (#10926)
ESLint has a whole new config format, so I figure it's a good time to
make the config system saner.

- First, we no longer have separate eslint-no-types configs. Lint
  performance shouldn't be enough of a problem to justify the
  relevant maintenance complexity.

- Second, our base config should work out-of-the-box now. `npx eslint`
  will work as expected, without any CLI flags. You should still use
  `npm run lint` which adds the `--cached` flag for performance.

- Third, whatever updates I did fixed style linting, which apparently
  has been bugged for quite some time, considering all the obvious
  mixed-tabs-and-spaces issues I found in the upgrade.

Also here are some changes to our style rules. In particular:

- Curly brackets (for objects etc) now have spaces inside them. Sorry
  for the huge change. ESLint doesn't support our old style, and most
  projects use Prettier style, so we might as well match them in this way.
  See https://github.com/eslint-stylistic/eslint-stylistic/issues/415

- String + number concatenation is no longer allowed. We now
  consistently use template strings for this.
2025-02-25 20:03:46 -08:00

122 lines
3.3 KiB
TypeScript

/**
* Minor activities
* Pokemon Showdown - http://pokemonshowdown.com/
*
* Minor activities are representations of non-game activities that rooms
* regularly use, such as polls and announcements. Rooms are limited to
* one minor activity at a time.
*
* Minor activities keep track of users in the form of userids and IPs.
* If a player votes for a poll under one IP, they cannot vote for the same
* poll again.
*
* The user-tracking system is not implemented at the base level: Announcements
* do not have a reason to keep track of users' IPs/IDs because they're just used
* to broadcast a message to a room.
*
* @license MIT
*/
export interface MinorActivityData {
readonly activityid: 'announcement' | 'poll';
activityNumber?: number;
source?: string;
timeoutMins?: number;
timerEnd?: number;
question: string;
supportHTML: boolean;
multiPoll: boolean;
pendingVotes?: { [userid: string]: number[] };
voters?: { [k: string]: number[] };
voterIps?: { [k: string]: number[] };
totalVotes?: number;
isQuiz?: boolean;
answers: string[] | { name: string, votes: number, correct?: boolean }[];
}
// globally Rooms.MinorActivity
export abstract class MinorActivity {
abstract activityid: ID;
abstract name: string;
timeout: NodeJS.Timeout | null;
timeoutMins: number;
timerEnd: number;
roomid: RoomID;
room: Room;
supportHTML: boolean;
constructor(room: Room) {
this.timeout = null;
this.timeoutMins = 0;
this.timerEnd = 0;
this.roomid = room.roomid;
this.room = room;
this.supportHTML = false;
}
setTimer(options: { timeoutMins?: number, timerEnd?: number }) {
if (this.timeout) clearTimeout(this.timeout);
this.timeoutMins = options.timeoutMins || 0;
if (!this.timeoutMins) {
this.timerEnd = 0;
this.timeout = null;
return;
}
const now = Date.now();
this.timerEnd = options.timerEnd || now + this.timeoutMins * 60000;
this.timeout = setTimeout(() => {
const room = this.room;
if (!room) return; // someone forgot to `.destroy()`
this.end(room);
}, this.timerEnd - now);
this.save();
}
end(room: Room, MinorActivityClass?: new (...args: any[]) => any) {
room.minorActivity?.destroy();
if (room.minorActivityQueue?.length) {
const pollData = room.minorActivityQueue.shift()!;
if (!room.minorActivityQueue.length) room.clearMinorActivityQueue();
if (!room.settings.minorActivityQueue?.length) {
delete room.settings.minorActivityQueue;
room.saveSettings();
}
if (pollData.activityid !== 'poll') throw new Error(`Unexpected Minor Activity (${pollData.activityid}) in queue`);
room.add(`|c|~|/log ${room.tr`The queued poll was started.`}`).update();
room.modlog({
action: 'POLL',
note: '(queued)',
});
if (!MinorActivityClass) {
if (pollData.activityid === 'poll') {
const { Poll } = require('./chat-plugins/poll');
room.setMinorActivity(new Poll(room, pollData));
}
} else {
room.setMinorActivity(new MinorActivityClass(room, pollData));
}
}
}
endTimer() {
if (!this.timeout) return false;
clearTimeout(this.timeout);
this.timeoutMins = 0;
this.timerEnd = 0;
return true;
}
abstract destroy(): void;
abstract display(): void;
onConnect?(user: User, connection: Connection | null): void;
onRename?(user: User, oldid: ID, joining: boolean): void;
abstract toJSON(): AnyObject;
abstract save(): void;
}