Skip to content
Permalink
Browse files

problem session works

  • Loading branch information...
Karan Goel
Karan Goel committed Jul 20, 2014
1 parent 254c990 commit d0fe92b4a6c83ef56a5e67e4955c76e469aac51d
Showing with 38 additions and 31 deletions.
  1. +3 −3 package.json
  2. +35 −28 private/routes/index.js
@@ -6,17 +6,17 @@
"start": "node index.js"
},
"dependencies": {
"async": "^0.9.0",
"connect-mongo": "^0.4.0",
"connect-redis": "1.4.7",
"express": "3.4.8",
"hbs": "^2.7.0",
"hbs": "~2.7.0",
"mongodb": ">= 1.3.19",
"mongoose": ">= 3.6.20",
"passport": "^0.2.0",
"passport-facebook": "^1.0.3",
"redis": "^0.11.0",
"request": "^2.37.0",
"socket.io": "^1.0.6",
"hbs": "~2.7.0"
"socket.io": "^1.0.6"
}
}
@@ -4,6 +4,7 @@ var ProblemSession = require('./../models/problemsession');
var Friends = require('./../helpers/friends');
var request = require('request');


exports.index = function (req, res){
if (req.isAuthenticated()) {
return res.render('index', {user: req.user});
@@ -54,47 +55,53 @@ function nextRandomProblem(callback) {
}


function processAndServePs(reqUser, friends, randProblem, callback) {
var ps = {};
var readyToReturn = false;
function commonPs(reqUser, thisFriend, randProblem, callback) {
ProblemSession.findOne({ $or:[
{problem: randProblem.id, user1: reqUser.id, user2: thisFriend.id},
{problem: randProblem.id, user1: thisFriend.id, user2: reqUser.id}
]}, function(err, ps) {
callback(ps);
});
}


function savePs(randProblem, reqUser, thisFriend, callback) {
new ProblemSession({
problem: randProblem.id,
user1: reqUser.id,
user2: thisFriend.id,
user_solution: ''
}).save(function(err, newPS) {
callback(newPS);
});
}

for (var i = 0; i < friends.length; i++) {
var thisFriend = friends[i];

console.log(thisFriend.id + " -- " + reqUser.id + ' -- ' + (thisFriend.id === reqUser.id));

function processAndServePs(reqUser, friends, randProblem, callback) {
(function checkOne() {
var thisFriend = friends.splice(0, 1)[0]; // get the first record of coll and reduce coll by one
if (thisFriend.id !== reqUser.id) {
console.log("after if: " + thisFriend.id);
ProblemSession.findOne({ $or:[
{problem: randProblem.id, user1: reqUser.id, user2: thisFriend.id},
{problem: randProblem.id, user1: thisFriend.id, user2: reqUser.id}
]}, function(err, ps) {
commonPs(reqUser, thisFriend, randProblem, function(ps) {
// ps is the problem session where both users solved this problem
if (!ps) {
readyToReturn = true;
new ProblemSession({
problem: randProblem.id,
user1: reqUser.id,
user2: thisFriend.id,
user_solution: ''
}).save(function(err, newPS) {
console.log("new ps saved = " + newPS);

ps = {'problem': randProblem,
'users': [reqUser, thisFriend],
'problemsession': newPS.id};
savePs(randProblem, reqUser, thisFriend, function(newPS) {
return_struct = {'problem': randProblem,
'users': [reqUser, thisFriend],
'problemsession': newPS.id};

if (readyToReturn || i === friends.length - 1) {
console.log("ending");
callback(ps);
if (return_struct != {}) {
callback(return_struct);
} else {
checkOne();
}
});
} else {
callback(ps);
callback({});
}
});
}
}
})();
}


0 comments on commit d0fe92b

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