Correctly sorting unsolved bonfires

This commit is contained in:
Nathan Leniz
2015-01-27 02:51:59 -05:00
parent 227c938445
commit be0cf978f5
4 changed files with 179 additions and 177 deletions

23
app.js
View File

@ -326,6 +326,17 @@ app.post('/completed-bonfire/', function (req, res) {
if (err) {
return err;
} else {
var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
req.user.uncompletedBonfires.splice(index,1)
}
index = pairedWith.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
pairedWith.uncompletedBonfires.splice(index,1)
}
pairedWith = pairedWith.pop();
pairedWith.completedBonfires.push({
_id: bonfireHash,
@ -341,17 +352,8 @@ app.post('/completed-bonfire/', function (req, res) {
solution: isSolution
})
var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
req.user.uncompletedBonfires.splice(index,1)
}
index = pairedWith.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
req.user.uncompletedBonfires.splice(index,1)
}
debug('saving user with a pair');
req.user.save();
pairedWith.save();
@ -370,6 +372,7 @@ app.post('/completed-bonfire/', function (req, res) {
if (index > -1) {
req.user.uncompletedBonfires.splice(index,1)
}
debug("Saving user without a pair");
req.user.save();
}
});

View File

@ -30,25 +30,19 @@ exports.index = function(req, res) {
bonfireHash: 'test'
});
Bonfire.find({}, null, { sort: { difficulty: 1 } }, function(err, c) {
if (err) {
debug('bonfire err: ', err);
next(err);
}
});
};
exports.returnNextBonfire = function(req, res, next) {
var bonfireNumber;
if (!req.user) {
req.user = new User();
//return res.redirect('/bonfires/meet-bonfire');
}
var currentTime = parseInt(+new Date() / 1000)
if (currentTime - req.user.lastContentSync > 86400) {
req.user.lastContentSync = currentTime;
var completed = req.user.completedBonfires;
var completed = req.user.completedBonfires.map(function(elem) {
return elem._id;
});
// TODO : remove debug statement
debug(req.user, 'this is the user');
req.user.uncompletedBonfires = resources.allBonfireIds().filter(function(elem) {
@ -57,6 +51,8 @@ exports.returnNextBonfire = function(req, res, next) {
}
});
}
debug('These are completed bonfires', completed);
debug('These are uncompleted bonfires', req.user.uncompletedBonfires);
var uncompletedBonfires = req.user.uncompletedBonfires;
@ -66,7 +62,7 @@ exports.returnNextBonfire = function(req, res, next) {
if (err) {
next(err);
}
debug('Finding next bonfire for user', bonfire);
nameString = bonfire[0].name.toLowerCase().replace(/\s/g, '-');
return res.redirect('/bonfires/' + nameString);
//res.render('bonfire/show', {

View File

@ -6,7 +6,8 @@ var User = require('../models/User'),
Challenge = require('./../models/Challenge'),
bonfires = require('../seed_data/bonfires.json');
Client = require('node-rest-client').Client,
client = new Client();
client = new Client(),
debug = require('debug')('freecc:cntr:bonfires');
/**
* GET /

View File

@ -33,12 +33,15 @@ $(document).ready(function() {
}
});
function completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash) {
$('#complete-bonfire-dialog').modal('show');
// Only post to server if there is an authenticated user
if ($('.signup-btn-nav').length < 1) {
l = location.pathname.split('/');
cn = l[l.length - 1];
$.ajax({
type: 'POST',
data: {
@ -49,7 +52,7 @@ $(document).ready(function() {
}
},
url: '/completed-bonfire/'
});
})
}
}
@ -66,15 +69,14 @@ $(document).ready(function() {
window.location = '/challenges/' + (parseInt(l[l.length - 1]) + 1);
});
// TODO: refactor this to create meaningful variable names, what the heck i l?
$('.next-bonfire-button').on('click', function() {
var bonfireSolution = myCodeMirror.getValue();
var thisBonfireHash = passedBonfireHash || null;
var didCompleteWith = $('#completed-with').val() || null;
console.log(didCompleteWith);
console.log(bonfireSolution, thisBonfireHash);
completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash);
window.location = '/bonfires/';
window.location('/bonfires');
});
// Bonfire instructions functions