codecombat/server/achievements/Achievement.coffee

70 lines
2.9 KiB
CoffeeScript
Raw Normal View History

2014-06-30 22:16:26 -04:00
mongoose = require 'mongoose'
jsonschema = require '../../app/schemas/models/achievement'
log = require 'winston'
utils = require '../../app/lib/utils'
plugins = require('../plugins/plugins')
2014-06-13 12:04:25 -04:00
AchievablePlugin = require '../plugins/achievements'
# `pre` and `post` are not called for update operations executed directly on the database,
# including `Model.update`,`.findByIdAndUpdate`,`.findOneAndUpdate`, `.findOneAndRemove`,and `.findByIdAndRemove`.order
# to utilize `pre` or `post` middleware, you should `find()` the document, and call the `init`, `validate`, `save`,
# or `remove` functions on the document. See [explanation](http://github.com/LearnBoost/mongoose/issues/964).
AchievementSchema = new mongoose.Schema({
userField: String
}, {strict: false})
AchievementSchema.methods.objectifyQuery = ->
try
2014-06-30 22:16:26 -04:00
@set('query', JSON.parse(@get('query'))) if typeof @get('query') == 'string'
catch error
log.error "Couldn't convert query string to object because of #{error}"
@set('query', {})
AchievementSchema.methods.stringifyQuery = ->
2014-06-30 22:16:26 -04:00
@set('query', JSON.stringify(@get('query'))) if typeof @get('query') != 'string'
AchievementSchema.methods.getExpFunction = ->
kind = @get('function')?.kind or jsonschema.properties.function.default.kind
parameters = @get('function')?.parameters or jsonschema.properties.function.default.parameters
2014-06-13 12:04:25 -04:00
return utils.functionCreators[kind](parameters) if kind of utils.functionCreators
AchievementSchema.statics.jsonschema = jsonschema
AchievementSchema.statics.achievements = {}
# Reloads all achievements into memory.
# TODO might want to tweak this to only load new achievements
AchievementSchema.statics.loadAchievements = (done) ->
AchievementSchema.statics.resetAchievements()
Achievement = require('../achievements/Achievement')
query = Achievement.find({})
query.exec (err, docs) ->
_.each docs, (achievement) ->
category = achievement.get 'collection'
AchievementSchema.statics.achievements[category] = [] unless category of AchievementSchema.statics.achievements
AchievementSchema.statics.achievements[category].push achievement
done(AchievementSchema.statics.achievements) if done?
AchievementSchema.statics.getLoadedAchievements = ->
AchievementSchema.statics.achievements
AchievementSchema.statics.resetAchievements = ->
delete AchievementSchema.statics.achievements[category] for category of AchievementSchema.statics.achievements
# Queries are stored as JSON strings, objectify them upon loading
2014-06-13 12:04:25 -04:00
AchievementSchema.post 'init', (doc) -> doc.objectifyQuery()
2014-06-13 12:04:25 -04:00
AchievementSchema.pre 'save', (next) ->
@stringifyQuery()
next()
2014-06-13 12:04:25 -04:00
# Reload achievements upon save
AchievementSchema.post 'save', -> @constructor.loadAchievements()
AchievementSchema.plugin(plugins.NamedPlugin)
AchievementSchema.plugin(plugins.SearchablePlugin, {searchable: ['name']})
module.exports = Achievement = mongoose.model('Achievement', AchievementSchema, 'achievements')
2014-06-10 14:05:32 -04:00
AchievementSchema.statics.loadAchievements()