From 719bc1a87f4c2e57a70b3d286b368a9425ecafad Mon Sep 17 00:00:00 2001 From: Guangcong Luo Date: Sat, 1 Mar 2025 03:11:12 -0800 Subject: [PATCH] Fix crash when leaving UNO game Fixes #10821 --- server/chat-plugins/uno.ts | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/server/chat-plugins/uno.ts b/server/chat-plugins/uno.ts index 5e203bde82..bbede1c9fd 100644 --- a/server/chat-plugins/uno.ts +++ b/server/chat-plugins/uno.ts @@ -176,13 +176,10 @@ export class UNO extends Rooms.RoomGame { } override leaveGame(user: User) { - if (!(user.id in this.playerTable)) return false; const player = this.playerTable[user.id]; - if ((this.state === 'signups' && this.removePlayer(player)) || this.eliminate(user.id)) { - this.sendToRoom(`${user.name} has left the game of UNO.`); - return true; - } - return false; + if (!player) return false; + this.sendToRoom(`${user.name} has left the game of UNO.`); + return this.state === 'signups' ? this.removePlayer(player) : this.eliminate(player); } /** @@ -202,17 +199,14 @@ export class UNO extends Rooms.RoomGame { this.renamePlayer(user, oldUserid); } - eliminate(userid: ID | undefined) { - if (!userid) return null; - const player = this.playerTable[userid]; + eliminate(player: UNOPlayer | null) { if (!player) return false; - const name = player.name; if (this.playerCount === 2) { this.removePlayer(player); this.onWin(this.players[0]); - return name; + return true; } // handle current player... @@ -243,7 +237,7 @@ export class UNO extends Rooms.RoomGame { if (removingCurrentPlayer) { this.nextTurn(true); } - return name; + return true; } sendToRoom(msg: string, overrideSuppress = false) { @@ -310,7 +304,7 @@ export class UNO extends Rooms.RoomGame { this.timer = setTimeout(() => { this.sendToRoom(`|c:|${Math.floor(Date.now() / 1000)}|~|${player.name} has been automatically disqualified.`); - this.eliminate(player.id); + this.eliminate(player); }, this.maxTime * 1000); }); } @@ -425,7 +419,7 @@ export class UNO extends Rooms.RoomGame { this.isPlusFour = true; this.timer = setTimeout(() => { this.sendToRoom(`|c:|${Math.floor(Date.now() / 1000)}|~|${this.currentPlayer!.name} has been automatically disqualified.`); - this.eliminate(this.currentPlayer!.id); + this.eliminate(this.currentPlayer); }, this.maxTime * 1000); break; case 'Wild': @@ -433,7 +427,7 @@ export class UNO extends Rooms.RoomGame { this.state = 'color'; this.timer = setTimeout(() => { this.sendToRoom(`|c:|${Math.floor(Date.now() / 1000)}|~|${this.currentPlayer!.name} has been automatically disqualified.`); - this.eliminate(this.currentPlayer!.id); + this.eliminate(this.currentPlayer); }, this.maxTime * 1000); break; } @@ -709,7 +703,7 @@ export const commands: Chat.ChatCommands = { game.maxTime = amount; if (game.timer) clearTimeout(game.timer); game.timer = setTimeout(() => { - game.eliminate(game.currentPlayer?.id); + game.eliminate(game.currentPlayer); }, amount * 1000); this.addModAction(`${user.name} has set the UNO automatic disqualification timer to ${amount} seconds.`); this.modlog('UNO TIMER', null, `${amount} seconds`); @@ -743,11 +737,12 @@ export const commands: Chat.ChatCommands = { this.checkCan('minigame', null, room); const game = this.requireGame(UNO); - const disqualified = game.eliminate(toID(target)); - if (disqualified === false) throw new Chat.ErrorMessage(`Unable to disqualify ${target}.`); - this.privateModAction(`${user.name} has disqualified ${disqualified} from the UNO game.`); + const player = game.playerTable[toID(target)]; + if (!player) throw new Chat.ErrorMessage(`Player "${target}" not found.`); + game.eliminate(player); + this.privateModAction(`${user.name} has disqualified ${player.name} from the UNO game.`); this.modlog('UNO DQ', toID(target)); - room.add(`${disqualified} has been disqualified from the UNO game.`).update(); + room.add(`${player.name} has been disqualified from the UNO game.`).update(); }, // player/user commands