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) {
gather.voteForMap(socket._user, data.map.id);
gather.toggleMapVote(socket._user, data.map.id);
}
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")) {

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));
};
Gather.prototype.voteForServer = function (voter, serverId) {
this.modifyGatherer(voter, gatherer => gatherer.voteForServer(serverId));
Gather.prototype.toggleServerVote = function (voter, serverId) {
this.modifyGatherer(voter, gatherer => gatherer.toggleServerVote(serverId));
};
// Returns an array of IDs representing votes for leaders

View file

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

View file

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

View file

@ -364,25 +364,25 @@ describe("Gather Model:", function () {
});
});
describe("voteForMap", function () {
describe("toggleMapVote", function () {
beforeEach(function() {
gather.addGatherer(user);
});
it ("assigns map vote to gatherer", function () {
var mapId = 1;
gather.voteForMap(user, mapId);
gather.toggleMapVote(user, mapId);
var gatherer = gather.getGatherer(user);
assert.equal(gatherer.mapVote, mapId);
});
});
describe("voteForServer", function () {
describe("toggleServerVote", function () {
beforeEach(function() {
gather.addGatherer(user);
});
it ("assigns map vote to gatherer", function () {
var serverId = 1;
gather.voteForServer(user, serverId);
gather.toggleServerVote(user, serverId);
var gatherer = gather.getGatherer(user);
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 () {
assert.equal(gatherer.serverVote.length, 0);
gatherer.voteForServer(1);
gatherer.toggleServerVote(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 ("removes server vote if toggled twice", function () {
gatherer.toggleServerVote(1);
gatherer.toggleServerVote(1);
assert.equal(gatherer.serverVote.length, 0);
});
it ("allows a maximum of 2 votes", function () {
gatherer.voteForServer(1);
gatherer.voteForServer(2);
gatherer.voteForServer(3);
gatherer.toggleServerVote(1);
gatherer.toggleServerVote(2);
gatherer.toggleServerVote(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);
gatherer.toggleServerVote(1);
gatherer.toggleServerVote(2);
gatherer.toggleServerVote(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));