import {utility} from './util.js' const socket = io(); var currentGame = null; document.getElementById("launch").addEventListener("click", launchGame); // respond to the game state received from the server socket.on('state', function(game) { currentGame = game; if (game.message) { document.getElementById("message-box").innerText = game.message; } console.log(currentGame); buildGameBasedOnState(); }); function buildGameBasedOnState() { switch(currentGame.state) { case "lobby": renderLobby(); break; case "started": renderGame(); break; default: break; } } function launchGame() { randomlyDealCardsToPlayers(); socket.emit('startGame', { players: currentGame.players , code: currentGame.accessCode}); } function randomlyDealCardsToPlayers() { for (let player of currentGame.players) { player.card = drawRandomCard(); } } function drawRandomCard() { return currentGame.deck.splice(utility.getRandomInt(currentGame.deck.length) - 1, 1)[0]; } function getLiveCount() { let liveCount = 0; for (let player of currentGame.players) { if (!player.dead) { liveCount ++; } } return liveCount; } function renderGame() { const player = currentGame.players.find((player) => player.id === sessionStorage.getItem("id")); const card = player.card; // render the players card document.getElementById("lobby-container").setAttribute("class", "hidden"); document.getElementById("launch").setAttribute("class", "hidden"); document.getElementById("game-container").setAttribute("class", "game-container"); document.getElementById("game-container").innerHTML = "
" + card.description + "
" + "" + player.name + "
"; document.getElementById("lobby-container").appendChild(playerContainer); document.getElementById("join-count").innerText = currentGame.players.length.toString(); } i ++; } // display the launch button if the player is the host if (sessionStorage.getItem("host")) { currentGame.players.length === currentGame.size ? document.getElementById("launch").innerHTML = "" : document.getElementById("launch").innerHTML = "" } else { document.getElementById("launch").innerHTML = "The host will start the game.
" } } // request the current state of the game from the server window.onload = function() { socket.emit('requestState', {code: sessionStorage.getItem("code")}); };