Correctly sorting unsolved bonfires
This commit is contained in:
23
app.js
23
app.js
@ -326,6 +326,17 @@ app.post('/completed-bonfire/', function (req, res) {
|
|||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
} else {
|
} 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 = pairedWith.pop();
|
||||||
pairedWith.completedBonfires.push({
|
pairedWith.completedBonfires.push({
|
||||||
_id: bonfireHash,
|
_id: bonfireHash,
|
||||||
@ -341,17 +352,8 @@ app.post('/completed-bonfire/', function (req, res) {
|
|||||||
solution: isSolution
|
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();
|
req.user.save();
|
||||||
pairedWith.save();
|
pairedWith.save();
|
||||||
|
|
||||||
@ -370,6 +372,7 @@ app.post('/completed-bonfire/', function (req, res) {
|
|||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
req.user.uncompletedBonfires.splice(index,1)
|
req.user.uncompletedBonfires.splice(index,1)
|
||||||
}
|
}
|
||||||
|
debug("Saving user without a pair");
|
||||||
req.user.save();
|
req.user.save();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -30,25 +30,19 @@ exports.index = function(req, res) {
|
|||||||
bonfireHash: 'test'
|
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) {
|
exports.returnNextBonfire = function(req, res, next) {
|
||||||
var bonfireNumber;
|
|
||||||
if (!req.user) {
|
if (!req.user) {
|
||||||
req.user = new User();
|
req.user = new User();
|
||||||
//return res.redirect('/bonfires/meet-bonfire');
|
|
||||||
}
|
}
|
||||||
var currentTime = parseInt(+new Date() / 1000)
|
var currentTime = parseInt(+new Date() / 1000)
|
||||||
if (currentTime - req.user.lastContentSync > 86400) {
|
if (currentTime - req.user.lastContentSync > 86400) {
|
||||||
req.user.lastContentSync = currentTime;
|
req.user.lastContentSync = currentTime;
|
||||||
var completed = req.user.completedBonfires;
|
var completed = req.user.completedBonfires.map(function(elem) {
|
||||||
|
return elem._id;
|
||||||
|
});
|
||||||
// TODO : remove debug statement
|
// TODO : remove debug statement
|
||||||
debug(req.user, 'this is the user');
|
debug(req.user, 'this is the user');
|
||||||
req.user.uncompletedBonfires = resources.allBonfireIds().filter(function(elem) {
|
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;
|
var uncompletedBonfires = req.user.uncompletedBonfires;
|
||||||
|
|
||||||
@ -66,7 +62,7 @@ exports.returnNextBonfire = function(req, res, next) {
|
|||||||
if (err) {
|
if (err) {
|
||||||
next(err);
|
next(err);
|
||||||
}
|
}
|
||||||
debug('Finding next bonfire for user', bonfire);
|
|
||||||
nameString = bonfire[0].name.toLowerCase().replace(/\s/g, '-');
|
nameString = bonfire[0].name.toLowerCase().replace(/\s/g, '-');
|
||||||
return res.redirect('/bonfires/' + nameString);
|
return res.redirect('/bonfires/' + nameString);
|
||||||
//res.render('bonfire/show', {
|
//res.render('bonfire/show', {
|
||||||
|
@ -6,7 +6,8 @@ var User = require('../models/User'),
|
|||||||
Challenge = require('./../models/Challenge'),
|
Challenge = require('./../models/Challenge'),
|
||||||
bonfires = require('../seed_data/bonfires.json');
|
bonfires = require('../seed_data/bonfires.json');
|
||||||
Client = require('node-rest-client').Client,
|
Client = require('node-rest-client').Client,
|
||||||
client = new Client();
|
client = new Client(),
|
||||||
|
debug = require('debug')('freecc:cntr:bonfires');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET /
|
* GET /
|
||||||
|
@ -33,12 +33,15 @@ $(document).ready(function() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash) {
|
function completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash) {
|
||||||
$('#complete-bonfire-dialog').modal('show');
|
$('#complete-bonfire-dialog').modal('show');
|
||||||
// Only post to server if there is an authenticated user
|
// Only post to server if there is an authenticated user
|
||||||
if ($('.signup-btn-nav').length < 1) {
|
if ($('.signup-btn-nav').length < 1) {
|
||||||
l = location.pathname.split('/');
|
l = location.pathname.split('/');
|
||||||
cn = l[l.length - 1];
|
cn = l[l.length - 1];
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
data: {
|
data: {
|
||||||
@ -49,7 +52,7 @@ $(document).ready(function() {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
url: '/completed-bonfire/'
|
url: '/completed-bonfire/'
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,15 +69,14 @@ $(document).ready(function() {
|
|||||||
window.location = '/challenges/' + (parseInt(l[l.length - 1]) + 1);
|
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() {
|
$('.next-bonfire-button').on('click', function() {
|
||||||
var bonfireSolution = myCodeMirror.getValue();
|
var bonfireSolution = myCodeMirror.getValue();
|
||||||
var thisBonfireHash = passedBonfireHash || null;
|
var thisBonfireHash = passedBonfireHash || null;
|
||||||
var didCompleteWith = $('#completed-with').val() || null;
|
var didCompleteWith = $('#completed-with').val() || null;
|
||||||
console.log(didCompleteWith);
|
|
||||||
console.log(bonfireSolution, thisBonfireHash);
|
|
||||||
completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash);
|
completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash);
|
||||||
window.location = '/bonfires/';
|
window.location('/bonfires');
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Bonfire instructions functions
|
// Bonfire instructions functions
|
||||||
|
Reference in New Issue
Block a user