From a65202357e04cf19bbb05dba0e2be313fa6ac507 Mon Sep 17 00:00:00 2001 From: Nathaniel Tampus Date: Mon, 16 Jan 2023 15:10:53 +0800 Subject: [PATCH] add timeout for inactive games #1 also removed some redundant code --- server/src/socket/game.socket.ts | 91 ++++++++++++++------------------ types/index.ts | 1 + 2 files changed, 41 insertions(+), 51 deletions(-) diff --git a/server/src/socket/game.socket.ts b/server/src/socket/game.socket.ts index 0f31787..9eab1c1 100644 --- a/server/src/socket/game.socket.ts +++ b/server/src/socket/game.socket.ts @@ -20,6 +20,11 @@ export async function joinLobby(this: Socket, gameCode: string) { await leaveLobby.call(this); } + if (game.timeout) { + clearTimeout(game.timeout); + game.timeout = undefined; + } + await this.join(gameCode); this.emit("receivedLatestGame", game); io.to(game.code as string).emit("receivedLatestLobby", game); @@ -27,63 +32,47 @@ export async function joinLobby(this: Socket, gameCode: string) { } export async function leaveLobby(this: Socket, code?: string) { - if (this.rooms.size === 2) { - const game = activeGames.find((g) => g.code === (code || Array.from(this.rooms)[1])); - if (game) { - const user = game.observers?.find((o) => o.id === this.request.session.id); - let name = ""; - if (user) { - name = user.name as string; - game.observers?.splice(game.observers?.indexOf(user), 1); - } - if (game.black?.id === this.request.session.id) { - name = game.black?.name as string; - game.black = undefined; - } - if (game.white?.id === this.request.session.id) { - name = game.white?.name as string; - game.white = undefined; - } + if (this.rooms.size >= 3 && !code) { + console.log(`[WARNING] leaveLobby: room size is ${this.rooms.size}, aborting...`); + return; + } + const game = activeGames.find( + (g) => + g.code === (code || this.rooms.size === 2 ? Array.from(this.rooms)[1] : 0) || + g.black?.id === this.request.session.id || + g.white?.id === this.request.session.id || + g.observers?.find((o) => this.request.session.id === o.id) + ); - if (!game.white && !game.black && !game.observers) { - // TODO - //activeGames.splice(activeGames.indexOf(game), 1); // remove game if empty - } else { - this.to(game.code as string).emit("userLeft", name); - this.to(game.code as string).emit("receivedLatestLobby", game); - } + if (game) { + const user = game.observers?.find((o) => o.id === this.request.session.id); + let name = ""; + if (user) { + name = user.name as string; + game.observers?.splice(game.observers?.indexOf(user), 1); } - await this.leave(code || Array.from(this.rooms)[1]); - } else if (this.rooms.size >= 3) { - console.log(`[WARNING] leaveLobby: room size is ${this.rooms.size}, aborting...`); - } else { - // try to find a game with this user - const game = activeGames.find( - (g) => - g.code === code || - g.black?.id === this.request.session.id || - g.white?.id === this.request.session.id || - g.observers?.find((o) => this.request.session.id === o.id) - ); - if (game) { - const user = game.observers?.find((o) => this.request.session.id === o.id); - let name = ""; - if (user) { - name = user.name as string; - game.observers?.splice(game.observers?.indexOf(user), 1); - } - if (game.black?.id === this.request.session.id) { - name = game.black?.name as string; - game.black = undefined; - } - if (game.white?.id === this.request.session.id) { - name = game.white?.name as string; - game.white = undefined; - } + if (game.black?.id === this.request.session.id) { + name = game.black?.name as string; + game.black = undefined; + } + if (game.white?.id === this.request.session.id) { + name = game.white?.name as string; + game.white = undefined; + } + + if (!game.white && !game.black && (!game.observers || game.observers.length === 0)) { + if (game.timeout) clearTimeout(game.timeout); + game.timeout = Number( + setTimeout(() => { + activeGames.splice(activeGames.indexOf(game), 1); + }, 1000 * 60 * 30) // 30 minutes + ); + } else { this.to(game.code as string).emit("userLeft", name); this.to(game.code as string).emit("receivedLatestLobby", game); } } + await this.leave(code || Array.from(this.rooms)[1]); } export async function getLatestGame(this: Socket) { diff --git a/types/index.ts b/types/index.ts index 074798c..022cf0e 100644 --- a/types/index.ts +++ b/types/index.ts @@ -7,6 +7,7 @@ export interface Game { host?: User; code?: string; open?: boolean; + timeout?: number; observers?: User[]; }