From b8239737fc19bb701bdc09a4ec8b5dbeb9ac1bf1 Mon Sep 17 00:00:00 2001 From: Jake Dreher <jdreherschool@gmail.com> Date: Wed, 4 Sep 2024 17:26:30 -0400 Subject: [PATCH] Implement zoomed view --- src/game/game.js | 39 ++++++++++++++++++++++++++++++++++-- src/models/WebSocketModel.js | 24 ++++++++++++---------- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/game/game.js b/src/game/game.js index b04c7bc..6d0ea7a 100644 --- a/src/game/game.js +++ b/src/game/game.js @@ -302,7 +302,42 @@ module.exports = { getPlayerView: (gameBoard, pid) => { let updatedBoard = gameBoard.map((row) => row.map((cell) => ({ ...cell }))); - // YANG: Find head with pid = pid - // return [x][y] around head + + let width = 5; + let height = 5; + + let head = null; + 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) { + console.log("MY Player head found at: ", cell.x, cell.y); + head = cell; + } + } + } + + 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 playerView; }, }; diff --git a/src/models/WebSocketModel.js b/src/models/WebSocketModel.js index 7114a60..e8fae35 100644 --- a/src/models/WebSocketModel.js +++ b/src/models/WebSocketModel.js @@ -27,7 +27,7 @@ class WebSocketModel { if (deadPlayers.length > 0) { for (let conn of this.games[game].players) { - conn.send( + conn.connection.send( JSON.stringify({ type: "deadPlayers", data: deadPlayers, @@ -37,14 +37,15 @@ class WebSocketModel { } for (let conn of this.games[game].players) { - conn.send( + let playerView = gameModule.getPlayerView(this.games[game].gameBoard, conn.pid); + conn.connection.send( JSON.stringify({ type: "gameBoard", - data: this.games[game].gameBoard, // YANG: gameModule.getPlayerView()... + data: playerView, // YANG: gameModule.getPlayerView()... }), ); } - }, 1000); + }, 500); } constructor() { this.connections = []; @@ -148,13 +149,13 @@ class WebSocketModel { let roomId = connection.protocol; if (!this.games[roomId].started) { - this.games[roomId].players.push(connection); + this.games[roomId].players.push({connection, pid: this.games[roomId].players.length+1}); this.games[roomId].gameBoard = gameModule.addPlayer( this.games[roomId].gameBoard, this.games[roomId].players.length, ); } else { - this.games[roomId].players.push(connection); + this.games[roomId].players.push({connection, pid: this.games[roomId].players.length+1}); } connection.send( @@ -165,6 +166,7 @@ class WebSocketModel { ); connection.on("close", () => { + // Need to fix this this.games[roomId].players = this.games[roomId].players.filter( (curr) => curr !== this.games[roomId].players, ); @@ -182,7 +184,7 @@ class WebSocketModel { } else if (message.type === "chat") { console.log("Received chat: " + message.data); for (let conn of this.games[roomId].players) { - conn.send( + conn.connection.send( JSON.stringify({ type: "chat", data: message.data, @@ -193,7 +195,7 @@ class WebSocketModel { } else if (message.type === "join") { console.log("Received join: " + message.data); for (let conn of this.games[roomId].players) { - conn.send( + conn.connection.send( JSON.stringify({ type: "playerJoined", data: message.data, @@ -207,8 +209,8 @@ class WebSocketModel { } else if (message.type === "start") { console.log("Received start: " + message.data); for (let conn of this.games[roomId].players) { - if (conn !== connection) - conn.send( + if (conn.connection !== connection) + conn.connection.send( JSON.stringify({ type: "playerReady", data: message.data }), ); } @@ -218,7 +220,7 @@ class WebSocketModel { this.games[roomId].readyPlayers ) { for (let conn of this.games[roomId].players) { - conn.send( + conn.connection.send( JSON.stringify({ type: "start", data: message.data }), ); } -- GitLab