From 6bea4202c2ec9944c98958ef5c112b50d90450e9 Mon Sep 17 00:00:00 2001 From: Sam Hoile Date: Thu, 24 Oct 2024 12:46:37 +0100 Subject: [PATCH] refactored --- api/index.js | 48 +++++++++++++++++++---------- api/numberGame/Player.js | 5 ++- api/tests/numberGame/Player.test.js | 8 ++++- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/api/index.js b/api/index.js index 5821a64..a427f4e 100644 --- a/api/index.js +++ b/api/index.js @@ -16,16 +16,14 @@ io.on("connection", (socket) => { socket.on("disconnect", () => { console.log("User disconnected!"); console.log(`Users Connected: ${io.engine.clientsCount}`); - console.log(io.sockets.adapter.rooms) + console.log("disconnect, rooms: ", io.sockets.adapter.rooms) console.log(games) + }); socket.on("disconnecting", () => { socket.rooms.forEach((gameId) => { - if (games[gameId]) { - games[gameId].removePlayer(socket.id); - io.to(gameId).emit("receive_game", games[gameId]); - } + handlePlayerLeaving(gameId, socket) }); }); @@ -36,8 +34,11 @@ io.on("connection", (socket) => { socket.emit("receive_link", gameId); socket.join(gameId); games[gameId] = new Game(gameId); - games[gameId].addPlayer(new Player(socket.id, `${name} (Host)`, avatar)); + const player = new Player(socket.id, name, avatar) + player.setIsHost() + games[gameId].addPlayer(player); socket.emit('is_host') + console.log(games[gameId].players[0].id) io.to(gameId).emit("receive_game", games[gameId]); }); @@ -87,20 +88,35 @@ io.on("connection", (socket) => { }); socket.on("quit_game", (gameId) => { + handlePlayerLeaving(gameId, socket) + }) + + function handlePlayerLeaving(gameId, socket) { if (games[gameId]) { - games[gameId].removePlayer(socket.id) - socket.leave(gameId) - + games[gameId].removePlayer(socket.id); + socket.leave(gameId); + if (games[gameId].players.length === 0) { - delete games[gameId] - socket.rooms.delete(gameId) - } - else { - io.to(gameId).emit("receive_game", games[gameId]); + delete games[gameId]; + socket.rooms.delete(gameId); + } else { + handleHostLeaving(gameId); + io.to(gameId).emit("receive_game", games[gameId]); } } - }) + } + function handleHostLeaving(gameId) { + if (games[gameId].players.length === 0) { + delete games[gameId] + socket.rooms.delete(gameId) + } + else if (games[gameId].players.every(player => player.isHost === false)) { + console.log("No hosts. Reassigning host...") + games[gameId].players[0].setIsHost() + io.to(games[gameId].players[0].id).emit('is_host') + } + } async function startGameTimer(gameId) { await games[gameId].resetGame(); @@ -153,4 +169,4 @@ function listenForRequests() { }); } -listenForRequests(); +listenForRequests(); \ No newline at end of file diff --git a/api/numberGame/Player.js b/api/numberGame/Player.js index bb3e9a1..05a14dc 100644 --- a/api/numberGame/Player.js +++ b/api/numberGame/Player.js @@ -1,6 +1,7 @@ class Player { constructor(id, name, avatar) { this.id = id; + this.isHost = false this.name = name; this.currentGuess = null, this.totalScore = 0 @@ -27,7 +28,9 @@ class Player { } setIsHost() { - this.host = true + this.isHost = true + this.name += " (Host)" + console.log("name:", this.name, "isHost: ", this.isHost ) } } module.exports = Player \ No newline at end of file diff --git a/api/tests/numberGame/Player.test.js b/api/tests/numberGame/Player.test.js index d3ee203..81c7bb6 100644 --- a/api/tests/numberGame/Player.test.js +++ b/api/tests/numberGame/Player.test.js @@ -1,9 +1,10 @@ const Player = require("../../numberGame/Player") describe('player', () => { - test('initiates with id, name, currentGuess (null) and totalScore (0)', () => { + test('initiates with id, isHost, name, currentGuess (null) and totalScore (0)', () => { const player = new Player('17326746', 'Bob') expect(player.id).toBe('17326746') + expect(player.isHost).toBe(false) expect(player.name).toBe('Bob') expect(player.currentGuess).toEqual(null) expect(player.totalScore).toEqual(0) @@ -24,4 +25,9 @@ describe('player', () => { player.wonRound() expect(player.totalScore).toEqual(1) }) + test('setIsHost makes player isHost true', () => { + const player = new Player('17326746', 'Bob') + player.setIsHost() + expect(player.isHost).toEqual(true) + }) }) \ No newline at end of file