codecombat/server/levels/level_handler.coffee

165 lines
4.9 KiB
CoffeeScript
Raw Normal View History

Level = require('./Level')
Session = require('./sessions/LevelSession')
SessionHandler = require('./sessions/level_session_handler')
Feedback = require('./feedbacks/LevelFeedback')
Handler = require('../commons/Handler')
2014-01-03 13:32:13 -05:00
mongoose = require('mongoose')
LevelHandler = class LevelHandler extends Handler
modelClass: Level
editableProperties: [
'description'
'documentation'
'background'
'nextLevel'
'scripts'
'thangs'
'systems'
'victory'
'name'
'i18n'
'icon'
'goals'
2014-01-03 13:32:13 -05:00
]
postEditableProperties: ['name']
2014-01-03 13:32:13 -05:00
getByRelationship: (req, res, args...) ->
return @getSession(req, res, args[0]) if args[1] is 'session'
2014-02-07 18:51:05 -05:00
return @getLeaderboard(req, res, args[0]) if args[1] is 'leaderboard'
return @getAllSessions(req, res, args[0]) if args[1] is 'all_sessions'
2014-01-03 13:32:13 -05:00
return @getFeedback(req, res, args[0]) if args[1] is 'feedback'
return @sendNotFoundError(res)
fetchLevelByIDAndHandleErrors: (id, req, res, callback) ->
2014-01-03 13:32:13 -05:00
@getDocumentForIdOrSlug id, (err, level) =>
return @sendDatabaseError(res, err) if err
return @sendNotFoundError(res) unless level?
return @sendUnauthorizedError(res) unless @hasAccessToDocument(req, level, 'get')
callback err, level
2014-01-03 13:32:13 -05:00
getSession: (req, res, id) ->
@fetchLevelByIDAndHandleErrors id, req, res, (err, level) =>
sessionQuery =
level:
original: level.original.toString()
majorVersion: level.version.major
2014-01-03 13:32:13 -05:00
creator: req.user.id
2014-02-13 13:42:25 -05:00
# TODO: generalize this for levels that need teams
if req.query.team?
sessionQuery.team = req.query.team
else if level.name is 'Project DotA'
sessionQuery.team = 'humans'
2014-01-03 13:32:13 -05:00
Session.findOne(sessionQuery).exec (err, doc) =>
return @sendDatabaseError(res, err) if err
return @sendSuccess(res, doc) if doc?
@createAndSaveNewSession sessionQuery, req, res
2014-01-03 13:32:13 -05:00
createAndSaveNewSession: (sessionQuery, req, res) =>
initVals = sessionQuery
initVals.state =
complete:false
scripts:
currentScript:null # will not save empty objects
initVals.permissions = [
{
target:req.user.id
access:'owner'
}
{
target:'public'
access:'write'
}
]
session = new Session(initVals)
session.save (err) =>
return @sendDatabaseError(res, err) if err
@sendSuccess(res, @formatEntity(req, session))
# TODO: tying things like @formatEntity and saveChangesToDocument don't make sense
# associated with the handler, because the handler might return a different type
# of model, like in this case. Refactor to move that logic to the model instead.
getAllSessions: (req, res, id) ->
@fetchLevelByIDAndHandleErrors id, req, res, (err, level) =>
sessionQuery =
level:
original: level.original.toString()
majorVersion: level.version.major
2014-02-13 18:02:49 -05:00
submitted: true
propertiesToReturn = [
'_id'
'totalScore'
'submitted'
'team'
'creatorName'
]
2014-02-13 18:02:49 -05:00
query = Session
.find(sessionQuery)
.select(propertiesToReturn.join ' ')
2014-02-13 18:14:08 -05:00
query.exec (err, results) =>
if err then @sendDatabaseError(res, err) else @sendSuccess res, results
2014-02-13 18:14:08 -05:00
getLeaderboard: (req, res, id) ->
@validateLeaderboardRequestParameters req
[original, version] = id.split '.'
version = parseInt(version) ? 0
scoreQuery = {}
scoreQuery[if req.query.order is 1 then "$gte" else "$lte"] = req.query.scoreOffset
sessionsQueryParameters =
level:
original: original
majorVersion: version
2014-02-13 18:14:08 -05:00
team: req.query.team
totalScore: scoreQuery
submitted: true
sortParameters =
"totalScore": req.query.order
selectProperties = [
'totalScore'
'creatorName'
'creator'
]
query = Session
.find(sessionsQueryParameters)
.limit(req.query.limit)
.sort(sortParameters)
.select(selectProperties.join ' ')
query.exec (err, resultSessions) =>
2014-01-03 13:32:13 -05:00
return @sendDatabaseError(res, err) if err
resultSessions ?= []
@sendSuccess res, resultSessions
validateLeaderboardRequestParameters: (req) ->
req.query.order = parseInt(req.query.order) ? -1
req.query.scoreOffset = parseFloat(req.query.scoreOffset) ? 100000
req.query.team ?= 'humans'
req.query.limit = parseInt(req.query.limit) ? 20
2014-01-03 13:32:13 -05:00
getFeedback: (req, res, id) ->
@fetchLevelByIDAndHandleErrors id, req, res, (err, level) =>
feedbackQuery =
2014-01-03 13:32:13 -05:00
creator: mongoose.Types.ObjectId(req.user.id.toString())
'level.original': level.original.toString()
'level.majorVersion': level.version.major
Feedback.findOne(feedbackQuery).exec (err, doc) =>
return @sendDatabaseError(res, err) if err
return @sendNotFoundError(res) unless doc?
@sendSuccess(res, doc)
2014-01-03 13:32:13 -05:00
module.exports = new LevelHandler()