Merge pull request #1040 from codecombat/master

Made both game selections random
This commit is contained in:
Michael Schmatz 2014-05-20 12:46:38 -07:00
commit e5e1631363

View file

@ -112,6 +112,13 @@ module.exports.getTwoGames = (req, res) ->
"team":"humans" "team":"humans"
selection = "team totalScore transpiledCode teamSpells levelID creatorName creator" selection = "team totalScore transpiledCode teamSpells levelID creatorName creator"
LevelSession.count queryParams, (err, numberOfHumans) => LevelSession.count queryParams, (err, numberOfHumans) =>
if err? then return errors.serverError(res, "Couldn't get the number of human games")
ogreCountParams =
"levelID": "greed"
"submitted":true
"team":"ogres"
LevelSession.count ogreCountParams, (err, numberOfOgres) =>
if err? then return errors.serverError(res, "Couldnt' get the number of ogre games")
query = LevelSession query = LevelSession
.find(queryParams) .find(queryParams)
.limit(1) .limit(1)
@ -119,22 +126,21 @@ module.exports.getTwoGames = (req, res) ->
.skip(Math.floor(Math.random()*numberOfHumans)) .skip(Math.floor(Math.random()*numberOfHumans))
.lean() .lean()
query.exec (err, randomSession) => query.exec (err, randomSession) =>
if err? then return errors.serverError(res, "Couldn't select a top 15 random session!") if err? then return errors.serverError(res, "Couldn't select a random session!")
randomSession = randomSession[0] randomSession = randomSession[0]
queryParams = queryParams =
"levelID":"greed" "levelID":"greed"
"submitted":true "submitted":true
"totalScore":
$lte: randomSession.totalScore
"team": "ogres" "team": "ogres"
query = LevelSession query = LevelSession
.find(queryParams) .find(queryParams)
.select(selection)
.sort(totalScore: -1)
.limit(1) .limit(1)
.select(selection)
.skip(Math.floor(Math.random()*numberOfOgres))
.lean() .lean()
query.exec (err, otherSession) => query.exec (err, otherSession) =>
if err? then return errors.serverError(res, "Couldnt' select the other top 15 random session!") if err? then return errors.serverError(res, "Couldnt' select the other random session!")
otherSession = otherSession[0] otherSession = otherSession[0]
taskObject = taskObject =
"messageGenerated": Date.now() "messageGenerated": Date.now()