Implemented multiple votes for maps and servers

This commit is contained in:
Chris Blanchard 2015-10-03 18:17:31 +01:00
parent c11b94f9c7
commit 46b007f102
4 changed files with 85 additions and 13 deletions

View file

@ -323,11 +323,11 @@ Gather.prototype.toJson = function () {
};
Gather.prototype.voteForMap = function (voter, mapId) {
this.modifyGatherer(voter, gatherer => gatherer.mapVote = mapId);
this.modifyGatherer(voter, gatherer => gatherer.voteForMap(mapId));
};
Gather.prototype.voteForServer = function (voter, serverId) {
this.modifyGatherer(voter, gatherer => gatherer.serverVote = serverId);
this.modifyGatherer(voter, gatherer => gatherer.voteForServer(serverId));
};
// Returns an array of IDs representing votes for leaders

View file

@ -12,17 +12,34 @@
var User = require("../user/user");
var MAX_MAP_VOTES = 2;
var MAX_SERVER_VOTES = 2;
function Gatherer (user) {
this.leaderVote = null;
this.mapVote = null;
this.serverVote = null;
this.mapVote = [];
this.serverVote = [];
this.confirm = false;
this.id = user.id;
this.user = user;
this.leader = false;
this.team = "lobby";
this.regatherVote = false;
}
};
Gatherer.prototype.voteForMap = function (mapId) {
if (this.mapVote.some(votedId => votedId === mapId)) return;
this.mapVote.push(mapId);
this.mapVote = this.mapVote.slice(this.mapVote.length - MAX_MAP_VOTES,
this.mapVote.length);
};
Gatherer.prototype.voteForServer = function (serverId) {
if (this.serverVote.some(votedId => votedId === serverId)) return;
this.serverVote.push(serverId);
this.serverVote = this.serverVote.slice(this.serverVote.length -
MAX_SERVER_VOTES, this.serverVote.length);
};
Gatherer.prototype.voteForLeader = function (candidate) {
if (candidate === null) {

View file

@ -480,7 +480,7 @@ var ServerVoting = React.createClass({
votesForServer(server) {
return this.props.gather.gatherers.reduce((acc, gatherer) => {
if (server.id === gatherer.serverVote) acc++;
if (gatherer.serverVote.some(voteId => voteId === server.id)) acc++;
return acc;
}, 0);
},
@ -494,7 +494,7 @@ var ServerVoting = React.createClass({
return bVotes - aVotes;
}).map(server => {
let votes = self.votesForServer(server);
if (thisGatherer.serverVote === server.id) {
if (thisGatherer.serverVote.some(voteId => voteId === server.id)) {
return (
<a href="#"
className="list-group-item list-group-item-success"
@ -516,12 +516,13 @@ var ServerVoting = React.createClass({
}
});
let voted = thisGatherer.serverVote !== null;
let votes = thisGatherer.serverVote.length;
return (
<div className="panel panel-primary">
<div className="panel-heading">
{voted ? "Server Votes" : "Please Vote for a Server" }
{votes === 2 ? "Server Votes" :
`Please Vote for a Server. ${2 - votes} votes remaining` }
</div>
<div className="list-group gather-voting">
{servers}
@ -545,7 +546,7 @@ var MapVoting = React.createClass({
votesForMap(map) {
return this.props.gather.gatherers.reduce((acc, gatherer) => {
if (map.id === gatherer.mapVote) acc++;
if (gatherer.mapVote.some(voteId => voteId === map.id)) acc++;
return acc;
}, 0);
},
@ -559,7 +560,7 @@ var MapVoting = React.createClass({
return bVotes - aVotes;
}).map(map => {
let votes = self.votesForMap(map);
if (thisGatherer.mapVote === map.id) {
if (thisGatherer.mapVote.some(voteId => voteId === map.id)) {
return (
<a href="#"
key={map.id}
@ -582,11 +583,14 @@ var MapVoting = React.createClass({
}
});
let voted = (thisGatherer.mapVote !== null);
let votes = thisGatherer.mapVote.length;
return (
<div className="panel panel-primary">
<div className="panel-heading">
{votes === 2 ? "Map Votes" :
`Please Vote for a Map. ${2 - votes} votes remaining` }
{ voted ? "Map Votes" : "Please Vote for a Map" }
</div>
<div className="list-group gather-voting">
@ -942,7 +946,8 @@ var CompletedGather = React.createClass({
var GatherVotingResults = React.createClass({
countVotes(voteType) {
return this.props.gather.gatherers.reduce((acc, gatherer) => {
if (gatherer[voteType] !== null) acc.push(gatherer[voteType]);
let votes = gatherer[voteType];
if (votes.length > 0) votes.forEach(vote => acc.push(vote));
return acc;
}, []);
},

View file

@ -29,7 +29,57 @@ describe("Gather Model:", function () {
});
describe("#voteForMap", function () {
it ("assigns vote for map id", function () {
assert.equal(gatherer.mapVote.length, 0);
gatherer.voteForMap(1);
assert.isTrue(gatherer.mapVote.some(voteId => voteId === 1));
});
it ("only assigns vote once", function () {
gatherer.voteForMap(1);
gatherer.voteForMap(1);
assert.equal(gatherer.mapVote.length, 1);
});
it ("allows a maximum of 2 votes", function () {
gatherer.voteForMap(1);
gatherer.voteForMap(2);
gatherer.voteForMap(3);
assert.equal(gatherer.mapVote.length, 2);
});
it ("removes oldest vote if maximum vote exceeded", function () {
gatherer.voteForMap(1);
gatherer.voteForMap(2);
gatherer.voteForMap(3);
assert.isFalse(gatherer.mapVote.some(voteId => voteId === 1));
assert.isTrue(gatherer.mapVote.some(voteId => voteId === 2));
assert.isTrue(gatherer.mapVote.some(voteId => voteId === 3));
});
});
describe("#voteForServer", function () {
it ("assigns vote for server id", function () {
assert.equal(gatherer.serverVote.length, 0);
gatherer.voteForServer(1);
assert.isTrue(gatherer.serverVote.some(voteId => voteId === 1));
});
it ("only assigns vote once", function () {
gatherer.voteForServer(1);
gatherer.voteForServer(1);
assert.equal(gatherer.serverVote.length, 1);
});
it ("allows a maximum of 2 votes", function () {
gatherer.voteForServer(1);
gatherer.voteForServer(2);
gatherer.voteForServer(3);
assert.equal(gatherer.serverVote.length, 2);
});
it ("removes oldest vote if maximum vote exceeded", function () {
gatherer.voteForServer(1);
gatherer.voteForServer(2);
gatherer.voteForServer(3);
assert.isFalse(gatherer.serverVote.some(voteId => voteId === 1));
assert.isTrue(gatherer.serverVote.some(voteId => voteId === 2));
assert.isTrue(gatherer.serverVote.some(voteId => voteId === 3));
});
});
describe("#voteForLeader", function () {