3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-23 20:39:25 +01:00

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
'~loudest': function(event) {
var lines = {};
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();
this.internalAPI.highscore('user_stats', 'lines', function(lCounts) {
async.eachSeries(lCounts, function(lCount, next) {
dbot.api.users.getUser(lCount[0], function(user) {
lCount[0] = user.primaryNick;
next();
lCount[0] = user.primaryNick; next();
});
}, function() {
var output = "Loudest users: ";
for(var i=0;i<lCounts.length;i++) {
output += lCounts[i][0] + " (" + lCounts[i][1] + "), ";
}
event.reply(output.slice(0, -2));
});
});
event.reply(this.internalAPI.formatHighscore('Loudest users: ', lCounts));
}.bind(this));
}.bind(this));
},
'~cloudest': function(event) {
var lines = {};
this.db.scan('user_stats', function(uStats) {
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();
var pathString = 'channels.' + event.rChannel.id + '.lines';
this.internalAPI.highscore('user_stats', pathString, function(lCounts) {
async.eachSeries(lCounts, function(lCount, next) {
dbot.api.users.getUser(lCount[0], function(user) {
lCount[0] = user.primaryNick;
next();
lCount[0] = user.primaryNick; next();
});
}, function() {
var output = "Loudest users in " + event.channel + ": ";
for(var i=0;i<lCounts.length;i++) {
output += lCounts[i][0] + " (" + lCounts[i][1] + "), ";
}
event.reply(output.slice(0, -2));
});
});
event.reply(this.internalAPI.formatHighscore('Loudest users in ' + event.channel + ': ', lCounts));
}.bind(this));;
}.bind(this));
},
'~clines': function(event) {

View File

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