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 '
2015-03-21 21:49:32 -04:00
config = require ' ../../../server_config '
2014-01-03 13:32:13 -05:00
LevelSessionSchema = new mongoose . Schema ( {
created:
type: Date
' default ' : Date . now
2015-03-21 21:49:32 -04:00
} , { strict: false , read : config . mongo . readpref } )
2015-01-27 13:02:47 -05:00
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 } )
2015-08-15 15:44:19 -04:00
LevelSessionSchema . index ( { creator: 1 , level: 1 } ) # Looks like the ones operating on level as two separate fields might not be working, and sometimes this query uses the "level" index instead of the "creator" index.
2015-01-27 13:02:47 -05:00
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-08-15 09:45:38 -04:00
LevelSessionSchema . index ( { submitted: 1 , team: 1 , level: 1 , totalScore: - 1 } , { name: ' rank counting index ' , sparse: true } )
#LevelSessionSchema.index({level: 1, 'leagues.leagueID': 1, submitted: 1, team: 1, totalScore: -1}, {name: 'league rank counting index', sparse: true}) # needed for league leaderboards?
LevelSessionSchema . index ( { levelID: 1 , submitted: 1 , team: 1 } , { name: ' get all scores index ' , sparse: true } )
#LevelSessionSchema.index({levelID: 1, 'leagues.leagueID': 1, submitted: 1, team: 1}, {name: 'league get all scores index', sparse: true}) # needed for league histograms?
2015-04-12 14:34:19 -04:00
LevelSessionSchema . index ( { submitted: 1 , team: 1 , levelID: 1 , submitDate: - 1 } , { name: ' matchmaking index ' , sparse: true } )
LevelSessionSchema . index ( { submitted: 1 , team: 1 , levelID: 1 , randomSimulationIndex: - 1 } , { name: ' matchmaking random index ' , sparse: true } )
2015-08-15 08:38:47 -04:00
LevelSessionSchema . index ( { ' leagues.leagueID ' : 1 , submitted: 1 , levelID: 1 , team: 1 , randomSimulationIndex: - 1 } , { name: ' league-based matchmaking random index ' , sparse: true } ) # Really need MongoDB 3.2 for partial indexes for this and several others: https://jira.mongodb.org/browse/SERVER-785
2015-01-27 13:02:47 -05:00
2014-01-03 13:32:13 -05:00
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
LevelSessionSchema . post ' init ' , (doc) ->
2015-02-17 23:51:22 -05:00
unless doc . previousStateInfo
doc.previousStateInfo =
' 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
2015-01-26 19:31:26 -05:00
@ set ( ' changed ' , new Date ( ) )
2014-06-24 09:28:18 -04:00
id = @ get ( ' id ' )
2015-02-18 11:39:29 -05:00
initd = @ previousStateInfo ?
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
2015-02-18 11:39:29 -05:00
if not ( initd and @ previousStateInfo [ ' state.complete ' ] ) and @ get ( ' state.complete ' )
2015-02-18 20:08:54 -05:00
User . findByIdAndUpdate userID , { $inc: ' stats.gamesCompleted ' : 1 } , { } , (err, doc) ->
2014-06-24 09:28:18 -04:00
log . error err if err ?
2015-02-18 20:08:54 -05:00
oldCopy = doc . toObject ( )
oldCopy.stats = _ . clone oldCopy . stats
oldCopy.stats.gamesCompleted = oldCopy . stats . gamesCompleted - 1
User . schema . statics . createNewEarnedAchievements doc , oldCopy
2014-12-04 15:57:57 -05:00
activeUserEvent = " level-completed/ #{ levelID } "
# Spent at least 30s playing this level
2015-02-18 11:39:29 -05:00
if not initd and @ get ( ' playtime ' ) >= 30 or initd and ( @ get ( ' playtime ' ) - @ previousStateInfo [ ' playtime ' ] >= 30 )
2014-12-04 15:57:57 -05:00
activeUserEvent = " level-playtime/ #{ levelID } "
2014-06-24 09:28:18 -04:00
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-12-19 20:27:58 -05:00
' chat ' , ' teamSpells ' , ' submitted ' , ' submittedCodeLanguage ' ,
2015-01-30 17:45:34 -05:00
' unsubscribed ' , ' playtime ' , ' heroConfig ' , ' team ' , ' transpiledCode ' ,
' browser ' ]
2014-07-22 14:07:00 -04:00
LevelSessionSchema.statics.jsonSchema = jsonschema
2014-07-03 15:20:06 -04:00
module.exports = LevelSession = mongoose . model ( ' level.session ' , LevelSessionSchema , ' level.sessions ' )