ensl_gathers/lib/gather/gather.js

255 lines
6.5 KiB
JavaScript
Raw Normal View History

"use strict";
/*
* Implements Gather Model
*
* Gather States
2015-07-24 13:34:02 +00:00
* - Gathering
* - Election (Electing leaders)
* - Selection (Selecting teams)
* - Done
*
*/
var Gatherer = require("./gatherer");
2015-07-24 13:34:02 +00:00
var StateMachine = require("javascript-state-machine");
function Gather () {
if (!(this instanceof Gather)) {
return new Gather();
}
2015-07-24 13:34:02 +00:00
this.TEAM_SIZE = 6;
this.gatherers = [];
2015-07-24 13:34:02 +00:00
this.initState();
}
2015-07-24 13:34:02 +00:00
Gather.prototype.alienLeader = function () {
return this.gatherers.reduce(function (acc, gatherer) {
if (gatherer.team === "alien" && gatherer.leader) acc.push(gatherer);
return acc;
}, []).pop();
};
Gather.prototype.marineLeader = function () {
return this.gatherers.reduce(function (acc, gatherer) {
if (gatherer.team === "marine" && gatherer.leader) acc.push(gatherer);
return acc;
}, []).pop();
};
Gather.prototype.assignMarineLeader = function (id) {
this.modifyGatherer({id: id}, function (gatherer) {
gatherer.leader = true;
gatherer.team = "marine";
return gatherer;
});
};
Gather.prototype.assignAlienLeader = function (id) {
this.modifyGatherer({id: id}, function (gatherer) {
gatherer.leader = true;
gatherer.team = "alien";
return gatherer;
});
};
2015-07-23 13:36:51 +00:00
Gather.prototype.containsUser = function (user) {
return this.gatherers.some(function (gatherer) {
return gatherer.id === user.id;
});
};
2015-07-22 23:30:14 +00:00
2015-07-23 13:36:51 +00:00
Gather.prototype.addUser = function (user) {
if (this.containsUser(user)) {
return null;
}
var gatherer = new Gatherer(user);
this.gatherers.push(gatherer);
return gatherer;
2015-07-22 23:30:14 +00:00
};
2015-07-23 13:36:51 +00:00
Gather.prototype.removeUser = function (user) {
this.gatherers = this.gatherers.filter(function (gatherer) {
return user.id !== gatherer.id;
});
2015-07-22 23:30:14 +00:00
};
2015-07-24 13:34:02 +00:00
Gather.prototype.modifyGatherer = function (user, callback) {
2015-07-23 13:36:51 +00:00
this.gatherers.forEach(function (gatherer, index, array) {
if (gatherer.id === user.id) {
2015-07-24 13:34:02 +00:00
var modifiedUser = callback(gatherer);
array[index] = modifiedUser;
2015-07-23 13:36:51 +00:00
}
});
2015-07-24 13:34:02 +00:00
}
Gather.prototype.moveToMarine = function (user) {
if (this.marines().length >= this.TEAM_SIZE) return;
this.modifyGatherer(user, function (gatherer) {
gatherer.team = "marine";
return gatherer;
});
2015-07-22 23:30:14 +00:00
};
Gather.prototype.moveToAlien = function (user) {
2015-07-24 13:34:02 +00:00
if (this.aliens().length >= this.TEAM_SIZE) return;
this.modifyGatherer(user, function (gatherer) {
gatherer.team = "alien";
return gatherer;
2015-07-23 13:36:51 +00:00
});
};
Gather.prototype.moveToLobby = function (user) {
this.gatherers.forEach(function (gatherer, index, array) {
if (gatherer.id === user.id) {
gatherer.team = "lobby";
array[index] = gatherer;
}
});
};
Gather.prototype.retrieveGroup = function (team) {
return this.gatherers.filter(function (gatherer) {
return gatherer.team === team;
});
}
2015-07-22 23:30:14 +00:00
2015-07-23 13:36:51 +00:00
Gather.prototype.lobby = function () {
return this.retrieveGroup("lobby");
2015-07-22 23:30:14 +00:00
};
2015-07-23 13:36:51 +00:00
Gather.prototype.aliens = function () {
return this.retrieveGroup("alien");
};
Gather.prototype.marines = function () {
return this.retrieveGroup("marine");
};
Gather.prototype.toJson = function () {
return {
2015-07-24 15:01:56 +00:00
gatherers: this.gatherers,
state: this.current
2015-07-23 13:36:51 +00:00
}
2015-07-24 13:34:02 +00:00
};
2015-07-29 13:50:39 +00:00
Gather.prototype.voteForMap = function (voter, mapId) {
this.gatherers.forEach(function (gatherer, index, array) {
if (gatherer.id === voter.id) {
array[index].mapVote = mapId;
}
});
};
Gather.prototype.voteForServer = function (voter, serverId) {
this.gatherers.forEach(function (gatherer, index, array) {
if (gatherer.id === voter.id) {
array[index].serverVote = serverId;
}
});
};
2015-07-24 13:34:02 +00:00
// Returns an array of IDs representing votes for leaders
Gather.prototype.leaderVotes = function () {
var self = this;
return self.gatherers.map(function (gatherer) {
return gatherer.leaderVote
}).filter(function (leaderId) {
return (typeof leaderId === 'number');
}).filter(function (candidate) {
return self.gatherers.some(function (gatherer) {
return gatherer.id === candidate;
});
});
};
Gather.prototype.voteForLeader = function (voter, candidate) {
2015-07-27 11:55:36 +00:00
// Find voter and then assign their vote to candidate id
2015-07-24 13:34:02 +00:00
this.gatherers.forEach(function (gatherer, index, array) {
if (gatherer.id === voter.id) {
array[index].voteForLeader(candidate);
}
});
};
2015-07-28 15:54:29 +00:00
Gather.prototype.getGatherer = function (user) {
var matchingGatherer = null;
this.gatherers.forEach(function (gatherer) {
if (gatherer.id === user.id) matchingGatherer = gatherer;
});
return matchingGatherer;
};
2015-07-24 13:34:02 +00:00
// Gather States
// - Gathering
// - Election
// - Selection
// - Done
// gather.current - contains the current state
// gather.is(s) - return true if state s is the current state
// gather.can(e) - return true if event e can be fired in the current state
// gather.cannot(e) - return true if event e cannot be fired in the current state
// gather.transitions() - return list of events that are allowed from the current state
StateMachine.create({
target: Gather.prototype,
events: [
{ name: "initState", from: "none", to: "gathering" },
{ name: "addGatherer", from: "gathering", to: "election" },
{ name: "selectLeader", from: "election", to: "selection" },
{ name: "confirmSelection", from: "selection", to: "done" },
2015-07-24 17:05:12 +00:00
{ name: "removeGatherer", from: ["gathering", "election", "selection"], to: "gathering" }
2015-07-24 13:34:02 +00:00
],
callbacks: {
onbeforeaddGatherer: function (event, from, to, user) {
this.addUser(user);
if (this.gatherers.length !== 12) {
return false;
}
},
onbeforeselectLeader: function (event, from, to, voter, candidate) {
this.voteForLeader(voter, candidate);
if (this.leaderVotes().length !== 12) {
return false;
}
},
2015-07-24 17:05:12 +00:00
onbeforeremoveGatherer: function (event, from, to, user) {
2015-07-24 13:34:02 +00:00
this.removeUser(user);
},
onenterselection: function () {
// Remove all leaders and teams
this.gatherers.forEach(function (gatherer, index, array) {
array[index].leader = false;
array[index].team = "lobby";
});
// Assign leaders based on vote
// 1st place alien comm
// 2nd place marine comm
var voteCount = {};
this.gatherers.forEach(function (gatherer) {
voteCount[gatherer.id] = 0;
});
this.leaderVotes().forEach(function (candidateId) {
voteCount[candidateId]++;
});
var rank = [];
for (var candidate in voteCount) {
rank.push({ candidate: candidate, count: voteCount[candidate] });
}
rank.sort(function (a, b) {
return a.count - b.count;
});
this.assignAlienLeader(parseInt(rank.pop().candidate, 0));
this.assignMarineLeader(parseInt(rank.pop().candidate, 0));
},
onbeforeconfirmSelection: function (event, from, to, leader) {
2015-07-29 14:35:58 +00:00
return (this.aliens().length === this.TEAM_SIZE
2015-07-24 13:34:02 +00:00
&& this.marines().length === this.TEAM_SIZE);
}
}
});
2015-07-23 13:36:51 +00:00
2015-07-24 13:34:02 +00:00
module.exports = Gather;