diff --git a/assets/images/pause-button.svg b/assets/images/pause-button.svg new file mode 100644 index 0000000..5e740bf --- /dev/null +++ b/assets/images/pause-button.svg @@ -0,0 +1,8 @@ + + + Layer 1 + + + + + diff --git a/assets/images/play-button.svg b/assets/images/play-button.svg new file mode 100644 index 0000000..e0fd135 --- /dev/null +++ b/assets/images/play-button.svg @@ -0,0 +1,7 @@ + + + Layer 1 + + + + diff --git a/server.js b/server.js index f601d5c..1914044 100644 --- a/server.js +++ b/server.js @@ -64,6 +64,21 @@ io.on('connection', function(socket) { } io.to(gameData.code).emit('state', game); }); + socket.on('pauseGame', function(code) { + let game = activeGames[Object.keys(activeGames).find((key) => key === code)]; + game.pauseTime = (new Date()).toJSON(); + game.paused = true; + io.to(code).emit('state', game); + }); + socket.on('resumeGame', function(code) { + let game = activeGames[Object.keys(activeGames).find((key) => key === code)]; + game.paused = false; + let newTime = new Date(game.endTime).getTime() + (new Date().getTime() - new Date(game.pauseTime).getTime()); + let newDate = new Date(game.endTime); + newDate.setTime(newTime); + game.endTime = newDate.toJSON(); + io.to(code).emit('state', game); + }); socket.on('killPlayer', function(id, code) { let game = activeGames[Object.keys(activeGames).find((key) => key === code)]; let player = game.players.find((player) => player.id === id); diff --git a/static/game.js b/static/game.js index 48b0d92..8d9b748 100644 --- a/static/game.js +++ b/static/game.js @@ -55,9 +55,6 @@ function getLiveCount() { } function renderGame() { - if (currentGame.time) { - renderClock(); - } const player = currentGame.players.find((player) => player.id === sessionStorage.getItem("id")); const card = player.card; @@ -66,9 +63,10 @@ function renderGame() { document.getElementById("launch").setAttribute("class", "hidden"); document.getElementById("game-container").setAttribute("class", "game-container"); document.getElementById("game-container").innerHTML = - "
" + + "
" + "
" + getLiveCount() + "/" + currentGame.size + " alive
" + "
" + + "
" + "
" + "
" + "
" + @@ -81,6 +79,14 @@ function renderGame() { "
" + "
"; + if (currentGame.time) { + renderClock(); + document.getElementById("pause-container").innerHTML = currentGame.paused ? + "pause" + : "pause"; + document.getElementById("play-pause").addEventListener("click", pauseOrResumeGame) + } + // initially flip the card over for a reveal, allow it to be flipped on click/tap if (!cardDealt) { flipCard(); @@ -103,6 +109,14 @@ function renderGame() { document.getElementById("dead-btn").addEventListener("click", killPlayer); } +function pauseOrResumeGame() { + if (currentGame.paused) { + socket.emit('resumeGame', currentGame.accessCode); + } else { + socket.emit('pauseGame', currentGame.accessCode); + } +} + function flipCard() { cardFlippedOver ? document.getElementById("game-card").setAttribute("class", "flip-down") @@ -112,9 +126,12 @@ function flipCard() { function renderClock() { clock = setInterval(function() { - const now = new Date().getTime(); + const start = currentGame.paused ? new Date(currentGame.pauseTime) : new Date(); const end = new Date(currentGame.endTime); - const delta = end - now; + const delta = end - start; + if (currentGame.paused) { + clearInterval(clock); + } if (delta <= 0) { clearInterval(clock); endGame(true); diff --git a/static/styles.css b/static/styles.css index 896f4f8..063817d 100644 --- a/static/styles.css +++ b/static/styles.css @@ -64,6 +64,10 @@ html, body { font-family: 'sitewide-sans-serif', sans-serif; } +@keyframes fadein { + from { opacity: 0 } + to { opacity: 1 } +} .app-content { text-align: center; margin: 0 auto; @@ -194,6 +198,10 @@ button { margin: 3em 1em; } +#create-game-container, #lobby-container, #join-game-container, #game-container, #launch, #message-box { + animation: fadein 0.8s; +} + a { text-decoration: none; color: inherit; @@ -349,7 +357,7 @@ label { } #game-card p { - padding: 1em; + padding: 0.5em; } .game-container { @@ -404,13 +412,25 @@ label { } #flip-instruction { - margin: 7em 0 0 0; color: gray; } -.game-header { +#game-header { display: flex; margin: 0 auto; max-width: 35em; justify-content: space-evenly; } + +#play-pause { + width: 2.5em; + height: 2.5em; +} + +#play-pause:hover { +} + +#play-pause:active, #play-pause:focus { + opacity: 0.4; + transform: scale(0.90); +}