From d2ceb930dd9a3c3e6b3413b8c2a142c7dbb5cd80 Mon Sep 17 00:00:00 2001 From: Benjamin Kyd Date: Sun, 7 Feb 2021 21:26:10 +0000 Subject: [PATCH] fixed bug with user lookups --- client/public/lobby/index.js | 12 +++++--- server/src/error.js | 58 ++++++++++++++++++++++++++++++++++++ server/src/game-registrar.js | 14 +++++++-- server/src/router.js | 57 +---------------------------------- server/src/socketserver.js | 21 +++++++++---- 5 files changed, 95 insertions(+), 67 deletions(-) create mode 100644 server/src/error.js diff --git a/client/public/lobby/index.js b/client/public/lobby/index.js index 3f23442..63b79fb 100644 --- a/client/public/lobby/index.js +++ b/client/public/lobby/index.js @@ -5,7 +5,10 @@ ConnectionState.innerHTML = 'Waiting for connection' const socket = io(window.location.host); -ConnectionState.innerHTML = 'Waiting for identify' +socket.on('connect', (...args) => { + console.log('Socket Connected'); + ConnectionState.innerHTML = 'Waiting for identify' +}); socket.on('identify', (...args) => { ConnectionState.innerHTML = 'Identify recived' @@ -29,15 +32,16 @@ socket.on('identify', (...args) => { return; } - socket.emit('identify', { playerid: user.uid }); + socket.emit('identify', { userid: user.uid }); ConnectionState.innerHTML = 'Identify response'; }); socket.on('identify-success', (...args) => { - + console.log(args); }); socket.on('identify-error', (...args) => { - + console.log(args); + ConnectionState.innerHTML = JSON.stringify(args[0]); }); diff --git a/server/src/error.js b/server/src/error.js new file mode 100644 index 0000000..e9dbcd4 --- /dev/null +++ b/server/src/error.js @@ -0,0 +1,58 @@ +const Logger = require('./logger.js'); + +const Express = require('express'); + +/* +STANDARD ERROR RESPONSE +{ + errors: [ + { + code: error code, + error: "Short error", + desc: "Description of the error" + } + ] +} +*/ +// Would automatically convert code to error +// but that would be too much effort and i cba +// TODO: That ^ +module.exports = class Error +{ + constructor() + { + this.errors = []; + } + + addError(code, error, desc) + { + this.errors.push({code: code, error: error, desc: desc}); + } + + isErrored() + { + return this.errors.length >= 0; + } + + end(expressRes) + { + expressRes.status(this.code).end(this.toString); + } + + get toError() + { + return this.errors; + } + + get toString() + { + return JSON.stringify({errors: this.toError}); + } + + get code() + { + // Get the error code of the last error in the list + // to return to the client if there is multiple + return this.errors[this.errors.length - 1].code; + } +} \ No newline at end of file diff --git a/server/src/game-registrar.js b/server/src/game-registrar.js index c476ed1..39f8e78 100644 --- a/server/src/game-registrar.js +++ b/server/src/game-registrar.js @@ -2,7 +2,6 @@ const Logger = require('./logger.js'); const Server = require('./webserver.js'); const Crypto = require("crypto"); -const { getuid } = require('process'); /* USER OBJECT @@ -54,7 +53,7 @@ function ValidUsername(username) function GetUserByUID(uid) { - return OnlineUsers[id]; + return OnlineUsers[uid]; } function GetUserByUsername(username) @@ -64,6 +63,7 @@ function GetUserByUsername(username) return OnlineUsers[user]; } + function RegisterUser(username, ip) { // TODO: Don't assume this is unique, even with Crypto, UUIDv4? @@ -80,6 +80,16 @@ function RegisterUser(username, ip) return OnlineUsers[id]; } +function UserConnect() +{ + +} + +function UserDisconnect() +{ + +} + module.exports = { OnlineUsers: OnlineUsers, diff --git a/server/src/router.js b/server/src/router.js index 9cca9ea..83f1cdd 100644 --- a/server/src/router.js +++ b/server/src/router.js @@ -1,6 +1,7 @@ const Logger = require('./logger.js'); const Server = require('./webserver.js'); const Game = require('./game.js') +const Error = require('./error.js') const Express = require('express'); @@ -25,62 +26,6 @@ module.exports = { } -/* -STANDARD ERROR RESPONSE -{ - errors: [ - { - code: error code, - error: "Short error", - desc: "Description of the error" - } - ] -} -*/ -// Would automatically convert code to error -// but that would be too much effort and i cba -// TODO: That ^ -class Error -{ - constructor() - { - this.errors = []; - } - - addError(code, error, desc) - { - this.errors.push({code: code, error: error, desc: desc}); - } - - isErrored() - { - return this.errors.length >= 0; - } - - end(expressRes) - { - expressRes.status(this.code).end(this.toString); - } - - get toErrorObject() - { - return this.errors; - } - - get toString() - { - return JSON.stringify({errors: this.toErrorObject}); - } - - get code() - { - // Get the error code of the last error in the list - // to return to the client if there is multiple - return this.errors[this.errors.length - 1].code; - } -} - - /* EXPECTS { diff --git a/server/src/socketserver.js b/server/src/socketserver.js index ff9f8ee..c36d9b0 100644 --- a/server/src/socketserver.js +++ b/server/src/socketserver.js @@ -1,6 +1,7 @@ const Logger = require('./logger.js'); const WebServer = require('./webserver.js'); const Game = require('./game.js'); +const Error = require('./error.js'); /** * All socket communication follows a standard call/response & event @@ -11,6 +12,9 @@ const Game = require('./game.js'); * more event/response based * * Again, networking code will be seperated from domain logic with sockets + * + * Sockets will error with HTTP error codes because it's a pretty decent + * standard for standard errors that may occur */ function init() @@ -33,11 +37,10 @@ let ActiveSockets = []; async function Router(socket) { // First, ask socket to identify it's self - socket.on('connect', (...args) => { - socket.emit('identify'); - }) + socket.emit('identify'); - socket.on('identify', userid => ClientIdentify(socket, userid)); + + socket.on('identify', user => ClientIdentify(socket, user.userid)); socket.on('disconnect', args => HandleDisconnect(socket, ...args)); @@ -47,13 +50,21 @@ async function Router(socket) function ClientIdentify(socket, userid) { + const err = new Error; + const user = Game.Registrar.GetUserByUID(userid); + console.log(user); + if (!user) { - + err.addError(403, 'Forbidden', 'Unknown uid'); + socket.emit('identify-error', err.toError); + return; } + + } function HandleDisconnect(socket, args)