diff --git a/client/public/game/lobbies.js b/client/public/game/lobbies.js index dd1a603..8f323a5 100644 --- a/client/public/game/lobbies.js +++ b/client/public/game/lobbies.js @@ -1,3 +1,4 @@ + const LobbiesBlock = document.querySelector('#lobbies'); const CreateLobbyBlock = document.querySelector('#lobby-create'); const JoinLobbyBlock = document.querySelector('#lobby-join'); @@ -231,14 +232,22 @@ socket.on('lobby-join-error', obj => { function startGame() { - - // transition user intent - // call start game - // transition all other clients intent - // redirect with timeout - + socket.emit('identify-update-intent', {intent: 'GAMETRANSITION'}); + socket.emit('lobby-game-begin'); } +socket.on('request-intent-change', obj => { + + if (!obj.intent) return; + if (!obj.lobby) return; + if (!obj.intent === 'GAMETRANSITION') return; + + // window.location.search = `?uid=${obj.lobby.uid}`; + // window.location.pathname = '/scrabble'; + window.location = `/scrabble?uid=${obj.lobby.uid}`; + + +}); socket.on('lobby-update', obj => { if (!obj) return; diff --git a/client/public/scrabble/game.css b/client/public/scrabble/game.css index 3d69dce..a1a784c 100644 --- a/client/public/scrabble/game.css +++ b/client/public/scrabble/game.css @@ -47,7 +47,7 @@ score { } /*for small pieces - played or not*/ -.dragging-piece { +.small-piece { width: 40px; height: 40px; font-size: 25px; diff --git a/client/public/scrabble/network.js b/client/public/scrabble/network.js index 7658187..6ef4bcc 100644 --- a/client/public/scrabble/network.js +++ b/client/public/scrabble/network.js @@ -3,7 +3,7 @@ const urlParser = new URLSearchParams(window.location.search); let isSingleplayer = false; -if (urlParser.get('id') === null) +if (urlParser.get('uid') === null) { isSingleplayer = true; } diff --git a/client/public/scrabble/pieces.js b/client/public/scrabble/pieces.js index 1f5bb56..f44e6c3 100644 --- a/client/public/scrabble/pieces.js +++ b/client/public/scrabble/pieces.js @@ -117,7 +117,7 @@ function piecePickedUp(piece) BoardSounds[2].play(); - piece.classList.add('dragging-piece'); + piece.classList.add('small-piece'); isHoldingPiece = true; } @@ -149,7 +149,7 @@ function piecePlaced(piece) DrawerSounds[Math.floor(Math.random() * 3)].play(); piece.classList.remove('staged-piece'); - piece.classList.remove('dragging-piece'); + piece.classList.remove('small-piece'); piece.classList.add('unplayed-piece'); delete piece.dataset.coords; diff --git a/server/src/game-registrar.js b/server/src/game-registrar.js index 8db019b..b8afce9 100644 --- a/server/src/game-registrar.js +++ b/server/src/game-registrar.js @@ -1,7 +1,7 @@ const Logger = require('./logger.js'); const Server = require('./webserver.js'); -const Crypto = require("crypto"); +const Crypto = require('crypto'); /* USER OBJECT @@ -27,6 +27,8 @@ let OnlineUsers = []; // TODO: This won't scale very well lol +// FIXME: There is a potential bug where the username will be free +// during gametransition intents function CheckUsernameAvailability(username) { // un-reserve username if original owner has disconnected diff --git a/server/src/lobbies.js b/server/src/lobbies.js index c9914af..1cffce6 100644 --- a/server/src/lobbies.js +++ b/server/src/lobbies.js @@ -74,7 +74,8 @@ function IsLobbyReady(lobbyuid) if (!Lobbies[lobbyuid]) return false; // only support 2-4 players // https://en.wikipedia.org/wiki/Scrabble - if (Lobbies[lobbyuid].players.length <= 1) return false; + // TODO: ADD THIS BACK - REMOVED FOR TESTING + // if (Lobbies[lobbyuid].players.length <= 1) return false; if (Lobbies[lobbyuid].players.length > 4) return false; return Lobbies[lobbyuid].players.every(e => e.ready); diff --git a/server/src/socketserver.js b/server/src/socketserver.js index a4a7d45..a35569f 100644 --- a/server/src/socketserver.js +++ b/server/src/socketserver.js @@ -24,6 +24,8 @@ let io = {}; * sort of communication they will be recieving */ +// TODO: stop trusting the users UID, lookup with their connection ID + function init() { io = require('socket.io')(WebServer.Server); @@ -95,6 +97,7 @@ function ClientIdentify(socket, args) if (status === true) { socket.emit('identify-success', {connected: true, user: user}); + Game.Registrar.ChangeUserIntent(user.uid, intent); return; } else if (status === 'error-taken-user-connection') @@ -110,9 +113,13 @@ function ClientIdentify(socket, args) } } +// TODO: add checks to all these functions function UpdateIntent(socket, args) { - + const user = Game.Registrar.GetUserbyConnection(socket.id); + + const intent = args.intent; + Game.Registrar.ChangeUserIntent(user.uid, intent); } @@ -285,7 +292,23 @@ function LobbyUserUnReady(socket, args) function LobbyGameBegin(socket, args) { - + const user = Game.Registrar.GetUserbyConnection(socket.id); + const lobby = Game.Lobbies.GetLobbyByUserUID(user.uid); + // TODO: Maybe only the owner of the lobby should be able to start the game + + // Tells all other clients in the lobby to change intent to transition + // the clients don't need to request the server change their intent + // except the host that started the transition + for (const user of lobby.players) + { + Game.Registrar.ChangeUserIntent(user.uid, 'GAMETRANSITION'); + } + for (const user of lobby.spectators) + { + Game.Registrar.ChangeUserIntent(user.uid, 'GAMETRANSITION'); + } + + io.to(lobby.uid).emit('request-intent-change', { intent: 'GAMETRANSITION', lobby: lobby }); }