From b71af2f6d47d8cef0c2a86e53a77832d8aa7e980 Mon Sep 17 00:00:00 2001
From: zm343 <2639-zm343@users.noreply.gitlab.cci.drexel.edu>
Date: Sun, 18 Aug 2024 00:16:03 +0000
Subject: [PATCH] Fix movement

---
 app.js | 53 +++++++++++++++++++++++------------------------------
 1 file changed, 23 insertions(+), 30 deletions(-)

diff --git a/app.js b/app.js
index e71be10..e4614fa 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);
     }
   });
 
-- 
GitLab