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'
|
2014-05-13 16:46:56 -04:00
|
|
|
AchievablePlugin = require '../../plugins/achievements'
|
2014-06-30 22:16:26 -04:00
|
|
|
jsonschema = require '../../../app/schemas/models/level_session'
|
2014-06-24 09:28:18 -04:00
|
|
|
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)
|
2014-05-13 16:46:56 -04:00
|
|
|
LevelSessionSchema.plugin(AchievablePlugin)
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-06-24 09:28:18 -04:00
|
|
|
previous = {}
|
|
|
|
|
|
|
|
LevelSessionSchema.post 'init', (doc) ->
|
|
|
|
previous[doc.get 'id'] =
|
2014-12-04 15:57:57 -05:00
|
|
|
'state.complete': doc.get 'state.complete'
|
|
|
|
'playtime': doc.get 'playtime'
|
2014-06-24 09:28:18 -04:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
LevelSessionSchema.pre 'save', (next) ->
|
2014-12-04 15:57:57 -05:00
|
|
|
User = require '../../users/User' # Avoid mutual inclusion cycles
|
2014-01-03 13:32:13 -05:00
|
|
|
@set('changed', new Date())
|
2014-06-24 09:28:18 -04:00
|
|
|
|
|
|
|
id = @get('id')
|
|
|
|
initd = id of previous
|
2014-12-04 15:57:57 -05:00
|
|
|
levelID = @get('levelID')
|
|
|
|
userID = @get('creator')
|
|
|
|
activeUserEvent = null
|
2014-06-24 09:28:18 -04:00
|
|
|
|
2014-12-04 15:57:57 -05:00
|
|
|
# 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) ->
|
2014-06-24 09:28:18 -04:00
|
|
|
log.error err if err?
|
2014-12-04 15:57:57 -05:00
|
|
|
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}"
|
2014-06-24 09:28:18 -04:00
|
|
|
|
|
|
|
delete previous[id] if initd
|
2014-12-04 15:57:57 -05:00
|
|
|
if activeUserEvent?
|
|
|
|
User.saveActiveUser userID, activeUserEvent, next
|
|
|
|
else
|
|
|
|
next()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-07-22 14:07:00 -04:00
|
|
|
LevelSessionSchema.statics.privateProperties = ['code', 'submittedCode', 'unsubscribed']
|
|
|
|
LevelSessionSchema.statics.editableProperties = ['multiplayer', 'players', 'code', 'codeLanguage', 'completed', 'state',
|
|
|
|
'levelName', 'creatorName', 'levelID', 'screenshot',
|
2014-11-17 18:07:10 -05:00
|
|
|
'chat', 'teamSpells', 'submitted', 'submittedCodeLanguage',
|
2014-11-22 23:48:04 -05:00
|
|
|
'unsubscribed', 'playtime', 'heroConfig', 'team', 'transpiledCode']
|
2014-07-22 14:07:00 -04:00
|
|
|
LevelSessionSchema.statics.jsonSchema = jsonschema
|
|
|
|
|
2014-07-16 06:39:18 -04:00
|
|
|
LevelSessionSchema.index {user: 1, changed: -1}, {sparse: true, name: 'last played index'}
|
|
|
|
|
2014-07-03 15:20:06 -04:00
|
|
|
module.exports = LevelSession = mongoose.model('level.session', LevelSessionSchema, 'level.sessions')
|