diff --git a/lib/gather/gather.js b/lib/gather/gather.js index 00e7132..24b0730 100644 --- a/lib/gather/gather.js +++ b/lib/gather/gather.js @@ -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 diff --git a/lib/gather/gatherer.js b/lib/gather/gatherer.js index 5869335..95d36a4 100644 --- a/lib/gather/gatherer.js +++ b/lib/gather/gatherer.js @@ -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) { diff --git a/lib/react/gather.jsx b/lib/react/gather.jsx index 59875f8..ac94fb5 100644 --- a/lib/react/gather.jsx +++ b/lib/react/gather.jsx @@ -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 (
- {voted ? "Server Votes" : "Please Vote for a Server" } + {votes === 2 ? "Server Votes" : + `Please Vote for a Server. ${2 - votes} votes remaining` }
{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 (
+ {votes === 2 ? "Map Votes" : + `Please Vote for a Map. ${2 - votes} votes remaining` } + { voted ? "Map Votes" : "Please Vote for a Map" }
@@ -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; }, []); }, diff --git a/spec/gatherer.js b/spec/gatherer.js index 12c21c5..81c2542 100644 --- a/spec/gatherer.js +++ b/spec/gatherer.js @@ -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 () {