make it more efficient or whatever

This commit is contained in:
reality 2013-10-17 11:42:05 +00:00
parent a828536e97
commit 74af9642e1
2 changed files with 53 additions and 42 deletions

View File

@ -78,59 +78,28 @@ var commands = function(dbot) {
// TODO: too much repeated code between loudest and cloudest yo // TODO: too much repeated code between loudest and cloudest yo
'~loudest': function(event) { '~loudest': function(event) {
var lines = {}; this.internalAPI.highscore('user_stats', 'lines', function(lCounts) {
this.db.scan('user_stats', function(uStats) {
lines[uStats.id] = uStats.lines;
}, function() {
var lCounts = _.chain(lines)
.pairs()
.sortBy(function(user) { return user[1]; })
.reverse()
.first(10)
.value();
async.eachSeries(lCounts, function(lCount, next) { async.eachSeries(lCounts, function(lCount, next) {
dbot.api.users.getUser(lCount[0], function(user) { dbot.api.users.getUser(lCount[0], function(user) {
lCount[0] = user.primaryNick; lCount[0] = user.primaryNick; next();
next();
}); });
}, function() { }, function() {
var output = "Loudest users: "; event.reply(this.internalAPI.formatHighscore('Loudest users: ', lCounts));
for(var i=0;i<lCounts.length;i++) { }.bind(this));
output += lCounts[i][0] + " (" + lCounts[i][1] + "), "; }.bind(this));
}
event.reply(output.slice(0, -2));
});
});
}, },
'~cloudest': function(event) { '~cloudest': function(event) {
var lines = {}; var pathString = 'channels.' + event.rChannel.id + '.lines';
this.db.scan('user_stats', function(uStats) { this.internalAPI.highscore('user_stats', pathString, function(lCounts) {
if(_.has(uStats.channels, event.rChannel.id)) {
lines[uStats.id] = uStats.channels[event.rChannel.id].lines;
}
}, function() {
var lCounts = _.chain(lines)
.pairs()
.sortBy(function(user) { return user[1]; })
.reverse()
.first(10)
.value();
async.eachSeries(lCounts, function(lCount, next) { async.eachSeries(lCounts, function(lCount, next) {
dbot.api.users.getUser(lCount[0], function(user) { dbot.api.users.getUser(lCount[0], function(user) {
lCount[0] = user.primaryNick; lCount[0] = user.primaryNick; next();
next();
}); });
}, function() { }, function() {
var output = "Loudest users in " + event.channel + ": "; event.reply(this.internalAPI.formatHighscore('Loudest users in ' + event.channel + ': ', lCounts));
for(var i=0;i<lCounts.length;i++) { }.bind(this));;
output += lCounts[i][0] + " (" + lCounts[i][1] + "), "; }.bind(this));
}
event.reply(output.slice(0, -2));
});
});
}, },
'~clines': function(event) { '~clines': function(event) {

View File

@ -7,12 +7,54 @@ var _ = require('underscore')._;
var sstats = function(dbot) { var sstats = function(dbot) {
if(!_.has(dbot.db, 'ssinception')) dbot.db.ssinception = new Date().getTime(); if(!_.has(dbot.db, 'ssinception')) dbot.db.ssinception = new Date().getTime();
// This needs to be somewhere else
this.isUpperCase = function(word) { this.isUpperCase = function(word) {
return _.all(word.split(''), function(c) { return _.all(word.split(''), function(c) {
return c == c.toUpperCase(); return c == c.toUpperCase();
}); });
}; };
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.split('.');
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();
callback(pCounts);
});
}.bind(this),
'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) { this.listener = function(event) {
event.cStats.lines++; event.cStats.lines++;
event.uStats.lines++; event.uStats.lines++;