Added gather skeleton and auth error checking

This commit is contained in:
Chris Blanchard 2015-07-22 17:28:15 +01:00
parent 6883b0b938
commit 9618fb80ca
6 changed files with 123 additions and 32 deletions

View file

@ -4,18 +4,43 @@ var fs = require("fs");
var path = require("path");
var enslClient = require(path.join(__dirname, "../lib/ensl/client"))();
var chatController = require(path.join(__dirname, "../lib/chat/controller"));
var gatherController = require(path.join(__dirname, "../lib/gather/controller"));
var winston = require("winston");
var rootNamespace;
var userCache = {};
var refreshUsers = function (socket) {
var receiver = (socket !== undefined) ? socket : rootNamespace;
var newCache = {};
rootNamespace.sockets.forEach(function (socket) {
var user = socket._user;
newCache[user.id] = user;
});
userCache = newCache;
var users = [];
for (var id in userCache) {
if (userCache.hasOwnProperty(id)) {
users.push(userCache[id]);
}
}
receiver.emit('userCount', {
count: users.length,
users: users
});
};
module.exports = function (io) {
var root = io.of("/");
var authorised = io.of("/authorised");
rootNamespace = io.of("/");
var id = 2131;
// Authorisation
root.use(function (socket, next) {
rootNamespace.use(function (socket, next) {
enslClient.getUserById({
id: id
}, function (error, response, body) {
@ -28,33 +53,6 @@ module.exports = function (io) {
});
});
var refreshUsers = function (socket) {
var receiver = (socket !== undefined) ? socket : root;
var newCache = {};
root.sockets.forEach(function (socket) {
var user = socket._user;
newCache[user.id] = user;
});
userCache = newCache;
var users = [];
for (var id in userCache) {
if (userCache.hasOwnProperty(id)) {
users.push(userCache[id]);
}
}
receiver.emit('userCount', {
count: users.length,
users: users
});
};
// Activate chat controller on root namespace
chatController(root);
io.on('connection', function (socket) {
refreshUsers();
@ -66,8 +64,11 @@ module.exports = function (io) {
enslClient.getUserById({
id: id
}, function (error, response, body) {
if (error) {
return winston.error(error);
if (error || response.statusCode !== 200) {
winston.error("An error occurred in authorising id", id);
winston.error(error);
winston.error("ENSL API status:", response.statusCode);
return;
}
socket._user = body;
refreshUsers();
@ -78,4 +79,10 @@ module.exports = function (io) {
refreshUsers();
});
});
// Activate chat controller on rootNamespace namespace
chatController(rootNamespace);
// Activate gather controller on rootNamespace namespace
gatherController(rootNamespace);
};

15
lib/gather/controller.js Normal file
View file

@ -0,0 +1,15 @@
"use strict";
/*
* Gather Controller
*
* Server API
* Client API
*
*/
var Gather = require("./gather");
module.exports = function (namespace) {
};

26
lib/gather/gather.js Normal file
View file

@ -0,0 +1,26 @@
"use strict";
/*
* Implements Gather Model
*
* Gather States
* - Picking
* - Election
* - Selection
* - Done
*
*/
var Gatherer = require("./gatherer");
function Gather () {
if (!(this instanceof Gather)) {
return new Gather();
}
this.gatherers = [];
this.marines = [];
this.aliens = [];
}
module.exports = Gather;

17
lib/gather/gatherer.js Normal file
View file

@ -0,0 +1,17 @@
"use strict";
/*
* Implements Gatherer
*
* Stores necessary information including:
* - user data
* - voting preferences
* - leader status
*
*/
function Gatherer () {
}
module.exports = Gatherer;

13
lib/gather/map.js Normal file
View file

@ -0,0 +1,13 @@
"use strict";
/*
* Implements Map
*
* Will eventually pull maps off ENSL API
*/
function Map () {
}
module.exports = Map;

13
lib/gather/server.js Normal file
View file

@ -0,0 +1,13 @@
"use strict";
/*
* Implements Server
*
* Will eventually pull servers off ENSL API
*/
function Server () {
}
module.exports = Server;