diff --git a/TODO b/TODO index 053bf75..dc02178 100644 --- a/TODO +++ b/TODO @@ -1,13 +1,13 @@ # Long term -[ ] Add support for sharding / load balancing with Nginx -[ ] Use a database lol +☐ Add support for sharding / load balancing with Nginx +☐ Use a database lol -[ ] Lobby discovery system to find and join "public" lobbies +☐ Lobby discovery system to find and join "public" lobbies → Matchmaking # Short term, coursework scope -[ ] Translate +☐ Translate ✔ Portuagese (Ines) @done(21-04-11 01:03) → Update with new locales ✔ Spanish (Ines) @done(21-04-11 01:03) @@ -18,11 +18,11 @@ ✔ Sort out how intents work in game transitioning @done(21-04-12 21:09) -[ ] Lobbying logic +☐ Lobbying logic ✔ Create, join, delete and leave lobbies @done(21-04-12 20:51) → Make lobbies publically discoverable on a list -[ ] Dictionary +☐ Dictionary → Find appropriate scrabble dictionary for ✔ Portuagese @done(21-04-19 02:10) ✔ Spanish @done(21-04-19 02:10) @@ -30,11 +30,11 @@ → Czech → Optimise! n-ary tree -[ ] Game logic - → Game creation +☐ Game logic + ✔ Game creation @done(21-04-26 17:38) ✘ Singleplayer game logic - OUT OF SCOPE @cancelled(21-04-11 01:04) ✘ AI CPU player @cancelled(21-04-11 01:07) -[ ] Code +☐ Code → Refactor to code portsoc eslint → Refactor game client diff --git a/client/public/scrabble/network.js b/client/public/scrabble/network.js index 4d8ada3..3522f71 100644 --- a/client/public/scrabble/network.js +++ b/client/public/scrabble/network.js @@ -23,7 +23,7 @@ function initMultiplayer() socket.on('identify-success', args => onIdentifySuccess(socket, args)); socket.on('identify-error', args => onIdentifyError(socket, args)); - + socket.on('game-start', args => console.log(args)); } diff --git a/server/src/game-logic.js b/server/src/game-logic.js index e9e9ed8..80c5cda 100644 --- a/server/src/game-logic.js +++ b/server/src/game-logic.js @@ -8,7 +8,7 @@ const Helpers = require('./helpers.js'); GAME OBJECT { // reference UID - lobbyuid: uid, + uid: uid, locale: en, players: [{ uid: uid, @@ -19,7 +19,7 @@ GAME OBJECT }], score: int }], - // index of players + // index of players whos turn it is turn: int, tilebag: [], tileset: [] @@ -35,7 +35,7 @@ NOTES let ActiveGames = []; -function StartGame(lobby) +function BeginGame(lobby) { // game uses the owners language const gameowner = Registrar.GetUserByUID(lobby.owneruid); @@ -66,15 +66,13 @@ function StartGame(lobby) } ActiveGames[lobby.uid] = { - lobbyuid: lobby.uid, + uid: lobby.uid, locale: gameowner.locale, players: players, turn: 0, tilebag: tilebag }; - console.log(ActiveGames[lobby.uid]); - return ActiveGames[lobby.uid]; } @@ -92,5 +90,5 @@ function SelectTilesFromBag(tileset, num) module.exports = { - StartGame: StartGame, + BeginGame: BeginGame, } diff --git a/server/src/socketserver.js b/server/src/socketserver.js index 27dd605..66a32fd 100644 --- a/server/src/socketserver.js +++ b/server/src/socketserver.js @@ -52,10 +52,11 @@ async function Router(socket) // based socket.emit('identify'); - + // identify functions socket.on('identify', args => ClientIdentify(socket, args)); socket.on('identify-update-intent', args => UpdateIntent(socket, args)); + // lobby functions socket.on('lobby-create', args => LobbyCreate(socket, args)); socket.on('lobby-join', args => LobbyJoin(socket, args)); socket.on('lobby-leave', args => LobbyLeave(socket, args)); @@ -112,11 +113,9 @@ function ClientIdentify(socket, args) // User intends to enter a game if (intent === 'GAME' && oldIntent === 'GAMETRANSITION') { - const lobbyUID = args.lobbyuid; - // Make sure the user is actually in this game const lobby = Game.Lobbies.GetLobbyByUserUID(user.uid); - if (lobby.uid !== lobbyUID) + if (lobby.uid !== args.lobbyuid) { err.addError(500, 'Internal Server Error', 'error-illegal-intent'); socket.emit('identify-error', err.toError); @@ -125,34 +124,42 @@ function ClientIdentify(socket, args) Game.Lobbies.UserConnectGame(user.uid); - // If this user was the last player in the lobby to connect - // start the game + // Users at this point don't go through lobbying code + // so make sure that they are joined into a lobby for + // the networking + socket.join(lobby.uid); + // If this user was the last player in the lobby to connect + // start the game and tell every connected user if (Game.Lobbies.IsLobbyReadyForGame(lobby.uid)) { - - const game = Game.Logic.StartGame(lobby); - + Logger.debug(`ALL PLAYERS IN LOBBY ${lobby.uid} ARE CONNECTED TO GAME`); + // Make sure the last user to start the game is in the correct + // state to recieve the game-begin packet + socket.emit('identify-success', {connected: true, user: user}); + const game = Game.Logic.BeginGame(lobby); + + Logger.game(`GAME ${lobby.uid} IS BEGINNING`); + + EmitGameBegin(game); + + return; } } - if (status === true) { socket.emit('identify-success', {connected: true, user: user}); - return; } else if (status === 'error-taken-user-connection') { err.addError(500, 'Internal Server Error', 'error-taken-user-connection'); socket.emit('identify-error', err.toError); - return; } else { err.addError(500, 'Internal Server Error', 'error-illegal-user'); socket.emit('identify-error', err.toError); - return; } } @@ -337,7 +344,7 @@ 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 + // TODO: Maybe only the owner of the lobby should be able to begin 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 @@ -395,3 +402,23 @@ function LobbyUpdateCallback(user, lobby, state) }); Game.Lobbies.IsLobbyReady(lobby.uid) } + + + +// send the client their user as well as the rest of the game +function EmitGameBegin(game) +{ + // TODO: consider not sending all users the entire game state + // due to cheating + io.to(game.uid).emit('game-begin', { + game: game + }); + + console.log(game); + + // for (const user of game.players) + // { + // const gameuser = game.players.filter(i => i.uid === user.uid)[0]; + + // } +}