tracking of players accross session using id in storage, create game button for the host that is disabled until lobby is full. synchronized lobby state.

This commit is contained in:
Alec Maier
2019-08-30 20:37:11 -04:00
parent 385c5d4e79
commit f873540171
6 changed files with 92 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
const socket = io();
var currentGame = null
var currentGame = null;
// respond to the game state received from the server
socket.on('state', function(game) {
@@ -21,18 +21,36 @@ function renderLobby() {
header.setAttribute("class", "app-header");
header.innerText = "Lobby";
document.getElementById("lobby-container").appendChild(header);
let subHeader = document.createElement("div");
subHeader.setAttribute("id", "lobby-subheader");
subHeader.innerHTML = "<div>" +
"<span id='join-count'>" + Object.keys(currentGame.players).length + "</span>" +
"<span id='deck-size'>/" + currentGame.size + " Players</span>" +
"</div>" +
"<br>" +
"<div id='game-code'>Access Code: " + currentGame.accessCode + "</div>";
document.getElementById("lobby-container").appendChild(subHeader);
}
let i = 1;
for (let key in currentGame.players) {
if(!document.getElementById("player-" + i)) {
const playerContainer = document.createElement("div");
playerContainer.setAttribute("class", "lobby-player");
playerContainer.setAttribute("id", "player-" + i)
playerContainer.innerHTML = "<p>" + currentGame.players[key] + "</p>";
currentGame.players[key].id === sessionStorage.getItem("id") ?
playerContainer.setAttribute("class", "lobby-player highlighted")
: playerContainer.setAttribute("class", "lobby-player");
playerContainer.setAttribute("id", "player-" + i);
playerContainer.innerHTML = "<p>" + currentGame.players[key].name + "</p>";
document.getElementById("lobby-container").appendChild(playerContainer);
document.getElementById("join-count").innerText = Object.keys(currentGame.players).length.toString();
}
i ++;
}
// display the launch button if the player is the host
if (sessionStorage.getItem("host")) {
Object.keys(currentGame.players).length === currentGame.size ?
document.getElementById("launch-btn").innerHTML = "<button class='app-btn'>Start Game</button>"
: document.getElementById("launch-btn").innerHTML = "<button class='app-btn disabled'>Start Game</button>"
}
}
// request the current state of the game from the server