codecombat/server/queues/scoring.coffee

97 lines
3.1 KiB
CoffeeScript

config = require '../../server_config'
winston = require 'winston'
mongoose = require 'mongoose'
async = require 'async'
errors = require '../commons/errors'
aws = require 'aws-sdk'
db = require './../routes/db'
mongoose = require 'mongoose'
events = require 'events'
queues = require '../commons/queue'
LevelSession = require '../levels/sessions/LevelSession'
scoringTaskQueue = undefined
connectToScoringQueue = ->
queues.initializeQueueClient ->
queues.queueClient.registerQueue "scoring", {}, (err,data) ->
if err?
winston.error "There was an error registering the scoring queue."
throw new Error "There was an error registering the scoring queue."
scoringTaskQueue = data
winston.info "Connected to scoring task queue!" unless err?
module.exports.setup = (app) ->
connectToScoringQueue()
module.exports.dispatchTaskToConsumer = (req, res) ->
scoringTaskQueue.receiveMessage (err, message) ->
###Using Test Message
{ "sessionID": "52dfeb17c8b5f435c7000025" }
###
if message.isEmpty()
#TODO: Set message code as 504 Gateway Timeout
sendResponseObject req, res, {"error":"No messages were received."}
else
try
messageBody = JSON.parse message.getBody()
catch
return sendResponseObject req, res, {"error":"There was an error parsing the task."}
constructTaskObject messageBody, (taskConstructionError, taskObject) ->
if taskConstructionError?
sendResponseObject req, res, {"error":taskConstructionError}
else
sendResponseObject req, res, taskObject
module.exports.processTaskResult = (req, res) ->
#clientResponseObject = JSON.parse req.body
res.end("You posted an object to score!")
sendResponseObject = (req,res,object) ->
res.setHeader('Content-Type', 'application/json')
res.send(object)
res.end()
constructTaskObject = (taskMessageBody, callback) ->
getSessionInformation taskMessageBody.sessionID, (err, sessionInformation) ->
return callback err, data if err?
taskObject =
"messageGenerated": Date.now()
"sessionID": sessionInformation.sessionID
"sessionChangedTime": sessionInformation.changed
"taskGeneratingPlayerID": sessionInformation.creator
"code": sessionInformation.code
"players": sessionInformation.players
callback err, taskObject
###
"players" : [
{"ID":"51eb2714fa058cb20d0006ef", "team":"humans", "userCodeMap": "code goes here"}
{"ID":"51eb2714fa058cb20d00fedg", "team":"ogres","userCodeMap": "code goes here"}
]
###
getSessionInformation = (sessionIDString, callback) ->
LevelSession.findOne {"_id": sessionIDString }, (err, session) ->
if err?
callback err, {"error":"There was an error retrieving the session."}
else
session = session.toJSON()
sessionInformation =
"sessionID": session._id
"players": _.cloneDeep session.players
"code": _.cloneDeep session.code
"changed": session.changed
"creator": session.creator
callback err, sessionInformation
processClientResponse = (clientResponseBody, callback) ->
return