Skip to content
Snippets Groups Projects
Commit 9e5c7f5f authored by ys678's avatar ys678
Browse files

Update WebSocketModel.js

parent ad60b533
No related branches found
No related tags found
No related merge requests found
......@@ -5,96 +5,52 @@ class WebSocketModel {
createInterval(game) {
setInterval(() => {
if (!this.games[game].started) return;
let deadPlayers = [];
let interval = 500;
this.games[game].players.forEach(player => {
if (speedBoostTimers[player.pid]) {
interval = 250; // Move faster if player has speed boost
}
});
// Get the current time
const currentTime = Date.now();
// Move all players based on their speed
for (let player of this.games[game].players) {
if (player.spectating) continue;
let deadPlayers = [];
// Initialize lastMoveTime if it doesn't exist
if (!player.lastMoveTime) {
player.lastMoveTime = currentTime;
}
// Move the player if enough time has passed since their last move
if (currentTime - player.lastMoveTime >= player.speed) {
// Move the player
[this.games[game].gameBoard, deadPlayers] = gameModule.moveOneStep(
this.games[game].gameBoard,
);
if (!this.games[game].elapsedTime) {
this.games[game].elapsedTime = 0;
}
this.games[game].elapsedTime++;
// Update the player's last move time
player.lastMoveTime = currentTime;
//Every 10 seconds, generate borders
if (this.games[game].elapsedTime % 10 === 0) {
this.games[game].gameBoard = gameModule.applyBorders(
this.games[game].gameBoard,
this.games[game].borderCounter,
);
this.games[game].borderCounter++;
}
// Check if the player collided with the energy cell
let playerCollision = this.checkPlayerCollision(player.pid, this.games[game].gameBoard);
if (playerCollision === gameModule.COLLISIONTYPES.ENERGY) {
console.log(`Player ${player.pid} has eaten an energy cell!`);
if (deadPlayers.length > 0) {
for (let conn of this.games[game].players) {
for (let deadPlayer of deadPlayers) {
if (conn.pid === deadPlayer) {
conn.spectating = true;
// Boost the speed for 5 seconds
player.speed = 250; // Double speed (250ms)
setTimeout(() => {
player.speed = 500; // Reset speed after 5 seconds
}, 5000);
}
}
conn.connection.send(
JSON.stringify({
type: "deadPlayers",
data: deadPlayers,
}),
);
}
}
const alivePlayers = this.games[game].players.filter(
(player) => !player.spectating
);
if (alivePlayers.length === 1) {
const winner = alivePlayers[0].pid;;
for (let conn of this.games[game].players) {
conn.connection.send(
JSON.stringify({
type: "restart",
pid: winner
}),
);
if (deadPlayers.length > 0) {
this.handleDeadPlayers(game, deadPlayers);
}
this.resetGame(game);
return;
this.updatePlayersView(game);
}, 100); // Check every 100ms for more fine-grained control
}
for (let conn of this.games[game].players) {
let playerView = null;
if (conn.spectating) {
playerView = gameModule.getPlayerView(
this.games[game].gameBoard,
conn.spectatingPlayer,
);
} else {
playerView = gameModule.getPlayerView(
this.games[game].gameBoard,
conn.pid,
);
}
conn.connection.send(
JSON.stringify({
type: "gameBoard",
data: playerView.view,
headPosition: playerView.headPosition,
bodySize: playerView.bodySize
}),
);
}
}, 500);
}
constructor() {
this.connections = [];
this.games = {};
......@@ -134,6 +90,7 @@ class WebSocketModel {
this.games[gameId].gameBoard = gameModule.createGameBoard(100, 100);
this.games[gameId].players.forEach((player) => {
player.spectating = false;
player.speed = 500; // Reset speed to default
});
this.games[gameId].started = false;
this.games[gameId].readyPlayers = 0;
......@@ -171,8 +128,8 @@ class WebSocketModel {
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;
......@@ -181,12 +138,84 @@ class WebSocketModel {
}
}
checkPlayerCollision(pid, gameBoard) {
// Check if the player's head has collided with any special cells
for (let row of gameBoard) {
for (let cell of row) {
if (cell.pid === pid && cell.type === gameModule.cellTypes.PLAYERHEAD) {
if (cell.next && cell.next.type === gameModule.cellTypes.ENERGY) {
return gameModule.COLLISIONTYPES.ENERGY;
}
}
}
}
return gameModule.COLLISIONTYPES.NONE;
}
handleDeadPlayers(game, deadPlayers) {
for (let conn of this.games[game].players) {
for (let deadPlayer of deadPlayers) {
if (conn.pid === deadPlayer) {
conn.spectating = true;
}
}
conn.connection.send(
JSON.stringify({
type: "deadPlayers",
data: deadPlayers,
}),
);
}
const alivePlayers = this.games[game].players.filter(
(player) => !player.spectating
);
if (alivePlayers.length === 1) {
const winner = alivePlayers[0].pid;
for (let conn of this.games[game].players) {
conn.connection.send(
JSON.stringify({
type: "restart",
pid: winner,
}),
);
}
this.resetGame(game);
}
}
updatePlayersView(game) {
for (let conn of this.games[game].players) {
let playerView = null;
if (conn.spectating) {
playerView = gameModule.getPlayerView(
this.games[game].gameBoard,
conn.spectatingPlayer,
);
} else {
playerView = gameModule.getPlayerView(
this.games[game].gameBoard,
conn.pid,
);
}
conn.connection.send(
JSON.stringify({
type: "gameBoard",
data: playerView.view,
headPosition: playerView.headPosition,
bodySize: playerView.bodySize,
}),
);
}
}
onConnection() {
this.sockserver.on("connection", (connection) => {
console.log("New client connected!");
console.log(connection.protocol);
let roomId = connection.protocol;
if (
......@@ -198,8 +227,9 @@ class WebSocketModel {
pid: this.games[roomId].players.length + 1,
spectating: false,
spectatingPlayer: 1,
speed: 500, // Default speed
});
this.games[roomId].numPlayers = this.games[roomId].numPlayers + 1;
this.games[roomId].numPlayers++;
this.games[roomId].gameBoard = gameModule.addPlayer(
this.games[roomId].gameBoard,
this.games[roomId].players.length,
......@@ -210,6 +240,7 @@ class WebSocketModel {
pid: this.games[roomId].players.length + 1,
spectating: true,
spectatingPlayer: 1,
speed: 500, // Default speed even for spectators
});
}
......@@ -221,7 +252,6 @@ class WebSocketModel {
);
connection.on("close", () => {
// Need to fix this
this.games[roomId].players = this.games[roomId].players.filter(
(curr) => curr !== this.games[roomId].players,
);
......@@ -231,13 +261,9 @@ class WebSocketModel {
connection.on("message", (event) => {
try {
let message = JSON.parse(event);
console.log("Received message: " + message);
//All messages are expected to have a type
if (message.type === "move") {
console.log("Received move: " + message.data);
this.moveHandler(connection, message);
} else if (message.type === "chat") {
console.log("Received chat: " + message.data);
for (let conn of this.games[roomId].players) {
conn.connection.send(
JSON.stringify({
......@@ -248,7 +274,6 @@ class WebSocketModel {
);
}
} else if (message.type === "join") {
console.log("Received join: " + message.data);
for (let conn of this.games[roomId].players) {
conn.connection.send(
JSON.stringify({
......@@ -259,10 +284,7 @@ class WebSocketModel {
}),
);
}
} else if (message.type === "leave") {
console.log("Received leave: " + message.data);
} else if (message.type === "spectate") {
console.log("Received spectate: " + message.data);
for (let conn of this.games[roomId].players) {
if (conn.connection === connection) {
let player = conn.spectatingPlayer;
......@@ -274,7 +296,6 @@ class WebSocketModel {
}
}
} else if (message.type === "start") {
console.log("Received start: " + message.data);
for (let conn of this.games[roomId].players) {
if (conn.connection !== connection)
conn.connection.send(
......@@ -293,17 +314,11 @@ class WebSocketModel {
}
this.games[roomId].started = true;
}
} else {
console.log("Received: " + message);
}
} catch (e) {
console.log("Error parsing JSON: " + e.message);
}
});
connection.onerror = function () {
console.log("websocket error");
};
});
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment