2014-07-01 10:16:26 +08:00
|
|
|
mongoose = require 'mongoose'
|
2016-04-06 10:56:06 -07:00
|
|
|
EarnedAchievement = require '../models/EarnedAchievement'
|
2014-05-18 13:21:27 +02:00
|
|
|
LocalMongo = require '../../app/lib/LocalMongo'
|
2014-11-28 17:49:41 -08:00
|
|
|
util = require '../../app/core/utils'
|
2014-05-31 23:55:26 +02:00
|
|
|
log = require 'winston'
|
2014-05-13 22:46:56 +02:00
|
|
|
|
2014-06-14 20:12:17 +02:00
|
|
|
# Warning: To ensure proper functioning one must always `find` documents before saving them.
|
|
|
|
# Otherwise the schema's `post init` won't be triggered and the plugin can't keep track of changes
|
|
|
|
# TODO if this is still a common scenario I could implement a database hit after all, but only
|
|
|
|
# on the condition that it's necessary and still not too frequent in occurrence
|
2014-06-14 15:34:23 +02:00
|
|
|
AchievablePlugin = (schema, options) ->
|
2016-04-06 10:56:06 -07:00
|
|
|
User = require '../models/User' # Avoid mutual inclusion cycles
|
|
|
|
Achievement = require '../models/Achievement'
|
2014-06-09 12:26:47 +02:00
|
|
|
|
2014-07-03 21:20:06 +02:00
|
|
|
# Keep track the document before it's saved
|
2014-05-13 22:46:56 +02:00
|
|
|
schema.post 'init', (doc) ->
|
2015-01-07 12:25:31 -08:00
|
|
|
unless doc.unchangedCopy
|
|
|
|
doc.unchangedCopy = doc.toObject()
|
2014-05-18 17:24:21 +02:00
|
|
|
|
2014-07-03 21:20:06 +02:00
|
|
|
# Check if an achievement has been earned
|
2014-05-13 22:46:56 +02:00
|
|
|
schema.post 'save', (doc) ->
|
2015-02-18 17:08:54 -08:00
|
|
|
schema.statics.createNewEarnedAchievements doc
|
2014-05-18 13:21:27 +02:00
|
|
|
|
2015-02-18 17:08:54 -08:00
|
|
|
schema.statics.createNewEarnedAchievements = (doc, unchangedCopy) ->
|
|
|
|
unchangedCopy ?= doc.unchangedCopy
|
|
|
|
isNew = not doc.isInit('_id') or not unchangedCopy
|
|
|
|
|
|
|
|
if doc.isInit('_id') and not unchangedCopy
|
2014-06-14 20:12:17 +02:00
|
|
|
log.warn 'document was already initialized but did not go through `init` and is therefore treated as new while it might not be'
|
|
|
|
|
2014-06-24 13:49:54 +02:00
|
|
|
category = doc.constructor.collection.name
|
2014-06-14 15:34:23 +02:00
|
|
|
loadedAchievements = Achievement.getLoadedAchievements()
|
2014-05-18 13:21:27 +02:00
|
|
|
|
2014-06-14 15:34:23 +02:00
|
|
|
if category of loadedAchievements
|
2015-02-18 17:08:54 -08:00
|
|
|
#log.debug 'about to save ' + category + ', number of achievements is ' + loadedAchievements[category].length
|
2014-05-18 13:21:27 +02:00
|
|
|
docObj = doc.toObject()
|
2014-06-14 15:34:23 +02:00
|
|
|
for achievement in loadedAchievements[category]
|
2014-09-30 16:18:32 -07:00
|
|
|
do (achievement) ->
|
|
|
|
query = achievement.get('query')
|
2015-02-13 16:33:03 -08:00
|
|
|
return log.error("Empty achievement query for #{achievement.get('name')}.") if _.isEmpty query
|
2014-09-30 16:18:32 -07:00
|
|
|
isRepeatable = achievement.get('proportionalTo')?
|
2015-02-18 17:08:54 -08:00
|
|
|
alreadyAchieved = if isNew then false else LocalMongo.matchesQuery unchangedCopy, query
|
2014-09-30 16:18:32 -07:00
|
|
|
newlyAchieved = LocalMongo.matchesQuery(docObj, query)
|
2014-11-20 22:08:49 -08:00
|
|
|
return unless newlyAchieved and (not alreadyAchieved or isRepeatable)
|
2015-02-13 16:33:03 -08:00
|
|
|
#log.info "Making an achievement: #{achievement.get('name')} #{achievement.get('_id')} for doc: #{doc.get('name')} #{doc.get('_id')}"
|
2015-02-18 17:08:54 -08:00
|
|
|
EarnedAchievement.createForAchievement(achievement, doc, unchangedCopy)
|
2014-06-10 20:05:32 +02:00
|
|
|
|
2014-06-14 15:34:23 +02:00
|
|
|
module.exports = AchievablePlugin
|