From 2cd538e1eb7648dd45accff0a6b0ce159f471e71 Mon Sep 17 00:00:00 2001 From: reality Date: Thu, 17 Oct 2013 03:59:29 +0000 Subject: [PATCH] words data --- modules/sstats/api.js | 6 ++++++ modules/sstats/commands.js | 39 +++++++++++++++++++++++++++++++++++++ modules/sstats/config.json | 3 ++- modules/sstats/sstats.js | 32 +++++++++++++++++++++++++++++- modules/sstats/strings.json | 6 ++++++ 5 files changed, 84 insertions(+), 2 deletions(-) diff --git a/modules/sstats/api.js b/modules/sstats/api.js index 589ee64..b92e341 100644 --- a/modules/sstats/api.js +++ b/modules/sstats/api.js @@ -13,6 +13,9 @@ var api = function(dbot) { var uStats = { 'id': id, 'lines': 0, + 'words': 0, + 'capitals': 0, + 'curses': 0, 'channels': {}, 'creation': new Date().getTime() }; @@ -31,6 +34,9 @@ var api = function(dbot) { var cStats = { 'id': id, 'lines': 0, + 'words': 0, + 'capitals': 0, + 'curses': 0, 'creation': new Date().getTime() }; this.db.save('channel_stats', id, cStats, function(err, cStats) { diff --git a/modules/sstats/commands.js b/modules/sstats/commands.js index 2e01b84..a4ced41 100644 --- a/modules/sstats/commands.js +++ b/modules/sstats/commands.js @@ -2,6 +2,45 @@ var _ = require('underscore')._; var commands = function(dbot) { var commands = { + '~words': function(event) { + var getWords = function(user) { + this.api.getUserStats(user.id, function(uStats) { + if(uStats) { + var output = dbot.t('sstats_uwords', { + 'user': user.primaryNick, + 'words': uStats.words, + 'curses': uStats.curses, + 'capitals': uStats.capitals + }); + if(event.rChannel && _.has(uStats.channels, event.rChannel.id)) { + ucStats = uStats.channels[event.rChannel.id]; + output += dbot.t('sstats_ucwords', { + 'channel': event.channel, + 'words': ucStats.words, + 'curses': ucStats.curses, + 'capitals': ucStats.capitals + }); + } + event.reply(output); + } else { + event.reply(dbot.t('sstats_noustats')); + } + }); + }.bind(this); + + if(event.params[1]) { + dbot.api.users.resolveUser(event.server, event.params[1], function(user) { + if(user) { + getWords(user); + } else { + event.reply(dbot.t('sstats_unknown_user')); + } + }); + } else { + getWords(event.rUser); + } + }, + '~lines': function(event) { var getLines = function(user) { this.api.getUserStats(user.id, function(uStats) { diff --git a/modules/sstats/config.json b/modules/sstats/config.json index 9dfa6ad..121157d 100644 --- a/modules/sstats/config.json +++ b/modules/sstats/config.json @@ -1,4 +1,5 @@ { "dbType": "redis", - "dependencies": [ "users" ] + "dependencies": [ "users" ], + "curses": [ "shit", "piss", "fuck", "cunt", "cocksucker", "motherfucker", "tits" ] } diff --git a/modules/sstats/sstats.js b/modules/sstats/sstats.js index c8bbfdb..59e1cd2 100644 --- a/modules/sstats/sstats.js +++ b/modules/sstats/sstats.js @@ -7,15 +7,45 @@ var _ = require('underscore')._; var sstats = function(dbot) { if(!_.has(dbot.db, 'ssinception')) dbot.db.ssinception = new Date().getTime(); + this.isUpperCase = function(word) { + return _.all(word.split(''), function(c) { + return c == c.toUpperCase(); + }); + }; + this.listener = function(event) { event.cStats.lines++; event.uStats.lines++; + + var words = event.message.split(' '), + wCount = words.length, + capitals = 0, + curses = 0; + _.each(words, function(word) { + if(this.isUpperCase(word)) capitals++; + if(_.include(this.config.curses, word)) curses++; + }, this); + + event.uStats.words += wCount; + event.uStats.capitals += capitals; + event.uStats.curses += curses; + + event.cStats.words += wCount; + event.cStats.capitals += capitals; + event.cStats.curses += curses; + if(!_.has(event.uStats.channels, event.rChannel.id)) { event.uStats.channels[event.rChannel.id] = { - 'lines': 1 + 'lines': 1, + 'words': wCount, + 'capitals': capitals, + 'curses': curses }; } else { event.uStats.channels[event.rChannel.id].lines++; + event.uStats.channels[event.rChannel.id].words += wCount; + event.uStats.channels[event.rChannel.id].capitals += capitals; + event.uStats.channels[event.rChannel.id].curses += curses; } this.db.save('channel_stats', event.cStats.id, event.cStats, function() {}); this.db.save('user_stats', event.uStats.id, event.uStats, function() {}); diff --git a/modules/sstats/strings.json b/modules/sstats/strings.json index 183b725..223e03e 100644 --- a/modules/sstats/strings.json +++ b/modules/sstats/strings.json @@ -13,5 +13,11 @@ }, "sstats_clines": { "en": "{lines} lines posted in {channel}." + }, + "sstats_uwords": { + "en": "{user} has used {words} words ({curses} curses, {capitals} capitalised words)" + }, + "sstats_ucwords": { + "en": " - {words} words, {curses} curses, {capitals} capitalised words in {channel}" } }