From df492f71879c8a736a08b76d27e1214bf7e85901 Mon Sep 17 00:00:00 2001 From: Maier Date: Fri, 30 Aug 2019 13:29:12 -0400 Subject: [PATCH] On create or join, now retrieves initial game state form server and prints it in the console --- server.js | 13 ++++++++----- static/game.js | 11 ++++++++--- static/join.js | 1 + static/setup.js | 6 +++--- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/server.js b/server.js index 040724f..0c0609d 100644 --- a/server.js +++ b/server.js @@ -6,7 +6,7 @@ const app = express(); const server = http.Server(app); const io = socketIO(server); -var activeGames = []; +var activeGames = {}; app.set('port', 5000); app.use('/static', express.static(__dirname + '/static')); // Routing @@ -34,14 +34,17 @@ server.listen(5000, function() { // Add the WebSocket handlers io.on('connection', function(socket) { - console.log('Client connected.'); socket.on('newGame', function(game) { - activeGames.push(game); + activeGames[game.accessCode] = game; }); socket.on('joinGame', function(playerInfo) { - activeGames[activeGames.findIndex((game) => game.accessCode === playerInfo.code)].players.push(playerInfo.name); + activeGames[Object.keys(activeGames).find((key) => key === playerInfo.code)].players[socket.id] = playerInfo.name; console.log("Player " + playerInfo.name + " has joined the game"); }); - console.log('games: ', activeGames); + socket.on('requestState', function(data) { + console.log(data); + console.log(activeGames[Object.keys(activeGames).find((key) => key === data.code)]); + socket.emit('state', activeGames[Object.keys(activeGames).find((key) => key === data.code)]); + }); }); diff --git a/static/game.js b/static/game.js index f3a5dfa..670a666 100644 --- a/static/game.js +++ b/static/game.js @@ -1,5 +1,10 @@ const socket = io(); -var games = []; -socket.on('message', function(data) { - console.log(data); +var currentGame = null +socket.on('state', function(game) { + currentGame = game; + console.log(currentGame); }); + +window.onload = function() { + socket.emit('requestState', {code: sessionStorage.getItem("code")}); +} diff --git a/static/join.js b/static/join.js index ee0f509..461b66d 100644 --- a/static/join.js +++ b/static/join.js @@ -1,6 +1,7 @@ const socket = io(); document.getElementById("join-btn").addEventListener("click", function() { + sessionStorage.setItem("code", document.getElementById("code").value) const playerInfo = {name: document.getElementById("name").value, code: document.getElementById("code").value}; socket.emit('joinGame', playerInfo); window.location.replace('/' + document.getElementById("code").value); diff --git a/static/setup.js b/static/setup.js index a8fa566..4427b11 100644 --- a/static/setup.js +++ b/static/setup.js @@ -83,16 +83,16 @@ function createGame() { code += charPool[getRandomInt(61)] } console.log(code); + let id = socket.id const game = new Game( code, gameSize, deck, document.getElementById("time").value, - [document.getElementById("name").value] + { [socket.id]: document.getElementById("name").value } ); - socket.emit('newGame', game); - sessionStorage.setItem('accessCode', code); + sessionStorage.setItem('code', code); window.location.replace('/' + code); }