Skip to content
Permalink
Browse files

add leaderboard

  • Loading branch information...
Karan Goel
Karan Goel committed Jul 20, 2014
1 parent 203eb18 commit 3535e1a916f1f2432ec12548797a9778c8992b47
Showing with 22 additions and 3 deletions.
  1. +1 −0 index.js
  2. +2 −1 private/models/user.js
  3. +19 −2 private/routes/index.js
@@ -54,6 +54,7 @@ app.get('/auth/error', routes.authError);

// user
app.get('/user', routes.getUser); // get logged in user
app.get('/leaderboard', routes.leaderboard);

// session
// start a session
@@ -30,7 +30,8 @@ var userSchema = new Schema({
score: Number,
badges: [String],
friends: [],
online: Boolean
online: Boolean,
problems_solved: Number
});


@@ -3,6 +3,7 @@ var Problem = require('./../models/problem');
var ProblemSession = require('./../models/problemsession');
var Friends = require('./../helpers/friends');
var request = require('request');
var async = require('async');


exports.index = function (req, res){
@@ -26,7 +27,13 @@ exports.authSuccess = function(req, res) {
// get details for logged in user
exports.getUser = function(req, res) {
if (req.isAuthenticated()) {
return res.send(200, req.user);
ProblemSession.find({ $or:[ {user1: req.user.id}, {user2: req.user.id} ]},
function(err, ps) {
req.user.problems_solved = ps.length;
req.user.save(function(err, u) {
return res.send(200, u);
});
});
} else {
return res.send(401, {});
}
@@ -181,6 +188,16 @@ exports.setOnline = function (userId, isOnline, callback) {
// get all users for the leaderboard
exports.leaderboard = function(req, res) {
User.find({}, function(err, users) {
res.send(200, users);
async.mapLimit(users, 20, function(user, cb) {
ProblemSession.find({ $or:[ {user1: user.id}, {user2: user.id} ]},
function(err, ps) {
user.problems_solved = ps.length;
user.save(function(err, u) {
cb(null, u);
});
});
}, function (err, results) {
res.send(200, results);
});
});
}

0 comments on commit 3535e1a

Please sign in to comment.
You can’t perform that action at this time.