diff --git a/app.js b/app.js index e71be10317d2a6c145dfec0e596507ee828fd3c6..e4614fab940b64093697c895737c8fa350cf20dd 100644 --- a/app.js +++ b/app.js @@ -67,39 +67,31 @@ let moveHandler = (connection, message) => { console.log("Received move: " + message.data); let movement = message.data; let pid = message.pid; + let gameBoard = games[connection.protocol].gameBoard; console.log("pid: ", pid); for (var i = 0; i < gameBoard.length; i++) { for (var j = 0; j < gameBoard[0].length; j++) { - if (movement === "up" && gameBoard[i][j] === pid && i > 0) { - gameBoard[i][j] = 0; - gameBoard[i - 1][j] = pid; - return; - } - - if ( - movement === "down" && - gameBoard[i][j] === pid && - i < gameBoard.length - 1 - ) { - gameBoard[i][j] = 0; - gameBoard[i + 1][j] = pid; - return; - } - - if (movement === "left" && gameBoard[i][j] === pid && j > 0) { - gameBoard[i][j] = 0; - gameBoard[i][j - 1] = pid; - return; - } - - if ( - movement === "right" && - gameBoard[i][j] === pid && - j < gameBoard[0].length - 1 - ) { - gameBoard[i][j] = 0; - gameBoard[i][j + 1] = pid; + if (gameBoard[i][j].type === 1 && gameBoard[i][j].pid === pid) { + console.log("Updating direction to: " + movement); + switch (movement) { + case "up": + gameBoard[i][j].direction = 0; + break; + case "right": + console.log("1"); + console.log(gameBoard[i][j]); + gameBoard[i][j].direction = 1; + cell.direction = 1; + break; + case "down": + gameBoard[i][j].direction = 2; + break; + case "left": + gameBoard[i][j].direction = 3; + break; + } + console.log("New direction set to: " + gameBoard[i][j].direction); return; } } @@ -136,6 +128,7 @@ sockserver.on("connection", (connection) => { // All messages are expected to have a type if (message.type === "move") { console.log("Received move: " + message.data); + moveHandler(connection, message); } else if (message.type === "chat") { console.log("Received chat: " + message.data); } else if (message.type === "join") { @@ -146,7 +139,7 @@ sockserver.on("connection", (connection) => { console.log("Received: " + message); } } catch (e) { - console.log("Error parsing JSON: " + message); + console.log("Error parsing JSON: " + e.message); } });