basic sstats module with ~lines and ~clines because i love sundown

This commit is contained in:
reality 2013-10-16 15:12:05 +00:00
parent 96db56a25c
commit d7d8c34167
5 changed files with 176 additions and 0 deletions

45
modules/sstats/api.js Normal file
View File

@ -0,0 +1,45 @@
var _ = require('underscore')._,
databank = require('databank');
var api = function(dbot) {
var api = {
'getUserStats': function(id, callback) {
this.db.read('user_stats', id, function(err, uStats) {
callback(uStats);
});
},
'createUserStats': function(id, callback) {
var uStats = {
'id': id,
'lines': 0,
'channels': {}
};
this.db.save('user_stats', id, uStats, function(err, uStats) {
callback(uStats);
});
},
'getChannelStats': function(id, callback) {
this.db.read('channel_stats', id, function(err, cStats) {
callback(cStats);
});
},
'createChannelStats': function(id, callback) {
var cStats = {
'id': id,
'lines': 0
};
this.db.save('channel_stats', id, cStats, function(err, cStats) {
callback(cStats);
});
}
};
return api;
};
exports.fetch = function(dbot) {
return api(dbot);
};

View File

@ -0,0 +1,50 @@
var _ = require('underscore')._;
var commands = function(dbot) {
var commands = {
'~lines': function(event) {
if(event.params[1]) {
dbot.api.users.resolveUser(event.server, event.user, function(user) {
if(user) { // I disgust me
event.rUser = user;
delete event['params'];
commands['~lines'](event);
} else {
event.reply(dbot.t('sstats_unknown_user'));
}
});
} else {
this.api.getUserStats(event.rUser.id, function(uStats) {
if(uStats) {
var output = dbot.t('sstats_tlines', {
'user': event.rUser.primaryNick,
'lines': uStats.lines
});
if(event.rChannel && _.has(uStats.channels, event.rChannel.id)) {
output += dbot.t('sstats_uclines', {
'channel': event.channel,
'lines': uStats.channels[event.rChannel.id].lines
});
}
event.reply(output);
} else {
event.reply(dbot.t('sstats_noustats'));
}
});
}
},
'~clines': function(event) {
if(!event.cStats) return;
event.reply(dbot.t('sstats_clines', {
'channel': event.channel,
'lines': event.cStats.lines
}));
}
};
return commands;
};
exports.fetch = function(dbot) {
return commands(dbot);
};

View File

@ -0,0 +1,4 @@
{
"dbType": "redis",
"dependencies": [ "users" ]
}

60
modules/sstats/sstats.js Normal file
View File

@ -0,0 +1,60 @@
/**
* Module Name: sstats
* Description: Simple Stats, in the absence of good ones.
*/
var _ = require('underscore')._;
var sstats = function(dbot) {
this.listener = function(event) {
event.cStats.lines++;
event.uStats.lines++;
if(!_.has(event.uStats.channels, event.rChannel.id)) {
event.uStats.channels[event.rChannel.id] = {
'lines': 1
};
} else {
event.uStats.channels[event.rChannel.id].lines++;
}
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);
};

View File

@ -0,0 +1,17 @@
{
"sstats_unknown_user": {
"en": "Unknown user."
},
"sstats_tlines": {
"en": "{user} has posted {lines} lines"
},
"sstats_uclines": {
"en": " ({lines} in {channel})"
},
"sstats_noustats": {
"en": "No stats recorded for this user."
},
"sstats_clines": {
"en": "{lines} lines posted in {channel}."
}
}