Scheduling fixes

This commit is contained in:
Michael Schmatz 2014-03-02 18:55:07 -08:00
parent 94b98a2d46
commit 81466675ec

View file

@ -107,40 +107,49 @@ module.exports.processTaskResult = (req, res) ->
scoringTaskQueue.deleteMessage clientResponseObject.receiptHandle, (err) -> scoringTaskQueue.deleteMessage clientResponseObject.receiptHandle, (err) ->
console.log "Deleted message." console.log "Deleted message."
if err? then return errors.badInput res, "The queue message is already back in the queue, rejecting results." if err? then return errors.badInput res, "The queue message is already back in the queue, rejecting results."
logTaskComputation clientResponseObject, taskLog, (logErr) -> LevelSession.findOne(_id: clientResponseObject.originalSessionID).lean().exec (err, levelSession) ->
if logErr? then return errors.serverError res, "There as a problem logging the task computation: #{logErr}" if err? then return errors.serverError res, "There was a problem finding the level session:#{err}"
updateSessions clientResponseObject, (updateError, newScoreArray) -> console.log "Queue message created at: #{taskLogJSON.createdAt}, level session submitted at #{levelSession.submitDate}"
if updateError? then return errors.serverError res, "There was an error updating the scores.#{updateError}"
if taskLogJSON.createdAt <= levelSession.submitDate
newScoresObject = _.indexBy newScoreArray, 'id' console.log "Task has been resubmitted!"
return sendResponseObject req, res, {"message":"The game has been resubmitted. Removing from queue..."}
addMatchToSessions clientResponseObject, newScoresObject, (err, data) ->
if err? then return errors.serverError res, "There was an error updating the sessions with the match! #{JSON.stringify err}" logTaskComputation clientResponseObject, taskLog, (logErr) ->
if logErr? then return errors.serverError res, "There as a problem logging the task computation: #{logErr}"
originalSessionID = clientResponseObject.originalSessionID
originalSessionTeam = clientResponseObject.originalSessionTeam updateSessions clientResponseObject, (updateError, newScoreArray) ->
originalSessionRank = parseInt clientResponseObject.originalSessionRank if updateError? then return errors.serverError res, "There was an error updating the scores.#{updateError}"
determineIfSessionShouldContinueAndUpdateLog originalSessionID, originalSessionRank, (err, sessionShouldContinue) -> newScoresObject = _.indexBy newScoreArray, 'id'
if err? then return errors.serverError res, "There was an error determining if the session should continue, #{err}"
addMatchToSessions clientResponseObject, newScoresObject, (err, data) ->
if sessionShouldContinue if err? then return errors.serverError res, "There was an error updating the sessions with the match! #{JSON.stringify err}"
opposingTeam = calculateOpposingTeam(originalSessionTeam)
opponentID = _.pull(_.keys(newScoresObject), originalSessionID) originalSessionID = clientResponseObject.originalSessionID
sessionNewScore = newScoresObject[originalSessionID].totalScore originalSessionTeam = clientResponseObject.originalSessionTeam
opponentNewScore = newScoresObject[opponentID].totalScore originalSessionRank = parseInt clientResponseObject.originalSessionRank
findNearestBetterSessionID originalSessionID, sessionNewScore, opponentNewScore, opponentID ,opposingTeam, (err, opponentSessionID) ->
if err? then return errors.serverError res, "There was an error finding the nearest sessionID!" determineIfSessionShouldContinueAndUpdateLog originalSessionID, originalSessionRank, (err, sessionShouldContinue) ->
unless opponentSessionID then return sendResponseObject req, res, {"message":"There were no more games to rank(game is at top!"} if err? then return errors.serverError res, "There was an error determining if the session should continue, #{err}"
addPairwiseTaskToQueue [originalSessionID, opponentSessionID], (err, success) -> if sessionShouldContinue
if err? then return errors.serverError res, "There was an error sending the pairwise tasks to the queue!" opposingTeam = calculateOpposingTeam(originalSessionTeam)
sendResponseObject req, res, {"message":"The scores were updated successfully and more games were sent to the queue!"} opponentID = _.pull(_.keys(newScoresObject), originalSessionID)
else sessionNewScore = newScoresObject[originalSessionID].totalScore
console.log "Player lost, achieved rank #{originalSessionRank}" opponentNewScore = newScoresObject[opponentID].totalScore
sendResponseObject req, res, {"message":"The scores were updated successfully, person lost so no more games are being inserted!"} findNearestBetterSessionID originalSessionID, sessionNewScore, opponentNewScore, opponentID ,opposingTeam, (err, opponentSessionID) ->
if err? then return errors.serverError res, "There was an error finding the nearest sessionID!"
unless opponentSessionID then return sendResponseObject req, res, {"message":"There were no more games to rank(game is at top!"}
addPairwiseTaskToQueue [originalSessionID, opponentSessionID], (err, success) ->
if err? then return errors.serverError res, "There was an error sending the pairwise tasks to the queue!"
sendResponseObject req, res, {"message":"The scores were updated successfully and more games were sent to the queue!"}
else
console.log "Player lost, achieved rank #{originalSessionRank}"
sendResponseObject req, res, {"message":"The scores were updated successfully, person lost so no more games are being inserted!"}
determineIfSessionShouldContinueAndUpdateLog = (sessionID, sessionRank, cb) -> determineIfSessionShouldContinueAndUpdateLog = (sessionID, sessionRank, cb) ->