Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
shammy642 committed Oct 24, 2024
1 parent 5712f28 commit 6bea420
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
48 changes: 32 additions & 16 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
});

Expand All @@ -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]);
});

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -153,4 +169,4 @@ function listenForRequests() {
});
}

listenForRequests();
listenForRequests();
5 changes: 4 additions & 1 deletion api/numberGame/Player.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
8 changes: 7 additions & 1 deletion api/tests/numberGame/Player.test.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
})
})

0 comments on commit 6bea420

Please sign in to comment.