3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-03 17:39:28 +01:00

Downed tools for today

This commit is contained in:
Joe MacMahon 2012-06-18 21:16:22 +01:00
parent 52d8136e9d
commit c1c951f5c7
3 changed files with 51 additions and 6 deletions

View File

@ -24,7 +24,11 @@ var poll = function(dbot) {
'description': description, 'description': description,
'owner': event.user, 'owner': event.user,
'votes': {}, 'votes': {},
'options': []
}; };
for(var i=0;i<options.length;i++) {
polls[name].options.push(options[i]);
}
} else { } else {
polls[name] = { polls[name] = {
'av': av, 'av': av,
@ -34,10 +38,9 @@ var poll = function(dbot) {
'votes': {}, 'votes': {},
'votees': {} 'votees': {}
}; };
} for(var i=0;i<options.length;i++) {
polls[name]['votes'][options[i]] = 0;
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}) +
@ -100,11 +103,14 @@ var poll = function(dbot) {
var valid = true; var valid = true;
prefs.each(function(pref) { prefs.each(function(pref) {
valid = valid && polls[name].votes.hasOwnProperty(pref); console.log(pref);
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}));
} 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': vote, 'poll': name, 'user': event.user}));
@ -143,10 +149,38 @@ 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) { '~count': function(event) {
var name = event.input[1];
if(polls.hasOwnProperty(name)) {
if(polls[name].av) {
var finished = false;
var rounds = [];
var eliminated = [];
for(var roundn = 0; !finished; roundn++) {
// Populate candidates for this round
rounds[roundn] = {};
polls[name].options.each(function (option) {
if(!eliminated.hasOwnProperty(option))
rounds[roundn][option] = 0;
});
// Count votes
polls[name].votes.each(function (name, vote) {
console.log(name);
});
break;
}
} else {
event.reply("Not yet implemented.");
}
} else {
event.reply(dbot.t('poll_unexistent', {'name': name}));
}
} }
}; };
commands['~newpoll'].regex = [/~newpoll (av )?([^ ]+) \[options=([^ ]+)\] (.+)/, 5]; commands['~newpoll'].regex = [/~newpoll (av )?([^ ]+) \[options=([^ ]+)\] (.+)/, 5];
@ -154,6 +188,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];
return { return {
'name': 'poll', 'name': 'poll',

View File

@ -152,6 +152,12 @@ 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]);
};
/*** Integer ***/ /*** Integer ***/
Number.prototype.chanceIn = function(x, y) { Number.prototype.chanceIn = function(x, y) {

View File

@ -249,5 +249,9 @@
"av_voted": { "av_voted": {
"english": "{user} voted '{vote}' in {poll}.", "english": "{user} voted '{vote}' in {poll}.",
"spanish": "{user} votó '{vote}' en {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}'."
} }
} }