Skip to content

Instantly share code, notes, and snippets.

@mrryanjohnston
Created August 19, 2011 03:33
Show Gist options
  • Save mrryanjohnston/1155969 to your computer and use it in GitHub Desktop.
Save mrryanjohnston/1155969 to your computer and use it in GitHub Desktop.
User.find({}).sort('roundwins', -1).limit(3).execFind(function(err, foundusers) {
var place = -1;
var places = ['roundfirsts', 'roundseconds', 'roundthirds'];
foundusers.forEach(function(user) {
//Alright, so, sorry about this one, but:
//Increase the document's property equal to user.'whatever place', where
//'whatever place' is determined from the value of place increased by 1
//as an index of the places array
console.log(place);
console.log(user);
user[places[++place]]++;
console.log(place);
user.save(function(err) {
//if this is the last time we have to award a player, then FLUSH THE ROUND
if (place===foundusers.length-1) {
User.find({}, function (err, users) {
//Each user
users.forEach(function(user) {
user.roundplays = 0;
user.roundwins = 0;
user.roundlosses = 0;
user.save(function(err) {
if (err) { throw err; }
});
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment