diff --git a/public/game.html b/public/game.html index 3c1a48e958a66ee89ae64dbe09f1b7bc349cf923..7da9a4ea7cae852161bdf117cb96bb11c087bbab 100644 --- a/public/game.html +++ b/public/game.html @@ -31,9 +31,9 @@ </div> <script src="/public/gameClient.js"></script> - <div id="gameInfo"> - <span id="snakeCoords">Coords: (0, 0)</span> - <span id="snakeSize">Size: 1</span> - </div> + <div id="gameInfo"> + <span id="snakeCoords">Coords: (0, 0)</span> + <span id="snakeSize">Size: 1</span> + </div> </body> </html> diff --git a/public/gameClient.js b/public/gameClient.js index a6f40a88db2771fbed760a4463242b1ed64d8ac6..9093c046ce44f6c74cea692137a808f7dc720611 100644 --- a/public/gameClient.js +++ b/public/gameClient.js @@ -31,7 +31,7 @@ let gameBoardHandler = (event) => { let board = event.data; widthStep = width / board[0].length; heightStep = height / board.length; - + let coordsDiv = document.getElementById("snakeCoords"); let sizeDiv = document.getElementById("snakeSize"); @@ -152,7 +152,7 @@ socket.addEventListener("message", (event) => { messages.appendChild(messageElement); setTimeout(() => { //Redirect back to lobby after 10 seconds - window.location.href = msg.url; + window.location = window.location; }, 10000); } } catch (e) { diff --git a/src/game/game.js b/src/game/game.js index 49e0a5e70d24f04ff37a910ecd9c04fcbbdb7964..1f81b419aed4ded5413a79babd17b0902b3650ce 100644 --- a/src/game/game.js +++ b/src/game/game.js @@ -300,52 +300,57 @@ module.exports = { return gameBoard; }, - getPlayerView: (gameBoard, pid) => { - let updatedBoard = gameBoard.map((row) => row.map((cell) => ({ ...cell }))); - - let width = 11; - let height = 11; - let head = null; - let bodySize = 0; - - for (var i = 0; i < updatedBoard.length; i++) { - for (var j = 0; j < updatedBoard[i].length; j++) { - let cell = updatedBoard[i][j]; - if (cell.type === cellTypes.PLAYERHEAD && cell.pid === pid) { - head = cell; - } - if (cell.type === cellTypes.PLAYERBODY && cell.pid === pid) { - bodySize++; - } - } - } - - if (head === null) { - return []; - } - - let playerView = []; - - for (var i = 0; i <= height; i++) { - let row = []; - for (var j = 0; j <= width; j++) { - let x = head.x + j - Math.floor(width / 2); - let y = head.y + i - Math.floor(height / 2); - - if (x < 0 || x >= updatedBoard[0].length || y < 0 || y >= updatedBoard.length) { - row.push(cellTypes.BORDER); - } else { - row.push(updatedBoard[y][x]); - } - } - playerView.push(row); - } - - // Return the view along with head position and body size - return { - view: playerView, - headPosition: { x: head.x, y: head.y }, - bodySize: bodySize + 1 // Include head in body size - }; - } -}; \ No newline at end of file + getPlayerView: (gameBoard, pid) => { + let updatedBoard = gameBoard.map((row) => row.map((cell) => ({ ...cell }))); + + let width = 11; + let height = 11; + let head = null; + let bodySize = 0; + + for (var i = 0; i < updatedBoard.length; i++) { + for (var j = 0; j < updatedBoard[i].length; j++) { + let cell = updatedBoard[i][j]; + if (cell.type === cellTypes.PLAYERHEAD && cell.pid === pid) { + head = cell; + } + if (cell.type === cellTypes.PLAYERBODY && cell.pid === pid) { + bodySize++; + } + } + } + + if (head === null) { + return []; + } + + let playerView = []; + + for (var i = 0; i <= height; i++) { + let row = []; + for (var j = 0; j <= width; j++) { + let x = head.x + j - Math.floor(width / 2); + let y = head.y + i - Math.floor(height / 2); + + if ( + x < 0 || + x >= updatedBoard[0].length || + y < 0 || + y >= updatedBoard.length + ) { + row.push(cellTypes.BORDER); + } else { + row.push(updatedBoard[y][x]); + } + } + playerView.push(row); + } + + // Return the view along with head position and body size + return { + view: playerView, + headPosition: { x: head.x, y: head.y }, + bodySize: bodySize + 1, // Include head in body size + }; + }, +}; diff --git a/src/models/WebSocketModel.js b/src/models/WebSocketModel.js index c13e4b467e05cecd099dff0ebde3174482474ca4..637128bec34ac6f3c324579a6c6ef7db9abddee6 100644 --- a/src/models/WebSocketModel.js +++ b/src/models/WebSocketModel.js @@ -42,17 +42,17 @@ class WebSocketModel { } const alivePlayers = this.games[game].players.filter( - (player) => !player.spectating + (player) => !player.spectating, ); if (alivePlayers.length === 1) { - const winner = alivePlayers[0].pid;; + const winner = alivePlayers[0].pid; for (let conn of this.games[game].players) { conn.connection.send( JSON.stringify({ type: "restart", - pid: winner + pid: winner, }), ); } @@ -79,8 +79,8 @@ class WebSocketModel { JSON.stringify({ type: "gameBoard", data: playerView.view, - headPosition: playerView.headPosition, - bodySize: playerView.bodySize + headPosition: playerView.headPosition, + bodySize: playerView.bodySize, }), ); } @@ -90,7 +90,7 @@ class WebSocketModel { this.connections = []; this.games = {}; this.sockserver = new WebSocketServer({ port: 3001 }); - this.createGame("game1", "public"); + this.createGame("game1", "public", 25); this.createGame("game2", "public"); this.createGame("game3", "public"); this.onConnection(); @@ -108,9 +108,9 @@ class WebSocketModel { return publicGames; } - createGame(gameId, gameType) { + createGame(gameId, gameType, size = 100) { this.games[gameId] = { - gameBoard: gameModule.createGameBoard(100, 100), + gameBoard: gameModule.createGameBoard(size, size), players: [], type: gameType, started: false, @@ -123,9 +123,7 @@ class WebSocketModel { resetGame(gameId) { this.games[gameId].gameBoard = gameModule.createGameBoard(100, 100); - this.games[gameId].players.forEach((player) => { - player.spectating = false; - }); + this.games[gameId].players = []; this.games[gameId].started = false; this.games[gameId].readyPlayers = 0; this.games[gameId].borderCounter = 0; @@ -144,34 +142,51 @@ class WebSocketModel { for (var j = 0; j < gameBoard[0].length; j++) { if (gameBoard[i][j].type === 1 && gameBoard[i][j].pid === pid) { console.log("Updating direction to: " + movement); - - let currentDirection = gameBoard[i][j].direction; + + let currentDirection = gameBoard[i][j].direction; let newDirection = currentDirection; - - switch (movement) { - case "up": - if (currentDirection !== 2) newDirection = 0; // Cannot move up if going down - break; - case "right": - if (currentDirection !== 3) newDirection = 1; // Cannot move right if going left - break; - case "down": - if (currentDirection !== 0) newDirection = 2; // Cannot move down if going up - break; - case "left": - if (currentDirection !== 1) newDirection = 3; // Cannot move left if going right - break; - } - if (newDirection !== currentDirection) { - console.log("Updating direction to: " + movement); - gameBoard[i][j].direction = newDirection; - } + + switch (movement) { + case "up": + if (currentDirection !== 2) newDirection = 0; // Cannot move up if going down + break; + case "right": + if (currentDirection !== 3) newDirection = 1; // Cannot move right if going left + break; + case "down": + if (currentDirection !== 0) newDirection = 2; // Cannot move down if going up + break; + case "left": + if (currentDirection !== 1) newDirection = 3; // Cannot move left if going right + break; + } + if (newDirection !== currentDirection) { + console.log("Updating direction to: " + movement); + gameBoard[i][j].direction = newDirection; + } return; } } } } + arbitrateConnections(gameId) { + console.log( + "this.games[gameId].players.length: ", + this.games[gameId].players.length, + ); + for (let i = 0; i < this.games[gameId].players.length; i++) { + if (this.games[gameId].players[i].connection === undefined) { + delete this.games[gameId].players[i]; + console.log("Client has disconnected!"); + } + } + console.log( + "this.games[gameId].players.length: ", + this.games[gameId].players.length, + ); + } + onConnection() { this.sockserver.on("connection", (connection) => { console.log("New client connected!"); @@ -180,6 +195,8 @@ class WebSocketModel { let roomId = connection.protocol; + //this.arbitrateConnections(roomId); + if ( !this.games[roomId].started && this.games[roomId].players.length < 10 @@ -269,7 +286,7 @@ class WebSocketModel { for (let conn of this.games[roomId].players) { if (conn.connection !== connection) conn.connection.send( - JSON.stringify({ type: "playerReady", data: message.data }), + JSON.stringify({ type: "playerReady", pid: message.pid }), ); } this.games[roomId].readyPlayers++;