sync/poll.js

40 lines
945 B
JavaScript
Raw Normal View History

2013-03-16 21:49:58 +00:00
var Poll = function(initiator, title, options) {
this.initiator = initiator;
2013-03-16 21:49:58 +00:00
this.title = title;
this.options = options;
this.counts = new Array(options.length);
for(var i = 0; i < this.counts.length; i++) {
this.counts[i] = 0;
}
this.votes = {};
}
Poll.prototype.vote = function(ip, option) {
if(!(ip in this.votes) || this.votes[ip] == null) {
this.votes[ip] = option;
this.counts[option]++;
}
console.log(this.votes);
}
Poll.prototype.unvote = function(ip) {
console.log('unvote ' + ip + this.votes[ip]);
if(ip in this.votes && this.votes[ip] != null) {
this.counts[this.votes[ip]]--;
this.votes[ip] = null;
}
console.log(this.votes);
}
Poll.prototype.packUpdate = function() {
return {
title: this.title,
options: this.options,
counts: this.counts,
initiator: this.initiator
2013-03-16 21:49:58 +00:00
}
}
exports.Poll = Poll;