forked from GitHub/dbot
Added AV vote counting.
This commit is contained in:
parent
c1c951f5c7
commit
d192a17d19
@ -100,20 +100,19 @@ var poll = function(dbot) {
|
|||||||
if(polls.hasOwnProperty(name)) {
|
if(polls.hasOwnProperty(name)) {
|
||||||
if(polls[name].av) {
|
if(polls[name].av) {
|
||||||
var prefs = vote.split(',');
|
var prefs = vote.split(',');
|
||||||
|
prefs = prefs.uniq();
|
||||||
var valid = true;
|
var valid = true;
|
||||||
|
|
||||||
prefs.each(function(pref) {
|
prefs.each(function(pref) {
|
||||||
console.log(pref);
|
valid = valid && polls[name].options.indexOf(pref) != -1;
|
||||||
console.log(polls[name].options);
|
|
||||||
valid = valid && polls[name].options.hasOwnProperty(pref);
|
|
||||||
});
|
});
|
||||||
if(valid){
|
if(valid){
|
||||||
if(polls[name].votes.hasOwnProperty(event.user)) {
|
if(polls[name].votes.hasOwnProperty(event.user)) {
|
||||||
polls[name].votes[event.user] = prefs;
|
polls[name].votes[event.user] = prefs;
|
||||||
event.reply(dbot.t('av_changed_vote', {'vote': vote, 'poll': name, 'user': event.user}));
|
event.reply(dbot.t('av_changed_vote', {'vote': prefs.join(','), 'poll': name, 'user': event.user}));
|
||||||
} else {
|
} else {
|
||||||
polls[name].votes[event.user] = prefs;
|
polls[name].votes[event.user] = prefs;
|
||||||
event.reply(dbot.t('av_voted', {'vote': vote, 'poll': name, 'user': event.user}));
|
event.reply(dbot.t('av_voted', {'vote': prefs.join(','), 'poll': name, 'user': event.user}));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
event.reply(dbot.t('invalid_vote', {'vote': vote}));
|
event.reply(dbot.t('invalid_vote', {'vote': vote}));
|
||||||
@ -151,33 +150,70 @@ var poll = function(dbot) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
'~count': function(event) {
|
'~winner': function(event) {
|
||||||
var name = event.input[1];
|
var name = event.input[1];
|
||||||
|
|
||||||
if(polls.hasOwnProperty(name)) {
|
if(polls.hasOwnProperty(name)) {
|
||||||
|
var winner;
|
||||||
if(polls[name].av) {
|
if(polls[name].av) {
|
||||||
var finished = false;
|
var finished = false;
|
||||||
var rounds = [];
|
var rounds = [];
|
||||||
var eliminated = [];
|
var eliminated = [];
|
||||||
|
var voted;
|
||||||
|
|
||||||
for(var roundn = 0; !finished; roundn++) {
|
for(var roundn = 0; !finished; roundn++) {
|
||||||
|
var roundLoser;
|
||||||
|
|
||||||
// Populate candidates for this round
|
// Populate candidates for this round
|
||||||
rounds[roundn] = {};
|
rounds[roundn] = {};
|
||||||
polls[name].options.each(function (option) {
|
polls[name].options.each(function (option) {
|
||||||
if(!eliminated.hasOwnProperty(option))
|
if(eliminated.indexOf(option) == -1)
|
||||||
rounds[roundn][option] = 0;
|
rounds[roundn][option] = 0;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Count votes
|
// Count votes
|
||||||
polls[name].votes.each(function (name, vote) {
|
polls[name].votes.each(function (name, vote) {
|
||||||
console.log(name);
|
voted = false;
|
||||||
|
vote.each(function (pref) {
|
||||||
|
if(!voted && rounds[roundn].hasOwnProperty(pref)) {
|
||||||
|
rounds[roundn][pref]++;
|
||||||
|
voted = true;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Check for 50%
|
||||||
|
var max = 0;
|
||||||
|
var min = polls[name].votes.length()
|
||||||
|
rounds[roundn].each(function (option, count) {
|
||||||
|
if(count > max) {
|
||||||
|
winner = option;
|
||||||
|
max = count;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(count < min) {
|
||||||
|
roundLoser = option;
|
||||||
|
min = count;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if((2*max) > polls[name].votes.length()) {
|
||||||
|
finished = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
// Eliminate loser
|
||||||
event.reply("Not yet implemented.");
|
eliminated.push(roundLoser);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
var max = 0;
|
||||||
|
polls[name].votes.each(function (name, count) {
|
||||||
|
if(count > max) {
|
||||||
|
winner = name;
|
||||||
|
max = count;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
event.reply(dbot.t('winner', {'poll': name, 'description': polls[name].description, 'winner': winner}));
|
||||||
} else {
|
} else {
|
||||||
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
||||||
}
|
}
|
||||||
@ -188,7 +224,7 @@ var poll = function(dbot) {
|
|||||||
commands['~rmoption'].regex = [/~rmoption ([^ ]+) ([^ ]+)/, 3];
|
commands['~rmoption'].regex = [/~rmoption ([^ ]+) ([^ ]+)/, 3];
|
||||||
commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3];
|
commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3];
|
||||||
commands['~pdesc'].regex = [/~pdesc ([^ ]+)/, 2];
|
commands['~pdesc'].regex = [/~pdesc ([^ ]+)/, 2];
|
||||||
commands['~count'].regex = [/~count ([^ ]+)/, 2];
|
commands['~winner'].regex = [/~winner ([^ ]+)/, 2];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'name': 'poll',
|
'name': 'poll',
|
||||||
|
20
snippets.js
20
snippets.js
@ -48,6 +48,18 @@ Array.prototype.allGroupings = function() {
|
|||||||
return groupings;
|
return groupings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Array.prototype.uniq = function() {
|
||||||
|
var hash = {}
|
||||||
|
var result = [];
|
||||||
|
this.each(function(item) {
|
||||||
|
if(!hash.hasOwnProperty(item)){
|
||||||
|
hash[item] = true;
|
||||||
|
result.push(item);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/*** String ***/
|
/*** String ***/
|
||||||
|
|
||||||
String.prototype.valMatch = function(regex, expLength) {
|
String.prototype.valMatch = function(regex, expLength) {
|
||||||
@ -158,6 +170,14 @@ Object.prototype.each = function(fun) {
|
|||||||
fun(key, this[key]);
|
fun(key, this[key]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Object.prototype.length = function() {
|
||||||
|
var l = 0;
|
||||||
|
for(key in this)
|
||||||
|
if(this.hasOwnProperty(key))
|
||||||
|
l++;
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
/*** Integer ***/
|
/*** Integer ***/
|
||||||
|
|
||||||
Number.prototype.chanceIn = function(x, y) {
|
Number.prototype.chanceIn = function(x, y) {
|
||||||
|
@ -253,5 +253,9 @@
|
|||||||
"av_changed_vote": {
|
"av_changed_vote": {
|
||||||
"english": "{user} changed their vote in {poll} to '{vote}'.",
|
"english": "{user} changed their vote in {poll} to '{vote}'.",
|
||||||
"spanish" : "{user} cambió su voto en {poll} a '{vote}'."
|
"spanish" : "{user} cambió su voto en {poll} a '{vote}'."
|
||||||
|
},
|
||||||
|
"winner": {
|
||||||
|
"english": "The winner of poll '{poll}' ({description}) is: '{winner}'.",
|
||||||
|
"spanish": "La ganera de la votación '{poll}' ({description}) es: '{winner}'."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user