2013-10-16 17:12:05 +02:00
|
|
|
/**
|
|
|
|
* Module Name: sstats
|
|
|
|
* Description: Simple Stats, in the absence of good ones.
|
|
|
|
*/
|
|
|
|
var _ = require('underscore')._;
|
|
|
|
|
|
|
|
var sstats = function(dbot) {
|
2013-10-17 05:06:17 +02:00
|
|
|
if(!_.has(dbot.db, 'ssinception')) dbot.db.ssinception = new Date().getTime();
|
|
|
|
|
2013-10-17 05:59:29 +02:00
|
|
|
this.isUpperCase = function(word) {
|
|
|
|
return _.all(word.split(''), function(c) {
|
|
|
|
return c == c.toUpperCase();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-10-16 17:12:05 +02:00
|
|
|
this.listener = function(event) {
|
|
|
|
event.cStats.lines++;
|
|
|
|
event.uStats.lines++;
|
2013-10-17 05:59:29 +02:00
|
|
|
|
|
|
|
var words = event.message.split(' '),
|
|
|
|
wCount = words.length,
|
|
|
|
capitals = 0,
|
|
|
|
curses = 0;
|
|
|
|
_.each(words, function(word) {
|
|
|
|
if(this.isUpperCase(word)) capitals++;
|
2013-10-17 06:48:45 +02:00
|
|
|
if(_.any(this.config.curses, function(curse) { return word.toLowerCase().indexOf(curse) != -1; })) {
|
|
|
|
curses++;
|
|
|
|
}
|
2013-10-17 05:59:29 +02:00
|
|
|
}, 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;
|
|
|
|
|
2013-10-16 17:12:05 +02:00
|
|
|
if(!_.has(event.uStats.channels, event.rChannel.id)) {
|
|
|
|
event.uStats.channels[event.rChannel.id] = {
|
2013-10-17 05:59:29 +02:00
|
|
|
'lines': 1,
|
|
|
|
'words': wCount,
|
|
|
|
'capitals': capitals,
|
|
|
|
'curses': curses
|
2013-10-16 17:12:05 +02:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
event.uStats.channels[event.rChannel.id].lines++;
|
2013-10-17 05:59:29 +02:00
|
|
|
event.uStats.channels[event.rChannel.id].words += wCount;
|
|
|
|
event.uStats.channels[event.rChannel.id].capitals += capitals;
|
|
|
|
event.uStats.channels[event.rChannel.id].curses += curses;
|
2013-10-16 17:12:05 +02:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
};
|