Fixed many bugs, placing ships now works

This commit is contained in:
Maciej Gomoła 2024-03-04 12:23:47 +01:00
parent ee0b172067
commit 4629484a15
4 changed files with 32 additions and 23 deletions

3
.gitignore vendored
View File

@ -128,3 +128,6 @@ dist
.yarn/build-state.yml .yarn/build-state.yml
.yarn/install-state.gz .yarn/install-state.gz
.pnp.* .pnp.*
# Redis
dump.rdb

View File

@ -1,3 +1,5 @@
const PORT = 3737;
import express from 'express'; import express from 'express';
import { createServer } from 'node:http'; import { createServer } from 'node:http';
import { Server } from 'socket.io'; import { Server } from 'socket.io';
@ -266,7 +268,7 @@ io.on('connection', async (socket) => {
const playerGame = await GInfo.getPlayerGameData(socket); const playerGame = await GInfo.getPlayerGameData(socket);
if (playerGame.data.state === 'action') { if (playerGame.data.state === 'action') {
if (bships.checkTurn(playerGame, socket.id)) { if (bships.checkTurn(playerGame, socket.request.session.id)) {
} }
} }
@ -281,8 +283,8 @@ io.on('connection', async (socket) => {
} }
}); });
server.listen(7777, () => { server.listen(PORT, () => {
console.log('Server running at http://localhost:7777'); console.log(`Server running at http://localhost:${PORT}`);
}); });
function genID() { function genID() {

View File

@ -47,7 +47,9 @@ socket.on("removed ship", (data) => {
bsc.getField(field.pos[0], field.pos[1]).children('.shipField').removeClass("active"); bsc.getField(field.pos[0], field.pos[1]).children('.shipField').removeClass("active");
}); });
occupiedFields = shipFields.filter(n => !occupiedFields.includes(n)); console.log(occupiedFields);
occupiedFields = occupiedFields.filter(n => !shipFields.includes(n));
console.log(occupiedFields);
shipsLeft[data.type]++; shipsLeft[data.type]++;
refreshBoardView(); refreshBoardView();

View File

@ -47,18 +47,21 @@ export class GameInfo {
async placeShip(socket, shipData) { async placeShip(socket, shipData) {
const gameId = socket.session.activeGame; const gameId = socket.session.activeGame;
const key = `game:${gameId}`; const key = `game:${gameId}`;
const hostId = (await this.redis.json.get(key, {path: '.hostId'}))[0]; const hostId = (await this.redis.json.get(key, {path: '.hostId'}));
const playerIdx = socket.request.session.id === hostId ? 0 : 1; const playerIdx = socket.request.session.id === hostId ? 0 : 1;
console.log(socket.request.session.id, "=", hostId);
console.log("placeship", playerIdx);
await this.redis.json.arrAppend(key, `.boards[${playerIdx}].ships`, shipData); await this.redis.json.arrAppend(key, `.boards[${playerIdx}].ships`, shipData);
} }
async removeShip(socket, posX, posY) { async removeShip(socket, posX, posY) {
const gameId = socket.session.activeGame; const gameId = socket.session.activeGame;
const key = `game:${gameId}`; const key = `game:${gameId}`;
const hostId = (await this.redis.json.get(key, { path: '.hostId' }))[0]; const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
const playerIdx = socket.request.session.id === hostId ? 0 : 1; const playerIdx = socket.request.session.id === hostId ? 0 : 1;
console.log("removeship", playerIdx);
let playerShips = await this.redis.json.get(key, {path: `.boards[${playerIdx}].ships`}); let playerShips = await this.redis.json.get(key, {path: `.boards[${playerIdx}].ships`});
var deletedShip; var deletedShip;
@ -191,29 +194,28 @@ export function validateShipPosition(ships, type, posX, posY, rot) {
} }
ships.forEach(ship => { ships.forEach(ship => {
let multips; let multips;
switch (ship.rot) { switch (ship.rot) {
case 0: case 0:
multips = [0, 1];
break;
case 1:
multips = [1, 0]; multips = [1, 0];
break; break;
case 1:
multips = [0, 1];
break;
case 2: case 2:
multips = [0, -1]; multips = [-1, 0];
break; break;
case 3: case 3:
multips = [-1, 0]; multips = [0, -1];
break; break;
} }
for (let i = 0; i < ship.type + 1; i++) { for (let i = 0; i < ship.type + 1; i++) {
boardRender[ship.posX + multips[1] * i][ship.posY + multips[0] * i] = true; boardRender[ship.posX + multips[0] * i][ship.posY + multips[1] * i] = true;
} }
}); });
@ -221,31 +223,31 @@ export function validateShipPosition(ships, type, posX, posY, rot) {
switch (rot) { switch (rot) {
case 0: case 0:
multips = [0, 1];
break;
case 1:
multips = [1, 0]; multips = [1, 0];
break; break;
case 1:
multips = [0, 1];
break;
case 2: case 2:
multips = [0, -1]; multips = [-1, 0];
break; break;
case 3: case 3:
multips = [-1, 0]; multips = [0, -1];
break; break;
} }
for (let x = 0; x <= type; x++) { for (let x = 0; x <= type; x++) {
if (posX + multips[1] * x > 9 || posX + multips[1] * x < 0 || posY + multips[0] * x > 9 || posX + multips[0] * x < 0) { if (posX + multips[0] * x > 9 || posX + multips[0] * x < 0 || posY + multips[1] * x > 9 || posY + multips[1] * x < 0) {
return false; return false;
} }
let subtrahents = [[0, 0], [0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [1, -1], [-1, 1]]; // Usuń cztery ostatnie elementy jeżeli chcesz by statki mogły się stykać rogami let subtrahents = [[0, 0], [0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [1, -1], [-1, 1]]; // Usuń cztery ostatnie elementy jeżeli chcesz by statki mogły się stykać rogami
for (let y = 0; y < subtrahents.length; y++) { for (let y = 0; y < subtrahents.length; y++) {
const idxX = posX - subtrahents[y][1] + multips[0] * x; const idxX = posX - subtrahents[y][0] + multips[0] * x;
const idxY = posY - subtrahents[y][0] + multips[1] * x; const idxY = posY - subtrahents[y][1] + multips[1] * x;
if (!(idxX < 0 || idxX > 9 || idxY < 0 || idxY > 9) && boardRender[idxX][idxY]) { if (!(idxX < 0 || idxX > 9 || idxY < 0 || idxY > 9) && boardRender[idxX][idxY]) {
return false; return false;
} }