2014-02-05 12:39:14 -05:00
|
|
|
config = require '../../server_config'
|
|
|
|
winston = require 'winston'
|
|
|
|
mongoose = require 'mongoose'
|
|
|
|
async = require 'async'
|
|
|
|
errors = require '../commons/errors'
|
|
|
|
aws = require 'aws-sdk'
|
2014-02-06 16:25:11 -05:00
|
|
|
db = require './../routes/db'
|
2014-02-05 12:39:14 -05:00
|
|
|
mongoose = require 'mongoose'
|
|
|
|
queues = require '../commons/queue'
|
2014-02-05 18:07:15 -05:00
|
|
|
LevelSession = require '../levels/sessions/LevelSession'
|
2014-02-07 18:52:24 -05:00
|
|
|
TaskLog = require './task/ScoringTask'
|
2014-02-08 13:11:43 -05:00
|
|
|
bayes = new (require 'bayesian-battle')()
|
2014-02-05 12:39:14 -05:00
|
|
|
|
2014-02-06 17:32:35 -05:00
|
|
|
scoringTaskQueue = undefined
|
2014-02-08 13:11:43 -05:00
|
|
|
scoringTaskTimeoutInSeconds = 400
|
2014-02-06 17:32:35 -05:00
|
|
|
|
2014-02-05 18:07:15 -05:00
|
|
|
connectToScoringQueue = ->
|
2014-02-06 17:32:35 -05:00
|
|
|
queues.initializeQueueClient ->
|
|
|
|
queues.queueClient.registerQueue "scoring", {}, (err,data) ->
|
2014-02-07 15:36:56 -05:00
|
|
|
throwScoringQueueRegistrationError(err) if err?
|
2014-02-06 17:32:35 -05:00
|
|
|
scoringTaskQueue = data
|
2014-02-07 15:36:56 -05:00
|
|
|
winston.info "Connected to scoring task queue!"
|
|
|
|
|
|
|
|
throwScoringQueueRegistrationError = (error) ->
|
|
|
|
winston.error "There was an error registering the scoring queue: #{error}"
|
|
|
|
throw new Error "There was an error registering the scoring queue."
|
|
|
|
|
|
|
|
|
|
|
|
module.exports.setup = (app) -> connectToScoringQueue()
|
2014-02-05 12:39:14 -05:00
|
|
|
|
|
|
|
|
2014-02-06 16:25:11 -05:00
|
|
|
module.exports.dispatchTaskToConsumer = (req, res) ->
|
2014-02-07 18:52:24 -05:00
|
|
|
|
|
|
|
userID = getUserIDFromRequest req
|
|
|
|
return errors.forbidden res, "You need to be logged in to simulate games" unless userID?
|
|
|
|
|
|
|
|
scoringTaskQueue.receiveMessage (taskQueueReceiveError, message) ->
|
|
|
|
if message.isEmpty() or taskQueueReceiveError?
|
|
|
|
return errors.gatewayTimeoutError res, "No messages were receieved from the queue.#{taskQueueReceiveError}"
|
2014-02-06 16:25:11 -05:00
|
|
|
|
2014-02-07 15:36:56 -05:00
|
|
|
messageBody = parseTaskQueueMessage req, res, message
|
|
|
|
return errors.serverError res, "There was an error parsing the queue message" unless messageBody?
|
2014-02-06 16:25:11 -05:00
|
|
|
|
2014-02-07 15:36:56 -05:00
|
|
|
constructTaskObject messageBody, (taskConstructionError, taskObject) ->
|
|
|
|
return errors.serverError res, "There was an error constructing the scoring task" if taskConstructionError?
|
2014-02-07 18:52:24 -05:00
|
|
|
|
2014-02-08 13:11:43 -05:00
|
|
|
taskProcessingTimeInSeconds = 10
|
|
|
|
message.changeMessageVisibilityTimeout scoringTaskTimeoutInSeconds + taskProcessingTimeInSeconds
|
2014-02-07 18:52:24 -05:00
|
|
|
|
|
|
|
constructTaskLogObject userID,message.getReceiptHandle(), (taskLogError, taskLogObject) ->
|
|
|
|
return errors.serverError res, "There was an error creating the task log object." if taskLogError?
|
|
|
|
|
2014-02-08 13:11:43 -05:00
|
|
|
setTaskObjectTaskLogID taskObject, taskLogObject._id
|
2014-02-07 18:52:24 -05:00
|
|
|
|
|
|
|
sendResponseObject req, res, taskObject
|
2014-02-05 12:39:14 -05:00
|
|
|
|
|
|
|
|
2014-02-08 13:11:43 -05:00
|
|
|
|
|
|
|
setTaskObjectTaskLogID = (taskObject, taskLogObjectID) -> taskObject.taskID = taskLogObjectID
|
|
|
|
|
2014-02-07 15:36:56 -05:00
|
|
|
parseTaskQueueMessage = (req, res, message) ->
|
|
|
|
try
|
|
|
|
return messageBody = JSON.parse message.getBody()
|
|
|
|
catch e
|
2014-02-07 17:24:42 -05:00
|
|
|
sendResponseObject req, res, {"error":"There was an error parsing the task.Error: #{e}" }
|
2014-02-07 18:52:24 -05:00
|
|
|
return null
|
|
|
|
|
|
|
|
getUserIDFromRequest = (req) ->
|
|
|
|
if req.user? and req.user._id?
|
|
|
|
return req.user._id
|
|
|
|
else
|
|
|
|
return null
|
|
|
|
|
|
|
|
constructTaskLogObject = (calculatorUserID, messageIdentifierString, callback) ->
|
|
|
|
taskLogObject = new TaskLog
|
2014-02-08 17:21:16 -05:00
|
|
|
"createdAt": new Date()
|
2014-02-07 18:52:24 -05:00
|
|
|
"calculator":calculatorUserID
|
|
|
|
"sentDate": Date.now()
|
|
|
|
"messageIdentifierString":messageIdentifierString
|
2014-02-05 12:39:14 -05:00
|
|
|
|
2014-02-07 18:52:24 -05:00
|
|
|
taskLogObject.save callback
|
2014-02-05 12:39:14 -05:00
|
|
|
|
2014-02-05 18:07:15 -05:00
|
|
|
constructTaskObject = (taskMessageBody, callback) ->
|
2014-02-07 17:24:42 -05:00
|
|
|
async.map taskMessageBody.sessions, getSessionInformation, (err, sessions) ->
|
2014-02-05 18:07:15 -05:00
|
|
|
return callback err, data if err?
|
2014-02-07 15:36:56 -05:00
|
|
|
|
2014-02-05 18:07:15 -05:00
|
|
|
taskObject =
|
|
|
|
"messageGenerated": Date.now()
|
2014-02-07 18:52:24 -05:00
|
|
|
"sessions": []
|
2014-02-07 17:24:42 -05:00
|
|
|
|
|
|
|
for session in sessions
|
|
|
|
sessionInformation =
|
|
|
|
"sessionID": session.sessionID
|
|
|
|
"sessionChangedTime": session.changed
|
|
|
|
"team": session.team? "No team"
|
|
|
|
"code": session.code
|
2014-02-07 18:52:24 -05:00
|
|
|
taskObject.sessions.push sessionInformation
|
2014-02-05 19:41:01 -05:00
|
|
|
callback err, taskObject
|
2014-02-05 18:07:15 -05:00
|
|
|
|
|
|
|
|
2014-02-05 19:41:01 -05:00
|
|
|
getSessionInformation = (sessionIDString, callback) ->
|
|
|
|
LevelSession.findOne {"_id": sessionIDString }, (err, session) ->
|
2014-02-07 15:36:56 -05:00
|
|
|
return callback err, {"error":"There was an error retrieving the session."} if err?
|
2014-02-05 18:07:15 -05:00
|
|
|
|
2014-02-08 13:11:43 -05:00
|
|
|
session = session.toObject()
|
2014-02-07 15:36:56 -05:00
|
|
|
sessionInformation =
|
|
|
|
"sessionID": session._id
|
|
|
|
"code": _.cloneDeep session.code
|
|
|
|
"changed": session.changed
|
|
|
|
"creator": session.creator
|
2014-02-07 17:24:42 -05:00
|
|
|
"team": session.team
|
|
|
|
|
2014-02-07 15:36:56 -05:00
|
|
|
|
|
|
|
callback err, sessionInformation
|
|
|
|
|
|
|
|
|
|
|
|
sendResponseObject = (req,res,object) ->
|
|
|
|
res.setHeader('Content-Type', 'application/json')
|
|
|
|
res.send(object)
|
|
|
|
res.end()
|
|
|
|
|
|
|
|
module.exports.processTaskResult = (req, res) ->
|
2014-02-08 13:11:43 -05:00
|
|
|
clientResponseObject = verifyClientResponse req.body, res
|
2014-02-07 17:24:42 -05:00
|
|
|
|
|
|
|
if clientResponseObject?
|
2014-02-08 13:11:43 -05:00
|
|
|
TaskLog.findOne {"_id": clientResponseObject.taskID}, (err, taskLog) ->
|
|
|
|
return errors.serverError res, "There was an error retrieiving the task log object" if err?
|
2014-02-07 18:52:24 -05:00
|
|
|
|
2014-02-08 13:11:43 -05:00
|
|
|
taskLogJSON = taskLog.toObject()
|
2014-02-07 17:24:42 -05:00
|
|
|
|
2014-02-08 17:21:16 -05:00
|
|
|
#return errors.badInput res, "That computational task has already been performed" if taskLogJSON.calculationTimeMS
|
|
|
|
#return handleTimedOutTask req, res, clientResponseObject if hasTaskTimedOut taskLogJSON.sentDate
|
2014-02-07 17:24:42 -05:00
|
|
|
|
2014-02-08 13:11:43 -05:00
|
|
|
logTaskComputation clientResponseObject, taskLog, (loggingError) ->
|
2014-02-08 17:21:16 -05:00
|
|
|
return errors.serverError res, "There as a problem logging the task computation: #{loggingError}" if loggingError?
|
2014-02-08 13:11:43 -05:00
|
|
|
updateScores clientResponseObject, (updatingScoresError, newScores) ->
|
|
|
|
return errors.serverError res, "There was an error updating the scores.#{updatingScoresError}" if updatingScoresError?
|
|
|
|
sendResponseObject req, res, newScores
|
2014-02-07 17:24:42 -05:00
|
|
|
|
|
|
|
|
2014-02-07 18:52:24 -05:00
|
|
|
|
2014-02-08 13:11:43 -05:00
|
|
|
hasTaskTimedOut = (taskSentTimestamp) -> taskSentTimestamp + scoringTaskTimeoutInSeconds * 1000 < Date.now()
|
2014-02-07 18:52:24 -05:00
|
|
|
|
2014-02-08 13:11:43 -05:00
|
|
|
handleTimedOutTask = (req, res, taskBody) ->
|
|
|
|
errors.clientTimeout res, "The task results were not provided within a timely manner"
|
2014-02-07 18:52:24 -05:00
|
|
|
|
2014-02-07 17:24:42 -05:00
|
|
|
|
2014-02-08 13:11:43 -05:00
|
|
|
verifyClientResponse = (responseObject, res) ->
|
|
|
|
unless typeof responseObject is "object"
|
|
|
|
errors.badInput res, "The response to that query is required to be a JSON object."
|
2014-02-07 17:24:42 -05:00
|
|
|
return null
|
2014-02-08 13:11:43 -05:00
|
|
|
return responseObject
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logTaskComputation = (taskObject,taskLogObject, callback) ->
|
|
|
|
taskLogObject.calculationTimeMS = taskObject.calculationTimeMS
|
|
|
|
taskLogObject.sessions = taskObject.sessions
|
|
|
|
taskLogObject.save (err) -> callback err
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateScores = (taskObject,callback) ->
|
|
|
|
winston.info "Updating scores"
|
|
|
|
sessionIDs = _.pluck taskObject.sessions, 'sessionID'
|
|
|
|
async.map sessionIDs, retrieveOldScoreMetrics, (err, oldScores) ->
|
|
|
|
callback err, {"error": "There was an error retrieving the old scores"} if err?
|
2014-02-08 17:21:16 -05:00
|
|
|
oldScoreArray = _.toArray putRankingFromMetricsIntoScoreObject taskObject, oldScores
|
2014-02-08 13:11:43 -05:00
|
|
|
|
2014-02-08 17:21:16 -05:00
|
|
|
newScoreArray = bayes.updatePlayerSkills oldScoreArray
|
|
|
|
|
|
|
|
#TODO: database persistence here
|
|
|
|
callback err, newScoreArray
|
2014-02-07 15:36:56 -05:00
|
|
|
|
2014-02-08 17:21:16 -05:00
|
|
|
putRankingFromMetricsIntoScoreObject = (taskObject,scoreObject) ->
|
|
|
|
scoreObject = _.indexBy scoreObject, 'id'
|
|
|
|
for session in taskObject.sessions
|
|
|
|
scoreObject[session.sessionID].gameRanking = session.metrics.rank
|
|
|
|
|
|
|
|
return scoreObject
|
2014-02-05 18:07:15 -05:00
|
|
|
|
2014-02-08 13:11:43 -05:00
|
|
|
retrieveOldScoreMetrics = (sessionID, callback) ->
|
|
|
|
LevelSession.findOne {"_id":sessionID}, (err, session) ->
|
|
|
|
return callback err, {"error":"There was an error retrieving the session."} if err?
|
2014-02-08 13:18:41 -05:00
|
|
|
defaultScore = (25 - 1.8*(25/3))
|
|
|
|
defaultStandardDeviation = 25/3
|
2014-02-08 13:11:43 -05:00
|
|
|
oldScoreObject =
|
2014-02-08 13:18:41 -05:00
|
|
|
"standardDeviation":session.standardDeviation ? defaultStandardDeviation
|
2014-02-08 13:11:43 -05:00
|
|
|
"meanStrength":session.meanStrength ? 25
|
2014-02-08 13:18:41 -05:00
|
|
|
"totalScore":session.totalScore ? defaultScore
|
|
|
|
"id": sessionID
|
2014-02-08 13:11:43 -05:00
|
|
|
|
|
|
|
callback err, oldScoreObject
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-05 12:39:14 -05:00
|
|
|
|
2014-02-07 18:52:24 -05:00
|
|
|
|
|
|
|
|
2014-02-07 15:36:56 -05:00
|
|
|
###Sample Messages
|
|
|
|
sampleQueueMessage =
|
|
|
|
sessions: [
|
|
|
|
"52dfeb17c8b5f435c7000025"
|
2014-02-07 17:24:42 -05:00
|
|
|
"52dfe03ac8b5f435c7000009"
|
2014-02-07 15:36:56 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
sampleUndoneTaskObject =
|
|
|
|
"taskID": "507f191e810c19729de860ea"
|
|
|
|
"sessions" : [
|
|
|
|
{
|
|
|
|
"ID":"52dfeb17c8b5f435c7000025"
|
|
|
|
"sessionChangedTime": "2014-01-22T16:28:12.450Z"
|
|
|
|
"team":"humans"
|
|
|
|
"code": "code goes here"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ID":"51eb2714fa058cb20d00fedg"
|
|
|
|
"sessionChangedTime": "2014-01-22T16:28:12.450Z"
|
|
|
|
"team":"ogres"
|
|
|
|
"code": "code goes here"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
sampleResponseObject =
|
|
|
|
"taskID": "507f191e810c19729de860ea"
|
|
|
|
"calculationTime":3201
|
|
|
|
"sessions": [
|
|
|
|
{
|
|
|
|
"ID":"52dfeb17c8b5f435c7000025"
|
|
|
|
"sessionChangedTime": "2014-01-22T16:28:12.450Z"
|
|
|
|
"metrics": {
|
|
|
|
"rank":2
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ID":"51eb2714fa058cb20d00fedg"
|
|
|
|
"sessionChangedTime": "2014-01-22T16:28:12.450Z"
|
|
|
|
"metrics": {
|
|
|
|
"rank":1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
sampleTaskLogObject=
|
|
|
|
{
|
|
|
|
"_id":ObjectId("507f191e810c19729de860ea") #datestamp is built into objectId
|
|
|
|
"calculatedBy":ObjectId("51eb2714fa058cb20d0006ef")
|
|
|
|
"calculationTime":3201
|
2014-02-07 17:24:42 -05:00
|
|
|
timedOut: false
|
2014-02-07 15:36:56 -05:00
|
|
|
"sessions":[
|
|
|
|
{
|
|
|
|
"ID":ObjectId("52dfeb17c8b5f435c7000025")
|
|
|
|
"metrics": {
|
|
|
|
"rank":2
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ID":ObjectId("51eb2714fa058cb20d00feda")
|
|
|
|
"metrics": {
|
|
|
|
"rank":1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
###
|