forked from GitHub/dbot
commit
d1ab38d4b1
152
modules/poll.js
152
modules/poll.js
@ -6,9 +6,10 @@ var poll = function(dbot) {
|
|||||||
var polls = dbot.db.polls;
|
var polls = dbot.db.polls;
|
||||||
var commands = {
|
var commands = {
|
||||||
'~newpoll': function(event) {
|
'~newpoll': function(event) {
|
||||||
var name = event.input[1];
|
var av = event.input[1] != undefined;
|
||||||
var options = event.input[2].split(',');
|
var name = event.input[2];
|
||||||
var description = event.input[3];
|
var options = event.input[3].split(',');
|
||||||
|
var description = event.input[4];
|
||||||
|
|
||||||
if(name === undefined || name === 'help') {
|
if(name === undefined || name === 'help') {
|
||||||
event.reply(dbot.t('newpoll_usage'));
|
event.reply(dbot.t('newpoll_usage'));
|
||||||
@ -16,16 +17,30 @@ var poll = function(dbot) {
|
|||||||
if(polls.hasOwnProperty(name)) {
|
if(polls.hasOwnProperty(name)) {
|
||||||
event.reply(dbot.t('poll_exists', {'name': name}));
|
event.reply(dbot.t('poll_exists', {'name': name}));
|
||||||
} else {
|
} else {
|
||||||
polls[name] = {
|
if(av) {
|
||||||
'name': name,
|
polls[name] = {
|
||||||
'description': description,
|
'av': av,
|
||||||
'owner': event.user,
|
'name': name,
|
||||||
'votes': {},
|
'description': description,
|
||||||
'votees': {}
|
'owner': event.user,
|
||||||
};
|
'votes': {},
|
||||||
|
'options': []
|
||||||
for(var i=0;i<options.length;i++) {
|
};
|
||||||
polls[name]['votes'][options[i]] = 0;
|
for(var i=0;i<options.length;i++) {
|
||||||
|
polls[name].options.push(options[i]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
polls[name] = {
|
||||||
|
'av': av,
|
||||||
|
'name': name,
|
||||||
|
'description': description,
|
||||||
|
'owner': event.user,
|
||||||
|
'votes': {},
|
||||||
|
'votees': {}
|
||||||
|
};
|
||||||
|
for(var i=0;i<options.length;i++) {
|
||||||
|
polls[name]['votes'][options[i]] = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
event.reply(dbot.t('poll_created', {'name': name, 'description': description}) +
|
event.reply(dbot.t('poll_created', {'name': name, 'description': description}) +
|
||||||
@ -83,22 +98,43 @@ var poll = function(dbot) {
|
|||||||
var vote = event.input[2];
|
var vote = event.input[2];
|
||||||
|
|
||||||
if(polls.hasOwnProperty(name)) {
|
if(polls.hasOwnProperty(name)) {
|
||||||
if(polls[name].votes.hasOwnProperty(vote)) {
|
if(polls[name].av) {
|
||||||
if(polls[name].votees.hasOwnProperty(event.user)) {
|
var prefs = vote.split(',');
|
||||||
var oldVote = polls[name].votees[event.user];
|
prefs = prefs.uniq();
|
||||||
polls[name].votes[oldVote]--;
|
var valid = true;
|
||||||
polls[name].votes[vote]++;
|
|
||||||
polls[name].votees[event.user] = vote;
|
prefs.each(function(pref) {
|
||||||
event.reply(dbot.t('changed_vote', {'vote': vote, 'poll': name,
|
valid = valid && polls[name].options.indexOf(pref) != -1;
|
||||||
'count': polls[name].votes[vote], 'user': event.user}));
|
});
|
||||||
|
if(valid){
|
||||||
|
if(polls[name].votes.hasOwnProperty(event.user)) {
|
||||||
|
polls[name].votes[event.user] = prefs;
|
||||||
|
event.reply(dbot.t('av_changed_vote', {'vote': prefs.join(','), 'poll': name, 'user': event.user}));
|
||||||
|
} else {
|
||||||
|
polls[name].votes[event.user] = prefs;
|
||||||
|
event.reply(dbot.t('av_voted', {'vote': prefs.join(','), 'poll': name, 'user': event.user}));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
polls[name].votes[vote]++;
|
event.reply(dbot.t('invalid_vote', {'vote': vote}));
|
||||||
polls[name].votees[event.user] = vote;
|
|
||||||
event.reply(dbot.t('voted', {'vote': vote, 'poll': name,
|
|
||||||
'count': polls[name].votes[vote], 'user': event.user}));
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
event.reply(dbot.t('invalid_vote', {'vote': vote}));
|
if(polls[name].votes.hasOwnProperty(vote)) {
|
||||||
|
if(polls[name].votees.hasOwnProperty(event.user)) {
|
||||||
|
var oldVote = polls[name].votees[event.user];
|
||||||
|
polls[name].votes[oldVote]--;
|
||||||
|
polls[name].votes[vote]++;
|
||||||
|
polls[name].votees[event.user] = vote;
|
||||||
|
event.reply(dbot.t('changed_vote', {'vote': vote, 'poll': name,
|
||||||
|
'count': polls[name].votes[vote], 'user': event.user}));
|
||||||
|
} else {
|
||||||
|
polls[name].votes[vote]++;
|
||||||
|
polls[name].votees[event.user] = vote;
|
||||||
|
event.reply(dbot.t('voted', {'vote': vote, 'poll': name,
|
||||||
|
'count': polls[name].votes[vote], 'user': event.user}));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
event.reply(dbot.t('invalid_vote', {'vote': vote}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
||||||
@ -112,13 +148,75 @@ var poll = function(dbot) {
|
|||||||
} else {
|
} else {
|
||||||
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
'~count': function(event) {
|
||||||
|
var name = event.input[1];
|
||||||
|
|
||||||
|
if(polls.hasOwnProperty(name)) {
|
||||||
|
var order;
|
||||||
|
if(polls[name].av) {
|
||||||
|
var finished = false;
|
||||||
|
var rounds = [];
|
||||||
|
var eliminated = [];
|
||||||
|
var voted;
|
||||||
|
|
||||||
|
for(var roundn = 0; roundn < polls[name].options.length; roundn++) {
|
||||||
|
var roundLoser;
|
||||||
|
|
||||||
|
// Populate candidates for this round
|
||||||
|
rounds[roundn] = {};
|
||||||
|
polls[name].options.each(function (option) {
|
||||||
|
if(eliminated.indexOf(option) == -1)
|
||||||
|
rounds[roundn][option] = 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Count votes
|
||||||
|
polls[name].votes.each(function (name, vote) {
|
||||||
|
voted = false;
|
||||||
|
vote.each(function (pref) {
|
||||||
|
if(!voted && rounds[roundn].hasOwnProperty(pref)) {
|
||||||
|
rounds[roundn][pref]++;
|
||||||
|
voted = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Find the loser
|
||||||
|
var min = polls[name].votes.length() + 1;
|
||||||
|
rounds[roundn].each(function (option, count) {
|
||||||
|
if(count < min) {
|
||||||
|
roundLoser = option;
|
||||||
|
min = count;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Eliminate loser
|
||||||
|
eliminated.push(roundLoser);
|
||||||
|
}
|
||||||
|
order = eliminated.reverse().join(', ')
|
||||||
|
} else {
|
||||||
|
var votesArr = [];
|
||||||
|
polls[name].votes.each(function(option, count) {
|
||||||
|
votesArr.push([option, count]);
|
||||||
|
});
|
||||||
|
|
||||||
|
votesArr = votesArr.sort(function(a, b) { return b[1] - a[1]; });
|
||||||
|
|
||||||
|
order = votesArr.map(function(vote) { return vote[0]; });
|
||||||
|
}
|
||||||
|
event.reply(dbot.t('count', {'poll': name, 'description': polls[name].description, 'places': order}));
|
||||||
|
} else {
|
||||||
|
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
commands['~newpoll'].regex = [/~newpoll ([^ ]+) \[options=([^ ]+)\] (.+)/, 4];
|
commands['~newpoll'].regex = [/~newpoll (av )?([^ ]+) \[options=([^ ]+)\] (.+)/, 5];
|
||||||
commands['~addoption'].regex = [/~addoption ([^ ]+) ([^ ]+)/, 3];
|
commands['~addoption'].regex = [/~addoption ([^ ]+) ([^ ]+)/, 3];
|
||||||
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];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'name': 'poll',
|
'name': 'poll',
|
||||||
|
26
snippets.js
26
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) {
|
||||||
@ -152,6 +164,20 @@ Object.prototype.isArray = function(obj) {
|
|||||||
return Object.prototype.toString.call(obj) === '[object Array]';
|
return Object.prototype.toString.call(obj) === '[object Array]';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Object.prototype.each = function(fun) {
|
||||||
|
for(key in this)
|
||||||
|
if(this.hasOwnProperty(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) {
|
||||||
|
12
strings.json
12
strings.json
@ -245,5 +245,17 @@
|
|||||||
"option_removed": {
|
"option_removed": {
|
||||||
"english": "{user}: '{option}' removed from '{name}'",
|
"english": "{user}: '{option}' removed from '{name}'",
|
||||||
"spanish" : "{user}: '{option}' eliminado de '{name}'"
|
"spanish" : "{user}: '{option}' eliminado de '{name}'"
|
||||||
|
},
|
||||||
|
"av_voted": {
|
||||||
|
"english": "{user} voted '{vote}' in {poll}.",
|
||||||
|
"spanish": "{user} votó '{vote}' en {poll}."
|
||||||
|
},
|
||||||
|
"av_changed_vote": {
|
||||||
|
"english": "{user} changed their vote in {poll} to '{vote}'.",
|
||||||
|
"spanish" : "{user} cambió su voto en {poll} a '{vote}'."
|
||||||
|
},
|
||||||
|
"count": {
|
||||||
|
"english": "The running-order of poll '{poll}' ({description}) is: {places}.",
|
||||||
|
"spanish": ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user