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

Add handling for creating private games. Need a way to manage games,...

Add handling for creating private games. Need a way to manage games, determining if the game is complete and booting back to lobby and when games are closed
parent 1834d74f
Branches 6-rooms
No related tags found
1 merge request!12Move websocket code to its own module, move game.html script tag to its own...
......@@ -10,12 +10,24 @@ webserver.get("/", (_, res) => {
});
webserver.get("/public_games", (_, res) => {
let ids = Object.keys(games);
let ids = sockServer.getGameIds();
res.json({ ids: ids });
});
webserver.get("/join_game/:gameId", (req, res) => {
let { gameId } = req.params;
webserver.get("/join_game/:gameId/:gameType", (req, res) => {
let { gameId, gameType } = req.params;
if (gameType === "private") {
if (!sockServer.hasGame(gameId)) {
sockServer.createGame(gameId, gameType);
} else {
console.log("Game already exists");
if (sockServer.games[gameId].type === "public") {
// TODO: THIS IS A HACK
return res.sendFile("public/index.html", { root: __dirname });
}
}
return res.sendFile("public/game.html", { root: __dirname });
}
if (!sockServer.hasGame(gameId)) {
return res.status(404).send();
}
......
let pathParts = window.location.pathname.split("/");
let roomId = pathParts[pathParts.length - 1];
let roomId = pathParts[pathParts.length - 2];
const joinMessage = { type: "join", data: `${roomId}`, pid: 0 };
let moveMessage = { type: "move", data: "up", pid: 0 };
......
......@@ -75,7 +75,16 @@
aria-labelledby="headingTwo"
data-parent="#accordion"
>
<div class="card-body">Test2</div>
<div class="card-body">
<input type="text" id="gameId" placeholder="Game ID" />
<button
id="privButton"
class="btn btn-primary"
onclick="window.location = `/join_game/${document.getElementById('gameId').value}/private`"
>
Join Game
</button>
</div>
</div>
</div>
</div>
......@@ -87,6 +96,12 @@
publicBody.textContent = "Loading...";
let privButton = document.getElementById("privButton");
privButton.addEventListener("click", () => {
console.log("Joining game " + document.getElementById("gameId").value);
window.location = `/join_game/${document.getElementById("gameId").value}/private`;
});
fetch("/public_games")
.then((response) => {
return response.json();
......@@ -103,7 +118,7 @@
button.addEventListener("click", () => {
// will redirect to new chatroom immediately
console.log("Joining game " + data.ids[i]);
window.location = `/join_game/${data.ids[i]}`;
window.location = `/join_game/${data.ids[i]}/public`;
});
publicBody.appendChild(button);
}
......
......@@ -8,18 +8,21 @@ class WebSocketModel {
this.games["game1"] = {
gameBoard: gameModule.createGameBoard(10, 10),
players: [],
type: "public",
started: false,
readyPlayers: 0,
};
this.games["game2"] = {
gameBoard: gameModule.createGameBoard(10, 10),
players: [],
type: "public",
started: false,
readyPlayers: 0,
};
this.games["game3"] = {
gameBoard: gameModule.createGameBoard(10, 10),
players: [],
type: "public",
started: false,
readyPlayers: 0,
};
......@@ -50,6 +53,24 @@ class WebSocketModel {
return this.games.hasOwnProperty(gameId);
}
getGameIds() {
let publicGames = [];
publicGames = Object.keys(this.games).filter(
(game) => this.games[game].type === "public",
);
return publicGames;
}
createGame(gameId, gameType) {
this.games[gameId] = {
gameBoard: gameModule.createGameBoard(10, 10),
players: [],
type: gameType,
started: false,
readyPlayers: 0,
};
}
moveHandler(connection, message) {
console.log("Received move: " + message.data);
let movement = message.data;
......@@ -91,11 +112,15 @@ class WebSocketModel {
let roomId = connection.protocol;
if (!this.games[roomId].started) {
this.games[roomId].players.push(connection);
this.games[roomId].gameBoard = gameModule.addPlayer(
this.games[roomId].gameBoard,
this.games[roomId].players.length,
);
} else {
this.games[roomId].players.push(connection);
}
connection.send(
JSON.stringify({
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment