codecombat/server/levels/sessions/LevelSession.coffee

58 lines
2.2 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
# TODO: not updated since rename from level_instance, or since we redid how all models are done; probably busted
2014-06-30 22:16:26 -04:00
mongoose = require 'mongoose'
plugins = require '../../plugins/plugins'
AchievablePlugin = require '../../plugins/achievements'
2014-06-30 22:16:26 -04:00
jsonschema = require '../../../app/schemas/models/level_session'
log = require 'winston'
2014-01-03 13:32:13 -05:00
LevelSessionSchema = new mongoose.Schema({
created:
type: Date
'default': Date.now
}, {strict: false})
LevelSessionSchema.plugin(plugins.PermissionsPlugin)
LevelSessionSchema.plugin(AchievablePlugin)
2014-01-03 13:32:13 -05:00
previous = {}
LevelSessionSchema.post 'init', (doc) ->
previous[doc.get 'id'] =
'state.complete': doc.get 'state.complete'
'playtime': doc.get 'playtime'
2014-01-03 13:32:13 -05:00
LevelSessionSchema.pre 'save', (next) ->
User = require '../../users/User' # Avoid mutual inclusion cycles
2014-01-03 13:32:13 -05:00
@set('changed', new Date())
id = @get('id')
initd = id of previous
levelID = @get('levelID')
userID = @get('creator')
activeUserEvent = null
# Newly completed level
if not (initd and previous[id]['state']?['complete']) and @get('state.complete')
User.update {_id: userID}, {$inc: 'stats.gamesCompleted': 1}, {}, (err, count) ->
log.error err if err?
activeUserEvent = "level-completed/#{levelID}"
# Spent at least 30s playing this level
if not initd and @get('playtime') >= 30 or initd and (@get('playtime') - previous[id]['playtime'] >= 30)
activeUserEvent = "level-playtime/#{levelID}"
delete previous[id] if initd
if activeUserEvent?
User.saveActiveUser userID, activeUserEvent, next
else
next()
2014-01-03 13:32:13 -05:00
LevelSessionSchema.statics.privateProperties = ['code', 'submittedCode', 'unsubscribed']
LevelSessionSchema.statics.editableProperties = ['multiplayer', 'players', 'code', 'codeLanguage', 'completed', 'state',
'levelName', 'creatorName', 'levelID', 'screenshot',
'chat', 'teamSpells', 'submitted', 'submittedCodeLanguage',
2014-11-22 23:48:04 -05:00
'unsubscribed', 'playtime', 'heroConfig', 'team', 'transpiledCode']
LevelSessionSchema.statics.jsonSchema = jsonschema
module.exports = LevelSession = mongoose.model('level.session', LevelSessionSchema, 'level.sessions')