From 020282c931fcb8b6cecce0a34b37e8adebba7cc6 Mon Sep 17 00:00:00 2001 From: Alec Maier Date: Sun, 1 Sep 2019 03:32:19 -0400 Subject: [PATCH] Timer functionality --- package-lock.json | 5 +++++ package.json | 1 + server.js | 6 ++++++ static/game.js | 34 ++++++++++++++++++++++++++++++++-- static/setup.js | 1 + static/styles.css | 13 ++++++++++++- 6 files changed, 57 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index f345d98..e029a42 100644 --- a/package-lock.json +++ b/package-lock.json @@ -379,6 +379,11 @@ "mime-db": "1.40.0" } }, + "moment": { + "version": "2.24.0", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz", + "integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==" + }, "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", diff --git a/package.json b/package.json index 898f2ed..bbc8fd3 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "express": "^4.17.1", + "moment": "^2.24.0", "socket.io": "^2.2.0" } } diff --git a/server.js b/server.js index adfa184..d9984d5 100644 --- a/server.js +++ b/server.js @@ -5,6 +5,7 @@ const socketIO = require('socket.io'); const app = express(); const server = http.Server(app); const io = socketIO(server); +const moment = require('moment'); var activeGames = {}; @@ -57,6 +58,11 @@ io.on('connection', function(socket) { let game = activeGames[Object.keys(activeGames).find((key) => key === gameData.code)]; game.state = "started"; game.players = gameData.players; + if (game.time) { + let d = new Date(); + d.setMinutes(d.getMinutes() + parseInt(game.time)); + game.endTime = d.toJSON(); + } io.to(gameData.code).emit('state', game); }); socket.on('killPlayer', function(id, code) { diff --git a/static/game.js b/static/game.js index 910764a..e8fb926 100644 --- a/static/game.js +++ b/static/game.js @@ -1,7 +1,8 @@ import {utility} from './util.js' const socket = io(); -var currentGame = null; +let clock; +let currentGame = null; // respond to the game state received from the server socket.on('state', function(game) { @@ -52,6 +53,9 @@ function getLiveCount() { } function renderGame() { + if (currentGame.time) { + renderClock(); + } const player = currentGame.players.find((player) => player.id === sessionStorage.getItem("id")); const card = player.card; @@ -60,7 +64,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 + " Players alive
" + + "
" + + "
" + getLiveCount() + "/" + currentGame.size + " alive
" + + "
" + + "
" + "
" + "
" + @@ -85,6 +92,29 @@ function renderGame() { document.getElementById("dead-btn").addEventListener("click", killPlayer); } +function renderClock() { + clock = setInterval(function() { + const now = new Date().getTime(); + const end = new Date(currentGame.endTime); + const delta = end - now; + if (delta <= 0) { + clearInterval(clock); + endGame(true); + } else { + let minutes = Math.floor((delta % (1000 * 60 * 60)) / (1000 * 60)); + let seconds = Math.floor((delta % (1000 * 60)) / 1000); + seconds = seconds < 10 ? "0" + seconds : seconds; + document.getElementById("clock").innerText = minutes + ":" + seconds; + } + }, 1000); +} + +function endGame(timeExpired) { + if (timeExpired) { + console.log("expired"); + } +} + function killPlayer() { socket.emit("killPlayer", currentGame.players.find((player) => player.id === sessionStorage.getItem("id")).id, currentGame.accessCode); } diff --git a/static/setup.js b/static/setup.js index 94ae5e3..b1c6a6e 100644 --- a/static/setup.js +++ b/static/setup.js @@ -24,6 +24,7 @@ class Game { this.time = time; this.players = []; this.state = "lobby"; + this.endTime = null; } } diff --git a/static/styles.css b/static/styles.css index 2373fa4..618bfaa 100644 --- a/static/styles.css +++ b/static/styles.css @@ -191,7 +191,7 @@ button { #create-game-container, #join-game-container { display: inline-block; text-align: left; - margin-bottom: 3em; + margin: 3em 1em; } a { @@ -377,3 +377,14 @@ label { color: gray; margin-bottom: 0.5em; } + +#clock { + font-size: 1.7em; + width: 4em; + margin-bottom: 0.5em; +} + +.game-header { + display: flex; + justify-content: space-evenly; +}