This commit is contained in:
Chris Blanchard 2015-08-11 00:44:54 +01:00
parent 2e44439b5b
commit 1ae14e7912

View file

@ -19,17 +19,18 @@ function Gather (options) {
return new Gather(options);
}
var noop = () => {};
if (options && typeof options.onEvent === 'function') {
this.onEvent = options.onEvent;
} else {
this.onEvent = ()=>{};
this.onEvent = noop;
}
if (options && typeof options.onDone === 'function') {
this.onDone = options.onDone;
} else {
this.onDone = ()=>{};
this.onDone = noop;
}
this.TEAM_SIZE = 6;
@ -39,43 +40,41 @@ function Gather (options) {
this.initState();
}
Gather.prototype.alienLeader = function () {
return this.gatherers.reduce(function (acc, gatherer) {
Gather.prototype.alienLeader = () => {
return this.gatherers.reduce((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) {
Gather.prototype.marineLeader = () => {
return this.gatherers.reduce((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) {
Gather.prototype.assignMarineLeader = id => {
this.modifyGatherer({id: id}, gatherer => {
gatherer.leader = true;
gatherer.team = "marine";
return gatherer;
});
};
Gather.prototype.assignAlienLeader = function (id) {
this.modifyGatherer({id: id}, function (gatherer) {
Gather.prototype.assignAlienLeader = id => {
this.modifyGatherer({id: id}, gatherer => {
gatherer.leader = true;
gatherer.team = "alien";
return gatherer;
});
};
Gather.prototype.containsUser = function (user) {
return this.gatherers.some(function (gatherer) {
Gather.prototype.containsUser = user => {
return this.gatherers.some(gatherer => {
return gatherer.id === user.id;
});
};
Gather.prototype.addUser = function (user) {
Gather.prototype.addUser = user => {
if (this.containsUser(user)) {
return null;
}
@ -84,39 +83,28 @@ Gather.prototype.addUser = function (user) {
return gatherer;
};
Gather.prototype.removeUser = function (user) {
this.gatherers = this.gatherers.filter(function (gatherer) {
return user.id !== gatherer.id;
});
Gather.prototype.removeUser = user => {
this.gatherers = this.gatherers.filter(gatherer => user.id !== gatherer.id);
};
Gather.prototype.modifyGatherer = function (user, callback) {
this.gatherers.forEach(function (gatherer, index, array) {
if (gatherer.id === user.id) {
var modifiedUser = callback(gatherer);
array[index] = modifiedUser;
}
Gather.prototype.modifyGatherer = (user, callback) => {
this.gatherers.forEach(gatherer => {
if (gatherer.id === user.id) callback(gatherer);
});
}
Gather.prototype.moveToMarine = function (user) {
Gather.prototype.moveToMarine = user => {
if (this.marines().length >= this.TEAM_SIZE) return;
this.modifyGatherer(user, function (gatherer) {
gatherer.team = "marine";
return gatherer;
});
this.modifyGatherer(user, gatherer => gatherer.team = "marine");
};
Gather.prototype.moveToAlien = function (user) {
Gather.prototype.moveToAlien = user => {
if (this.aliens().length >= this.TEAM_SIZE) return;
this.modifyGatherer(user, function (gatherer) {
gatherer.team = "alien";
return gatherer;
});
this.modifyGatherer(user, gatherer => gatherer.team = "alien");
};
Gather.prototype.moveToLobby = function (user) {
this.gatherers.forEach(function (gatherer, index, array) {
Gather.prototype.moveToLobby = user => {
this.gatherers.forEach((gatherer, index, array) => {
if (gatherer.id === user.id) {
gatherer.team = "lobby";
array[index] = gatherer;
@ -124,25 +112,23 @@ Gather.prototype.moveToLobby = function (user) {
});
};
Gather.prototype.retrieveGroup = function (team) {
return this.gatherers.filter(function (gatherer) {
return gatherer.team === team;
});
}
Gather.prototype.retrieveGroup = team => {
return this.gatherers.filter(gatherer => gatherer.team === team);
};
Gather.prototype.lobby = function () {
Gather.prototype.lobby = () => {
return this.retrieveGroup("lobby");
};
Gather.prototype.aliens = function () {
Gather.prototype.aliens = () => {
return this.retrieveGroup("alien");
};
Gather.prototype.marines = function () {
Gather.prototype.marines = () => {
return this.retrieveGroup("marine");
};
Gather.prototype.toJson = function () {
Gather.prototype.toJson = () => {
return {
gatherers: this.gatherers,
state: this.current,
@ -153,16 +139,16 @@ Gather.prototype.toJson = function () {
}
};
Gather.prototype.voteForMap = function (voter, mapId) {
this.gatherers.forEach(function (gatherer, index, array) {
Gather.prototype.voteForMap = (voter, mapId) => {
this.gatherers.forEach((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) {
Gather.prototype.voteForServer = (voter, serverId) => {
this.gatherers.forEach((gatherer, index, array) => {
if (gatherer.id === voter.id) {
array[index].serverVote = serverId;
}
@ -170,31 +156,27 @@ Gather.prototype.voteForServer = function (voter, serverId) {
};
// Returns an array of IDs representing votes for leaders
Gather.prototype.leaderVotes = function () {
Gather.prototype.leaderVotes = () => {
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;
return self.gatherers.map(gatherer => gatherer.leaderVote)
.filter(leaderId => typeof leaderId === 'number')
.filter(candidate => {
return self.gatherers.some(gatherer => gatherer.id === candidate);
});
});
};
Gather.prototype.voteForLeader = function (voter, candidate) {
Gather.prototype.voteForLeader = (voter, candidate) => {
// Find voter and then assign their vote to candidate id
this.gatherers.forEach(function (gatherer, index, array) {
this.gatherers.forEach((gatherer, index, array) => {
if (gatherer.id === voter.id) {
array[index].voteForLeader(candidate);
}
});
};
Gather.prototype.getGatherer = function (user) {
Gather.prototype.getGatherer = (user) => {
var matchingGatherer = null;
this.gatherers.forEach(function (gatherer) {
this.gatherers.forEach((gatherer) => {
if (gatherer.id === user.id) matchingGatherer = gatherer;
});
return matchingGatherer;
@ -224,12 +206,12 @@ StateMachine.create({
],
callbacks: {
// Callbacks for events
onafterevent: function () {
onafterevent: () => {
this.onEvent.call(this);
},
// Gathering State
onbeforeaddGatherer: function (event, from, to, user) {
onbeforeaddGatherer: (event, from, to, user) => {
this.addUser(user);
if (this.gatherers.length !== 12) {
return false;
@ -237,64 +219,60 @@ StateMachine.create({
},
// Election State
onbeforeselectLeader: function (event, from, to, voter, candidate) {
onbeforeselectLeader: (event, from, to, voter, candidate) => {
this.voteForLeader(voter, candidate);
if (this.leaderVotes().length !== 12) {
return false;
}
},
onenterelection: function () {
onenterelection: () => {
// Setup timer for elections
var self = this;
self.electionStartTime = new Date();
setTimeout(function () {
setTimeout(() => {
if (self.can("electionTimeout")) {
self.electionTimeout();
}
}, self.ELECTION_INTERVAL);
},
onleaveelection: function () {
onleaveelection: () => {
this.electionStartTime = null;
},
// Selection State
onenterselection: function () {
onenterselection: () => {
// Remove all leaders and teams
this.gatherers.forEach(function (gatherer, index, array) {
array[index].leader = false;
array[index].team = "lobby";
this.gatherers.forEach(gatherer => {
gatherer.leader = false;
gatherer.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]++;
});
this.gatherers.forEach(gatherer => { voteCount[gatherer.id] = 0 });
this.leaderVotes().forEach(candidateId => { voteCount[candidateId]++ });
var rank = [];
for (var candidate in voteCount) {
rank.push({ candidate: candidate, count: voteCount[candidate] });
}
rank.sort(function (a, b) {
rank.sort((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) {
onbeforeconfirmSelection: (event, from, to, leader) => {
return (this.aliens().length === this.TEAM_SIZE
&& this.marines().length === this.TEAM_SIZE);
},
// Remove gatherer event
onbeforeremoveGatherer: function (event, from, to, user) {
onbeforeremoveGatherer: (event, from, to, user) => {
// Cancel transition if no gatherers have been removed
let userCount = this.gatherers.length;
this.removeUser(user);
@ -302,7 +280,7 @@ StateMachine.create({
},
// On enter done
onenterdone: function () {
onenterdone: () => {
this.onDone.call(this);
}
}