diff --git a/app.js b/app.js index 0b199b1..9c58c49 100644 --- a/app.js +++ b/app.js @@ -1,21 +1,38 @@ const { io } = require(`${__dirname}/index.js`); const { Player } = require(`${__dirname}/businesses/Player.js`); -const uuid = require("uuid"); let rooms = []; let players = []; io.on("connection", (socket) => { console.log("New connected : ", socket.id); - /* TODO : handle disconnection - socket.on("disconnect", (reason) => { - if (game.room.players.length === 2) { - game.endGame(); - delete game.room; + + socket.on("disconnect", () => { + const index = players.findIndex((p) => p.id === socket.id) + const roomIndex = rooms.findIndex(room => + room.players.some(player => player.id === socket.id) + ); + + if (roomIndex !== -1) { + const room = rooms[roomIndex]; + const opponent = room.players.find(player => player.id !== socket.id); + const inRoomIndex = room.players.findIndex(player => player.id === socket.id); + + if (opponent) { + io.to(opponent.id).emit('opponent left'); + } + + room.players.splice(inRoomIndex, 1) + + if (room.players.length === 0) { + rooms.splice(roomIndex, 1); + } } - console.debug(game); - }); - */ + + players.splice(index, 1) + + console.log(`Player disconnected: ${socket.id}`); + }) socket.on("first connection", (socketId) => { let player = new Player(socketId); @@ -57,8 +74,15 @@ io.on("connection", (socket) => { room.move(move); }); - socket.on("get player", (id, callback) => { - const out = players.find((p) => p.id === id); + socket.on("get player", (roomId, id, callback) => { + let out = "" + const room = rooms.find((r) => r.id === roomId) + + if (room === undefined) { + out = players.find((p) => p.id === id); + } else { + out = room.players.find((p) => p.id === id) + } callback({ player: out, @@ -69,6 +93,7 @@ io.on("connection", (socket) => { const room = rooms.find((r) => r.id === roomId); const out = room.players.find((p) => p.id !== id); + console.log(out.grid.cases[0]) callack({ player: out, }); @@ -83,6 +108,11 @@ io.on("connection", (socket) => { }); }); + socket.on("reset grid", (roomId) => { + const player = rooms.find((r) => r.id === roomId).players[0] + player.resetGrid(); + }) + socket.on("update piece", (playerId, piece) => { const player = players.find((p) => p.id === playerId); const index = player.pieces.findIndex((p) => p.id === piece.id); @@ -149,7 +179,7 @@ class Room { let playedCase = this.players.find((p) => p.id === this.ennemy).grid.cases[move.col][move.row]; if (playedCase.isPlayed === false) { - players.find((p) => p.id === this.ennemy).grid.cases[move.col][move.row].isPlayed = true; + this.players.find((p) => p.id === this.ennemy).grid.cases[move.col][move.row].isPlayed = true; playedMoove(this, playedCase.isShip, this.checkWin()); let tmp = this.actualPlayer; diff --git a/businesses/Player.js b/businesses/Player.js index ebe76bb..b3ceac6 100644 --- a/businesses/Player.js +++ b/businesses/Player.js @@ -9,7 +9,7 @@ class Player { this.pieces = []; this.pieces.push(new Piece(1, { x: 0, y: 0 }, { x: 0, y: 0 })); this.pieces.push(new Piece(2, { x: 2, y: 2 }, { x: 2, y: 3 })); - + this.pieces.forEach((piece) => { for (let i = piece.startPos.x; i <= piece.endPos.x; i++) { for (let j = piece.startPos.y; j <= piece.endPos.y; j++) { @@ -19,6 +19,21 @@ class Player { } }); } + + resetPiece() { + this.pieces = [] + this.createPiece() + } + + createPiece() { + this.pieces.push(new Piece(1, { x: 0, y: 0 }, { x: 0, y: 0 })); + this.pieces.push(new Piece(2, { x: 2, y: 2 }, { x: 2, y: 3 })); + } + + resetGrid() { + this.grid = new Grid() + this.resetPiece() + } } module.exports = { diff --git a/public/assets/style.css b/public/assets/style.css index ee38811..f444e6e 100644 --- a/public/assets/style.css +++ b/public/assets/style.css @@ -7,3 +7,33 @@ .hidden-element { display: none; } + +#opponentLeftModal { + display: none; + position: fixed; + z-index: 1; + left: 0; + top: 0; + width: 100%; + height: 100%; + overflow: auto; + background-color: rgb(0,0,0); + background-color: rgba(0,0,0,0.4); +} + +#opponentLeftModalContent { + background-color: #fefefe; + margin: 15% auto; + padding: 20px; + border: 1px solid #888; + width: 80%; + text-align: center; +} + +.close { + background-color: #4CAF50; + color: white; + padding: 15px 20px; + border: none; + cursor: pointer; +} \ No newline at end of file diff --git a/public/index.html b/public/index.html index 0a46a45..2e5ef9d 100644 --- a/public/index.html +++ b/public/index.html @@ -8,9 +8,15 @@ Maettleship -
- +
+
+

Your opponent has left the game.

+ +
+ +
+