mirror of
https://github.com/reality/dbot.git
synced 2024-11-27 14:29:29 +01:00
commit
d1ab38d4b1
152
modules/poll.js
152
modules/poll.js
@ -6,9 +6,10 @@ var poll = function(dbot) {
|
||||
var polls = dbot.db.polls;
|
||||
var commands = {
|
||||
'~newpoll': function(event) {
|
||||
var name = event.input[1];
|
||||
var options = event.input[2].split(',');
|
||||
var description = event.input[3];
|
||||
var av = event.input[1] != undefined;
|
||||
var name = event.input[2];
|
||||
var options = event.input[3].split(',');
|
||||
var description = event.input[4];
|
||||
|
||||
if(name === undefined || name === 'help') {
|
||||
event.reply(dbot.t('newpoll_usage'));
|
||||
@ -16,16 +17,30 @@ var poll = function(dbot) {
|
||||
if(polls.hasOwnProperty(name)) {
|
||||
event.reply(dbot.t('poll_exists', {'name': name}));
|
||||
} else {
|
||||
polls[name] = {
|
||||
'name': name,
|
||||
'description': description,
|
||||
'owner': event.user,
|
||||
'votes': {},
|
||||
'votees': {}
|
||||
};
|
||||
|
||||
for(var i=0;i<options.length;i++) {
|
||||
polls[name]['votes'][options[i]] = 0;
|
||||
if(av) {
|
||||
polls[name] = {
|
||||
'av': av,
|
||||
'name': name,
|
||||
'description': description,
|
||||
'owner': event.user,
|
||||
'votes': {},
|
||||
'options': []
|
||||
};
|
||||
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}) +
|
||||
@ -83,22 +98,43 @@ var poll = function(dbot) {
|
||||
var vote = event.input[2];
|
||||
|
||||
if(polls.hasOwnProperty(name)) {
|
||||
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}));
|
||||
if(polls[name].av) {
|
||||
var prefs = vote.split(',');
|
||||
prefs = prefs.uniq();
|
||||
var valid = true;
|
||||
|
||||
prefs.each(function(pref) {
|
||||
valid = valid && polls[name].options.indexOf(pref) != -1;
|
||||
});
|
||||
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 {
|
||||
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}));
|
||||
event.reply(dbot.t('invalid_vote', {'vote': vote}));
|
||||
}
|
||||
} 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 {
|
||||
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
||||
@ -112,13 +148,75 @@ var poll = function(dbot) {
|
||||
} else {
|
||||
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['~rmoption'].regex = [/~rmoption ([^ ]+) ([^ ]+)/, 3];
|
||||
commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3];
|
||||
commands['~pdesc'].regex = [/~pdesc ([^ ]+)/, 2];
|
||||
commands['~count'].regex = [/~count ([^ ]+)/, 2];
|
||||
|
||||
return {
|
||||
'name': 'poll',
|
||||
|
26
snippets.js
26
snippets.js
@ -48,6 +48,18 @@ Array.prototype.allGroupings = function() {
|
||||
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.prototype.valMatch = function(regex, expLength) {
|
||||
@ -152,6 +164,20 @@ Object.prototype.isArray = function(obj) {
|
||||
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 ***/
|
||||
|
||||
Number.prototype.chanceIn = function(x, y) {
|
||||
|
12
strings.json
12
strings.json
@ -245,5 +245,17 @@
|
||||
"option_removed": {
|
||||
"english": "{user}: '{option}' removed from '{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