diff --git a/modules/sstats/api.js b/modules/sstats/api.js new file mode 100644 index 0000000..2ca5a30 --- /dev/null +++ b/modules/sstats/api.js @@ -0,0 +1,45 @@ +var _ = require('underscore')._, + databank = require('databank'); + +var api = function(dbot) { + var api = { + 'getUserStats': function(id, callback) { + this.db.read('user_stats', id, function(err, uStats) { + callback(uStats); + }); + }, + + 'createUserStats': function(id, callback) { + var uStats = { + 'id': id, + 'lines': 0, + 'channels': {} + }; + this.db.save('user_stats', id, uStats, function(err, uStats) { + callback(uStats); + }); + }, + + 'getChannelStats': function(id, callback) { + this.db.read('channel_stats', id, function(err, cStats) { + callback(cStats); + }); + }, + + 'createChannelStats': function(id, callback) { + var cStats = { + 'id': id, + 'lines': 0 + }; + this.db.save('channel_stats', id, cStats, function(err, cStats) { + callback(cStats); + }); + } + }; + + return api; +}; + +exports.fetch = function(dbot) { + return api(dbot); +}; diff --git a/modules/sstats/commands.js b/modules/sstats/commands.js new file mode 100644 index 0000000..9e1a58c --- /dev/null +++ b/modules/sstats/commands.js @@ -0,0 +1,50 @@ +var _ = require('underscore')._; + +var commands = function(dbot) { + var commands = { + '~lines': function(event) { + if(event.params[1]) { + dbot.api.users.resolveUser(event.server, event.user, function(user) { + if(user) { // I disgust me + event.rUser = user; + delete event['params']; + commands['~lines'](event); + } else { + event.reply(dbot.t('sstats_unknown_user')); + } + }); + } else { + this.api.getUserStats(event.rUser.id, function(uStats) { + if(uStats) { + var output = dbot.t('sstats_tlines', { + 'user': event.rUser.primaryNick, + 'lines': uStats.lines + }); + if(event.rChannel && _.has(uStats.channels, event.rChannel.id)) { + output += dbot.t('sstats_uclines', { + 'channel': event.channel, + 'lines': uStats.channels[event.rChannel.id].lines + }); + } + event.reply(output); + } else { + event.reply(dbot.t('sstats_noustats')); + } + }); + } + }, + + '~clines': function(event) { + if(!event.cStats) return; + event.reply(dbot.t('sstats_clines', { + 'channel': event.channel, + 'lines': event.cStats.lines + })); + } + }; + return commands; +}; + +exports.fetch = function(dbot) { + return commands(dbot); +}; diff --git a/modules/sstats/config.json b/modules/sstats/config.json new file mode 100644 index 0000000..9dfa6ad --- /dev/null +++ b/modules/sstats/config.json @@ -0,0 +1,4 @@ +{ + "dbType": "redis", + "dependencies": [ "users" ] +} diff --git a/modules/sstats/sstats.js b/modules/sstats/sstats.js new file mode 100644 index 0000000..c093abd --- /dev/null +++ b/modules/sstats/sstats.js @@ -0,0 +1,60 @@ +/** + * Module Name: sstats + * Description: Simple Stats, in the absence of good ones. + */ +var _ = require('underscore')._; + +var sstats = function(dbot) { + this.listener = function(event) { + event.cStats.lines++; + event.uStats.lines++; + if(!_.has(event.uStats.channels, event.rChannel.id)) { + event.uStats.channels[event.rChannel.id] = { + 'lines': 1 + }; + } else { + event.uStats.channels[event.rChannel.id].lines++; + } + this.db.save('channel_stats', event.cStats.id, event.cStats, function() {}); + this.db.save('user_stats', event.uStats.id, event.uStats, function() {}); + }.bind(this); + this.on = 'PRIVMSG'; + + this.onLoad = function() { + // Preload user stats + dbot.instance.addPreEmitHook(function(event, callback) { + if(!event.rUser) return callback(); + this.api.getUserStats(event.rUser.id, function(uStats) { + if(uStats) { + event.uStats = uStats; + callback(); + } else { + this.api.createUserStats(event.rUser.id, function(uStats) { + event.uStats = uStats; + callback(); + }); + } + }.bind(this)); + }.bind(this)); + + // Preload channel stats + dbot.instance.addPreEmitHook(function(event, callback) { + if(!event.rChannel) return callback(); + this.api.getChannelStats(event.rChannel.id, function(cStats) { + if(cStats) { + event.cStats = cStats; + callback(); + } else { + this.api.createChannelStats(event.rChannel.id, function(cStats) { + event.cStats = cStats; + callback(); + }); + } + }.bind(this)); + }.bind(this)); + }.bind(this); +}; + +exports.fetch = function(dbot) { + return new sstats(dbot); +}; diff --git a/modules/sstats/strings.json b/modules/sstats/strings.json new file mode 100644 index 0000000..183b725 --- /dev/null +++ b/modules/sstats/strings.json @@ -0,0 +1,17 @@ +{ + "sstats_unknown_user": { + "en": "Unknown user." + }, + "sstats_tlines": { + "en": "{user} has posted {lines} lines" + }, + "sstats_uclines": { + "en": " ({lines} in {channel})" + }, + "sstats_noustats": { + "en": "No stats recorded for this user." + }, + "sstats_clines": { + "en": "{lines} lines posted in {channel}." + } +}