codecombat/server/levels/sessions/LevelSession.coffee

77 lines
3.6 KiB
CoffeeScript
Raw Normal View History

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,read:'nearest'})
LevelSessionSchema.index({creator: 1})
LevelSessionSchema.index({level: 1})
LevelSessionSchema.index({levelID: 1})
LevelSessionSchema.index({'level.majorVersion': 1})
LevelSessionSchema.index({'level.original': 1}, {name: 'Level Original'})
LevelSessionSchema.index({'level.original': 1, 'level.majorVersion': 1, 'creator': 1, 'team': 1})
LevelSessionSchema.index({playtime: 1}, {name: 'Playtime'})
LevelSessionSchema.index({submitDate: 1})
LevelSessionSchema.index({submitted: 1}, {sparse: true})
LevelSessionSchema.index({team: 1}, {sparse: true})
LevelSessionSchema.index({totalScore: 1}, {sparse: true})
LevelSessionSchema.index({user: 1, changed: -1}, {name: 'last played index', sparse: true})
2015-01-31 00:36:36 -05:00
LevelSessionSchema.index({'level.original': 1, 'state.topScores.type': 1, 'state.topScores.date': -1, 'state.topScores.score': -1}, {name: 'top scores index', sparse: true})
2015-03-04 00:04:14 -05:00
LevelSessionSchema.index({submitted: 1, team: 1, level:1, totalScore: -1}, {name: 'rank counting index', sparse:true})
LevelSessionSchema.index({levelID: 1, submitted:1, team: 1},{name:'get all scores index',sparse:true})
LevelSessionSchema.index({submitted: 1, team: 1, levelID: 1, submitDate: -1}, {name: 'matchmaking index', sparse:true})
2014-01-03 13:32:13 -05:00
LevelSessionSchema.plugin(plugins.PermissionsPlugin)
LevelSessionSchema.plugin(AchievablePlugin)
2014-01-03 13:32:13 -05:00
LevelSessionSchema.post 'init', (doc) ->
unless doc.previousStateInfo
doc.previousStateInfo =
'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
@set('changed', new Date())
id = @get('id')
initd = @previousStateInfo?
levelID = @get('levelID')
userID = @get('creator')
activeUserEvent = null
# Newly completed level
if not (initd and @previousStateInfo['state.complete']) and @get('state.complete')
User.findByIdAndUpdate userID, {$inc: 'stats.gamesCompleted': 1}, {}, (err, doc) ->
log.error err if err?
oldCopy = doc.toObject()
oldCopy.stats = _.clone oldCopy.stats
oldCopy.stats.gamesCompleted = oldCopy.stats.gamesCompleted - 1
User.schema.statics.createNewEarnedAchievements doc, oldCopy
activeUserEvent = "level-completed/#{levelID}"
# Spent at least 30s playing this level
if not initd and @get('playtime') >= 30 or initd and (@get('playtime') - @previousStateInfo['playtime'] >= 30)
activeUserEvent = "level-playtime/#{levelID}"
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',
2015-01-30 17:45:34 -05:00
'unsubscribed', 'playtime', 'heroConfig', 'team', 'transpiledCode',
'browser']
LevelSessionSchema.statics.jsonSchema = jsonschema
module.exports = LevelSession = mongoose.model('level.session', LevelSessionSchema, 'level.sessions')