Skip to content
Snippets Groups Projects
Commit 296fffd6 authored by jjd358's avatar jjd358
Browse files

Merge branch '1-develop-web-socket-code' into 'main'

Resolve "Develop Web Socket Code"

Closes #1

See merge request !7
parents cca97f87 83458353
Branches
No related tags found
1 merge request!7Resolve "Develop Web Socket Code"
...@@ -14,7 +14,7 @@ let connections = []; ...@@ -14,7 +14,7 @@ let connections = [];
let gameBoard = [ let gameBoard = [
[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
]; ];
...@@ -22,38 +22,40 @@ let gameBoard = [ ...@@ -22,38 +22,40 @@ let gameBoard = [
let moveHandler = (connection, message) => { let moveHandler = (connection, message) => {
console.log("Received move: " + message.data); console.log("Received move: " + message.data);
let movement = message.data; let movement = message.data;
let pid = message.pid;
console.log("pid: ", pid);
for (var i = 0; i < gameBoard.length; i++) { for (var i = 0; i < gameBoard.length; i++) {
for (var j = 0; j < gameBoard[0].length; j++) { for (var j = 0; j < gameBoard[0].length; j++) {
if (movement === "up" && gameBoard[i][j] === 1 && i > 0) { if (movement === "up" && gameBoard[i][j] === pid && i > 0) {
gameBoard[i][j] = 0; gameBoard[i][j] = 0;
gameBoard[i - 1][j] = 1; gameBoard[i - 1][j] = pid;
return; return;
} }
if ( if (
movement === "down" && movement === "down" &&
gameBoard[i][j] === 1 && gameBoard[i][j] === pid &&
i < gameBoard.length - 1 i < gameBoard.length - 1
) { ) {
gameBoard[i][j] = 0; gameBoard[i][j] = 0;
gameBoard[i + 1][j] = 1; gameBoard[i + 1][j] = pid;
return; return;
} }
if (movement === "left" && gameBoard[i][j] === 1 && j > 0) { if (movement === "left" && gameBoard[i][j] === pid && j > 0) {
gameBoard[i][j] = 0; gameBoard[i][j] = 0;
gameBoard[i][j - 1] = 1; gameBoard[i][j - 1] = pid;
return; return;
} }
if ( if (
movement === "right" && movement === "right" &&
gameBoard[i][j] === 1 && gameBoard[i][j] === pid &&
j < gameBoard[0].length - 1 j < gameBoard[0].length - 1
) { ) {
gameBoard[i][j] = 0; gameBoard[i][j] = 0;
gameBoard[i][j + 1] = 1; gameBoard[i][j + 1] = pid;
return; return;
} }
} }
...@@ -64,6 +66,9 @@ sockserver.on("connection", (connection) => { ...@@ -64,6 +66,9 @@ sockserver.on("connection", (connection) => {
console.log("New client connected!"); console.log("New client connected!");
connections.push(connection); connections.push(connection);
connection.send(
JSON.stringify({ type: "newUser", data: connections.length }),
);
connection.on("close", () => { connection.on("close", () => {
connections = connections.filter((curr) => curr !== connection); connections = connections.filter((curr) => curr !== connection);
...@@ -77,7 +82,9 @@ sockserver.on("connection", (connection) => { ...@@ -77,7 +82,9 @@ sockserver.on("connection", (connection) => {
if (message.type === "move") { if (message.type === "move") {
console.log("Received move: " + message.data); console.log("Received move: " + message.data);
moveHandler(connection, message); moveHandler(connection, message);
connection.send(JSON.stringify({ type: "gameBoard", data: gameBoard })); for (let conn of connections) {
conn.send(JSON.stringify({ type: "gameBoard", data: gameBoard }));
}
} else if (message.type === "chat") { } else if (message.type === "chat") {
console.log("Received chat: " + message.data); console.log("Received chat: " + message.data);
} else if (message.type === "join") { } else if (message.type === "join") {
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
const width = canvas.width; const width = canvas.width;
const height = canvas.height; const height = canvas.height;
let moveMessage = { type: "move", data: "up" }; let moveMessage = { type: "move", data: "up", pid: 0 };
let gameBoardHandler = (event) => { let gameBoardHandler = (event) => {
let ctx = canvas.getContext("2d"); let ctx = canvas.getContext("2d");
...@@ -60,6 +60,10 @@ ...@@ -60,6 +60,10 @@
} }
}; };
let newUserHandler = (event) => {
moveMessage.pid = event.data;
};
socket.addEventListener("open", () => { socket.addEventListener("open", () => {
console.log("Connected to server"); console.log("Connected to server");
socket.send(JSON.stringify(joinMessage)); socket.send(JSON.stringify(joinMessage));
...@@ -70,6 +74,8 @@ ...@@ -70,6 +74,8 @@
let msg = JSON.parse(event.data); let msg = JSON.parse(event.data);
if (msg.type === "gameBoard") { if (msg.type === "gameBoard") {
gameBoardHandler(msg); gameBoardHandler(msg);
} else if (msg.type === "newUser") {
newUserHandler(msg);
} else { } else {
const messages = document.getElementById("messages"); const messages = document.getElementById("messages");
const messageElement = document.createElement("div"); const messageElement = document.createElement("div");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment