Fix sorting crashing bug

This commit is contained in:
Michael Schmatz 2014-12-24 16:45:34 -05:00
parent 95f76ae9d8
commit 3d1ee9767f

View file

@ -126,6 +126,7 @@ module.exports.getTwoGames = (req, res) ->
ogresGameID = req.body.ogresGameID ogresGameID = req.body.ogresGameID
ladderGameIDs = ['greed', 'criss-cross', 'brawlwood', 'dungeon-arena', 'gold-rush', 'sky-span', 'dueling-grounds', 'cavern-survival', 'multiplayer-treasure-grove'] ladderGameIDs = ['greed', 'criss-cross', 'brawlwood', 'dungeon-arena', 'gold-rush', 'sky-span', 'dueling-grounds', 'cavern-survival', 'multiplayer-treasure-grove']
levelID = _.sample ladderGameIDs levelID = _.sample ladderGameIDs
sortLimit = 5000
unless ogresGameID and humansGameID unless ogresGameID and humansGameID
#fetch random games here #fetch random games here
queryParams = queryParams =
@ -138,6 +139,7 @@ module.exports.getTwoGames = (req, res) ->
unless numberOfHumans unless numberOfHumans
res.send(204, 'No games to score.') res.send(204, 'No games to score.')
return res.end() return res.end()
numberOfHumans = Math.min numberOfHumans, sortLimit
humanSkipCount = Math.floor(Math.random() * numberOfHumans) humanSkipCount = Math.floor(Math.random() * numberOfHumans)
ogreCountParams = ogreCountParams =
'levelID': levelID 'levelID': levelID
@ -148,14 +150,15 @@ module.exports.getTwoGames = (req, res) ->
unless numberOfOgres unless numberOfOgres
res.send(204, 'No games to score.') res.send(204, 'No games to score.')
return res.end() return res.end()
numberOfOgres = Math.min numberOfOgres, sortLimit
ogresSkipCount = Math.floor(Math.random() * numberOfOgres) ogresSkipCount = Math.floor(Math.random() * numberOfOgres)
query = LevelSession query = LevelSession
.aggregate() .aggregate()
.match(queryParams) .match(queryParams)
.project(selection) .project(selection)
.limit(5000)
.sort({'submitDate': -1}) .sort({'submitDate': -1})
.limit(sortLimit)
.skip(humanSkipCount) .skip(humanSkipCount)
.limit(1) .limit(1)
query.exec (err, randomSession) => query.exec (err, randomSession) =>
@ -169,8 +172,8 @@ module.exports.getTwoGames = (req, res) ->
.aggregate() .aggregate()
.match(queryParams) .match(queryParams)
.project(selection) .project(selection)
.limit(5000)
.sort({'submitDate': -1}) .sort({'submitDate': -1})
.limit(sortLimit)
.skip(ogresSkipCount) .skip(ogresSkipCount)
.limit(1) .limit(1)
query.exec (err, otherSession) => query.exec (err, otherSession) =>