Skip to content

Commit

Permalink
Merge pull request #104 from shammy642/fix_delete_game_bug_sorta
Browse files Browse the repository at this point in the history
temp fix
  • Loading branch information
joe-winter authored Oct 24, 2024
2 parents 2dd8e25 + 85161fa commit db13d2a
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ io.on("connection", (socket) => {
console.log("User disconnected!");
console.log(`Users Connected: ${io.engine.clientsCount}`);
console.log("disconnect, rooms: ", io.sockets.adapter.rooms)
console.log(games)
// console.log(games)

});

socket.on("disconnecting", () => {
socket.rooms.forEach((gameId) => {
handlePlayerLeaving(gameId, socket)
socket.rooms.forEach(async (gameId) => {
await handlePlayerLeaving(gameId, socket)
});
});

// name of the host player passed as a variable below
socket.on("create_room", (data) => {
console.log('create_room')
const { name, avatar } = data;
const gameId = crypto.randomBytes(3).toString("hex");
socket.emit("receive_link", gameId);
Expand All @@ -38,11 +39,12 @@ io.on("connection", (socket) => {
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]);
});

socket.on("join_room", (gameId, data) => {
console.log("join room")
const { name, avatar } = data;
socket.emit("receive_link", gameId);
console.log("Room ID:", gameId);
Expand All @@ -52,6 +54,7 @@ io.on("connection", (socket) => {
});

socket.on("start_game", () => {
console.log("start game")
socket.rooms.forEach((gameId) => {
if (games[gameId]) {
startGameTimer(gameId);
Expand All @@ -60,6 +63,7 @@ io.on("connection", (socket) => {
});

socket.on("send_number", (number) => {
console.log("send number")
socket.rooms.forEach((gameId) => {
if (games[gameId]) {
games[gameId].players.forEach((player) => {
Expand All @@ -73,6 +77,7 @@ io.on("connection", (socket) => {
});

socket.on("next_round", () => {
console.log("next round")
socket.rooms.forEach((gameId) => {
if (games[gameId]) {
games[gameId].players.forEach((player) => {
Expand All @@ -88,37 +93,41 @@ io.on("connection", (socket) => {
});

socket.on("quit_game", (gameId) => {
console.log("quit game")
handlePlayerLeaving(gameId, socket)
})

function handlePlayerLeaving(gameId, socket) {
async function handlePlayerLeaving(gameId, socket) {
console.log("handle Player Leaving")
if (games[gameId]) {
games[gameId].removePlayer(socket.id);
await games[gameId].removePlayer(socket.id);
socket.leave(gameId);

if (games[gameId].players.length === 0) {
delete games[gameId];
// delete games[gameId];
socket.rooms.delete(gameId);
} else {
handleHostLeaving(gameId);
await handleHostLeaving(gameId);
io.to(gameId).emit("receive_game", games[gameId]);
}
}
}

function handleHostLeaving(gameId) {
async function handleHostLeaving(gameId) {
console.log("handle host leaving")
if (games[gameId].players.length === 0) {
delete games[gameId]
// delete games[gameId]
socket.rooms.delete(gameId)
}
else if (games[gameId].players.every(player => player.isHost === false)) {
if (games[gameId].players.every(player => player.isHost === false)) {
console.log("No hosts. Reassigning host...")
games[gameId].players[0].setIsHost()
await games[gameId].players[0].setIsHost()
io.to(games[gameId].players[0].id).emit('is_host')
}
}

async function startGameTimer(gameId) {
console.log("starting game timer")
await games[gameId].resetGame();
io.to(gameId).emit("receive_game", games[gameId]);
io.to(gameId).emit("redirect", "/in-game");
Expand All @@ -142,6 +151,7 @@ io.on("connection", (socket) => {
}

function startNextRoundTimer(gameId) {
console.log("starting next round timer")
if (games[gameId]) {
let timeRemaining = 20;
io.to(gameId).emit("start_timer", timeRemaining);
Expand Down

0 comments on commit db13d2a

Please sign in to comment.