From 1bd84bee09b70cc3e7f11e00cb187d8ae435e79d Mon Sep 17 00:00:00 2001 From: MaciejkaG Date: Sun, 7 Apr 2024 00:21:09 +0200 Subject: [PATCH] Major bug fixes - Fixed ready up bug - Fixed a bug that made shooting impossible --- index.js | 7 +++---- utils/battleships.js | 14 +++++++------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 2c9ad74..4badd5a 100644 --- a/index.js +++ b/index.js @@ -549,6 +549,7 @@ io.on('connection', async (socket) => { if (!(callback && typeof callback === 'function')) { return; } + const playerGame = await GInfo.getPlayerGameData(socket); let timeLeft = await GInfo.timerLeft(playerGame.id); @@ -593,9 +594,7 @@ io.on('connection', async (socket) => { await GInfo.timer(playerGame.id, Math.max(timeLeft / 2.5, 15), async () => { await finishPrepPhase(socket, playerGame); }); - } // else if (playerGame.data.ready[1]) { - // // Guest set ready - // } + } } }); @@ -637,7 +636,7 @@ io.on('connection', async (socket) => { let playerGame = await GInfo.getPlayerGameData(socket); if (playerGame && playerGame.data.state === 'action') { - if (bships.checkTurn(playerGame.data, socket.session.id)) { + if (bships.checkTurn(playerGame.data, session.userId)) { const enemyIdx = session.userId === playerGame.data.hostId ? 1 : 0; let hit = await GInfo.shootShip(socket, posX, posY); diff --git a/utils/battleships.js b/utils/battleships.js index 3812376..bbc6e0f 100644 --- a/utils/battleships.js +++ b/utils/battleships.js @@ -61,14 +61,14 @@ export class GameInfo { async incrStat(socket, statKey, by = 1) { const game = await this.redis.json.get(`game:${socket.session.activeGame}`); - const idx = socket.request.session.id === game.hostId ? 0 : 1; + const idx = socket.request.session.userId === game.hostId ? 0 : 1; this.redis.json.numIncrBy(`game:${socket.session.activeGame}`, `.boards[${idx}].stats.${statKey}`, by); } async getPlayerShips(socket) { const game = await this.redis.json.get(`game:${socket.session.activeGame}`); - const idx = socket.request.session.id === game.hostId ? 0 : 1; + const idx = socket.request.session.userId === game.hostId ? 0 : 1; return game.boards[idx].ships; } @@ -101,7 +101,7 @@ export class GameInfo { const key = `game:${gameId}`; const hostId = (await this.redis.json.get(key, { path: '.hostId' })); - const playerIdx = socket.request.session.id === hostId ? 0 : 1; + const playerIdx = socket.request.session.userId === hostId ? 0 : 1; await this.redis.json.arrAppend(key, `.boards[${playerIdx}].ships`, shipData); } @@ -110,7 +110,7 @@ export class GameInfo { const key = `game:${gameId}`; const hostId = (await this.redis.json.get(key, { path: '.hostId' })); - const playerIdx = socket.request.session.id === hostId ? 0 : 1; + const playerIdx = socket.request.session.userId === hostId ? 0 : 1; var playerShips = (await this.redis.json.get(key, { path: `.boards[${playerIdx}].ships` })); @@ -221,7 +221,7 @@ export class GameInfo { const key = `game:${gameId}`; const hostId = (await this.redis.json.get(key, { path: '.hostId' })); - const playerIdx = socket.request.session.id === hostId ? 0 : 1; + const playerIdx = socket.request.session.userId === hostId ? 0 : 1; let playerShips = await this.redis.json.get(key, {path: `.boards[${playerIdx}].ships`}); var deletedShip; @@ -242,7 +242,7 @@ export class GameInfo { const key = `game:${gameId}`; const hostId = (await this.redis.json.get(key, { path: '.hostId' })); - const enemyIdx = socket.request.session.id === hostId ? 1 : 0; + const enemyIdx = socket.request.session.userId === hostId ? 1 : 0; // const playerIdx = enemyIdx ? 0 : 1; let playerBoard = await this.redis.json.get(key, { path: `.boards[${enemyIdx}]` }); @@ -291,7 +291,7 @@ export class GameInfo { const key = `game:${gameId}`; const hostId = (await this.redis.json.get(key, { path: '.hostId' })); - const playerIdx = socket.request.session.id === hostId ? 0 : 1; + const playerIdx = socket.request.session.userId === hostId ? 0 : 1; await this.redis.json.set(key, `.ready[${playerIdx}]`, true); }