Skip to content

Commit

Permalink
Remove unlinkUser (#10101)
Browse files Browse the repository at this point in the history
This refactor removes `player.unlinkUser()` which was used for several
unrelated use cases.

These have been split apart into:

- at the end of a game, `game.setEnded()` now clears `user.games`,
  while leaving `player.id` untouched
- when trying to change the user associated with the player, we now
  consistently use the higher-level `game.updatePlayer()`

There's a good chance this fixes the lagspikes currently affecting PS.
  • Loading branch information
Zarel authored Jan 9, 2024
1 parent c1100c8 commit eb0ef53
Show file tree
Hide file tree
Showing 16 changed files with 215 additions and 240 deletions.
2 changes: 1 addition & 1 deletion server/chat-plugins/hangman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ try {
const maxMistakes = 6;

export class Hangman extends Rooms.SimpleRoomGame {
override readonly gameid = 'hangman' as ID;
gameNumber: number;
creator: ID;
word: string;
Expand All @@ -69,7 +70,6 @@ export class Hangman extends Rooms.SimpleRoomGame {

this.gameNumber = room.nextGameNumber();

this.gameid = 'hangman' as ID;
this.title = 'Hangman';
this.creator = user.id;
this.word = word;
Expand Down
41 changes: 15 additions & 26 deletions server/chat-plugins/helptickets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,41 +176,34 @@ export function writeStats(line: string) {
}

export class HelpTicket extends Rooms.SimpleRoomGame {
room: ChatRoom;
override readonly gameid = "helpticket" as ID;
override readonly allowRenames = true;
override room: ChatRoom;
ticket: TicketState;
claimQueue: string[];
involvedStaff: Set<ID>;
involvedStaff = new Set<ID>();
createTime: number;
activationTime: number;
emptyRoom: boolean;
firstClaimTime: number;
unclaimedTime: number;
emptyRoom = false;
firstClaimTime = 0;
unclaimedTime = 0;
lastUnclaimedStart: number;
closeTime: number;
resolution: 'unknown' | 'dead' | 'unresolved' | 'resolved';
result: TicketResult | null;
closeTime = 0;
resolution: 'unknown' | 'dead' | 'unresolved' | 'resolved' = 'unknown';
result: TicketResult | null = null;

constructor(room: ChatRoom, ticket: TicketState) {
super(room);
this.room = room;
this.room.settings.language = Users.get(ticket.creator)?.language || 'english' as ID;
this.title = `Help Ticket - ${ticket.type}`;
this.gameid = "helpticket" as ID;
this.allowRenames = true;
this.ticket = ticket;
this.claimQueue = [];

/* Stats */
this.involvedStaff = new Set();
this.createTime = Date.now();
this.activationTime = (ticket.active ? this.createTime : 0);
this.emptyRoom = false;
this.firstClaimTime = 0;
this.unclaimedTime = 0;
this.lastUnclaimedStart = (ticket.active ? this.createTime : 0);
this.closeTime = 0;
this.resolution = 'unknown';
this.result = null;
}

onJoin(user: User, connection: Connection) {
Expand Down Expand Up @@ -477,15 +470,11 @@ export class HelpTicket extends Rooms.SimpleRoomGame {
}

this.room.game = null;
// @ts-ignore
this.room = null;
for (const player of this.players) {
player.destroy();
}
// @ts-ignore
this.players = null;
// @ts-ignore
this.playerTable = null;
(this.room as any) = null;
this.setEnded();
for (const player of this.players) player.destroy();
(this.players as any) = null;
(this.playerTable as any) = null;
}
onChatMessage(message: string, user: User) {
HelpTicket.uploadReplaysFrom(message, user, user.connections[0]);
Expand Down
20 changes: 10 additions & 10 deletions server/chat-plugins/mafia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ class MafiaPlayer extends Rooms.RoomGamePlayer<Mafia> {
}

class Mafia extends Rooms.RoomGame<MafiaPlayer> {
override readonly gameid = 'mafia' as ID;
started: boolean;
theme: MafiaDataTheme | null;
hostid: ID;
Expand Down Expand Up @@ -275,12 +276,10 @@ class Mafia extends Rooms.RoomGame<MafiaPlayer> {
constructor(room: ChatRoom, host: User) {
super(room);

this.gameid = 'mafia' as ID;
this.title = 'Mafia';
this.playerCap = 20;
this.allowRenames = false;
this.started = false;
this.ended = false;

this.theme = null;

Expand Down Expand Up @@ -1016,6 +1015,12 @@ class Mafia extends Rooms.RoomGame<MafiaPlayer> {
return this.hasPlurality;
}

override removePlayer(player: MafiaPlayer) {
delete this.playerTable[player.id];
player.updateHtmlRoom();
return super.removePlayer(player);
}

eliminate(toEliminate: string, ability: string) {
if (!(toEliminate in this.playerTable || toEliminate in this.dead)) return;
if (!this.started) {
Expand All @@ -1028,10 +1033,7 @@ class Mafia extends Rooms.RoomGame<MafiaPlayer> {
if (this.requestedSub.includes(player.id)) {
this.requestedSub.splice(this.requestedSub.indexOf(player.id), 1);
}
delete this.playerTable[player.id];
this.playerCount--;
player.updateHtmlRoom();
player.destroy();
this.removePlayer(player);
return;
}
if (toEliminate in this.playerTable) {
Expand Down Expand Up @@ -1081,16 +1083,14 @@ class Mafia extends Rooms.RoomGame<MafiaPlayer> {
}
}
this.clearVotes(player.id);
delete this.playerTable[player.id];
let subIndex = this.requestedSub.indexOf(player.id);
if (subIndex !== -1) this.requestedSub.splice(subIndex, 1);
subIndex = this.hostRequestedSub.indexOf(player.id);
if (subIndex !== -1) this.hostRequestedSub.splice(subIndex, 1);

this.playerCount--;
this.updateRoleString();
this.updatePlayers();
player.updateHtmlRoom();
this.removePlayer(player);
}

revealRole(user: User, toReveal: MafiaPlayer, revealAs: string) {
Expand Down Expand Up @@ -1758,7 +1758,7 @@ class Mafia extends Rooms.RoomGame<MafiaPlayer> {
}

end() {
this.ended = true;
this.setEnded();
this.sendHTML(this.roomWindow());
this.updatePlayers();
if (this.room.roomid === 'mafia' && this.started) {
Expand Down
3 changes: 1 addition & 2 deletions server/chat-plugins/scavengers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ class ScavengerHuntDatabase {
}
}
export class ScavengerHunt extends Rooms.RoomGame<ScavengerHuntPlayer> {
override readonly gameid = 'scavengerhunt' as ID;
gameType: GameTypes;
joinedIps: string[];
startTime: number;
Expand All @@ -330,7 +331,6 @@ export class ScavengerHunt extends Rooms.RoomGame<ScavengerHuntPlayer> {
mods: {[k: string]: ModEvent[]};
staffHostId: string;
staffHostName: string;
gameid: ID;
scavGame: true;
timerEnd: number | null;
timer: NodeJS.Timer | null;
Expand Down Expand Up @@ -372,7 +372,6 @@ export class ScavengerHunt extends Rooms.RoomGame<ScavengerHuntPlayer> {
this.staffHostName = staffHost.name;
this.cacheUserIps(staffHost); // store it in case of host subbing

this.gameid = 'scavengerhunt' as ID;
this.title = 'Scavenger Hunt';
this.scavGame = true;

Expand Down
5 changes: 2 additions & 3 deletions server/chat-plugins/trivia/trivia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ class TriviaPlayer extends Rooms.RoomGamePlayer<Trivia> {
}

export class Trivia extends Rooms.RoomGame<TriviaPlayer> {
gameid: ID;
override readonly gameid = 'trivia' as ID;
kickedUsers: Set<string>;
canLateJoin: boolean;
game: TriviaGame;
Expand All @@ -383,7 +383,6 @@ export class Trivia extends Rooms.RoomGame<TriviaPlayer> {
isRandomMode = false, isSubGame = false, isRandomCategory = false,
) {
super(room, isSubGame);
this.gameid = 'trivia' as ID;
this.title = 'Trivia';
this.allowRenames = true;
this.playerCap = Number.MAX_SAFE_INTEGER;
Expand Down Expand Up @@ -1200,6 +1199,7 @@ export class TriumvirateModeTrivia extends Trivia {
* which is a game of First mode trivia that ends after a specified interval.
*/
export class Mastermind extends Rooms.SimpleRoomGame {
override readonly gameid = 'mastermind' as ID;
/** userid:score Map */
leaderboard: Map<ID, {score: number, hasLeft?: boolean}>;
phase: string;
Expand All @@ -1210,7 +1210,6 @@ export class Mastermind extends Rooms.SimpleRoomGame {
super(room);

this.leaderboard = new Map();
this.gameid = 'mastermind' as ID;
this.title = 'Mastermind';
this.allowRenames = true;
this.playerCap = Number.MAX_SAFE_INTEGER;
Expand Down
Loading

0 comments on commit eb0ef53

Please sign in to comment.