From e6ac8558aca844ac40b55b0a18b140ff9e3868d1 Mon Sep 17 00:00:00 2001 From: Joe MacMahon Date: Tue, 19 Jun 2012 23:39:26 +0100 Subject: [PATCH] Added running-orders, not just winners --- modules/poll.js | 38 +++++++++++++++----------------------- strings.json | 6 +++--- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/modules/poll.js b/modules/poll.js index 39d8a98..e8deed1 100644 --- a/modules/poll.js +++ b/modules/poll.js @@ -150,18 +150,18 @@ var poll = function(dbot) { } }, - '~winner': function(event) { + '~count': function(event) { var name = event.input[1]; if(polls.hasOwnProperty(name)) { - var winner; + var order; if(polls[name].av) { var finished = false; var rounds = []; var eliminated = []; var voted; - for(var roundn = 0; !finished; roundn++) { + for(var roundn = 0; roundn < polls[name].options.length; roundn++) { var roundLoser; // Populate candidates for this round @@ -182,38 +182,30 @@ var poll = function(dbot) { }); }); - // Check for 50% - var max = 0; - var min = polls[name].votes.length() + // Find the loser + var min = polls[name].votes.length() + 1; 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; - } // Eliminate loser eliminated.push(roundLoser); } + order = eliminated.reverse().join(', ') } else { - var max = 0; - polls[name].votes.each(function (name, count) { - if(count > max) { - winner = name; - max = count; - } + 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('winner', {'poll': name, 'description': polls[name].description, 'winner': winner})); + event.reply(dbot.t('count', {'poll': name, 'description': polls[name].description, 'places': order})); } else { event.reply(dbot.t('poll_unexistent', {'name': name})); } @@ -224,7 +216,7 @@ var poll = function(dbot) { commands['~rmoption'].regex = [/~rmoption ([^ ]+) ([^ ]+)/, 3]; commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3]; commands['~pdesc'].regex = [/~pdesc ([^ ]+)/, 2]; - commands['~winner'].regex = [/~winner ([^ ]+)/, 2]; + commands['~count'].regex = [/~count ([^ ]+)/, 2]; return { 'name': 'poll', diff --git a/strings.json b/strings.json index 3cee76d..15f955b 100644 --- a/strings.json +++ b/strings.json @@ -254,8 +254,8 @@ "english": "{user} changed their vote in {poll} to '{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}'." + "count": { + "english": "The running-order of poll '{poll}' ({description}) is: {places}.", + "spanish": "" } }