mirror of
https://github.com/ENSL/ensl_gathers.git
synced 2025-02-20 18:52:27 +00:00
Node v4.1 ready
This commit is contained in:
parent
afb791600b
commit
85815293b6
9 changed files with 71 additions and 58 deletions
|
@ -18,7 +18,7 @@ messageSchema.index({ deleted: 1, createdAt: -1 });
|
|||
|
||||
// Instance Methods
|
||||
|
||||
messageSchema.methods.toJson = () => {
|
||||
messageSchema.methods.toJson = function () {
|
||||
return {
|
||||
id: this.id,
|
||||
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})
|
||||
|
||||
if (options.before) {
|
||||
|
|
|
@ -22,7 +22,7 @@ var profileSchema = new Schema({
|
|||
profileSchema.path('userId').index({ unique: true });
|
||||
|
||||
profileSchema.static({
|
||||
findOrCreate: (user, callback) => {
|
||||
findOrCreate: function (user, callback) {
|
||||
if (!user || typeof user.id !== 'number') return callback(new Error("Invalid user"));
|
||||
let self = this;
|
||||
self.findOne({userId: user.id}, (error, profile) => {
|
||||
|
@ -37,7 +37,7 @@ profileSchema.static({
|
|||
});
|
||||
|
||||
profileSchema.method({
|
||||
toJson: () => {
|
||||
toJson: function () {
|
||||
let output = {};
|
||||
output.abilities = this.abilities;
|
||||
output.skill = this.skill;
|
||||
|
|
|
@ -20,7 +20,7 @@ function EnslClient (options) {
|
|||
this.baseUrl = config.ensl_url;
|
||||
}
|
||||
|
||||
EnslClient.prototype.getUserById = (options, callback) => {
|
||||
EnslClient.prototype.getUserById = function (options, callback) {
|
||||
var id = options.id;
|
||||
var url = this.baseUrl + "api/v1/users/" + id;
|
||||
return request({
|
||||
|
@ -29,7 +29,7 @@ EnslClient.prototype.getUserById = (options, callback) => {
|
|||
}, callback);
|
||||
};
|
||||
|
||||
EnslClient.prototype.getServers = callback => {
|
||||
EnslClient.prototype.getServers = function (callback) {
|
||||
const url = this.baseUrl + "api/v1/servers";
|
||||
return request({
|
||||
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";
|
||||
return request({
|
||||
url: url,
|
||||
|
@ -59,11 +59,11 @@ EnslClient.prototype.getMaps = callback => {
|
|||
});
|
||||
};
|
||||
|
||||
EnslClient.prototype.getFullAvatarUri = url => {
|
||||
EnslClient.prototype.getFullAvatarUri = function (url) {
|
||||
return this.baseUrl + url.replace(/^\//, "");
|
||||
};
|
||||
|
||||
EnslClient.parseCookies = (socket) => {
|
||||
EnslClient.parseCookies = function (socket) {
|
||||
let cookieString = socket.request.headers.cookie;
|
||||
if (typeof cookieString !== 'string') return null;
|
||||
let cookies = socket.request.headers.cookie.split(";")
|
||||
|
@ -78,7 +78,7 @@ EnslClient.parseCookies = (socket) => {
|
|||
return cookies;
|
||||
};
|
||||
|
||||
EnslClient.decodeSession = (sessionCookie, callback) => {
|
||||
EnslClient.decodeSession = function (sessionCookie, callback) {
|
||||
if (typeof sessionCookie !== 'string') {
|
||||
return callback(new Error("Invalid cookie"), null);
|
||||
}
|
||||
|
|
|
@ -58,33 +58,33 @@ StateMachine.create({
|
|||
],
|
||||
callbacks: {
|
||||
// Callbacks for events
|
||||
onafterevent: () => {
|
||||
onafterevent: function () {
|
||||
this.onEvent.call(this);
|
||||
},
|
||||
|
||||
// Gathering State
|
||||
onbeforeaddGatherer: (event, from, to, user) => {
|
||||
onbeforeaddGatherer: function (event, from, to, user) {
|
||||
this.addUser(user);
|
||||
if (this.gatherers.length !== 12) return false;
|
||||
},
|
||||
|
||||
// Election State
|
||||
onbeforeselectLeader: (event, from, to, voter, candidate) => {
|
||||
onbeforeselectLeader: function (event, from, to, voter, candidate) {
|
||||
this.voteForLeader(voter, candidate);
|
||||
if (this.leaderVotes().length !== 12) return false;
|
||||
},
|
||||
|
||||
onenterelection: () => {
|
||||
onenterelection: function () {
|
||||
// Setup timer for elections
|
||||
this.startElectionCountdown();
|
||||
},
|
||||
|
||||
onleaveelection: () => {
|
||||
onleaveelection: function () {
|
||||
this.cancelElectionCountdown();
|
||||
},
|
||||
|
||||
// Selection State
|
||||
onenterselection: () => {
|
||||
onenterselection: function () {
|
||||
// Remove all leaders and teams
|
||||
this.gatherers.forEach(gatherer => {
|
||||
gatherer.leader = false;
|
||||
|
@ -108,13 +108,13 @@ StateMachine.create({
|
|||
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
|
||||
&& this.marines().length === this.TEAM_SIZE);
|
||||
},
|
||||
|
||||
// Remove gatherer event
|
||||
onbeforeremoveGatherer: (event, from, to, user) => {
|
||||
onbeforeremoveGatherer: function (event, from, to, user) {
|
||||
// Cancel transition if no gatherers have been removed
|
||||
let userCount = this.gatherers.length;
|
||||
this.removeUser(user);
|
||||
|
@ -122,7 +122,7 @@ StateMachine.create({
|
|||
},
|
||||
|
||||
// Set gatherer vote & if threshold met, reset gather
|
||||
onbeforeregather: (event, from, to, user, vote) => {
|
||||
onbeforeregather: function (event, from, to, user, vote) {
|
||||
let self = this;
|
||||
self.modifyGatherer(user, (gatherer) => gatherer.voteRegather(vote));
|
||||
if (self.regatherVotes() >= self.REGATHER_THRESHOLD) {
|
||||
|
@ -134,64 +134,64 @@ StateMachine.create({
|
|||
},
|
||||
|
||||
// On enter done
|
||||
onenterdone: () => {
|
||||
onenterdone: function () {
|
||||
this.done.time = new Date();
|
||||
this.onDone.call(this);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Gather.prototype.resetState = () => {
|
||||
Gather.prototype.resetState = function () {
|
||||
this.gatherers = [];
|
||||
return this;
|
||||
};
|
||||
|
||||
Gather.prototype.alienLeader = () => {
|
||||
Gather.prototype.alienLeader = function () {
|
||||
return this.gatherers.reduce((acc, gatherer) => {
|
||||
if (gatherer.team === "alien" && gatherer.leader) acc.push(gatherer);
|
||||
return acc;
|
||||
}, []).pop();
|
||||
};
|
||||
|
||||
Gather.prototype.marineLeader = () => {
|
||||
Gather.prototype.marineLeader = function () {
|
||||
return this.gatherers.reduce((acc, gatherer) => {
|
||||
if (gatherer.team === "marine" && gatherer.leader) acc.push(gatherer);
|
||||
return acc;
|
||||
}, []).pop();
|
||||
};
|
||||
|
||||
Gather.prototype.assignMarineLeader = id => {
|
||||
Gather.prototype.assignMarineLeader = function (id) {
|
||||
this.modifyGatherer({id: id}, gatherer => {
|
||||
gatherer.leader = true;
|
||||
gatherer.team = "marine";
|
||||
});
|
||||
};
|
||||
|
||||
Gather.prototype.assignAlienLeader = id => {
|
||||
Gather.prototype.assignAlienLeader = function (id) {
|
||||
this.modifyGatherer({id: id}, gatherer => {
|
||||
gatherer.leader = true;
|
||||
gatherer.team = "alien";
|
||||
});
|
||||
};
|
||||
|
||||
Gather.prototype.containsUser = user => {
|
||||
Gather.prototype.containsUser = function (user) {
|
||||
return this.gatherers.some(gatherer => {
|
||||
return gatherer.id === user.id;
|
||||
});
|
||||
};
|
||||
|
||||
Gather.prototype.addUser = user => {
|
||||
Gather.prototype.addUser = function (user) {
|
||||
if (this.containsUser(user)) return null;
|
||||
let gatherer = new Gatherer(user);
|
||||
this.gatherers.push(gatherer);
|
||||
return gatherer;
|
||||
};
|
||||
|
||||
Gather.prototype.removeUser = user => {
|
||||
Gather.prototype.removeUser = function (user) {
|
||||
this.gatherers = this.gatherers.filter(gatherer => user.id !== gatherer.id);
|
||||
};
|
||||
|
||||
Gather.prototype.modifyGatherer = (user, callback) => {
|
||||
Gather.prototype.modifyGatherer = function (user, callback){
|
||||
return this.gatherers
|
||||
.filter(gatherer => gatherer.id === user.id)
|
||||
.forEach(callback);
|
||||
|
@ -201,7 +201,7 @@ Gather.prototype.modifyGatherer = (user, callback) => {
|
|||
// Marine 1 pick first
|
||||
// 2 picks for each team subsequently
|
||||
|
||||
Gather.prototype.pickingTurn = () => {
|
||||
Gather.prototype.pickingTurn = function () {
|
||||
if (this.current !== 'selection') return null;
|
||||
let alienCount = this.aliens().length;
|
||||
let marineCount = this.marines().length;
|
||||
|
@ -224,7 +224,7 @@ Gather.prototype.pickingTurn = () => {
|
|||
// Moves player to marine
|
||||
// Optional `mover` argument which will check mover credentials to select
|
||||
// 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 (mover && this.containsUser(mover)) {
|
||||
|
@ -245,7 +245,7 @@ Gather.prototype.moveToMarine = (user, mover) => {
|
|||
// Optional `mover` argument which will check mover credentials to select
|
||||
// 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 (mover && this.containsUser(mover)) {
|
||||
|
@ -259,36 +259,35 @@ Gather.prototype.moveToAlien = (user, mover) => {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
return this.modifyGatherer(user, gatherer => gatherer.team = "alien");
|
||||
};
|
||||
|
||||
Gather.prototype.moveToLobby = user => {
|
||||
Gather.prototype.moveToLobby = function (user) {
|
||||
this.modifyGatherer(user, gatherer => gatherer.team = "lobby");
|
||||
};
|
||||
|
||||
Gather.prototype.retrieveGroup = team => {
|
||||
Gather.prototype.retrieveGroup = function (team) {
|
||||
return this.gatherers.filter(gatherer => gatherer.team === team);
|
||||
};
|
||||
|
||||
Gather.prototype.lobby = () => {
|
||||
Gather.prototype.lobby = function () {
|
||||
return this.retrieveGroup("lobby");
|
||||
};
|
||||
|
||||
Gather.prototype.aliens = () => {
|
||||
Gather.prototype.aliens = function () {
|
||||
return this.retrieveGroup("alien");
|
||||
};
|
||||
|
||||
Gather.prototype.marines = () => {
|
||||
Gather.prototype.marines = function () {
|
||||
return this.retrieveGroup("marine");
|
||||
};
|
||||
|
||||
Gather.prototype.electionStartTime = () => {
|
||||
Gather.prototype.electionStartTime = function () {
|
||||
return (this.election.startTime === null) ?
|
||||
null : this.election.startTime.toISOString();
|
||||
};
|
||||
|
||||
Gather.prototype.toJson = () => {
|
||||
Gather.prototype.toJson = function () {
|
||||
return {
|
||||
gatherers: this.gatherers,
|
||||
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);
|
||||
};
|
||||
|
||||
Gather.prototype.voteForServer = (voter, serverId) => {
|
||||
Gather.prototype.voteForServer = function (voter, serverId) {
|
||||
this.modifyGatherer(voter, gatherer => gatherer.serverVote = serverId);
|
||||
};
|
||||
|
||||
// Returns an array of IDs representing votes for leaders
|
||||
Gather.prototype.leaderVotes = () => {
|
||||
Gather.prototype.leaderVotes = function () {
|
||||
let self = this;
|
||||
return self.gatherers
|
||||
.map(gatherer => gatherer.leaderVote)
|
||||
|
@ -320,17 +319,17 @@ Gather.prototype.leaderVotes = () => {
|
|||
.filter(leaderId => self.containsUser({id: leaderId}));
|
||||
};
|
||||
|
||||
Gather.prototype.voteForLeader = (voter, candidate) => {
|
||||
Gather.prototype.voteForLeader = function (voter, candidate) {
|
||||
this.modifyGatherer(voter, gatherer => gatherer.voteForLeader(candidate));
|
||||
};
|
||||
|
||||
Gather.prototype.getGatherer = (user) => {
|
||||
Gather.prototype.getGatherer = function (user) {
|
||||
return this.gatherers
|
||||
.filter(gatherer => gatherer.id === user.id)
|
||||
.pop() || null;
|
||||
};
|
||||
|
||||
Gather.prototype.regatherVotes = () => {
|
||||
Gather.prototype.regatherVotes = function () {
|
||||
let self = this;
|
||||
return self.gatherers.reduce((acc, gatherer) => {
|
||||
if (gatherer.regatherVote) acc++;
|
||||
|
@ -339,7 +338,7 @@ Gather.prototype.regatherVotes = () => {
|
|||
};
|
||||
|
||||
// Initiates a timer which will push gather into next state
|
||||
Gather.prototype.startElectionCountdown = () => {
|
||||
Gather.prototype.startElectionCountdown = function () {
|
||||
let self = this;
|
||||
self.election.startTime = new Date();
|
||||
this.election.timer = setTimeout(() => {
|
||||
|
@ -347,7 +346,7 @@ Gather.prototype.startElectionCountdown = () => {
|
|||
}, self.election.INTERVAL);
|
||||
};
|
||||
|
||||
Gather.prototype.cancelElectionCountdown = () => {
|
||||
Gather.prototype.cancelElectionCountdown = function () {
|
||||
clearInterval(this.election.timer);
|
||||
this.election.timer = null;
|
||||
this.election.startTime = null;
|
||||
|
|
|
@ -24,7 +24,7 @@ function Gatherer (user) {
|
|||
this.regatherVote = false;
|
||||
}
|
||||
|
||||
Gatherer.prototype.voteForLeader = candidate => {
|
||||
Gatherer.prototype.voteForLeader = function (candidate) {
|
||||
if (candidate === null) {
|
||||
return this.leaderVote = null;
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ Gatherer.prototype.voteForLeader = candidate => {
|
|||
this.leaderVote = candidate.id;
|
||||
};
|
||||
|
||||
Gatherer.prototype.voteRegather = (vote) => {
|
||||
Gatherer.prototype.voteRegather = function (vote) {
|
||||
if (vote !== undefined && typeof vote === 'boolean') {
|
||||
return this.regatherVote = vote;
|
||||
} else {
|
||||
|
|
|
@ -14,7 +14,7 @@ function HiveClient (options) {
|
|||
this.baseUrl = config.hive_url;
|
||||
}
|
||||
|
||||
HiveClient.prototype.getUserStats = (user, callback) => {
|
||||
HiveClient.prototype.getUserStats = function (user, callback) {
|
||||
if (!user || !user.hive.id) {
|
||||
return callback(new Error("Invalid user instance supplied"));
|
||||
}
|
||||
|
|
|
@ -38,14 +38,14 @@ function User (user) {
|
|||
}
|
||||
}
|
||||
|
||||
User.prototype.getSteamId = () => {
|
||||
User.prototype.getSteamId = function () {
|
||||
if (this.steam.url === null) return null;
|
||||
var urlId = this.steam.url.match(/\d*$/);
|
||||
if (!urlId || urlId[0].length === 0) return null;
|
||||
return steam.convertToText(urlId[0]);
|
||||
};
|
||||
|
||||
User.prototype.getHiveId = () => {
|
||||
User.prototype.getHiveId = function () {
|
||||
var steamId = this.steam.id;
|
||||
if (!steamId) return null;
|
||||
var index = steamId.match(/:0:/) ? 0 : 1;
|
||||
|
@ -55,7 +55,7 @@ User.prototype.getHiveId = () => {
|
|||
|
||||
var allowedAttributes = ["enslo", "division", "skill", "gatherMusic"];
|
||||
var allowedAbilities = ["skulk", "lerk", "fade", "gorge", "onos", "commander"];
|
||||
User.prototype.updateProfile = (data, callback) => {
|
||||
User.prototype.updateProfile = function (data, callback) {
|
||||
let self = this;
|
||||
Profile.findOne({userId: self.id}, (error, profile) => {
|
||||
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({
|
||||
id: id
|
||||
}, (error, response, body) => {
|
||||
|
|
|
@ -8,11 +8,11 @@
|
|||
"Skulks with Shotguns"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "NODE_ENV=test mocha --harmony spec/",
|
||||
"start": "npm run compile && node --harmony index.js",
|
||||
"test": "NODE_ENV=test mocha spec/",
|
||||
"start": "npm run compile && node index.js",
|
||||
"watch": "node_modules/gulp/bin/gulp.js && node_modules/gulp/bin/gulp.js watch",
|
||||
"compile": "node_modules/gulp/bin/gulp.js",
|
||||
"dev": "nodemon --harmony index.js"
|
||||
"dev": "nodemon index.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
|
|
@ -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 () {
|
||||
beforeEach(function () {
|
||||
gather.addGatherer(user);
|
||||
|
|
Loading…
Reference in a new issue