mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-23 23:58:02 -05:00
Increment user's concept stats on level completion
This commit is contained in:
parent
45e5e975f1
commit
34715befcc
4 changed files with 25 additions and 7 deletions
|
@ -280,6 +280,7 @@ _.extend UserSchema.properties,
|
|||
pollMiscPatches: c.int()
|
||||
campaignTranslationPatches: c.int()
|
||||
campaignMiscPatches: c.int()
|
||||
concepts: {type: 'object', additionalProperties: c.int(), description: 'Number of levels completed using each programming concept.'}
|
||||
|
||||
earned: c.RewardSchema 'earned by achievements'
|
||||
purchased: c.RewardSchema 'purchased with gems or money'
|
||||
|
|
|
@ -106,6 +106,7 @@ module.exports = class SpellView extends CocoView
|
|||
@ace.setAnimatedScroll true
|
||||
@ace.setShowFoldWidgets false
|
||||
@ace.setKeyboardHandler @keyBindings[aceConfig.keyBindings ? 'default']
|
||||
@ace.$blockScrolling = Infinity
|
||||
@toggleControls null, @writable
|
||||
@aceSession.selection.on 'changeCursor', @onCursorActivity
|
||||
$(@ace.container).find('.ace_gutter').on 'click mouseenter', '.ace_error, .ace_warning, .ace_info', @onAnnotationClick
|
||||
|
@ -459,7 +460,7 @@ module.exports = class SpellView extends CocoView
|
|||
entry.captureReturn = switch e.language
|
||||
when 'io' then varName + ' := '
|
||||
when 'javascript' then 'var ' + varName + ' = '
|
||||
when 'clojure' then '(let [' + varName + ' '
|
||||
when 'clojure' then '(let [' + varName + ' '
|
||||
else varName + ' = '
|
||||
|
||||
# TODO: Generalize this snippet replacement
|
||||
|
|
|
@ -44,6 +44,7 @@ LevelSessionSchema.post 'init', (doc) ->
|
|||
|
||||
LevelSessionSchema.pre 'save', (next) ->
|
||||
User = require '../../users/User' # Avoid mutual inclusion cycles
|
||||
Level = require '../Level'
|
||||
@set('changed', new Date())
|
||||
|
||||
id = @get('id')
|
||||
|
@ -54,12 +55,20 @@ LevelSessionSchema.pre 'save', (next) ->
|
|||
|
||||
# Newly completed level
|
||||
if not (initd and @previousStateInfo['state.complete']) and @get('state.complete')
|
||||
User.findByIdAndUpdate userID, {$inc: 'stats.gamesCompleted': 1}, {}, (err, doc) ->
|
||||
Level.findOne({slug: levelID}).select('concepts -_id').lean().exec (err, level) ->
|
||||
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
|
||||
update = $inc: {'stats.gamesCompleted': 1}
|
||||
for concept in level?.concepts ? []
|
||||
update.$inc["stats.concepts.#{concept}"] = 1
|
||||
User.findByIdAndUpdate userID, update, {}, (err, user) ->
|
||||
log.error err if err?
|
||||
oldCopy = user.toObject()
|
||||
oldCopy.stats = _.clone oldCopy.stats
|
||||
--oldCopy.stats.gamesCompleted
|
||||
oldCopy.stats.concepts ?= {}
|
||||
for concept in level?.concepts ? []
|
||||
--oldCopy.stats.concepts[concept]
|
||||
User.schema.statics.createNewEarnedAchievements user, oldCopy
|
||||
activeUserEvent = "level-completed/#{levelID}"
|
||||
|
||||
# Spent at least 30s playing this level
|
||||
|
|
|
@ -192,7 +192,14 @@ UserSchema.statics.incrementStat = (id, statName, done, inc=1) ->
|
|||
user.incrementStat statName, done, inc
|
||||
|
||||
UserSchema.methods.incrementStat = (statName, done, inc=1) ->
|
||||
@set statName, (@get(statName) or 0) + inc
|
||||
if /^concepts\./.test statName
|
||||
# Concept stats are nested a level deeper.
|
||||
concepts = @get('concepts') or {}
|
||||
concept = statName.split('.')[1]
|
||||
concepts[concept] = (concepts[concept] or 0) + inc
|
||||
@set 'concepts', concepts
|
||||
else
|
||||
@set statName, (@get(statName) or 0) + inc
|
||||
@save (err) -> done?(err)
|
||||
|
||||
UserSchema.statics.unconflictName = unconflictName = (name, done) ->
|
||||
|
|
Loading…
Reference in a new issue