From f70c92c16d70255059c5933709916e3fc65cfe14 Mon Sep 17 00:00:00 2001 From: Daniel Evans Date: Fri, 15 Jun 2012 16:15:02 +0100 Subject: [PATCH 1/3] Web display for poll module --- modules/poll.js | 3 +++ modules/web.js | 23 +++++++++++++++++++++++ public/styles.css | 40 ++++++++++++++++++++++++++++++++++++++++ run.js | 2 +- views/polllist.jade | 7 +++++++ views/polls.jade | 35 +++++++++++++++++++++++++++++++++++ 6 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 views/polllist.jade create mode 100644 views/polls.jade diff --git a/modules/poll.js b/modules/poll.js index 7fa3c6c..73a9c6e 100644 --- a/modules/poll.js +++ b/modules/poll.js @@ -1,4 +1,7 @@ var poll = function(dbot) { + if(!dbot.db.hasOwnProperty('polls')) { + dbot.db.polls = {}; + } var polls = dbot.db.polls; var commands = { '~newpoll': function(event) { diff --git a/modules/web.js b/modules/web.js index 911e88f..9c88f69 100644 --- a/modules/web.js +++ b/modules/web.js @@ -33,6 +33,29 @@ var webInterface = function(dbot) { var rCategory = Object.keys(dbot.db.quoteArrs).random(); res.render('quotes', { 'name': dbot.name, 'quotes': dbot.db.quoteArrs[rCategory], locals: { 'url_regex': RegExp.prototype.url_regex() } }); }); + + // Lists all of the polls + app.get('/polls', function(req, res) { + res.render('polllist', { 'name': dbot.name, 'polllist': Object.keys(dbot.db.polls) }); + }); + + // Shows the results of a poll + app.get('/polls/:key', function(req, res) { + var key = req.params.key.toLowerCase(); + if(dbot.db.polls.hasOwnProperty(key)) { + // tally the votes + var totalVotes = 0; + for( var v in dbot.db.polls[key].votes ) { + var N = Number(dbot.db.polls[key].votes[v]); + if( !isNaN(N) ) { + totalVotes += N; + } + } + res.render('polls', { 'name': dbot.name, 'description': dbot.db.polls[key].description, 'votees': dbot.db.polls[key].votees, 'options': dbot.db.polls[key].votes, locals: { 'totalVotes': totalVotes, 'url_regex': RegExp.prototype.url_regex() } }); + } else { + res.render('error', { 'name': dbot.name, 'message': 'No polls under that key.' }); + } + }); app.listen(dbot.webPort); diff --git a/public/styles.css b/public/styles.css index 600520d..6d08800 100644 --- a/public/styles.css +++ b/public/styles.css @@ -21,6 +21,15 @@ body { text-shadow: 1px 1px 2px #2B2B2B; } +h1,h2 { + margin: 0; + padding: 0; +} + +p { + margin: 15px 5px; +} + div#page { width: 90%; margin: 0 auto 0 auto; @@ -105,3 +114,34 @@ li.quote { img { max-width: 100%; } + +/* Polls */ +#votelist { + margin: 10px 0; + padding: 0 40px; +} + +li.option { + list-style: none; + text-align: left; + font-size: 1.2em; +} + +li.option-votes { + list-style: none; + text-align: left; + margin-bottom: 10px; +} + +.vote-percentage { + height: 20px; + background: #444; +} +.vote-track { + height: 20px; + width: 100%; + background: #EEE; + margin: 2px 0; + + box-shadow: inset 0px 0px 3px #444; +} diff --git a/run.js b/run.js index e6505a5..ca207e6 100644 --- a/run.js +++ b/run.js @@ -53,7 +53,7 @@ var DBot = function(timers) { // Populate bot properties with config data this.name = this.config.name || 'dbox'; this.admin = this.config.admin || [ 'reality' ]; - this.moduleNames = this.config.modules || [ 'ignore', 'admin', 'command', 'dice', 'js', 'kick', 'puns', 'quotes', 'spelling', 'youare' ]; + this.moduleNames = this.config.modules || [ 'ignore', 'admin', 'command', 'dice', 'js', 'kick', 'puns', 'quotes', 'spelling', 'youare', 'web', 'poll' ]; this.language = this.config.language || 'english'; this.webPort = this.config.webPort || 80; diff --git a/views/polllist.jade b/views/polllist.jade new file mode 100644 index 0000000..5f0e73b --- /dev/null +++ b/views/polllist.jade @@ -0,0 +1,7 @@ +div#controls + input(type="text", name="search", id="search-text", oninput="search(this.value)") +ul#quotelist + -each poll in polllist + a(href='/polls/'+poll) + li.quotes #{poll} + diff --git a/views/polls.jade b/views/polls.jade new file mode 100644 index 0000000..aed4c0f --- /dev/null +++ b/views/polls.jade @@ -0,0 +1,35 @@ +h2 #{description} +p Voters (#{locals.totalVotes}): + -each voter in votees + | #{voter} +ul#votelist + -var hasYouTubeVids=false + -each votes,option in options + -var percentage = votes/locals.totalVotes*100 + -if(options.hasOwnProperty(option)) + -if(option.match(locals.url_regex)) + li.option + -if(option.match(/(jpg|png|gif|jpeg|tiff)$/)) + a(href=option) + img(src=option) + -else if(option.match(/youtube.com\/watch/)) + -hasYouTubeVids = true + span(class='ytplaceholder') + =option + -else + a(href=option) + =option + -else + li.option #{option} + li.option-votes + .vote-track + -if(!isNaN(percentage)) + .vote-percentage(style="width: #{percentage}%") + -if(votes == 1) + |#{votes} vote + -else + |#{votes} votes + -if(!isNaN(percentage)) + |(#{percentage.toFixed(2)}%) + -if(hasYouTubeVids) + script(src='/ytembed.js') From a409af06fd13791275bd165d5279e9543eb24819 Mon Sep 17 00:00:00 2001 From: Daniel Evans Date: Fri, 15 Jun 2012 17:17:58 +0200 Subject: [PATCH 2/3] Undoing my commit. --- run.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run.js b/run.js index ca207e6..e6505a5 100644 --- a/run.js +++ b/run.js @@ -53,7 +53,7 @@ var DBot = function(timers) { // Populate bot properties with config data this.name = this.config.name || 'dbox'; this.admin = this.config.admin || [ 'reality' ]; - this.moduleNames = this.config.modules || [ 'ignore', 'admin', 'command', 'dice', 'js', 'kick', 'puns', 'quotes', 'spelling', 'youare', 'web', 'poll' ]; + this.moduleNames = this.config.modules || [ 'ignore', 'admin', 'command', 'dice', 'js', 'kick', 'puns', 'quotes', 'spelling', 'youare' ]; this.language = this.config.language || 'english'; this.webPort = this.config.webPort || 80; From 0eb051eef9e6c8b5f19e8584a1a7c90770d4c6e6 Mon Sep 17 00:00:00 2001 From: Daniel Evans Date: Fri, 15 Jun 2012 17:18:36 +0200 Subject: [PATCH 3/3] Fixed formatting. --- modules/poll.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/poll.js b/modules/poll.js index 73a9c6e..8b883c1 100644 --- a/modules/poll.js +++ b/modules/poll.js @@ -1,7 +1,7 @@ var poll = function(dbot) { - if(!dbot.db.hasOwnProperty('polls')) { - dbot.db.polls = {}; - } + if(!dbot.db.hasOwnProperty('polls')) { + dbot.db.polls = {}; + } var polls = dbot.db.polls; var commands = { '~newpoll': function(event) {