From 0c4448404c659e5f2f433a0f1cf699cb03450f8c Mon Sep 17 00:00:00 2001 From: MaciejkaG Date: Sun, 10 Mar 2024 11:59:18 +0100 Subject: [PATCH] Major changes - Fixed a bug causing players to be able to shoot one field multiple times and lose turn - Enhanced ship placing animation --- index.js | 7 ++++++- public/assets/js/battleships-lib.js | 9 ++++++--- public/assets/js/socket-game.js | 1 + utils/battleships.js | 23 ++++++++++++++--------- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 1d4080a..687a24f 100644 --- a/index.js +++ b/index.js @@ -143,7 +143,7 @@ io.on('connection', async (socket) => { { // typ 2 to trójmasztowiec pozycja i obrót na planszy które pola zostały trafione ships: [], // zawiera np. {type: 2, posX: 3, posY: 4, rot: 2, hits: [false, false, true]} // pozycja na planszy czy strzał miał udział w zatopieniu statku? - shots: [], // zawiera np. {posX: 3, posY: 5, sunk: true} + shots: [], // zawiera np. {posX: 3, posY: 5} }, { ships: [], @@ -286,6 +286,8 @@ io.on('connection', async (socket) => { const enemyIdx = socket.request.session.id === playerGame.data.hostId ? 1 : 0; let hit = await GInfo.shootShip(socket, posX, posY); + + await redis.json.arrAppend(`game:${playerGame.id}`, `.boards[${enemyIdx}].shots`, { posX: posX, posY: posY }); if (!hit.status) { io.to(playerGame.id).emit("shot missed", enemyIdx, posX, posY); } else if (hit.status === 1) { @@ -309,6 +311,9 @@ io.on('connection', async (socket) => { endGame(playerGame.id); return; } + } else if (hit.status === -1) { + socket.emit("toast", "Już strzeliłeś w to miejsce"); + return; } await GInfo.passTurn(socket); diff --git a/public/assets/js/battleships-lib.js b/public/assets/js/battleships-lib.js index 54594f1..50166ef 100644 --- a/public/assets/js/battleships-lib.js +++ b/public/assets/js/battleships-lib.js @@ -110,9 +110,12 @@ class Battleships { break; } - fields.forEach(field => { - this.getField(field[0], field[1]).addClass("active"); - }); + for (let i = 0; i < fields.length; i++) { + const field = fields[i]; + setTimeout(() => { + this.getField(field[0], field[1]).addClass("active"); + }, i * 150); + } return fields; } diff --git a/public/assets/js/socket-game.js b/public/assets/js/socket-game.js index 7ff7c90..48f855a 100644 --- a/public/assets/js/socket-game.js +++ b/public/assets/js/socket-game.js @@ -45,6 +45,7 @@ socket.on('toast', (msg) => { socket.on("placed ship", (data) => { let shipFields = bsc.placeShip(data); + lastTimeClick = new Date().getTime() / 1000; shipFields.forEach(field => { occupiedFields.push({pos: field, origin: [data.posX, data.posY]}); }); diff --git a/utils/battleships.js b/utils/battleships.js index 136ca2a..bbeb9c5 100644 --- a/utils/battleships.js +++ b/utils/battleships.js @@ -110,25 +110,30 @@ export class GameInfo { const enemyIdx = socket.request.session.id === hostId ? 1 : 0; // const playerIdx = enemyIdx ? 0 : 1; - let playerShips = await this.redis.json.get(key, { path: `.boards[${enemyIdx}].ships` }); + let playerBoard = await this.redis.json.get(key, { path: `.boards[${enemyIdx}]` }); - var check = checkHit(playerShips, posX, posY); + let shot = playerBoard.shots.find((shot) => shot.posX === posX && shot.posY === posY); + if (shot) { + return { status: -1 } + } + + var check = checkHit(playerBoard.ships, posX, posY); if (!check) { return { status: 0 }; } var shotShip; - for (let i = 0; i < playerShips.length; i++) { - const ship = playerShips[i]; + for (let i = 0; i < playerBoard.ships.length; i++) { + const ship = playerBoard.ships[i]; if (ship.posX === check.originPosX & ship.posY === check.originPosY) { shotShip = ship; - playerShips[i].hits[check.fieldIdx] = true; - if (!playerShips[i].hits.includes(false)) { + playerBoard.ships[i].hits[check.fieldIdx] = true; + if (!playerBoard.ships[i].hits.includes(false)) { let gameFinished = true; - await this.redis.json.set(key, `.boards[${enemyIdx}].ships`, playerShips); - playerShips.every(ship => { + await this.redis.json.set(key, `.boards[${enemyIdx}]`, playerBoard); + playerBoard.ships.every(ship => { if (ship.hits.includes(false)) { gameFinished = false; return false; @@ -142,7 +147,7 @@ export class GameInfo { } } - await this.redis.json.set(key, `.boards[${enemyIdx}].ships`, playerShips); + await this.redis.json.set(key, `.boards[${enemyIdx}]`, playerBoard); return { status: 1, ship: shotShip }; } }