From 6845c05eee6ce0edd219647635f714740afcb722 Mon Sep 17 00:00:00 2001 From: viastolfi Date: Mon, 12 Aug 2024 21:08:46 +0200 Subject: [PATCH] :goal_net: All errors are handled (i believe) --- businesses/Room.js | 20 ++- index.js | 263 ++++++++++++++++++++----------- package-lock.json | 298 ++++++++++++++++++++++++++++++++++++ package.json | 1 + public/pages/errorView.html | 15 ++ public/pages/gameView.html | 4 - public/scripts/app.js | 63 ++++++-- public/scripts/error.js | 3 + public/scripts/game.js | 47 +++++- 9 files changed, 586 insertions(+), 128 deletions(-) create mode 100644 public/pages/errorView.html create mode 100644 public/scripts/error.js diff --git a/businesses/Room.js b/businesses/Room.js index dc8a094..bfd4622 100644 --- a/businesses/Room.js +++ b/businesses/Room.js @@ -69,16 +69,20 @@ class Room { } validBoards() { - this.players.forEach((player) => { - // sometimes i get error here - player.pieces.forEach((piece) => { - for (let i = piece.startPos.x; i <= piece.endPos.x; i++) { - for (let j = piece.startPos.y; j <= piece.endPos.y; j++) { - player.grid.cases[i][j].isShip = true; + try { + this.players.forEach((player) => { + // sometimes i get error here + player.pieces.forEach((piece) => { + for (let i = piece.startPos.x; i <= piece.endPos.x; i++) { + for (let j = piece.startPos.y; j <= piece.endPos.y; j++) { + player.grid.cases[i][j].isShip = true; + } } - } + }); }); - }); + } catch { + throw new Error() + } } generateRoomId() { diff --git a/index.js b/index.js index 4b6fb87..452b2b3 100644 --- a/index.js +++ b/index.js @@ -1,37 +1,41 @@ +const path = require("path"); const express = require("express"); const app = express(); -const http = require("http").Server(app); -const io = require("socket.io")(http); -const port = 3000; -const db = require("./database.js") const jwt = require('jsonwebtoken'); const cookieParser = require('cookie-parser'); const bodyParser = require("body-parser"); -const path = require("path"); const bcrypt = require('bcrypt'); +const http = require("http").Server(app); +const io = require("socket.io")(http); -const { Room } = require('./businesses/Room.js'); -const { Player } = require(`${__dirname}/businesses/Player.js`); +const db = require(path.normalize(`${__dirname}/database.js`)) +const { Room } = require(path.normalize(`${__dirname}/businesses/Room.js`)); +const { Player } = require(path.normalize(`${__dirname}/businesses/Player.js`)); +const secretKey = process.env.COOKIE_SECRET_KEY; +const port = 3000; app.use(express.static("public")) app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(cookieParser()); -const secretKey = process.env.COOKIE_SECRET_KEY; - // #region routing and cookies app.get('/', (req, res) => { - return res.sendFile(path.join(__dirname, '/public/pages/connectionView.html')) + return res.sendFile(path.normalize(path.join(__dirname, '/public/pages/connectionView.html'))) }) app.get('/register', (req, res) => { - return res.sendFile(path.join(__dirname, '/public/pages/signupView.html')) + return res.sendFile(path.normalize(path.join(__dirname, '/public/pages/signupView.html'))) }) app.get('/game', (req, res) => { - return res.sendFile(path.join(__dirname, '/public/pages/gameView.html')) + return res.sendFile(path.normalize(path.join(__dirname, '/public/pages/gameView.html'))) +}) + +app.get('/error', (req, res) => { + res.status(200) + return res.sendFile(path.normalize(path.join(__dirname, '/public/pages/errorView.html'))) }) app.post('/logIn', (req, res) => { @@ -162,10 +166,22 @@ io.on("connection", (socket) => { console.log(`Player disconnected: ${socket.id}`); }) - socket.on('delete room', (roomId) => { - const roomIndex = rooms.findIndex((r) => r.id === roomId) - rooms.splice(roomIndex, 1) - console.log(rooms) + socket.on("handle error", (id, roomId) => { + const room = rooms.find((r) => r.id === roomId) + + console.log(room) + + if (room != null) { + const playerIndex = room.players.findIndex((p) => p.id === id) + room.players.splice(playerIndex, 1) + + if (room.players.length > 0) { + io.to(room.players[0].id).emit("opponent left") + } else { + const roomIndex = rooms.findIndex((r) => r.id === roomId) + rooms.splice(roomIndex, 1) + } + } }) socket.on("first connection", (socketId) => { @@ -202,49 +218,56 @@ io.on("connection", (socket) => { }); }); - socket.on("ask for room", (roomId, id, callack) => { + socket.on("ask for room", (roomId, id, callback) => { let room = rooms.find((r) => r.id === roomId); - if (room == null) { - callack({ - status: false, - message: "No room for this code" + try { + if (room == null) { + callback({ + status: false, + message: "No room for this code" + }) + return + } + + if (room.players.length >= 2) { + callback({ + status: false, + message: "Room is full" + }) + return + } + + callback({ + status: true }) - return - } - - if (room.players.length >= 2) { - callack({ + + room.addPlayer(players.find((p) => p.id === id)); + room.validBoards(); + + for (let i = 0; i < room.players.length; i++) { + io.to(room.players[i].id).emit("start game") + } + + askToPlay(room.start()); + } catch { + callback({ status: false, - message: "Room is full" + reason: "exception" }) - return - } - - callack({ - status: true - }) - - room.addPlayer(players.find((p) => p.id === id)); - room.validBoards(); - - for (let i = 0; i < room.players.length; i++) { - io.to(room.players[i].id).emit("start game") } - - /* - room.players.forEach((player) => { - io.to(player.id).emit("start game"); - }); - */ - - askToPlay(room.start()); }); - socket.on("play", (roomId, id, move) => { + socket.on("play", (roomId, id, move, callback) => { let room = rooms.find((r) => r.id === roomId); - sendMoveToPlayers(room.move(move)); + try { + sendMoveToPlayers(room.move(move)); + } catch { + callback({ + status: false + }) + } }); socket.on("get player", (roomId, id, callback) => { @@ -262,83 +285,141 @@ io.on("connection", (socket) => { }); }); - socket.on("get ennemy", (roomId, id, callack) => { + socket.on("get ennemy", (roomId, id, callback) => { const room = rooms.find((r) => r.id === roomId); - const out = room.players.find((p) => p.id !== id); - callack({ - player: out, - }); + try { + const out = room.players.find((p) => p.id !== id); + + callback({ + status: true, + player: out, + }); + } catch { + callback({ + status: false + }) + } }); socket.on("update grid", (id, grid, callback) => { const player = players.find((p) => p.id === id); - player.grid = grid; - callback({ - status: true, - }); + try { + player.grid = grid; + callback({ + status: true, + }); + } catch { + callback({ + status: false + }) + } }); - socket.on("game ended", (roomId) => { + socket.on("game ended", (roomId, callback) => { const roomIndex = rooms.findIndex((r) => r.id === roomId) - rooms[roomIndex].players.forEach(player => { - player.resetGrid() - io.to(player.id).emit("go to menu") - }); - - rooms.splice(roomIndex, 1) + try { + rooms[roomIndex].players.forEach(player => { + player.resetGrid() + io.to(player.id).emit("go to menu") + }); + + rooms.splice(roomIndex, 1) + } catch { + callback({ + status: false + }) + } }) - socket.on("reset grid", (roomId) => { + socket.on("reset grid", (roomId, callback) => { const player = rooms.find((r) => r.id === roomId).players[0] - player.resetGrid(); + try { + player.resetGrid(); + } catch { + callback({ + status: false + }) + } }) - socket.on("update piece", (playerId, piece) => { + socket.on("update piece", (playerId, piece, callback) => { const player = players.find((p) => p.id === playerId); - const index = player.pieces.findIndex((p) => p.id === piece.id); - player.pieces[index] = piece; + try { + const index = player.pieces.findIndex((p) => p.id === piece.id); + + player.pieces[index] = piece; + } catch { + callback({ + status: false + }) + } }); - socket.on("change selection status", (playerId, pieceId, status) => { - players - .find((p) => p.id === playerId) - .pieces.find((piece) => piece.id === pieceId).isSelected = status; + socket.on("change selection status", (playerId, pieceId, status, callback) => { + try { + players + .find((p) => p.id === playerId) + .pieces.find((piece) => piece.id === pieceId).isSelected = status; + } catch { + callback({ + status: false + }) + } }); // #region rematch hanlding - socket.on("ask for rematch", (roomId, playerId) => { + socket.on("ask for rematch", (roomId, playerId, callback) => { const room = rooms.find((r) => r.id === roomId) - const opponent = room.players.find((p) => p.id !== playerId) - io.to(opponent.id).emit("ask for rematch"); + try { + const opponent = room.players.find((p) => p.id !== playerId) + + io.to(opponent.id).emit("ask for rematch"); + } catch { + callback({ + statu: false + }) + } }) - socket.on("rematch grid", (roomId) => { + socket.on("rematch grid", (roomId, callback) => { const room = rooms.find((r) => r.id === roomId) - room.players.forEach(p => { - p.resetGrid() - io.to(p.id).emit("rematch grid") - }) + try { + room.players.forEach(p => { + p.resetGrid() + io.to(p.id).emit("rematch grid") + }) + } catch { + callback({ + status: false + }) + } }) - socket.on("valid grid", (roomId) => { + socket.on("valid grid", (roomId, callback) => { const room = rooms.find((r) => r.id === roomId) - room.wantRematch = room.wantRematch + 1 - - if (room.wantRematch === 2) { - room.validBoards(); - room.wantRematch = 0 - room.players.forEach(p => { - io.to(p.id).emit("start game") + try { + room.wantRematch = room.wantRematch + 1 + + if (room.wantRematch === 2) { + room.validBoards(); + room.wantRematch = 0 + room.players.forEach(p => { + io.to(p.id).emit("start game") + }) + askToPlay(room.start()); + } + } catch { + callback({ + status: false }) - askToPlay(room.start()); } }) diff --git a/package-lock.json b/package-lock.json index 34309e8..39ff853 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "jsonwebtoken": "^9.0.2", "mysql": "^2.18.1", "mysql2": "^3.11.0", + "nodemon": "^3.1.4", "socket.io": "^4.7.5", "uuid": "^9.0.1" } @@ -133,6 +134,19 @@ "node": ">=8" } }, + "node_modules/anymatch": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", + "integrity": "sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==", + "license": "ISC", + "dependencies": { + "normalize-path": "^3.0.0", + "picomatch": "^2.0.4" + }, + "engines": { + "node": ">= 8" + } + }, "node_modules/aproba": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/aproba/-/aproba-2.0.0.tgz", @@ -220,6 +234,18 @@ "node": "*" } }, + "node_modules/binary-extensions": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.3.0.tgz", + "integrity": "sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==", + "license": "MIT", + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/body-parser": { "version": "1.20.2", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", @@ -254,6 +280,18 @@ "concat-map": "0.0.1" } }, + "node_modules/braces": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", + "license": "MIT", + "dependencies": { + "fill-range": "^7.1.1" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/buffer-equal-constant-time": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", @@ -288,6 +326,30 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/chokidar": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz", + "integrity": "sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==", + "license": "MIT", + "dependencies": { + "anymatch": "~3.1.2", + "braces": "~3.0.2", + "glob-parent": "~5.1.2", + "is-binary-path": "~2.1.0", + "is-glob": "~4.0.1", + "normalize-path": "~3.0.0", + "readdirp": "~3.6.0" + }, + "engines": { + "node": ">= 8.10.0" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + }, + "optionalDependencies": { + "fsevents": "~2.3.2" + } + }, "node_modules/chownr": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/chownr/-/chownr-2.0.0.tgz", @@ -625,6 +687,18 @@ "node": ">= 0.6" } }, + "node_modules/fill-range": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", + "license": "MIT", + "dependencies": { + "to-regex-range": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/finalhandler": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz", @@ -691,6 +765,20 @@ "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", "license": "ISC" }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, "node_modules/function-bind": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", @@ -770,6 +858,18 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/glob-parent": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", + "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", + "license": "ISC", + "dependencies": { + "is-glob": "^4.0.1" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/gopd": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.0.1.tgz", @@ -782,6 +882,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==", + "license": "MIT", + "engines": { + "node": ">=4" + } + }, "node_modules/has-property-descriptors": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/has-property-descriptors/-/has-property-descriptors-1.0.2.tgz", @@ -900,6 +1009,12 @@ "node": ">=0.10.0" } }, + "node_modules/ignore-by-default": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/ignore-by-default/-/ignore-by-default-1.0.1.tgz", + "integrity": "sha512-Ius2VYcGNk7T90CppJqcIkS5ooHUZyIQK+ClZfMfMNFEF9VSE73Fq+906u/CWu92x4gzZMWOwfFYckPObzdEbA==", + "license": "ISC" + }, "node_modules/inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", @@ -926,6 +1041,27 @@ "node": ">= 0.10" } }, + "node_modules/is-binary-path": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", + "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", + "license": "MIT", + "dependencies": { + "binary-extensions": "^2.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/is-extglob": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", + "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/is-fullwidth-code-point": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", @@ -935,6 +1071,27 @@ "node": ">=8" } }, + "node_modules/is-glob": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", + "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", + "license": "MIT", + "dependencies": { + "is-extglob": "^2.1.1" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "license": "MIT", + "engines": { + "node": ">=0.12.0" + } + }, "node_modules/is-property": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz", @@ -1316,6 +1473,57 @@ } } }, + "node_modules/nodemon": { + "version": "3.1.4", + "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.1.4.tgz", + "integrity": "sha512-wjPBbFhtpJwmIeY2yP7QF+UKzPfltVGtfce1g/bB15/8vCGZj8uxD62b/b9M9/WVgme0NZudpownKN+c0plXlQ==", + "license": "MIT", + "dependencies": { + "chokidar": "^3.5.2", + "debug": "^4", + "ignore-by-default": "^1.0.1", + "minimatch": "^3.1.2", + "pstree.remy": "^1.1.8", + "semver": "^7.5.3", + "simple-update-notifier": "^2.0.0", + "supports-color": "^5.5.0", + "touch": "^3.1.0", + "undefsafe": "^2.0.5" + }, + "bin": { + "nodemon": "bin/nodemon.js" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/nodemon" + } + }, + "node_modules/nodemon/node_modules/debug": { + "version": "4.3.6", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.6.tgz", + "integrity": "sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg==", + "license": "MIT", + "dependencies": { + "ms": "2.1.2" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/nodemon/node_modules/ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", + "license": "MIT" + }, "node_modules/nopt": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz", @@ -1331,6 +1539,15 @@ "node": ">=6" } }, + "node_modules/normalize-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/npmlog": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/npmlog/-/npmlog-5.0.1.tgz", @@ -1410,6 +1627,18 @@ "integrity": "sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ==", "license": "MIT" }, + "node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "license": "MIT", + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, "node_modules/process-nextick-args": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", @@ -1429,6 +1658,12 @@ "node": ">= 0.10" } }, + "node_modules/pstree.remy": { + "version": "1.1.8", + "resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz", + "integrity": "sha512-77DZwxQmxKnu3aR542U+X8FypNzbfJ+C5XQDk3uWjWxn6151aIMGthWYRXTqT1E5oJvg+ljaa2OJi+VfvCOQ8w==", + "license": "MIT" + }, "node_modules/qs": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", @@ -1489,6 +1724,18 @@ "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", "license": "MIT" }, + "node_modules/readdirp": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", + "integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==", + "license": "MIT", + "dependencies": { + "picomatch": "^2.2.1" + }, + "engines": { + "node": ">=8.10.0" + } + }, "node_modules/rimraf": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", @@ -1646,6 +1893,18 @@ "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", "license": "ISC" }, + "node_modules/simple-update-notifier": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/simple-update-notifier/-/simple-update-notifier-2.0.0.tgz", + "integrity": "sha512-a2B9Y0KlNXl9u/vsW6sTIu9vGEpfKu2wRV6l1H3XEas/0gUIzGzBoP/IouTcUQbm9JWZLH3COxyn03TYlFax6w==", + "license": "MIT", + "dependencies": { + "semver": "^7.5.3" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/socket.io": { "version": "4.7.5", "resolved": "https://registry.npmjs.org/socket.io/-/socket.io-4.7.5.tgz", @@ -1815,6 +2074,18 @@ "node": ">=8" } }, + "node_modules/supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "license": "MIT", + "dependencies": { + "has-flag": "^3.0.0" + }, + "engines": { + "node": ">=4" + } + }, "node_modules/tar": { "version": "6.2.1", "resolved": "https://registry.npmjs.org/tar/-/tar-6.2.1.tgz", @@ -1832,6 +2103,18 @@ "node": ">=10" } }, + "node_modules/to-regex-range": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "license": "MIT", + "dependencies": { + "is-number": "^7.0.0" + }, + "engines": { + "node": ">=8.0" + } + }, "node_modules/toidentifier": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", @@ -1841,6 +2124,15 @@ "node": ">=0.6" } }, + "node_modules/touch": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/touch/-/touch-3.1.1.tgz", + "integrity": "sha512-r0eojU4bI8MnHr8c5bNo7lJDdI2qXlWWJk6a9EAFG7vbhTjElYhBVS3/miuE0uOuoLdb8Mc/rVfsmm6eo5o9GA==", + "license": "ISC", + "bin": { + "nodetouch": "bin/nodetouch.js" + } + }, "node_modules/tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", @@ -1860,6 +2152,12 @@ "node": ">= 0.6" } }, + "node_modules/undefsafe": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.5.tgz", + "integrity": "sha512-WxONCrssBM8TSPRqN5EmsjVrsv4A8X12J4ArBiiayv3DyyG3ZlIg6yysuuSYdZsVz3TKcTg2fd//Ujd4CHV1iA==", + "license": "MIT" + }, "node_modules/undici-types": { "version": "6.13.0", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.13.0.tgz", diff --git a/package.json b/package.json index 7bf64ae..06004c0 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "jsonwebtoken": "^9.0.2", "mysql": "^2.18.1", "mysql2": "^3.11.0", + "nodemon": "^3.1.4", "socket.io": "^4.7.5", "uuid": "^9.0.1" } diff --git a/public/pages/errorView.html b/public/pages/errorView.html new file mode 100644 index 0000000..652b836 --- /dev/null +++ b/public/pages/errorView.html @@ -0,0 +1,15 @@ + + + + + + Error + + + An error occured + + + + + + \ No newline at end of file diff --git a/public/pages/gameView.html b/public/pages/gameView.html index 803f58b..e29241d 100644 --- a/public/pages/gameView.html +++ b/public/pages/gameView.html @@ -83,10 +83,6 @@ Validate ! - -

diff --git a/public/scripts/app.js b/public/scripts/app.js index c87d6d3..8aad78b 100644 --- a/public/scripts/app.js +++ b/public/scripts/app.js @@ -3,7 +3,7 @@ import { drawGrid, drawEnnemyGrid, play, selectPiece } from "./game.js"; export const socket = io(); export let roomId = ""; -function startConnection() { +socket.on("connect", () => { socket.emit("first connection", socket.id) drawGrid(); @@ -15,6 +15,13 @@ function startConnection() { document .querySelector("#join") .addEventListener("click", onJoinRoom()); +}) + + +export function handleError() { + console.log("an error occurs") + socket.emit("handle error", socket.id, roomId) + window.location.href = "/error" } socket.on("start game", () => { @@ -113,7 +120,10 @@ document.getElementById("validGrid").addEventListener('click', () => { const waitChoiceModal = document.getElementById("waitChoiceModal") waitChoiceModal.style.display = 'block' - socket.emit("valid grid", roomId) + socket.emit("valid grid", roomId, (response) => { + if (response.status === false) { + } + }) }) // #endregion rematch handling @@ -153,7 +163,11 @@ function gameEnd() { export function sendMove(move) { const notification = document.querySelector("#play_notification"); - socket.emit("play", roomId, socket.id, move); + socket.emit("play", roomId, socket.id, move, (response) => { + if (response.status === false) { + handleError() + } + }); notification.style.display = 'none' } @@ -186,6 +200,9 @@ function onJoinRoom() { socket.emit("ask for room", roomKey, socket.id, (response) => { if (response.status !== true) { + if (response.reason === "exception") { + handleError() + } errorHolder.textContent = "Error : " + response.message } else { loader.style.display = "none"; @@ -201,7 +218,11 @@ document.getElementById('closeModalButton').addEventListener('click', () => { const modal = document.getElementById('opponentLeftModal'); modal.style.display = 'none'; - socket.emit("reset grid", roomId) + socket.emit("reset grid", roomId, (respone) => { + if (respone.status === false) { + handleError() + } + }) goToMenu() }); @@ -210,19 +231,29 @@ document.getElementById('acceptButton').addEventListener('click', () => { const rematchModal = document.getElementById('rematchModal') rematchModal.style.display = 'none' - socket.emit('rematch grid', roomId) + socket.emit('rematch grid', roomId, (response) => { + if (response.status === false) { + handleError() + } + }) }) +function sendGameEnded() { + socket.emit("game ended", roomId, (response) => { + if (response.status === false) { + handleError() + } + }); +} + document.getElementById('goToMenuButton').addEventListener('click', () => { - socket.emit("game ended", roomId); + sendGameEnded() }) document.getElementById('goToMenuButton2').addEventListener('click', () => { - socket.emit("game ended", roomId); + sendGameEnded() }) -//document.getElementById('acceptButton').addEventListener('') - document.getElementById('rematchButton').addEventListener('click', () => { const endGameModal = document.getElementById('gameEndedModal') const waitChoicemodal = document.getElementById('waitChoiceModal'); @@ -230,11 +261,9 @@ document.getElementById('rematchButton').addEventListener('click', () => { waitChoicemodal.style.display = 'block' endGameModal.style.display = 'none' - socket.emit("ask for rematch", roomId, socket.id) -}) - -document.getElementById('resetRoom').addEventListener('click', () => { - socket.emit("delete room", roomId) -}) - -setTimeout(startConnection, 100); \ No newline at end of file + socket.emit("ask for rematch", roomId, socket.id, (response) => { + if (response.status === false) { + handleError() + } + }) +}) \ No newline at end of file diff --git a/public/scripts/error.js b/public/scripts/error.js new file mode 100644 index 0000000..ec1095c --- /dev/null +++ b/public/scripts/error.js @@ -0,0 +1,3 @@ +document.getElementById("goToHomeButton").addEventListener('click', () => { + window.location.href = "/" +}) \ No newline at end of file diff --git a/public/scripts/game.js b/public/scripts/game.js index ade024a..1a0db50 100644 --- a/public/scripts/game.js +++ b/public/scripts/game.js @@ -1,4 +1,4 @@ -import { sendMove, socket, roomId } from "./app.js"; +import { sendMove, socket, roomId, handleError } from "./app.js"; const ownCanvas = document.getElementById("own_board"); const ownCtx = ownCanvas.getContext("2d"); @@ -12,6 +12,9 @@ let selectedPiece = ""; export function drawGrid() { ownCtx.strokeStyle = "black"; socket.emit("get player", roomId, socket.id, (response) => { + if (response.status === false) { + handleError() + } let player = response.player; player.pieces.forEach((piece) => { for (let i = piece.startPos.x; i <= piece.endPos.x; i++) { @@ -78,6 +81,10 @@ export function drawEnnemyGrid() { ennemyCtx.strokeStyle = "red"; socket.emit("get ennemy", roomId, socket.id, (response) => { + if (response.status === false) { + handleError() + } + let player = response.player; for (let i = 0; i < player.grid.cases.length; i++) { for (let j = 0; j < player.grid.cases.length; j++) { @@ -217,6 +224,9 @@ function clickNewCase(piece) { let selectedCase = getCursorPosition(ownCanvas, event); socket.emit("get player", " ", socket.id, (response) => { + if (response.status === false) { + handleError() + } let player = response.player; player.pieces.forEach((p) => { if ( @@ -252,12 +262,16 @@ function clickNewCase(piece) { player.grid.cases[i][j].isShip = true; } } - socket.emit("update piece", socket.id, p); + socket.emit("update piece", socket.id, p, (response) => { + if (response.status === false) { + handleError() + } + }); socket.emit("update grid", socket.id, player.grid, (response) => { if (response.status === true) { drawGrid(); } else { - // TODO : error handling + handleError() } }); } @@ -273,6 +287,9 @@ function rotatePiece(piece) { event.preventDefault(); socket.emit("get player", " ", socket.id, (response) => { + if (response.status === false) { + handleError() + } let player = response.player; player.pieces.forEach((p) => { if ( @@ -305,12 +322,16 @@ function rotatePiece(piece) { } } } - socket.emit("update piece", socket.id, p); + socket.emit("update piece", socket.id, p, (response) => { + if (response.status === false) { + handleError() + } + }); socket.emit("update grid", socket.id, player.grid, (response) => { if (response.status === true) { drawGrid(); } else { - // TODO : error handling + handleError() } }); }); @@ -324,7 +345,9 @@ function clickChoose(event) { let selectedCase = getCursorPosition(ownCanvas, event); socket.emit("get player", " ", socket.id, (response) => { - // Sometimes i get an error there + if (response.status === false) { + handleError() + } let player = response.player; if (player.grid.cases[selectedCase.col][selectedCase.row].isShip) { const rotate_button = document.querySelector("#rotate"); @@ -335,11 +358,19 @@ function clickChoose(event) { let oldPieceId = selectedPiece.id; player.pieces.forEach((p) => { if (p.id === oldPieceId) { - socket.emit("change selection status", socket.id, p.id, false); + socket.emit("change selection status", socket.id, p.id, false, (response) => { + if (response.status === false) { + handleError() + } + }); } if (p.id === piece.id) { selectedPiece = p; - socket.emit("change selection status", socket.id, p.id, true); + socket.emit("change selection status", socket.id, p.id, true, (response) => { + if (response.status === false) { + handleError() + } + }); } }); }