Skip to content

Instantly share code, notes, and snippets.

@ecto
Created January 24, 2011 22:29
Show Gist options
  • Save ecto/794110 to your computer and use it in GitHub Desktop.
Save ecto/794110 to your computer and use it in GitHub Desktop.
app.get('/battle/:winner/:loser', function(req, res){
if (req.params.winner && req.params.loser) {
var winner = req.params.winner;
var loser = req.params.loser;
db.mget('image:' + winner + ':rating',
'image:' + loser + ':rating',
function (err, ratings) {
if (err) { throw err; }
ratings[0] = ratings[0] ? parseFloat(ratings[0]) : 1000;
ratings[1] = ratings[1] ? parseFloat(ratings[1]) : 1000;
var winnerExpected = 1 / (1 + (Math.pow(10,(ratings[1] - ratings[0]) / 400)));
var loserExpected = 1 / (1 + (Math.pow(10,(ratings[0] - ratings[1]) / 400)));
var k = 30;
var winnerAdjustment = Math.round(ratings[0] + (k * (1 - winnerExpected)));
var loserAdjustment = Math.round(ratings[1] + (k * (0 - loserExpected)));
db.mset('image:' + winner + ':rating', winnerAdjustment,
'image:' + loser + ':rating', loserAdjustment,
function (err, newRatings) {
if (err) { throw err; }
res.redirect('/');
});
});
} else { res.redirect('/'); }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment