2014-06-30 22:16:26 -04:00
|
|
|
mongoose = require 'mongoose'
|
|
|
|
jsonschema = require '../../app/schemas/models/achievement'
|
2014-05-21 13:47:17 -04:00
|
|
|
log = require 'winston'
|
2014-06-14 14:12:17 -04:00
|
|
|
utils = require '../../app/lib/utils'
|
2014-06-09 11:28:35 -04:00
|
|
|
plugins = require('../plugins/plugins')
|
2014-06-13 12:04:25 -04:00
|
|
|
AchievablePlugin = require '../plugins/achievements'
|
2014-05-13 16:46:56 -04:00
|
|
|
|
|
|
|
# `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({
|
2014-05-21 13:47:17 -04:00
|
|
|
userField: String
|
2014-05-13 16:46:56 -04:00
|
|
|
}, {strict: false})
|
|
|
|
|
2014-06-03 06:40:47 -04:00
|
|
|
AchievementSchema.methods.objectifyQuery = ->
|
2014-05-21 13:47:17 -04:00
|
|
|
try
|
2014-06-30 22:16:26 -04:00
|
|
|
@set('query', JSON.parse(@get('query'))) if typeof @get('query') == 'string'
|
2014-05-21 13:47:17 -04:00
|
|
|
catch error
|
2014-05-24 14:45:53 -04:00
|
|
|
log.error "Couldn't convert query string to object because of #{error}"
|
2014-05-21 13:47:17 -04:00
|
|
|
@set('query', {})
|
|
|
|
|
2014-06-03 06:40:47 -04:00
|
|
|
AchievementSchema.methods.stringifyQuery = ->
|
2014-06-30 22:16:26 -04:00
|
|
|
@set('query', JSON.stringify(@get('query'))) if typeof @get('query') != 'string'
|
2014-05-21 13:47:17 -04:00
|
|
|
|
2014-06-14 09:34:23 -04:00
|
|
|
AchievementSchema.methods.getExpFunction = ->
|
2014-06-14 14:12:17 -04:00
|
|
|
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
|
2014-06-03 06:40:47 -04:00
|
|
|
|
2014-06-14 14:12:17 -04:00
|
|
|
AchievementSchema.statics.jsonschema = jsonschema
|
2014-06-14 09:34:23 -04:00
|
|
|
AchievementSchema.statics.achievements = {}
|
|
|
|
|
2014-06-24 07:49:54 -04:00
|
|
|
# Reloads all achievements into memory.
|
|
|
|
# TODO might want to tweak this to only load new achievements
|
2014-06-14 09:34:23 -04:00
|
|
|
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
|
|
|
|
|
2014-06-24 07:49:54 -04:00
|
|
|
# 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-05-21 13:47:17 -04:00
|
|
|
|
2014-06-13 12:04:25 -04:00
|
|
|
AchievementSchema.pre 'save', (next) ->
|
2014-05-21 13:47:17 -04:00
|
|
|
@stringifyQuery()
|
|
|
|
next()
|
2014-06-13 12:04:25 -04:00
|
|
|
|
|
|
|
# Reload achievements upon save
|
2014-06-14 09:34:23 -04:00
|
|
|
AchievementSchema.post 'save', -> @constructor.loadAchievements()
|
2014-05-21 13:47:17 -04:00
|
|
|
|
2014-05-19 17:20:50 -04:00
|
|
|
AchievementSchema.plugin(plugins.NamedPlugin)
|
2014-05-31 17:19:55 -04:00
|
|
|
AchievementSchema.plugin(plugins.SearchablePlugin, {searchable: ['name']})
|
2014-06-09 11:28:35 -04:00
|
|
|
|
2014-07-03 15:20:06 -04:00
|
|
|
module.exports = Achievement = mongoose.model('Achievement', AchievementSchema, 'achievements')
|
2014-06-10 14:05:32 -04:00
|
|
|
|
2014-06-14 09:34:23 -04:00
|
|
|
AchievementSchema.statics.loadAchievements()
|