Skip to content
Snippets Groups Projects
Commit 9ee2a213 authored by Jake Dreher's avatar Jake Dreher
Browse files

Small changes

parent d69aedb5
No related branches found
No related tags found
1 merge request!16Potential restart
...@@ -152,7 +152,7 @@ socket.addEventListener("message", (event) => { ...@@ -152,7 +152,7 @@ socket.addEventListener("message", (event) => {
messages.appendChild(messageElement); messages.appendChild(messageElement);
setTimeout(() => { setTimeout(() => {
//Redirect back to lobby after 10 seconds //Redirect back to lobby after 10 seconds
window.location.href = msg.url; window.location = window.location;
}, 10000); }, 10000);
} }
} catch (e) { } catch (e) {
......
...@@ -332,7 +332,12 @@ module.exports = { ...@@ -332,7 +332,12 @@ module.exports = {
let x = head.x + j - Math.floor(width / 2); let x = head.x + j - Math.floor(width / 2);
let y = head.y + i - Math.floor(height / 2); let y = head.y + i - Math.floor(height / 2);
if (x < 0 || x >= updatedBoard[0].length || y < 0 || y >= updatedBoard.length) { if (
x < 0 ||
x >= updatedBoard[0].length ||
y < 0 ||
y >= updatedBoard.length
) {
row.push(cellTypes.BORDER); row.push(cellTypes.BORDER);
} else { } else {
row.push(updatedBoard[y][x]); row.push(updatedBoard[y][x]);
...@@ -345,7 +350,7 @@ module.exports = { ...@@ -345,7 +350,7 @@ module.exports = {
return { return {
view: playerView, view: playerView,
headPosition: { x: head.x, y: head.y }, headPosition: { x: head.x, y: head.y },
bodySize: bodySize + 1 // Include head in body size bodySize: bodySize + 1, // Include head in body size
}; };
} },
}; };
...@@ -42,17 +42,17 @@ class WebSocketModel { ...@@ -42,17 +42,17 @@ class WebSocketModel {
} }
const alivePlayers = this.games[game].players.filter( const alivePlayers = this.games[game].players.filter(
(player) => !player.spectating (player) => !player.spectating,
); );
if (alivePlayers.length === 1) { if (alivePlayers.length === 1) {
const winner = alivePlayers[0].pid;; const winner = alivePlayers[0].pid;
for (let conn of this.games[game].players) { for (let conn of this.games[game].players) {
conn.connection.send( conn.connection.send(
JSON.stringify({ JSON.stringify({
type: "restart", type: "restart",
pid: winner pid: winner,
}), }),
); );
} }
...@@ -80,7 +80,7 @@ class WebSocketModel { ...@@ -80,7 +80,7 @@ class WebSocketModel {
type: "gameBoard", type: "gameBoard",
data: playerView.view, data: playerView.view,
headPosition: playerView.headPosition, headPosition: playerView.headPosition,
bodySize: playerView.bodySize bodySize: playerView.bodySize,
}), }),
); );
} }
...@@ -90,7 +90,7 @@ class WebSocketModel { ...@@ -90,7 +90,7 @@ class WebSocketModel {
this.connections = []; this.connections = [];
this.games = {}; this.games = {};
this.sockserver = new WebSocketServer({ port: 3001 }); this.sockserver = new WebSocketServer({ port: 3001 });
this.createGame("game1", "public"); this.createGame("game1", "public", 25);
this.createGame("game2", "public"); this.createGame("game2", "public");
this.createGame("game3", "public"); this.createGame("game3", "public");
this.onConnection(); this.onConnection();
...@@ -108,9 +108,9 @@ class WebSocketModel { ...@@ -108,9 +108,9 @@ class WebSocketModel {
return publicGames; return publicGames;
} }
createGame(gameId, gameType) { createGame(gameId, gameType, size = 100) {
this.games[gameId] = { this.games[gameId] = {
gameBoard: gameModule.createGameBoard(100, 100), gameBoard: gameModule.createGameBoard(size, size),
players: [], players: [],
type: gameType, type: gameType,
started: false, started: false,
...@@ -123,9 +123,7 @@ class WebSocketModel { ...@@ -123,9 +123,7 @@ class WebSocketModel {
resetGame(gameId) { resetGame(gameId) {
this.games[gameId].gameBoard = gameModule.createGameBoard(100, 100); this.games[gameId].gameBoard = gameModule.createGameBoard(100, 100);
this.games[gameId].players.forEach((player) => { this.games[gameId].players = [];
player.spectating = false;
});
this.games[gameId].started = false; this.games[gameId].started = false;
this.games[gameId].readyPlayers = 0; this.games[gameId].readyPlayers = 0;
this.games[gameId].borderCounter = 0; this.games[gameId].borderCounter = 0;
...@@ -172,6 +170,23 @@ class WebSocketModel { ...@@ -172,6 +170,23 @@ class WebSocketModel {
} }
} }
arbitrateConnections(gameId) {
console.log(
"this.games[gameId].players.length: ",
this.games[gameId].players.length,
);
for (let i = 0; i < this.games[gameId].players.length; i++) {
if (this.games[gameId].players[i].connection === undefined) {
delete this.games[gameId].players[i];
console.log("Client has disconnected!");
}
}
console.log(
"this.games[gameId].players.length: ",
this.games[gameId].players.length,
);
}
onConnection() { onConnection() {
this.sockserver.on("connection", (connection) => { this.sockserver.on("connection", (connection) => {
console.log("New client connected!"); console.log("New client connected!");
...@@ -180,6 +195,8 @@ class WebSocketModel { ...@@ -180,6 +195,8 @@ class WebSocketModel {
let roomId = connection.protocol; let roomId = connection.protocol;
//this.arbitrateConnections(roomId);
if ( if (
!this.games[roomId].started && !this.games[roomId].started &&
this.games[roomId].players.length < 10 this.games[roomId].players.length < 10
...@@ -269,7 +286,7 @@ class WebSocketModel { ...@@ -269,7 +286,7 @@ class WebSocketModel {
for (let conn of this.games[roomId].players) { for (let conn of this.games[roomId].players) {
if (conn.connection !== connection) if (conn.connection !== connection)
conn.connection.send( conn.connection.send(
JSON.stringify({ type: "playerReady", data: message.data }), JSON.stringify({ type: "playerReady", pid: message.pid }),
); );
} }
this.games[roomId].readyPlayers++; this.games[roomId].readyPlayers++;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment