diff --git a/client/public/game/init.js b/client/public/game/init.js index c4e22c1..32029b1 100644 --- a/client/public/game/init.js +++ b/client/public/game/init.js @@ -49,18 +49,18 @@ socket.on('identify', (...args) => { return; } - socket.emit('identify', { userid: user.uid }); + socket.emit('identify', { userid: user.uid, intent: 'LOBYING' }); ConnectionState.innerHTML = 'Identify response'; }); socket.on('identify-success', (...args) => { console.log(args); - ConnectionState.innerHTML = JSON.stringify(args[0]); + ConnectionState.innerHTML = JSON.stringify(args[0], undefined, 4); }); socket.on('identify-error', (...args) => { console.log(args); - ConnectionState.innerHTML = JSON.stringify(args[0]); + ConnectionState.innerHTML = JSON.stringify(args[0], undefined, 4); }); diff --git a/server/src/game-registrar.js b/server/src/game-registrar.js index 273d8ca..f3bf0bc 100644 --- a/server/src/game-registrar.js +++ b/server/src/game-registrar.js @@ -8,7 +8,11 @@ USER OBJECT { username: username, uid: id, - ip: ip + ip: ip, + // REGISTERED, CONNECTED, DISCONNECTED, INGAME + state: 'REGISTERED', + // Doesn't update if state changes + connectionid: 'none', } NOTES - Socket relations are handled by the socket server in order @@ -62,6 +66,7 @@ function GetUserByUsername(username) for (const user in OnlineUsers) if (OnlineUsers[user].username === username) return OnlineUsers[user]; + return false; } @@ -74,7 +79,7 @@ function RegisterUser(username, ip) username: username, uid: uid, ip: ip, - // REGISTERED, CONNECTED, DISCONNECTED + // REGISTERED, CONNECTED, DISCONNECTED, INGAME state: 'REGISTERED', // Doesn't update if state changes connectionid: 'none', @@ -86,8 +91,21 @@ function RegisterUser(username, ip) } -// Can return string errors, or true if success -// yes multiple return types i know its bad + +function UserConnectionExists(userid) +{ + if (OnlineUsers[userid].state === 'CONNECTED') return true; + +} + +function GetUserbyConnection(connectionid) +{ + for (const user in OnlineUsers) + if (OnlineUsers[user].connectionid === connectionid) + return OnlineUsers[user]; + return false; +} + function UserConnect(userid, connectionid) { if (OnlineUsers[userid].state === 'CONNECTED') return 'User Connected'; @@ -115,6 +133,11 @@ module.exports = { RegisterUser: RegisterUser, + + UserConnectionExists: UserConnectionExists, + + GetUserbyConnection: GetUserbyConnection, + UserConnect: UserConnect, UserDisconnect: UserDisconnect } diff --git a/server/src/socketserver.js b/server/src/socketserver.js index 570e5fe..0092026 100644 --- a/server/src/socketserver.js +++ b/server/src/socketserver.js @@ -16,6 +16,10 @@ const Error = require('./error.js'); * Sockets will error with HTTP error codes because it's a pretty decent * standard for standard errors that may occur. Then general errors will * be 500 errors with a description + * + * Clients connect to identify with an 'intent' to be placed in a game or to + * be part of the lobbying system, the intent of the client defines what + * sort of communication they will be recieving */ function init() @@ -41,7 +45,7 @@ async function Router(socket) socket.emit('identify'); - socket.on('identify', user => ClientIdentify(socket, user.userid)); + socket.on('identify', args => ClientIdentify(socket, args)); socket.on('disconnect', args => HandleDisconnect(socket, ...args)); @@ -49,21 +53,29 @@ async function Router(socket) } -function ClientIdentify(socket, userid) +function ClientIdentify(socket, args) { const err = new Error; - const user = Game.Registrar.GetUserByUID(userid); - + const user = Game.Registrar.GetUserByUID(args.userid); + const intent = args.intent; + + if (!intent) + { + err.addError(403, 'Forbidden', 'Client has no intent'); + socket.emit('identify-error', err.toError); + return; + } + if (!user) { err.addError(403, 'Forbidden', 'Unknown uid'); socket.emit('identify-error', err.toError); return; } - - console.log(user); - + + // TODO: Sort out client intent + const status = Game.Registrar.UserConnect(userid, socket.id); if (status === true) @@ -72,6 +84,7 @@ function ClientIdentify(socket, userid) return; } else { + // TODO: wrong error err.addError(500, 'Internal Server Error', 'Socket busy'); socket.emit('identify-error', err.toError); return; @@ -80,7 +93,10 @@ function ClientIdentify(socket, userid) function HandleDisconnect(socket, args) { - Logger.debug(`${socket.id} DISCONNECTED`) + Logger.debug(`${socket.id} DISCONNECTED`); + const user = Game.Registrar.GetUserbyConnection(socket.id); + if (!user) return; + Game.Registrar.UserDisconnect(user.id, socket.id); }