mirror of
https://github.com/ENSL/ensl_gathers.git
synced 2025-02-17 01:12:20 +00:00
Added gather skeleton and auth error checking
This commit is contained in:
parent
6883b0b938
commit
9618fb80ca
6 changed files with 123 additions and 32 deletions
|
@ -4,18 +4,43 @@ var fs = require("fs");
|
||||||
var path = require("path");
|
var path = require("path");
|
||||||
var enslClient = require(path.join(__dirname, "../lib/ensl/client"))();
|
var enslClient = require(path.join(__dirname, "../lib/ensl/client"))();
|
||||||
var chatController = require(path.join(__dirname, "../lib/chat/controller"));
|
var chatController = require(path.join(__dirname, "../lib/chat/controller"));
|
||||||
|
var gatherController = require(path.join(__dirname, "../lib/gather/controller"));
|
||||||
var winston = require("winston");
|
var winston = require("winston");
|
||||||
|
|
||||||
|
var rootNamespace;
|
||||||
|
|
||||||
var userCache = {};
|
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) {
|
module.exports = function (io) {
|
||||||
var root = io.of("/");
|
rootNamespace = io.of("/");
|
||||||
var authorised = io.of("/authorised");
|
|
||||||
|
|
||||||
var id = 2131;
|
var id = 2131;
|
||||||
|
|
||||||
// Authorisation
|
// Authorisation
|
||||||
root.use(function (socket, next) {
|
rootNamespace.use(function (socket, next) {
|
||||||
enslClient.getUserById({
|
enslClient.getUserById({
|
||||||
id: id
|
id: id
|
||||||
}, function (error, response, body) {
|
}, 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) {
|
io.on('connection', function (socket) {
|
||||||
refreshUsers();
|
refreshUsers();
|
||||||
|
|
||||||
|
@ -66,8 +64,11 @@ module.exports = function (io) {
|
||||||
enslClient.getUserById({
|
enslClient.getUserById({
|
||||||
id: id
|
id: id
|
||||||
}, function (error, response, body) {
|
}, function (error, response, body) {
|
||||||
if (error) {
|
if (error || response.statusCode !== 200) {
|
||||||
return winston.error(error);
|
winston.error("An error occurred in authorising id", id);
|
||||||
|
winston.error(error);
|
||||||
|
winston.error("ENSL API status:", response.statusCode);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
socket._user = body;
|
socket._user = body;
|
||||||
refreshUsers();
|
refreshUsers();
|
||||||
|
@ -78,4 +79,10 @@ module.exports = function (io) {
|
||||||
refreshUsers();
|
refreshUsers();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Activate chat controller on rootNamespace namespace
|
||||||
|
chatController(rootNamespace);
|
||||||
|
|
||||||
|
// Activate gather controller on rootNamespace namespace
|
||||||
|
gatherController(rootNamespace);
|
||||||
};
|
};
|
||||||
|
|
15
lib/gather/controller.js
Normal file
15
lib/gather/controller.js
Normal 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
26
lib/gather/gather.js
Normal 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
17
lib/gather/gatherer.js
Normal 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
13
lib/gather/map.js
Normal 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
13
lib/gather/server.js
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Implements Server
|
||||||
|
*
|
||||||
|
* Will eventually pull servers off ENSL API
|
||||||
|
*/
|
||||||
|
|
||||||
|
function Server () {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Server;
|
Loading…
Reference in a new issue