Vulcan/server/scoring.js

33 lines
1.2 KiB
JavaScript
Raw Normal View History

2012-09-27 13:13:55 -07:00
// "secret" server code to recalculate scores
2012-09-11 15:32:25 +10:00
2012-09-27 13:13:55 -07:00
var updateScore = function (collection, id) {
var object = collection.findOne(id);
2012-09-12 08:53:13 +09:00
2012-09-27 13:13:55 -07:00
// use baseScore if defined, if not just use the number of votes
// note: for transition period, also use votes if there are more votes than baseScore
var baseScore = Math.max(object.votes || 0, object.baseScore || 0);
2012-09-17 12:51:35 +09:00
2012-09-27 13:13:55 -07:00
// now multiply by 'age' exponentiated
// FIXME: timezones <-- set by server or is getTime() ok?
var ageInHours = (new Date().getTime() - object.submitted) / (60 * 60 * 1000);
2012-09-27 13:13:55 -07:00
// HN algorithm (same as Bindle)
var newScore = baseScore / Math.pow(ageInHours + 2, 1.3);
2012-10-02 10:54:04 +09:00
if(object.sticky)
newScore=999;
2012-09-27 13:13:55 -07:00
collection.update(id, {$set: {score: newScore}});
};
Meteor.startup(function () {
2012-10-22 14:00:47 +09:00
var scoreInterval = getSetting("scoreInterval") || 30;
// recalculate scores every N seconds
if(scoreInterval>0){
intervalId=Meteor.setInterval(function () {
2012-10-23 12:24:38 +09:00
// console.log('tick ('+scoreInterval+')');
2012-10-22 14:00:47 +09:00
Posts.find().forEach(function (post) { updateScore(Posts, post._id); });
Comments.find().forEach(function (comment) { updateScore(Comments, comment._id); });
}, scoreInterval * 1000);
}
2012-09-27 13:13:55 -07:00
});