3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-24 21:09:31 +01:00
dbot/modules/sstats/sstats.js

220 lines
8.0 KiB
JavaScript
Raw Normal View History

/**
* Module Name: sstats
* Description: Simple Stats, in the absence of good ones.
*/
2013-10-17 14:46:55 +02:00
var _ = require('underscore')._,
async = require('async');
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 13:42:05 +02:00
// This needs to be somewhere else
2013-10-17 05:59:29 +02:00
this.isUpperCase = function(word) {
return _.all(word.split(''), function(c) {
return c == c.toUpperCase();
});
};
2013-10-17 13:42:05 +02:00
this.internalAPI = {
// I'm not a huge fan of this but it's better than all the code
// repetition
'highscore': function(key, property, callback) {
var pList = {},
pPointer = property;
if(!_.isArray(pPointer)) {
2013-10-17 13:42:05 +02:00
pPointer = property.split('.');
}
2013-10-17 13:42:05 +02:00
this.db.scan(key, function(item) {
var id = item.id;
for(var i=0;i<pPointer.length;i++) {
if(_.has(item, pPointer[i])) {
item = item[pPointer[i]];
} else {
item = null; break;
}
}
if(item) {
pList[id] = item;
}
}, function() {
var pCounts = _.chain(pList)
.pairs()
.sortBy(function(p) { return p[1]; })
.reverse()
.first(10)
.value();
2013-10-17 14:46:55 +02:00
async.eachSeries(pCounts, function(pCount, next) {
dbot.api.users.getUser(pCount[0], function(err, user) {
2014-11-09 14:59:00 +01:00
if(user) {
pCount[0] = user.primaryNick;
}
next();
2013-10-17 14:46:55 +02:00
});
}, function() {
callback(pCounts);
}.bind(this));
2013-10-17 13:42:05 +02:00
});
}.bind(this),
2013-10-17 14:46:55 +02:00
'channelHighscore': function(key, server, channel, property, callback) {
var cId = channel + '.' + server;
var newProperty = ['channels', cId, property];
this.internalAPI.highscore(key, newProperty, callback);
2013-10-17 14:46:55 +02:00
}.bind(this),
2013-10-17 13:42:05 +02:00
'formatHighscore': function(string, pCounts) {
var output = string;
for(var i=0;i<pCounts.length;i++) {
output += pCounts[i][0] + " (" + pCounts[i][1] + "), ";
}
return output.slice(0, -2);
}
};
this.listener = function(event) {
2015-10-13 07:36:49 +02:00
if(event.channel == event.user) return; // ignore PMs
event.cStats.lines++;
event.uStats.lines++;
2013-10-17 05:59:29 +02:00
2013-10-20 20:53:08 +02:00
var message = event.message.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g, "");
var cId = event.channel + '.' + event.server;
2013-10-20 20:53:08 +02:00
var words = message.split(' '),
2013-10-17 05:59:29 +02:00
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;
2013-10-17 16:31:05 +02:00
event.uStats.last = new Date().getTime();
2013-10-17 05:59:29 +02:00
event.cStats.words += wCount;
event.cStats.capitals += capitals;
event.cStats.curses += curses;
if(!_.has(event.uStats.channels, cId)) {
event.uStats.channels[cId] = {
2013-10-17 05:59:29 +02:00
'lines': 1,
'words': wCount,
'capitals': capitals,
'curses': curses
};
} else {
event.uStats.channels[cId].lines++;
event.uStats.channels[cId].words += wCount;
event.uStats.channels[cId].capitals += capitals;
event.uStats.channels[cId].curses += curses;
}
2013-10-20 20:29:54 +02:00
// Look for tracked words.
2013-10-20 20:53:27 +02:00
if(event.message.charAt(0) != '~') {
2013-10-20 20:53:08 +02:00
var wMap = {}; // Why reduce isn't working idk
_.each(words, function(word) {
word = word.toLowerCase();
if(!_.has(wMap, word)) wMap[word] = 0;
wMap[word]++;
});
_.each(wMap, function(count, word) {
this.api.getTrackedWord(word, function(tWord) {
if(tWord) {
tWord.total += count;
if(!_.has(tWord.channels, cId)) tWord.channels[cId] = 0;
2013-10-20 20:53:08 +02:00
if(!_.has(tWord.users, event.rUser.id)) tWord.users[event.rUser.id] = 0;
tWord.channels[cId] += count;
2013-10-20 20:53:08 +02:00
tWord.users[event.rUser.id] += count;
this.db.save('tracked_words', word, tWord, function() {});
}
}.bind(this));
}, this);
}
2013-10-20 20:29:54 +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.channel) return callback();
var id = event.channel + '.' + event.server;
this.api.getChannelStats(id, function(cStats) {
if(cStats) {
event.cStats = cStats;
callback();
} else {
this.api.createChannelStats(id, function(cStats) {
event.cStats = cStats;
callback();
});
}
}.bind(this));
}.bind(this));
2013-10-22 16:25:53 +02:00
dbot.api.event.addHook('~mergeusers', function(server, oldUser, newUser) {
this.api.getUserStats(oldUser.id, function(ouStats) {
this.api.getUserStats(newUser.id, function(nuStats) {
_.each(ouStats, function(stat, key) {
2013-10-23 13:46:07 +02:00
if(key != 'creation' && key != 'id') {
if(_.isObject(stat)) {
_.each(ouStats[key], function(stat, sKey) {
_.each(ouStats[key][sKey], function(stat, ssKey) {
2014-07-08 13:33:56 +02:00
if(_.has(nuStats[key][sKey], ssKey)) {
nuStats[key][sKey][ssKey] += stat;
2014-07-08 13:33:56 +02:00
}
});
2013-10-23 13:46:07 +02:00
});
} else {
nuStats[key] += stat;
}
2013-10-22 16:25:53 +02:00
}
});
this.db.del('user_stats', oldUser.id, function() {});
2013-10-22 16:45:21 +02:00
this.db.save('user_stats', newUser.id, nuStats, function() {});
2013-10-22 16:35:37 +02:00
}.bind(this));
}.bind(this));
2013-10-22 16:25:53 +02:00
this.db.scan('tracked_words', function(tWord) {
if(_.has(tWord.users, oldUser.id)) {
if(_.has(tWord.users, newUser.id)) {
tWord.users[newUser.id] += tWord.users[oldUser.id];
} else {
tWord.users[newUser.id] = tWord.users[oldUser.id];
}
delete tWord.users[oldUser.id];
this.db.save('tracked_words', tWord.word, tWord, function() {});
}
2013-10-22 16:43:18 +02:00
}.bind(this), function() {});
2013-10-22 16:25:53 +02:00
}.bind(this));
}.bind(this);
};
exports.fetch = function(dbot) {
return new sstats(dbot);
};