fixed bug with user lookups

This commit is contained in:
Benjamin Kyd
2021-02-07 21:26:10 +00:00
parent a181796b72
commit d2ceb930dd
5 changed files with 95 additions and 67 deletions

View File

@@ -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]);
});

58
server/src/error.js Normal file
View File

@@ -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;
}
}

View File

@@ -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,

View File

@@ -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
{

View File

@@ -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)