Make server votes toggleable, rename map and server vote methods in gather

This commit is contained in:
Chris Blanchard 2015-12-25 08:01:08 +00:00
parent cc95d9789e
commit ae5b566eef
6 changed files with 38 additions and 44 deletions

View file

@ -166,11 +166,11 @@ module.exports = function (namespace) {
} }
if (data.map) { if (data.map) {
gather.voteForMap(socket._user, data.map.id); gather.toggleMapVote(socket._user, data.map.id);
} }
if (data.server) { if (data.server) {
gather.voteForServer(socket._user, data.server.id); gather.toggleServerVote(socket._user, data.server.id);
} }
if (typeof data.regather === 'boolean' && gather.can("regather")) { if (typeof data.regather === 'boolean' && gather.can("regather")) {

View file

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

View file

@ -29,7 +29,7 @@ function Gatherer (user) {
Gatherer.prototype.toggleMapVote = function (mapId) { Gatherer.prototype.toggleMapVote = function (mapId) {
if (this.mapVote.some(votedId => votedId === mapId)) { if (this.mapVote.some(votedId => votedId === mapId)) {
this.mapVote = this.mapVote.filter(voteId => voteId !== mapId) this.mapVote = this.mapVote.filter(voteId => voteId !== mapId);
return; return;
} }
this.mapVote.push(mapId); this.mapVote.push(mapId);
@ -37,8 +37,11 @@ Gatherer.prototype.toggleMapVote = function (mapId) {
this.mapVote.length); this.mapVote.length);
}; };
Gatherer.prototype.voteForServer = function (serverId) { Gatherer.prototype.toggleServerVote = function (serverId) {
if (this.serverVote.some(votedId => votedId === serverId)) return; if (this.serverVote.some(votedId => votedId === serverId)) {
this.serverVote = this.serverVote.filter(voteId => voteId !== serverId);
return;
}
this.serverVote.push(serverId); this.serverVote.push(serverId);
this.serverVote = this.serverVote.slice(this.serverVote.length - this.serverVote = this.serverVote.slice(this.serverVote.length -
MAX_SERVER_VOTES, this.serverVote.length); MAX_SERVER_VOTES, this.serverVote.length);

View file

@ -504,26 +504,17 @@ var ServerVoting = React.createClass({
return bVotes - aVotes; return bVotes - aVotes;
}).map(server => { }).map(server => {
let votes = self.votesForServer(server); let votes = self.votesForServer(server);
if (thisGatherer.serverVote.some(voteId => voteId === server.id)) { let style = thisGatherer.serverVote.some(voteId => voteId === server.id) ?
return ( "list-group-item list-group-item-success" : "list-group-item";
<a href="#" return (
className="list-group-item list-group-item-success" <a href="#"
onClick={ e => e.preventDefault() } className={style}
key={server.id}> onClick={ e => e.preventDefault() }
<span className="badge">{votes}</span> key={server.id}>
{server.name || server.description} <span className="badge">{votes}</span>
</a> {server.name || server.description}
); </a>
} else { );
return (
<a href="#" className="list-group-item"
onClick={self.voteHandler(server.id)}
key={server.id}>
<span className="badge">{votes}</span>
{server.name || server.description}
</a>
);
}
}); });
let votes = thisGatherer.serverVote.length; let votes = thisGatherer.serverVote.length;

View file

@ -364,25 +364,25 @@ describe("Gather Model:", function () {
}); });
}); });
describe("voteForMap", function () { describe("toggleMapVote", function () {
beforeEach(function() { beforeEach(function() {
gather.addGatherer(user); gather.addGatherer(user);
}); });
it ("assigns map vote to gatherer", function () { it ("assigns map vote to gatherer", function () {
var mapId = 1; var mapId = 1;
gather.voteForMap(user, mapId); gather.toggleMapVote(user, mapId);
var gatherer = gather.getGatherer(user); var gatherer = gather.getGatherer(user);
assert.equal(gatherer.mapVote, mapId); assert.equal(gatherer.mapVote, mapId);
}); });
}); });
describe("voteForServer", function () { describe("toggleServerVote", function () {
beforeEach(function() { beforeEach(function() {
gather.addGatherer(user); gather.addGatherer(user);
}); });
it ("assigns map vote to gatherer", function () { it ("assigns map vote to gatherer", function () {
var serverId = 1; var serverId = 1;
gather.voteForServer(user, serverId); gather.toggleServerVote(user, serverId);
var gatherer = gather.getGatherer(user); var gatherer = gather.getGatherer(user);
assert.equal(gatherer.serverVote, serverId); assert.equal(gatherer.serverVote, serverId);
}); });

View file

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