From 2c90cec1e34ca4e016162c71d3b051ec93eec9db Mon Sep 17 00:00:00 2001 From: vincentastolfi Date: Wed, 12 Jun 2024 16:20:32 +0200 Subject: [PATCH] :art: Move Room game to make everything cleaner --- businesses/Room.js | 103 +++++++++++++++++++++++ index.js | 125 ++++------------------------ public/pages/gameView.html | 4 +- public/scripts/{index.js => app.js} | 1 + public/scripts/game.js | 2 +- 5 files changed, 121 insertions(+), 114 deletions(-) create mode 100644 businesses/Room.js rename public/scripts/{index.js => app.js} (98%) diff --git a/businesses/Room.js b/businesses/Room.js new file mode 100644 index 0000000..b15e4ca --- /dev/null +++ b/businesses/Room.js @@ -0,0 +1,103 @@ +class Room { + constructor(room) { + this.id = this.generateRoomId(); // change the id with something prettier + this.players = []; + this.room = room; + this.actualPlayer = ""; + this.ennemy = ""; + } + + addPlayer(player) { + this.players.push(player); + } + + start() { + let rand = Math.floor(Math.random() * (1 - 0 + 1) + 0); + this.actualPlayer = this.players[rand].id; + rand === 0 + ? (this.ennemy = this.players[1].id) + : (this.ennemy = this.players[0].id); + + this.players.forEach((p) => { + for (let i = 0; i < p.pieces.length; i++) { + p.pieces[i].isMovable = false; + p.pieces[i].isSelected = false; + } + }); + + return this.actualPlayer + } + + /* + endGame() { + this.room.players.forEach((player) => + io.to(player.socketId).emit("end game"), + ); + } + */ + + move(move) { + let ret = {isMove: false, player: this.actualPlayer} + let playedCase = this.players.find((p) => p.id === this.ennemy).grid.cases[move.col][move.row]; + + if (playedCase.isPlayed === false) { + this.players.find((p) => p.id === this.ennemy).grid.cases[move.col][move.row].isPlayed = true; + + let tmp = this.actualPlayer; + this.actualPlayer = this.ennemy; + this.ennemy = tmp; + + ret = {isMove: true, players: this.players, isHit: playedCase.isShip, + isWin: this.checkWin(), player: this.actualPlayer} + } + + return ret + } + + checkWin() { + const e = this.players.find((p) => p.id === this.ennemy); + let w = true; + + for (let i = 0; i < e.grid.cases.length; i++) { + for (let j = 0; j < e.grid.cases.length; j++) { + let c = e.grid.cases[i][j]; + + if (c.isShip && !c.isPlayed) { + w = false; + break; + } + } + } + + return w; + } + + validBoards() { + this.players.forEach((player) => { + player.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++) { + player.grid.cases[i][j].isShip = true; + } + } + }); + }); + } + + generateRoomId() { + const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + const idLength = 5; + let roomId = ''; + + for (let i = 0; i < idLength; i++) { + const randomIndex = Math.floor(Math.random() * characters.length); + roomId += characters[randomIndex]; + } + + return roomId; + } +} + +module.exports = { + Room, +} \ No newline at end of file diff --git a/index.js b/index.js index 72165a7..cf6efe8 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,8 @@ const db = require("./database.js") const bodyParser = require("body-parser"); const path = require("path"); +const { Room } = require('./businesses/Room.js'); + app.use(express.static("public")) app.use(express.json()); @@ -18,7 +20,6 @@ app.get('/', (req, res) => { }) app.get('/game', (req, res) => { - console.log('get game') res.sendFile(path.join(__dirname, '/public/pages/gameView.html')) }) @@ -104,13 +105,13 @@ io.on("connection", (socket) => { io.to(player.id).emit("start game"); }); - room.start(); + askToPlay(room.start()); }); socket.on("play", (roomId, id, move) => { let room = rooms.find((r) => r.id === roomId); - room.move(move); + sendMoveToPlayers(room.move(move)); }); socket.on("get player", (roomId, id, callback) => { @@ -165,117 +166,19 @@ io.on("connection", (socket) => { }); }); -const askToPlay = (game) => { - io.to(game.actualPlayer).emit("play"); -}; - -const playedMoove = (game, isHit, isWin) => { - game.players.forEach((player) => { - io.to(player.id).emit("played move", isHit, isWin); - }); -}; - -class Room { - constructor(room) { - this.id = this.generateRoomId(); // change the id with something prettier - this.players = []; - this.room = room; - this.actualPlayer = ""; - this.ennemy = ""; - } - - addPlayer(player) { - this.players.push(player); - } - - start() { - let rand = Math.floor(Math.random() * (1 - 0 + 1) + 0); - this.actualPlayer = this.players[rand].id; - rand === 0 - ? (this.ennemy = this.players[1].id) - : (this.ennemy = this.players[0].id); - - players.forEach((p) => { - for (let i = 0; i < p.pieces.length; i++) { - p.pieces[i].isMovable = false; - p.pieces[i].isSelected = false; - } - }); - - askToPlay(this); - } - - /* - endGame() { - this.room.players.forEach((player) => - io.to(player.socketId).emit("end game"), - ); - } - */ - - move(move) { - let playedCase = this.players.find((p) => p.id === this.ennemy).grid.cases[move.col][move.row]; - - if (playedCase.isPlayed === false) { - 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; - this.actualPlayer = this.ennemy; - this.ennemy = tmp; - } - - askToPlay(this); - } - - checkWin() { - const e = this.players.find((p) => p.id === this.ennemy); - let w = true; - - for (let i = 0; i < e.grid.cases.length; i++) { - for (let j = 0; j < e.grid.cases.length; j++) { - let c = e.grid.cases[i][j]; - - if (c.isShip && !c.isPlayed) { - w = false; - break; - } - } - } - - return w; - } - - validBoards() { - this.players.forEach((player) => { - player.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++) { - player.grid.cases[i][j].isShip = true; - } - } - }); - }); - } +const askToPlay = (player) => { + io.to(player).emit("play") +} - generateRoomId() { - const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; - const idLength = 5; - let roomId = ''; - - for (let i = 0; i < idLength; i++) { - const randomIndex = Math.floor(Math.random() * characters.length); - roomId += characters[randomIndex]; +const sendMoveToPlayers = (moveData) => { + if (moveData.isMove === true) { + for (let i = 0; i <= 1; i++) { + io.to(moveData.players[i].id).emit("played move", moveData.isHit, moveData.isWin); } - - return roomId; } -} + askToPlay(moveData.player) +}; http.listen(port, () => { console.log(`Listening on http://localhost:${port}`); -}); - -module.exports = { - io, -}; +}); \ No newline at end of file diff --git a/public/pages/gameView.html b/public/pages/gameView.html index 4688257..5e2ee2a 100644 --- a/public/pages/gameView.html +++ b/public/pages/gameView.html @@ -59,7 +59,7 @@

WIN !

- - + + diff --git a/public/scripts/index.js b/public/scripts/app.js similarity index 98% rename from public/scripts/index.js rename to public/scripts/app.js index c9ca81e..c76c73e 100644 --- a/public/scripts/index.js +++ b/public/scripts/app.js @@ -42,6 +42,7 @@ socket.on("play", () => { }); socket.on("played move", (isHit, isWin) => { + console.log("test") const hitNotification = document.querySelector("#hit_notification"); const winNotification = document.querySelector("#win_notification"); diff --git a/public/scripts/game.js b/public/scripts/game.js index 9d0b291..f0312d6 100644 --- a/public/scripts/game.js +++ b/public/scripts/game.js @@ -1,4 +1,4 @@ -import { sendMove, socket, roomId } from "./index.js"; +import { sendMove, socket, roomId } from "./app.js"; const ownCanvas = document.getElementById("own_board"); const ownCtx = ownCanvas.getContext("2d");