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
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);

View File

@ -110,9 +110,12 @@ class Battleships {
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");
});
}, i * 150);
}
return fields;
}

View File

@ -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]});
});

View File

@ -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 };
}
}