Node v4.1 ready

This commit is contained in:
Chris Blanchard 2015-09-25 22:23:30 +01:00
parent afb791600b
commit 85815293b6
9 changed files with 71 additions and 58 deletions

View file

@ -18,7 +18,7 @@ messageSchema.index({ deleted: 1, createdAt: -1 });
// Instance Methods // Instance Methods
messageSchema.methods.toJson = () => { messageSchema.methods.toJson = function () {
return { return {
id: this.id, id: this.id,
author: this.author, author: this.author,
@ -27,7 +27,7 @@ messageSchema.methods.toJson = () => {
}; };
}; };
messageSchema.statics.list = (options, callback) => { messageSchema.statics.list = function (options, callback) {
let query = this.find({deleted: false}) let query = this.find({deleted: false})
if (options.before) { if (options.before) {

View file

@ -22,7 +22,7 @@ var profileSchema = new Schema({
profileSchema.path('userId').index({ unique: true }); profileSchema.path('userId').index({ unique: true });
profileSchema.static({ profileSchema.static({
findOrCreate: (user, callback) => { findOrCreate: function (user, callback) {
if (!user || typeof user.id !== 'number') return callback(new Error("Invalid user")); if (!user || typeof user.id !== 'number') return callback(new Error("Invalid user"));
let self = this; let self = this;
self.findOne({userId: user.id}, (error, profile) => { self.findOne({userId: user.id}, (error, profile) => {
@ -37,7 +37,7 @@ profileSchema.static({
}); });
profileSchema.method({ profileSchema.method({
toJson: () => { toJson: function () {
let output = {}; let output = {};
output.abilities = this.abilities; output.abilities = this.abilities;
output.skill = this.skill; output.skill = this.skill;

View file

@ -20,7 +20,7 @@ function EnslClient (options) {
this.baseUrl = config.ensl_url; this.baseUrl = config.ensl_url;
} }
EnslClient.prototype.getUserById = (options, callback) => { EnslClient.prototype.getUserById = function (options, callback) {
var id = options.id; var id = options.id;
var url = this.baseUrl + "api/v1/users/" + id; var url = this.baseUrl + "api/v1/users/" + id;
return request({ return request({
@ -29,7 +29,7 @@ EnslClient.prototype.getUserById = (options, callback) => {
}, callback); }, callback);
}; };
EnslClient.prototype.getServers = callback => { EnslClient.prototype.getServers = function (callback) {
const url = this.baseUrl + "api/v1/servers"; const url = this.baseUrl + "api/v1/servers";
return request({ return request({
url: url, url: url,
@ -44,7 +44,7 @@ EnslClient.prototype.getServers = callback => {
}); });
}; };
EnslClient.prototype.getMaps = callback => { EnslClient.prototype.getMaps = function (callback) {
const url = this.baseUrl + "api/v1/maps"; const url = this.baseUrl + "api/v1/maps";
return request({ return request({
url: url, url: url,
@ -59,11 +59,11 @@ EnslClient.prototype.getMaps = callback => {
}); });
}; };
EnslClient.prototype.getFullAvatarUri = url => { EnslClient.prototype.getFullAvatarUri = function (url) {
return this.baseUrl + url.replace(/^\//, ""); return this.baseUrl + url.replace(/^\//, "");
}; };
EnslClient.parseCookies = (socket) => { EnslClient.parseCookies = function (socket) {
let cookieString = socket.request.headers.cookie; let cookieString = socket.request.headers.cookie;
if (typeof cookieString !== 'string') return null; if (typeof cookieString !== 'string') return null;
let cookies = socket.request.headers.cookie.split(";") let cookies = socket.request.headers.cookie.split(";")
@ -78,7 +78,7 @@ EnslClient.parseCookies = (socket) => {
return cookies; return cookies;
}; };
EnslClient.decodeSession = (sessionCookie, callback) => { EnslClient.decodeSession = function (sessionCookie, callback) {
if (typeof sessionCookie !== 'string') { if (typeof sessionCookie !== 'string') {
return callback(new Error("Invalid cookie"), null); return callback(new Error("Invalid cookie"), null);
} }

View file

@ -58,33 +58,33 @@ StateMachine.create({
], ],
callbacks: { callbacks: {
// Callbacks for events // Callbacks for events
onafterevent: () => { onafterevent: function () {
this.onEvent.call(this); this.onEvent.call(this);
}, },
// Gathering State // Gathering State
onbeforeaddGatherer: (event, from, to, user) => { onbeforeaddGatherer: function (event, from, to, user) {
this.addUser(user); this.addUser(user);
if (this.gatherers.length !== 12) return false; if (this.gatherers.length !== 12) return false;
}, },
// Election State // Election State
onbeforeselectLeader: (event, from, to, voter, candidate) => { onbeforeselectLeader: function (event, from, to, voter, candidate) {
this.voteForLeader(voter, candidate); this.voteForLeader(voter, candidate);
if (this.leaderVotes().length !== 12) return false; if (this.leaderVotes().length !== 12) return false;
}, },
onenterelection: () => { onenterelection: function () {
// Setup timer for elections // Setup timer for elections
this.startElectionCountdown(); this.startElectionCountdown();
}, },
onleaveelection: () => { onleaveelection: function () {
this.cancelElectionCountdown(); this.cancelElectionCountdown();
}, },
// Selection State // Selection State
onenterselection: () => { onenterselection: function () {
// Remove all leaders and teams // Remove all leaders and teams
this.gatherers.forEach(gatherer => { this.gatherers.forEach(gatherer => {
gatherer.leader = false; gatherer.leader = false;
@ -108,13 +108,13 @@ StateMachine.create({
this.assignMarineLeader(parseInt(rank.pop().candidate, 0)); this.assignMarineLeader(parseInt(rank.pop().candidate, 0));
}, },
onbeforeconfirmSelection: (event, from, to, leader) => { onbeforeconfirmSelection: function (event, from, to, leader) {
return (this.aliens().length === this.TEAM_SIZE return (this.aliens().length === this.TEAM_SIZE
&& this.marines().length === this.TEAM_SIZE); && this.marines().length === this.TEAM_SIZE);
}, },
// Remove gatherer event // Remove gatherer event
onbeforeremoveGatherer: (event, from, to, user) => { onbeforeremoveGatherer: function (event, from, to, user) {
// Cancel transition if no gatherers have been removed // Cancel transition if no gatherers have been removed
let userCount = this.gatherers.length; let userCount = this.gatherers.length;
this.removeUser(user); this.removeUser(user);
@ -122,7 +122,7 @@ StateMachine.create({
}, },
// Set gatherer vote & if threshold met, reset gather // Set gatherer vote & if threshold met, reset gather
onbeforeregather: (event, from, to, user, vote) => { onbeforeregather: function (event, from, to, user, vote) {
let self = this; let self = this;
self.modifyGatherer(user, (gatherer) => gatherer.voteRegather(vote)); self.modifyGatherer(user, (gatherer) => gatherer.voteRegather(vote));
if (self.regatherVotes() >= self.REGATHER_THRESHOLD) { if (self.regatherVotes() >= self.REGATHER_THRESHOLD) {
@ -134,64 +134,64 @@ StateMachine.create({
}, },
// On enter done // On enter done
onenterdone: () => { onenterdone: function () {
this.done.time = new Date(); this.done.time = new Date();
this.onDone.call(this); this.onDone.call(this);
} }
} }
}); });
Gather.prototype.resetState = () => { Gather.prototype.resetState = function () {
this.gatherers = []; this.gatherers = [];
return this; return this;
}; };
Gather.prototype.alienLeader = () => { Gather.prototype.alienLeader = function () {
return this.gatherers.reduce((acc, gatherer) => { return this.gatherers.reduce((acc, gatherer) => {
if (gatherer.team === "alien" && gatherer.leader) acc.push(gatherer); if (gatherer.team === "alien" && gatherer.leader) acc.push(gatherer);
return acc; return acc;
}, []).pop(); }, []).pop();
}; };
Gather.prototype.marineLeader = () => { Gather.prototype.marineLeader = function () {
return this.gatherers.reduce((acc, gatherer) => { return this.gatherers.reduce((acc, gatherer) => {
if (gatherer.team === "marine" && gatherer.leader) acc.push(gatherer); if (gatherer.team === "marine" && gatherer.leader) acc.push(gatherer);
return acc; return acc;
}, []).pop(); }, []).pop();
}; };
Gather.prototype.assignMarineLeader = id => { Gather.prototype.assignMarineLeader = function (id) {
this.modifyGatherer({id: id}, gatherer => { this.modifyGatherer({id: id}, gatherer => {
gatherer.leader = true; gatherer.leader = true;
gatherer.team = "marine"; gatherer.team = "marine";
}); });
}; };
Gather.prototype.assignAlienLeader = id => { Gather.prototype.assignAlienLeader = function (id) {
this.modifyGatherer({id: id}, gatherer => { this.modifyGatherer({id: id}, gatherer => {
gatherer.leader = true; gatherer.leader = true;
gatherer.team = "alien"; gatherer.team = "alien";
}); });
}; };
Gather.prototype.containsUser = user => { Gather.prototype.containsUser = function (user) {
return this.gatherers.some(gatherer => { return this.gatherers.some(gatherer => {
return gatherer.id === user.id; return gatherer.id === user.id;
}); });
}; };
Gather.prototype.addUser = user => { Gather.prototype.addUser = function (user) {
if (this.containsUser(user)) return null; if (this.containsUser(user)) return null;
let gatherer = new Gatherer(user); let gatherer = new Gatherer(user);
this.gatherers.push(gatherer); this.gatherers.push(gatherer);
return gatherer; return gatherer;
}; };
Gather.prototype.removeUser = user => { Gather.prototype.removeUser = function (user) {
this.gatherers = this.gatherers.filter(gatherer => user.id !== gatherer.id); this.gatherers = this.gatherers.filter(gatherer => user.id !== gatherer.id);
}; };
Gather.prototype.modifyGatherer = (user, callback) => { Gather.prototype.modifyGatherer = function (user, callback){
return this.gatherers return this.gatherers
.filter(gatherer => gatherer.id === user.id) .filter(gatherer => gatherer.id === user.id)
.forEach(callback); .forEach(callback);
@ -201,7 +201,7 @@ Gather.prototype.modifyGatherer = (user, callback) => {
// Marine 1 pick first // Marine 1 pick first
// 2 picks for each team subsequently // 2 picks for each team subsequently
Gather.prototype.pickingTurn = () => { Gather.prototype.pickingTurn = function () {
if (this.current !== 'selection') return null; if (this.current !== 'selection') return null;
let alienCount = this.aliens().length; let alienCount = this.aliens().length;
let marineCount = this.marines().length; let marineCount = this.marines().length;
@ -224,7 +224,7 @@ Gather.prototype.pickingTurn = () => {
// Moves player to marine // Moves player to marine
// Optional `mover` argument which will check mover credentials to select // Optional `mover` argument which will check mover credentials to select
// Credentials: Must be leader, must belong to team, must be turn to pick // Credentials: Must be leader, must belong to team, must be turn to pick
Gather.prototype.moveToMarine = (user, mover) => { Gather.prototype.moveToMarine = function (user, mover) {
if (this.marines().length >= this.TEAM_SIZE) return; if (this.marines().length >= this.TEAM_SIZE) return;
if (mover && this.containsUser(mover)) { if (mover && this.containsUser(mover)) {
@ -245,7 +245,7 @@ Gather.prototype.moveToMarine = (user, mover) => {
// Optional `mover` argument which will check mover credentials to select // Optional `mover` argument which will check mover credentials to select
// Credentials: Must be leader, must belong to team, must be turn to pick // Credentials: Must be leader, must belong to team, must be turn to pick
Gather.prototype.moveToAlien = (user, mover) => { Gather.prototype.moveToAlien = function (user, mover) {
if (this.aliens().length >= this.TEAM_SIZE) return; if (this.aliens().length >= this.TEAM_SIZE) return;
if (mover && this.containsUser(mover)) { if (mover && this.containsUser(mover)) {
@ -259,36 +259,35 @@ Gather.prototype.moveToAlien = (user, mover) => {
} }
} }
return this.modifyGatherer(user, gatherer => gatherer.team = "alien"); return this.modifyGatherer(user, gatherer => gatherer.team = "alien");
}; };
Gather.prototype.moveToLobby = user => { Gather.prototype.moveToLobby = function (user) {
this.modifyGatherer(user, gatherer => gatherer.team = "lobby"); this.modifyGatherer(user, gatherer => gatherer.team = "lobby");
}; };
Gather.prototype.retrieveGroup = team => { Gather.prototype.retrieveGroup = function (team) {
return this.gatherers.filter(gatherer => gatherer.team === team); return this.gatherers.filter(gatherer => gatherer.team === team);
}; };
Gather.prototype.lobby = () => { Gather.prototype.lobby = function () {
return this.retrieveGroup("lobby"); return this.retrieveGroup("lobby");
}; };
Gather.prototype.aliens = () => { Gather.prototype.aliens = function () {
return this.retrieveGroup("alien"); return this.retrieveGroup("alien");
}; };
Gather.prototype.marines = () => { Gather.prototype.marines = function () {
return this.retrieveGroup("marine"); return this.retrieveGroup("marine");
}; };
Gather.prototype.electionStartTime = () => { Gather.prototype.electionStartTime = function () {
return (this.election.startTime === null) ? return (this.election.startTime === null) ?
null : this.election.startTime.toISOString(); null : this.election.startTime.toISOString();
}; };
Gather.prototype.toJson = () => { Gather.prototype.toJson = function () {
return { return {
gatherers: this.gatherers, gatherers: this.gatherers,
state: this.current, state: this.current,
@ -303,16 +302,16 @@ Gather.prototype.toJson = () => {
} }
}; };
Gather.prototype.voteForMap = (voter, mapId) => { Gather.prototype.voteForMap = function (voter, mapId) {
this.modifyGatherer(voter, gatherer => gatherer.mapVote = mapId); this.modifyGatherer(voter, gatherer => gatherer.mapVote = mapId);
}; };
Gather.prototype.voteForServer = (voter, serverId) => { Gather.prototype.voteForServer = function (voter, serverId) {
this.modifyGatherer(voter, gatherer => gatherer.serverVote = serverId); this.modifyGatherer(voter, gatherer => gatherer.serverVote = serverId);
}; };
// Returns an array of IDs representing votes for leaders // Returns an array of IDs representing votes for leaders
Gather.prototype.leaderVotes = () => { Gather.prototype.leaderVotes = function () {
let self = this; let self = this;
return self.gatherers return self.gatherers
.map(gatherer => gatherer.leaderVote) .map(gatherer => gatherer.leaderVote)
@ -320,17 +319,17 @@ Gather.prototype.leaderVotes = () => {
.filter(leaderId => self.containsUser({id: leaderId})); .filter(leaderId => self.containsUser({id: leaderId}));
}; };
Gather.prototype.voteForLeader = (voter, candidate) => { Gather.prototype.voteForLeader = function (voter, candidate) {
this.modifyGatherer(voter, gatherer => gatherer.voteForLeader(candidate)); this.modifyGatherer(voter, gatherer => gatherer.voteForLeader(candidate));
}; };
Gather.prototype.getGatherer = (user) => { Gather.prototype.getGatherer = function (user) {
return this.gatherers return this.gatherers
.filter(gatherer => gatherer.id === user.id) .filter(gatherer => gatherer.id === user.id)
.pop() || null; .pop() || null;
}; };
Gather.prototype.regatherVotes = () => { Gather.prototype.regatherVotes = function () {
let self = this; let self = this;
return self.gatherers.reduce((acc, gatherer) => { return self.gatherers.reduce((acc, gatherer) => {
if (gatherer.regatherVote) acc++; if (gatherer.regatherVote) acc++;
@ -339,7 +338,7 @@ Gather.prototype.regatherVotes = () => {
}; };
// Initiates a timer which will push gather into next state // Initiates a timer which will push gather into next state
Gather.prototype.startElectionCountdown = () => { Gather.prototype.startElectionCountdown = function () {
let self = this; let self = this;
self.election.startTime = new Date(); self.election.startTime = new Date();
this.election.timer = setTimeout(() => { this.election.timer = setTimeout(() => {
@ -347,7 +346,7 @@ Gather.prototype.startElectionCountdown = () => {
}, self.election.INTERVAL); }, self.election.INTERVAL);
}; };
Gather.prototype.cancelElectionCountdown = () => { Gather.prototype.cancelElectionCountdown = function () {
clearInterval(this.election.timer); clearInterval(this.election.timer);
this.election.timer = null; this.election.timer = null;
this.election.startTime = null; this.election.startTime = null;

View file

@ -24,7 +24,7 @@ function Gatherer (user) {
this.regatherVote = false; this.regatherVote = false;
} }
Gatherer.prototype.voteForLeader = candidate => { Gatherer.prototype.voteForLeader = function (candidate) {
if (candidate === null) { if (candidate === null) {
return this.leaderVote = null; return this.leaderVote = null;
} }
@ -34,7 +34,7 @@ Gatherer.prototype.voteForLeader = candidate => {
this.leaderVote = candidate.id; this.leaderVote = candidate.id;
}; };
Gatherer.prototype.voteRegather = (vote) => { Gatherer.prototype.voteRegather = function (vote) {
if (vote !== undefined && typeof vote === 'boolean') { if (vote !== undefined && typeof vote === 'boolean') {
return this.regatherVote = vote; return this.regatherVote = vote;
} else { } else {

View file

@ -14,7 +14,7 @@ function HiveClient (options) {
this.baseUrl = config.hive_url; this.baseUrl = config.hive_url;
} }
HiveClient.prototype.getUserStats = (user, callback) => { HiveClient.prototype.getUserStats = function (user, callback) {
if (!user || !user.hive.id) { if (!user || !user.hive.id) {
return callback(new Error("Invalid user instance supplied")); return callback(new Error("Invalid user instance supplied"));
} }

View file

@ -38,14 +38,14 @@ function User (user) {
} }
} }
User.prototype.getSteamId = () => { User.prototype.getSteamId = function () {
if (this.steam.url === null) return null; if (this.steam.url === null) return null;
var urlId = this.steam.url.match(/\d*$/); var urlId = this.steam.url.match(/\d*$/);
if (!urlId || urlId[0].length === 0) return null; if (!urlId || urlId[0].length === 0) return null;
return steam.convertToText(urlId[0]); return steam.convertToText(urlId[0]);
}; };
User.prototype.getHiveId = () => { User.prototype.getHiveId = function () {
var steamId = this.steam.id; var steamId = this.steam.id;
if (!steamId) return null; if (!steamId) return null;
var index = steamId.match(/:0:/) ? 0 : 1; var index = steamId.match(/:0:/) ? 0 : 1;
@ -55,7 +55,7 @@ User.prototype.getHiveId = () => {
var allowedAttributes = ["enslo", "division", "skill", "gatherMusic"]; var allowedAttributes = ["enslo", "division", "skill", "gatherMusic"];
var allowedAbilities = ["skulk", "lerk", "fade", "gorge", "onos", "commander"]; var allowedAbilities = ["skulk", "lerk", "fade", "gorge", "onos", "commander"];
User.prototype.updateProfile = (data, callback) => { User.prototype.updateProfile = function (data, callback) {
let self = this; let self = this;
Profile.findOne({userId: self.id}, (error, profile) => { Profile.findOne({userId: self.id}, (error, profile) => {
if (error) return callback(error); if (error) return callback(error);
@ -77,7 +77,7 @@ User.prototype.updateProfile = (data, callback) => {
}); });
}; };
User.find = (id, callback) => { User.find = function (id, callback) {
enslClient.getUserById({ enslClient.getUserById({
id: id id: id
}, (error, response, body) => { }, (error, response, body) => {

View file

@ -8,11 +8,11 @@
"Skulks with Shotguns" "Skulks with Shotguns"
], ],
"scripts": { "scripts": {
"test": "NODE_ENV=test mocha --harmony spec/", "test": "NODE_ENV=test mocha spec/",
"start": "npm run compile && node --harmony index.js", "start": "npm run compile && node index.js",
"watch": "node_modules/gulp/bin/gulp.js && node_modules/gulp/bin/gulp.js watch", "watch": "node_modules/gulp/bin/gulp.js && node_modules/gulp/bin/gulp.js watch",
"compile": "node_modules/gulp/bin/gulp.js", "compile": "node_modules/gulp/bin/gulp.js",
"dev": "nodemon --harmony index.js" "dev": "nodemon index.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View file

@ -516,6 +516,20 @@ describe("Gather Model:", function () {
}); });
}); });
describe("modifyGatherer", function () {
beforeEach(function () {
gather.addGatherer(user);
});
it ("modifies a gatherer", function () {
assert.isFalse(gather.gatherers[0].confirm);
gather.modifyGatherer(user, gatherer => {
gatherer.confirm = true;
});
let g = gather.getGatherer(user);
assert.isTrue(g.confirm);
});
});
describe("getGatherer", function () { describe("getGatherer", function () {
beforeEach(function () { beforeEach(function () {
gather.addGatherer(user); gather.addGatherer(user);