Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored #101

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Down Expand Up @@ -28,7 +29,9 @@ class Player {
}

setIsHost() {
this.host = true
this.isHost = true
this.name += " (Host)"
console.log("name:", this.name, "isHost: ", this.isHost )
}
}
module.exports = Player
17 changes: 11 additions & 6 deletions 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 @@ -19,24 +20,28 @@ describe('player', () => {
const player = new Player('17326746', 'Bob')
expect(player.getTotalScore()).toBe(0)
})

test('wonRound adds 1 to total score', () => {
const player = new Player('17326746', 'Bob')
player.wonRound()
expect(player.totalScore).toEqual(1)
})

test('setIsHost', () => {
const player = new Player('17326746', 'Bob')
player.setIsHost()
expect(player.isHost).toEqual(true)
})

test('voteNextRound', () => {
const player = new Player('17326746', 'Bob')
player.voteNextRound()
expect(player.nextRound).toEqual(true)
})

test('setAvatar', () => {
const player = new Player('17326746', 'Bob')
player.setAvatar("avatar")
expect(player.avatar).toEqual("avatar")
})
test('setIsHost', () => {
const player = new Player('17326746', 'Bob')
player.setIsHost()
expect(player.host).toEqual(true)
})
})