diff --git a/modules/sstats/commands.js b/modules/sstats/commands.js index 9438d76..40c9d60 100644 --- a/modules/sstats/commands.js +++ b/modules/sstats/commands.js @@ -77,64 +77,75 @@ var commands = function(dbot) { }, '~loudest': function(event) { - this.internalAPI.highscore('user_stats', 'lines', function(lCounts) { - async.eachSeries(lCounts, function(lCount, next) { - dbot.api.users.getUser(lCount[0], function(user) { - lCount[0] = user.primaryNick; next(); - }); - }, function() { + var channel = event.params[1]; + + if(_.isUndefined(channel)) { + this.internalAPI.highscore('user_stats', 'lines', function(lCounts) { event.reply(this.internalAPI.formatHighscore('Loudest users: ', lCounts)); }.bind(this)); - }.bind(this)); + } else { + this.internalAPI.channelHighscore('user_stats', event.server, channel, 'lines', function(lCounts) { + if(lCounts) { + event.reply(this.internalAPI.formatHighscore('Loudest users in ' + channel + ': ', lCounts)); + } else { + event.reply('Unknown channel.'); + } + }.bind(this)); + } }, '~uncouth': function(event) { - this.internalAPI.highscore('user_stats', 'curses', function(lCounts) { - async.eachSeries(lCounts, function(lCount, next) { - dbot.api.users.getUser(lCount[0], function(user) { - lCount[0] = user.primaryNick; next(); - }); - }, function() { + var channel = event.params[1]; + + if(_.isUndefined(channel)) { + this.internalAPI.highscore('user_stats', 'curses', function(lCounts) { event.reply(this.internalAPI.formatHighscore('Most uncouth users: ', lCounts)); }.bind(this)); - }.bind(this)); + } else { + this.internalAPI.channelHighscore('user_stats', event.server, channel, 'curses', function(lCounts) { + if(lCounts) { + event.reply(this.internalAPI.formatHighscore('Most uncouth users in ' + channel + ': ', lCounts)); + } else { + event.reply('Unknown channel.'); + } + }.bind(this)); + } }, '~shoutiest': function(event) { - this.internalAPI.highscore('user_stats', 'capitals', function(lCounts) { - async.eachSeries(lCounts, function(lCount, next) { - dbot.api.users.getUser(lCount[0], function(user) { - lCount[0] = user.primaryNick; next(); - }); - }, function() { + var channel = event.params[1]; + + if(_.isUndefined(channel)) { + this.internalAPI.highscore('user_stats', 'capitals', function(lCounts) { event.reply(this.internalAPI.formatHighscore('Shoutiest users: ', lCounts)); }.bind(this)); - }.bind(this)); + } else { + this.internalAPI.channelHighscore('user_stats', event.server, channel, 'capitals', function(lCounts) { + if(lCounts) { + event.reply(this.internalAPI.formatHighscore('Shoutiest users in ' + channel + ': ', lCounts)); + } else { + event.reply('Unknown channel.'); + } + }.bind(this)); + } }, '~wordiest': function(event) { - this.internalAPI.highscore('user_stats', 'words', function(lCounts) { - async.eachSeries(lCounts, function(lCount, next) { - dbot.api.users.getUser(lCount[0], function(user) { - lCount[0] = user.primaryNick; next(); - }); - }, function() { + var channel = event.params[1]; + + if(_.isUndefined(channel)) { + this.internalAPI.highscore('user_stats', 'words', function(lCounts) { event.reply(this.internalAPI.formatHighscore('Wordiest users: ', lCounts)); }.bind(this)); - }.bind(this)); - }, - - '~cloudest': function(event) { - var pathString = 'channels.' + event.rChannel.id + '.lines'; - this.internalAPI.highscore('user_stats', pathString, function(lCounts) { - async.eachSeries(lCounts, function(lCount, next) { - dbot.api.users.getUser(lCount[0], function(user) { - lCount[0] = user.primaryNick; next(); - }); - }, function() { - event.reply(this.internalAPI.formatHighscore('Loudest users in ' + event.channel + ': ', lCounts)); - }.bind(this));; - }.bind(this)); + } else { + this.internalAPI.channelHighscore('user_stats', event.server, channel, 'words', function(lCounts) { + if(lCounts) { + event.reply(this.internalAPI.formatHighscore('Wordiest users in ' + channel + ': ', lCounts)); + } else { + event.reply('Unknown channel.'); + } + }.bind(this)); + } }, '~clines': function(event) { diff --git a/modules/sstats/sstats.js b/modules/sstats/sstats.js index 72e03e1..1421801 100644 --- a/modules/sstats/sstats.js +++ b/modules/sstats/sstats.js @@ -2,7 +2,8 @@ * Module Name: sstats * Description: Simple Stats, in the absence of good ones. */ -var _ = require('underscore')._; +var _ = require('underscore')._, + async = require('async'); var sstats = function(dbot) { if(!_.has(dbot.db, 'ssinception')) dbot.db.ssinception = new Date().getTime(); @@ -42,10 +43,27 @@ var sstats = function(dbot) { .first(10) .value(); - callback(pCounts); + async.eachSeries(pCounts, function(pCount, next) { + dbot.api.users.getUser(pCount[0], function(user) { + pCount[0] = user.primaryNick; next(); + }); + }, function() { + callback(pCounts); + }.bind(this)); }); }.bind(this), + 'channelHighscore': function(key, server, channel, property, callback) { + dbot.api.users.resolveChannel(server, channel, function(channel) { + if(channel) { + var newProperty = 'channels.' + channel.id + '.' + property; + this.internalAPI.highscore(key, newProperty, callback); + } else { + callback(null); + } + }.bind(this)); + }.bind(this), + 'formatHighscore': function(string, pCounts) { var output = string; for(var i=0;i