Created
September 9, 2016 19:34
-
-
Save kurtzilla/8901425f9766615eb06bf5c98f8f7b63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////////// | |
// Discuss the basic function and purpose of promises | |
//////////////////////////////////////////// | |
module.exports.recordWebhook = function(json_event){ | |
return new Promise(function(resolve, reject){ | |
return resolve('aok') | |
}); | |
}; | |
Config.processForm = function(form, input, currentConfig){ | |
var deferred = $q.defer(); | |
var errors = []; | |
$http.post('/api/configs', { | |
input: input, | |
current: currentConfig | |
}) | |
.then(function(data){ | |
var returnData = data.data; | |
deferred.resolve(returnData); | |
}) | |
.catch(function(err){ | |
errors.push(err.data); | |
deferred.reject(errors); | |
}) | |
return deferred.promise; | |
}; | |
//////////////////////////////////////////// | |
// Make parallel requests using promise.all | |
//////////////////////////////////////////// | |
exports.stripeGetAuthTokens = function(code){ | |
return new Promise(function(resolve, reject) { | |
request.post({ | |
url: process.env.STRIPE_TOKEN_URL, | |
form: { | |
grant_type: "authorization_code", | |
client_id: process.env.STRIPE_CLIENT_ID, | |
code: code, | |
client_secret: process.env.STRIPE_SECRET | |
} | |
}, function (err, response, body) { | |
if (err) { | |
reject('ERROR: ' + err); | |
} else { | |
var _body = JSON.parse(body); | |
resolve(_body); | |
} | |
}); | |
}); | |
}; | |
router.get('/:id', function(req, res, next){ | |
var _game, _players, _events; | |
var _userplayers = []; | |
knex('games') | |
.where({ id: parseInt(req.params.id) }) | |
.first() | |
.then(function(game){ | |
// console.log('GAME', game); | |
_game = game; | |
return knex.select('*') | |
.from('players') | |
.where({ gameid: _game.id }); | |
}) | |
.then(function(playerIds){ | |
var playerPromises = []; | |
for(var i=0;i<playerIds.length;i++){ | |
playerPromises.push( | |
query.getUserPlayer_ByPlayerId(playerIds[i].id)); | |
} | |
return Promise.all(playerPromises); | |
}) | |
.then(function(userPlayers){ | |
var targetPromises = []; | |
for(var i=0;i<userPlayers.length;i++){ | |
_userplayers.push(userPlayers[i].rows[0]); | |
targetPromises.push( | |
query.getUserPlayer_ByPlayerId(userPlayers[i].rows[0].targetplayer_id) | |
); | |
} | |
return Promise.all(targetPromises); | |
}) | |
.then(function(targetPlayers){ | |
var _targetPlayers = []; | |
for(var i=0;i<targetPlayers.length;i++){ | |
_targetPlayers.push(targetPlayers[i].rows[0]); | |
} | |
// now match userplayers to targets and add in info for targets | |
// console.log('TARGETS', _targetPlayers); | |
for(var i=0;i<_userplayers.length;i++){ | |
var targetIdx = _userplayers[i].targetplayer_id; | |
//console.log('tidx',targetIdx); | |
// console.log('TARGETS', _targetPlayers); | |
var target; | |
if(targetIdx){ | |
// console.log('yes'); | |
target = _targetPlayers.find(function(e, i, ar){ | |
if(e.player_id === targetIdx){ | |
return e; | |
} | |
}); | |
} | |
// console.log('target',target); | |
_userplayers[i].targetplayer_image = (target) ? target.imageurl : ''; | |
_userplayers[i].targetplayer_lastlocation = (target) ? target.lastlocation : ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment