var poll = function(dbot) { if(!dbot.db.hasOwnProperty('polls')) { dbot.db.polls = {}; } var polls = dbot.db.polls; var commands = { '~newpoll': function(event) { 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')); } else { if(polls.hasOwnProperty(name)) { event.reply(dbot.t('poll_exists', {'name': name})); } else { if(av) { polls[name] = { 'av': av, 'name': name, 'description': description, 'owner': event.user, 'votes': {}, 'options': [] }; for(var i=0;i max) { winner = option; max = count; } if(count < min) { roundLoser = option; min = count; } }); if((2*max) > polls[name].votes.length()) { finished = true; break; } // Eliminate loser 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 { event.reply(dbot.t('poll_unexistent', {'name': name})); } } }; 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['~winner'].regex = [/~winner ([^ ]+)/, 2]; return { 'name': 'poll', 'ignorable': true, 'commands': commands }; }; exports.fetch = function(dbot) { return poll(dbot); }