From ee0b17206750f32d4cfa37093c060bcca26d6bfa Mon Sep 17 00:00:00 2001 From: MaciejkaG Date: Sun, 3 Mar 2024 22:59:52 +0100 Subject: [PATCH] Major changes - Added ship removing, after changing their mind or missclicking, a player can rightclick a ship to remove it - Multiple bug fixes and stability improvements --- index.js | 13 +- package-lock.json | 184 ++++++++++++++++++---------- package.json | 8 +- public/assets/js/battleships-lib.js | 4 +- public/assets/js/main.js | 8 +- public/assets/js/socket-game.js | 29 ++++- utils/battleships.js | 37 ++++-- views/layouts/main.handlebars | 2 +- 8 files changed, 198 insertions(+), 87 deletions(-) diff --git a/index.js b/index.js index 3ef75af..b1df09a 100644 --- a/index.js +++ b/index.js @@ -229,6 +229,8 @@ io.on('connection', async (socket) => { }); await redis.json.set(`game:${playerGame.id}`, '$.state', "preparation"); + } else if (io.sockets.adapter.rooms.get(playerGame.id).size > 2) { + socket.disconnect(); } } @@ -246,11 +248,20 @@ io.on('connection', async (socket) => { socket.emit("toast", "Nie masz już statków tego typu"); } else { await GInfo.placeShip(socket, { type: type, posX: posX, posY: posY, rot: rot }); - socket.emit("placed ship", { type: type, posX: posY, posY: posX, rot: rot }); + socket.emit("placed ship", { type: type, posX: posX, posY: posY, rot: rot }); } } }); + socket.on('remove ship', async (posX, posY) => { + const playerGame = await GInfo.getPlayerGameData(socket); + + if (playerGame.data.state === 'preparation') { + const deletedShip = await GInfo.removeShip(socket, posX, posY); + socket.emit("removed ship", { posX: posX, posY: posY, type: deletedShip.type }); + } + }); + socket.on('shoot', async () => { const playerGame = await GInfo.getPlayerGameData(socket); diff --git a/package-lock.json b/package-lock.json index 946fd4b..705b2c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,11 +9,11 @@ "version": "1.0.0", "license": "ISC", "dependencies": { - "express": "^4.18.2", + "express": "^4.18.3", "express-handlebars": "^7.1.2", - "express-session": "^1.17.3", - "redis": "^4.6.12", - "socket.io": "^4.7.2", + "express-session": "^1.18.0", + "redis": "^4.6.13", + "socket.io": "^4.7.4", "uuid": "^9.0.1", "uuid4": "^2.0.3" } @@ -52,9 +52,9 @@ } }, "node_modules/@redis/client": { - "version": "1.5.13", - "resolved": "https://registry.npmjs.org/@redis/client/-/client-1.5.13.tgz", - "integrity": "sha512-epkUM9D0Sdmt93/8Ozk43PNjLi36RZzG+d/T1Gdu5AI8jvghonTeLYV69WVWdilvFo+PYxbP0TZ0saMvr6nscQ==", + "version": "1.5.14", + "resolved": "https://registry.npmjs.org/@redis/client/-/client-1.5.14.tgz", + "integrity": "sha512-YGn0GqsRBFUQxklhY7v562VMOP0DcmlrHHs3IV1mFE3cbxe31IITUkqhBcIhVSI/2JqtWAJXg5mjV4aU+zD0HA==", "dependencies": { "cluster-key-slot": "1.1.2", "generic-pool": "3.9.0", @@ -175,12 +175,12 @@ } }, "node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", + "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", "dependencies": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", @@ -188,7 +188,7 @@ "iconv-lite": "0.4.24", "on-finished": "2.4.1", "qs": "6.11.0", - "raw-body": "2.5.1", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" }, @@ -227,13 +227,18 @@ } }, "node_modules/call-bind": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.5.tgz", - "integrity": "sha512-C3nQxfFZxFRVoJoGKKI8y3MOEo129NQ+FgQ08iye+Mk4zNZZGdjfs06bVTr+DBSlA66Q2VEcMki/cUCP4SercQ==", + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.7.tgz", + "integrity": "sha512-GHTSNSYICQ7scH7sZ+M2rFopRoLh8t2bLSW6BbgrtLsahOIB5iyAVJf9GjWK3cYTDaMj4XdBpM1cA6pIS0Kv2w==", "dependencies": { + "es-define-property": "^1.0.0", + "es-errors": "^1.3.0", "function-bind": "^1.1.2", - "get-intrinsic": "^1.2.1", - "set-function-length": "^1.1.1" + "get-intrinsic": "^1.2.4", + "set-function-length": "^1.2.1" + }, + "engines": { + "node": ">= 0.4" }, "funding": { "url": "https://github.com/sponsors/ljharb" @@ -337,16 +342,19 @@ } }, "node_modules/define-data-property": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/define-data-property/-/define-data-property-1.1.1.tgz", - "integrity": "sha512-E7uGkTzkk1d0ByLeSc6ZsFS79Axg+m1P/VsgYsxHgiuc3tFSj+MjMIwe90FC4lOAZzNBdY7kkO2P2wKdsQ1vgQ==", + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/define-data-property/-/define-data-property-1.1.4.tgz", + "integrity": "sha512-rBMvIzlpA8v6E+SJZoo++HAYqsLrkg7MSfIinMPFhmkorw7X+dOXVJQs+QT69zGkzMyfDnIMN2Wid1+NbL3T+A==", "dependencies": { - "get-intrinsic": "^1.2.1", - "gopd": "^1.0.1", - "has-property-descriptors": "^1.0.0" + "es-define-property": "^1.0.0", + "es-errors": "^1.3.0", + "gopd": "^1.0.1" }, "engines": { "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" } }, "node_modules/depd": { @@ -417,6 +425,25 @@ "node": ">=10.0.0" } }, + "node_modules/es-define-property": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.0.tgz", + "integrity": "sha512-jxayLKShrEqqzJ0eumQbVhTYQM27CfT1T35+gCgDFoL82JLsXqTJ76zv6A0YLOgEnLUMvLzsDsGIrl8NFpT2gQ==", + "dependencies": { + "get-intrinsic": "^1.2.4" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "engines": { + "node": ">= 0.4" + } + }, "node_modules/escape-html": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", @@ -431,13 +458,13 @@ } }, "node_modules/express": { - "version": "4.18.2", - "resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz", - "integrity": "sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==", + "version": "4.18.3", + "resolved": "https://registry.npmjs.org/express/-/express-4.18.3.tgz", + "integrity": "sha512-6VyCijWQ+9O7WuVMTRBTl+cjNNIzD5cY5mQ1WM8r/LEkI2u8EYpOotESNwzNlyCn3g+dmjKYI6BmNneSr/FSRw==", "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", - "body-parser": "1.20.1", + "body-parser": "1.20.2", "content-disposition": "0.5.4", "content-type": "~1.0.4", "cookie": "0.5.0", @@ -485,12 +512,12 @@ } }, "node_modules/express-session": { - "version": "1.17.3", - "resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.3.tgz", - "integrity": "sha512-4+otWXlShYlG1Ma+2Jnn+xgKUZTMJ5QD3YvfilX3AcocOAbIkVylSWEklzALe/+Pu4qV6TYBj5GwOBFfdKqLBw==", + "version": "1.18.0", + "resolved": "https://registry.npmjs.org/express-session/-/express-session-1.18.0.tgz", + "integrity": "sha512-m93QLWr0ju+rOwApSsyso838LQwgfs44QtOP/WBiwtAgPIo/SAh1a5c6nn2BR6mFNZehTpqKDESzP+fRHVbxwQ==", "dependencies": { - "cookie": "0.4.2", - "cookie-signature": "1.0.6", + "cookie": "0.6.0", + "cookie-signature": "1.0.7", "debug": "2.6.9", "depd": "~2.0.0", "on-headers": "~1.0.2", @@ -502,6 +529,19 @@ "node": ">= 0.8.0" } }, + "node_modules/express-session/node_modules/cookie": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz", + "integrity": "sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/express-session/node_modules/cookie-signature": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.7.tgz", + "integrity": "sha512-NXdYc3dLr47pBkpUCHtKSwIOQXLVn8dZEuywboCOJY/osA0wFSLlSawr3KN8qXJEyX66FcONTH8EIlVuK0yyFA==" + }, "node_modules/express-session/node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -614,15 +654,19 @@ } }, "node_modules/get-intrinsic": { - "version": "1.2.2", - "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.2.tgz", - "integrity": "sha512-0gSo4ml/0j98Y3lngkFEot/zhiCeWsbYIlZ+uZOVgzLyLaUw7wxUL+nCTP0XJvJg1AXulJRI3UJi8GsbDuxdGA==", + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.4.tgz", + "integrity": "sha512-5uYhsJH8VJBTv7oslg4BznJYhDoRI6waYCxMmCdnTrcCrHA/fCFKoTFz2JKKE0HdDFUF7/oQuhzumXJK7paBRQ==", "dependencies": { + "es-errors": "^1.3.0", "function-bind": "^1.1.2", "has-proto": "^1.0.1", "has-symbols": "^1.0.3", "hasown": "^2.0.0" }, + "engines": { + "node": ">= 0.4" + }, "funding": { "url": "https://github.com/sponsors/ljharb" } @@ -685,20 +729,20 @@ } }, "node_modules/has-property-descriptors": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/has-property-descriptors/-/has-property-descriptors-1.0.1.tgz", - "integrity": "sha512-VsX8eaIewvas0xnvinAe9bw4WfIeODpGYikiWYLH+dma0Jw6KHYqWiWfhQlgOVK8D6PvjubK5Uc4P0iIhIcNVg==", + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/has-property-descriptors/-/has-property-descriptors-1.0.2.tgz", + "integrity": "sha512-55JNKuIW+vq4Ke1BjOTjM2YctQIvCT7GFzHwmfZPGo5wnrgkid0YQtnAleFSqumZm4az3n2BS+erby5ipJdgrg==", "dependencies": { - "get-intrinsic": "^1.2.2" + "es-define-property": "^1.0.0" }, "funding": { "url": "https://github.com/sponsors/ljharb" } }, "node_modules/has-proto": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/has-proto/-/has-proto-1.0.1.tgz", - "integrity": "sha512-7qE+iP+O+bgF9clE5+UoBFzE65mlBiVj3tKCrlNQ0Ogwm0BjpT/gK4SlLYDMybDh5I3TCTKnPPa0oMG7JDYrhg==", + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/has-proto/-/has-proto-1.0.3.tgz", + "integrity": "sha512-SJ1amZAJUiZS+PhsVLf5tGydlaVB8EdFpaSO4gmiUKUOxk8qzn5AIy4ZeJUmh22znIdk/uMAUT2pl3FxzVUH+Q==", "engines": { "node": ">= 0.4" }, @@ -718,9 +762,9 @@ } }, "node_modules/hasown": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.0.tgz", - "integrity": "sha512-vUptKVTpIJhcczKBbgnS+RtcuYMB8+oNzPK2/Hp3hanz8JmpATdmmgLgSaadVREkDm+e2giHwY3ZRkyjSIDDFA==", + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.1.tgz", + "integrity": "sha512-1/th4MHjnwncwXsIW6QMzlvYL9kG5e/CpVvLRZe4XPa8TOUNbCELqmvhDmnkNsAjwaG4+I8gJJL0JBvTTLO9qA==", "dependencies": { "function-bind": "^1.1.2" }, @@ -1018,9 +1062,9 @@ } }, "node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", "dependencies": { "bytes": "3.1.2", "http-errors": "2.0.0", @@ -1032,12 +1076,12 @@ } }, "node_modules/redis": { - "version": "4.6.12", - "resolved": "https://registry.npmjs.org/redis/-/redis-4.6.12.tgz", - "integrity": "sha512-41Xuuko6P4uH4VPe5nE3BqXHB7a9lkFL0J29AlxKaIfD6eWO8VO/5PDF9ad2oS+mswMsfFxaM5DlE3tnXT+P8Q==", + "version": "4.6.13", + "resolved": "https://registry.npmjs.org/redis/-/redis-4.6.13.tgz", + "integrity": "sha512-MHgkS4B+sPjCXpf+HfdetBwbRz6vCtsceTmw1pHNYJAsYxrfpOP6dz+piJWGos8wqG7qb3vj/Rrc5qOlmInUuA==", "dependencies": { "@redis/bloom": "1.2.0", - "@redis/client": "1.5.13", + "@redis/client": "1.5.14", "@redis/graph": "1.1.1", "@redis/json": "1.0.6", "@redis/search": "1.1.6", @@ -1124,14 +1168,16 @@ } }, "node_modules/set-function-length": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/set-function-length/-/set-function-length-1.1.1.tgz", - "integrity": "sha512-VoaqjbBJKiWtg4yRcKBQ7g7wnGnLV3M8oLvVWwOk2PdYY6PEFegR1vezXR0tw6fZGF9csVakIRjrJiy2veSBFQ==", + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/set-function-length/-/set-function-length-1.2.1.tgz", + "integrity": "sha512-j4t6ccc+VsKwYHso+kElc5neZpjtq9EnRICFZtWyBsLojhmeF/ZBd/elqm22WJh/BziDe/SBiOeAt0m2mfLD0g==", "dependencies": { - "define-data-property": "^1.1.1", - "get-intrinsic": "^1.2.1", + "define-data-property": "^1.1.2", + "es-errors": "^1.3.0", + "function-bind": "^1.1.2", + "get-intrinsic": "^1.2.3", "gopd": "^1.0.1", - "has-property-descriptors": "^1.0.0" + "has-property-descriptors": "^1.0.1" }, "engines": { "node": ">= 0.4" @@ -1162,13 +1208,17 @@ } }, "node_modules/side-channel": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz", - "integrity": "sha512-q5XPytqFEIKHkGdiMIrY10mvLRvnQh42/+GoBlFW3b2LXLE2xxJpZFdm94we0BaoV3RwJyGqg5wS7epxTv0Zvw==", + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.6.tgz", + "integrity": "sha512-fDW/EZ6Q9RiO8eFG8Hj+7u/oW+XrPTIChwCOM2+th2A6OblDtYYIpve9m+KvI9Z4C9qSEXlaGR6bTEYHReuglA==", "dependencies": { - "call-bind": "^1.0.0", - "get-intrinsic": "^1.0.2", - "object-inspect": "^1.9.0" + "call-bind": "^1.0.7", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.4", + "object-inspect": "^1.13.1" + }, + "engines": { + "node": ">= 0.4" }, "funding": { "url": "https://github.com/sponsors/ljharb" @@ -1186,9 +1236,9 @@ } }, "node_modules/socket.io": { - "version": "4.7.2", - "resolved": "https://registry.npmjs.org/socket.io/-/socket.io-4.7.2.tgz", - "integrity": "sha512-bvKVS29/I5fl2FGLNHuXlQaUH/BlzX1IN6S+NKLNZpBsPZIDH+90eQmCs2Railn4YUiww4SzUedJ6+uzwFnKLw==", + "version": "4.7.4", + "resolved": "https://registry.npmjs.org/socket.io/-/socket.io-4.7.4.tgz", + "integrity": "sha512-DcotgfP1Zg9iP/dH9zvAQcWrE0TtbMVwXmlV4T4mqsvY+gw+LqUGPfx2AoVyRk0FLME+GQhufDMyacFmw7ksqw==", "dependencies": { "accepts": "~1.3.4", "base64id": "~2.0.0", diff --git a/package.json b/package.json index b3a426f..f61bc9e 100644 --- a/package.json +++ b/package.json @@ -19,11 +19,11 @@ }, "homepage": "https://github.com/MaciejkaG/statki-backend#readme", "dependencies": { - "express": "^4.18.2", + "express": "^4.18.3", "express-handlebars": "^7.1.2", - "express-session": "^1.17.3", - "redis": "^4.6.12", - "socket.io": "^4.7.2", + "express-session": "^1.18.0", + "redis": "^4.6.13", + "socket.io": "^4.7.4", "uuid": "^9.0.1", "uuid4": "^2.0.3" } diff --git a/public/assets/js/battleships-lib.js b/public/assets/js/battleships-lib.js index cbc9b27..1bff20d 100644 --- a/public/assets/js/battleships-lib.js +++ b/public/assets/js/battleships-lib.js @@ -12,7 +12,7 @@ class Battleships { for (var i = 0; i < size; i++) { let row = "
"; for (var n = 0; n < size; n++) { - row += `
`; + row += `
`; } row += "
"; board += row; @@ -77,5 +77,7 @@ class Battleships { fields.forEach(field => { this.getField(field[0], field[1]).children(".shipField").addClass("active"); }); + + return fields; } } \ No newline at end of file diff --git a/public/assets/js/main.js b/public/assets/js/main.js index 60d1dc7..44a637d 100644 --- a/public/assets/js/main.js +++ b/public/assets/js/main.js @@ -20,8 +20,8 @@ refreshBoardView(); $("#board .field").hover(function () { hoveredField = this; // Pokaż "miarki" - let posY = parseInt($(this).data("pos-x")); - let posX = parseInt($(this).data("pos-y")); + let posX = parseInt($(this).data("pos-x")); + let posY = parseInt($(this).data("pos-y")); let row = bsc.getRow(posY); let column = bsc.getColumn(posX); @@ -168,8 +168,8 @@ function refreshBoardView() { }); changedFields.length = 0; - let posY = parseInt($(hoveredField).data("pos-x")); - let posX = parseInt($(hoveredField).data("pos-y")); + let posX = parseInt($(hoveredField).data("pos-x")); + let posY = parseInt($(hoveredField).data("pos-y")); let row = bsc.getRow(posY); let column = bsc.getColumn(posX); diff --git a/public/assets/js/socket-game.js b/public/assets/js/socket-game.js index 456cd75..c686ecd 100644 --- a/public/assets/js/socket-game.js +++ b/public/assets/js/socket-game.js @@ -3,11 +3,20 @@ const socket = io(); var playerIdx; var timerDestination = null; var gamePhase = 'pregame'; +var occupiedFields = []; $('.field').on('click', function () { socket.emit("place ship", selectedShip, $(this).data('pos-x'), $(this).data('pos-y'), shipRotation); }); +$('.field').on('contextmenu', function () { + if ($(this).children('.shipField').hasClass('active')) { + let originPos = occupiedFields.find((elem) => elem.pos[0] == $(this).data('pos-x') && elem.pos[1] == $(this).data('pos-y')).origin; + + socket.emit("remove ship", originPos[0], originPos[1]); + } +}); + socket.on('toast', (msg) => { Toastify({ text: msg, @@ -21,11 +30,29 @@ socket.on('toast', (msg) => { }); socket.on("placed ship", (data) => { - bsc.placeShip(data); + let shipFields = bsc.placeShip(data); + shipFields.forEach(field => { + occupiedFields.push({pos: field, origin: [data.posX, data.posY]}); + }); shipsLeft[data.type]--; refreshBoardView(); }); +socket.on("removed ship", (data) => { + const shipFields = occupiedFields.filter(elem => { + return elem.origin[0] == data.posX && elem.origin[1] == data.posY; + }); + + shipFields.forEach(field => { + bsc.getField(field.pos[0], field.pos[1]).children('.shipField').removeClass("active"); + }); + + occupiedFields = shipFields.filter(n => !occupiedFields.includes(n)); + + shipsLeft[data.type]++; + refreshBoardView(); +}); + socket.on('connect', () => { $(".cover h1").html("Oczekiwanie na serwer..."); }); diff --git a/utils/battleships.js b/utils/battleships.js index 6a7d49e..6fb07db 100644 --- a/utils/battleships.js +++ b/utils/battleships.js @@ -47,10 +47,31 @@ export class GameInfo { async placeShip(socket, shipData) { const gameId = socket.session.activeGame; 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'}))[0]; const playerIdx = socket.request.session.id === hostId ? 0 : 1; - let res = 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) { + const gameId = socket.session.activeGame; + const key = `game:${gameId}`; + const hostId = (await this.redis.json.get(key, { path: '.hostId' }))[0]; + + const playerIdx = socket.request.session.id === hostId ? 0 : 1; + let playerShips = await this.redis.json.get(key, {path: `.boards[${playerIdx}].ships`}); + + var deletedShip; + playerShips = playerShips.filter(function (ship) { + if (!ship.posX != posX || ship.posY != posY) { + deletedShip = ship; + } + + return ship.posX != posX || ship.posY != posY + }); + + await this.redis.json.set(key, `.boards[${playerIdx}].ships`, playerShips); + return deletedShip; } } @@ -151,11 +172,11 @@ export function checkHit(data, playerIdx, posX, posY) { } for (let i = 0; i < ship.type + 2; i++) { - boardRender[ship.posY + multips[1] * i][ship.posX + multips[0] * i] = true; + boardRender[ship.posX + multips[1] * i][ship.posY + multips[0] * i] = true; } }); - return boardRender[posY][posX]; + return boardRender[posX][posY]; } export function validateShipPosition(ships, type, posX, posY, rot) { @@ -192,7 +213,7 @@ export function validateShipPosition(ships, type, posX, posY, rot) { } for (let i = 0; i < ship.type + 1; i++) { - boardRender[ship.posY + multips[1] * i][ship.posX + multips[0] * i] = true; + boardRender[ship.posX + multips[1] * i][ship.posY + multips[0] * i] = true; } }); @@ -217,15 +238,15 @@ export function validateShipPosition(ships, type, posX, posY, rot) { } for (let x = 0; x <= type; x++) { - if (posY + multips[1] * x > 9 || posY + multips[1] * x < 0 || posX + multips[0] * x > 9 || posX + multips[0] * x < 0) { + if (posX + multips[1] * x > 9 || posX + multips[1] * x < 0 || posY + multips[0] * x > 9 || posX + multips[0] * x < 0) { 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 for (let y = 0; y < subtrahents.length; y++) { - const idxY = posY - subtrahents[y][0] + multips[1] * x; const idxX = posX - subtrahents[y][1] + multips[0] * x; - if (!(idxY < 0 || idxY > 9 || idxX < 0 || idxX > 9) && boardRender[idxY][idxX]) { + const idxY = posY - subtrahents[y][0] + multips[1] * x; + if (!(idxX < 0 || idxX > 9 || idxY < 0 || idxY > 9) && boardRender[idxX][idxY]) { return false; } } diff --git a/views/layouts/main.handlebars b/views/layouts/main.handlebars index 1c33e88..79502a4 100644 --- a/views/layouts/main.handlebars +++ b/views/layouts/main.handlebars @@ -8,7 +8,7 @@ Statki - +