Major changes

- Fixed a bug causing players to be able to shoot one field multiple times and lose turn
- Enhanced ship placing animation
This commit is contained in:
MaciejkaG 2024-03-10 11:59:18 +01:00
parent 44f13469cf
commit 0c4448404c
4 changed files with 27 additions and 13 deletions

View File

@ -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 { // 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]} 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? // 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: [], ships: [],
@ -286,6 +286,8 @@ io.on('connection', async (socket) => {
const enemyIdx = socket.request.session.id === playerGame.data.hostId ? 1 : 0; const enemyIdx = socket.request.session.id === playerGame.data.hostId ? 1 : 0;
let hit = await GInfo.shootShip(socket, posX, posY); 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) { if (!hit.status) {
io.to(playerGame.id).emit("shot missed", enemyIdx, posX, posY); io.to(playerGame.id).emit("shot missed", enemyIdx, posX, posY);
} else if (hit.status === 1) { } else if (hit.status === 1) {
@ -309,6 +311,9 @@ io.on('connection', async (socket) => {
endGame(playerGame.id); endGame(playerGame.id);
return; return;
} }
} else if (hit.status === -1) {
socket.emit("toast", "Już strzeliłeś w to miejsce");
return;
} }
await GInfo.passTurn(socket); await GInfo.passTurn(socket);

View File

@ -110,9 +110,12 @@ class Battleships {
break; break;
} }
fields.forEach(field => { for (let i = 0; i < fields.length; i++) {
const field = fields[i];
setTimeout(() => {
this.getField(field[0], field[1]).addClass("active"); this.getField(field[0], field[1]).addClass("active");
}); }, i * 150);
}
return fields; return fields;
} }

View File

@ -45,6 +45,7 @@ socket.on('toast', (msg) => {
socket.on("placed ship", (data) => { socket.on("placed ship", (data) => {
let shipFields = bsc.placeShip(data); let shipFields = bsc.placeShip(data);
lastTimeClick = new Date().getTime() / 1000;
shipFields.forEach(field => { shipFields.forEach(field => {
occupiedFields.push({pos: field, origin: [data.posX, data.posY]}); occupiedFields.push({pos: field, origin: [data.posX, data.posY]});
}); });

View File

@ -110,25 +110,30 @@ export class GameInfo {
const enemyIdx = socket.request.session.id === hostId ? 1 : 0; const enemyIdx = socket.request.session.id === hostId ? 1 : 0;
// const playerIdx = enemyIdx ? 0 : 1; // 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) { if (!check) {
return { status: 0 }; return { status: 0 };
} }
var shotShip; var shotShip;
for (let i = 0; i < playerShips.length; i++) { for (let i = 0; i < playerBoard.ships.length; i++) {
const ship = playerShips[i]; const ship = playerBoard.ships[i];
if (ship.posX === check.originPosX & ship.posY === check.originPosY) { if (ship.posX === check.originPosX & ship.posY === check.originPosY) {
shotShip = ship; shotShip = ship;
playerShips[i].hits[check.fieldIdx] = true; playerBoard.ships[i].hits[check.fieldIdx] = true;
if (!playerShips[i].hits.includes(false)) { if (!playerBoard.ships[i].hits.includes(false)) {
let gameFinished = true; let gameFinished = true;
await this.redis.json.set(key, `.boards[${enemyIdx}].ships`, playerShips); await this.redis.json.set(key, `.boards[${enemyIdx}]`, playerBoard);
playerShips.every(ship => { playerBoard.ships.every(ship => {
if (ship.hits.includes(false)) { if (ship.hits.includes(false)) {
gameFinished = false; gameFinished = false;
return 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 }; return { status: 1, ship: shotShip };
} }
} }