2014-05-18 07:21:27 -04:00
|
|
|
mongoose = require 'mongoose'
|
|
|
|
jsonschema = require '../../app/schemas/models/earned_achievement'
|
2014-11-28 20:49:41 -05:00
|
|
|
util = require '../../app/core/utils'
|
2014-11-21 13:04:50 -05:00
|
|
|
log = require 'winston'
|
2014-05-18 07:21:27 -04:00
|
|
|
|
|
|
|
EarnedAchievementSchema = new mongoose.Schema({
|
|
|
|
notified:
|
|
|
|
type: Boolean
|
|
|
|
default: false
|
|
|
|
}, {strict:false})
|
|
|
|
|
2014-06-03 06:40:47 -04:00
|
|
|
EarnedAchievementSchema.pre 'save', (next) ->
|
2015-01-08 14:28:52 -05:00
|
|
|
@set('changed', new Date())
|
2014-06-03 06:40:47 -04:00
|
|
|
next()
|
|
|
|
|
2014-05-19 19:25:41 -04:00
|
|
|
EarnedAchievementSchema.index({user: 1, achievement: 1}, {unique: true, name: 'earned achievement index'})
|
2014-05-24 14:45:53 -04:00
|
|
|
EarnedAchievementSchema.index({user: 1, changed: -1}, {name: 'latest '})
|
2014-05-19 19:25:41 -04:00
|
|
|
|
2015-01-07 15:25:31 -05:00
|
|
|
EarnedAchievementSchema.statics.createForAchievement = (achievement, doc, originalDocObj=null, previouslyEarnedAchievement=null, done) ->
|
2014-11-21 01:08:49 -05:00
|
|
|
User = require '../users/User'
|
|
|
|
userObjectID = doc.get(achievement.get('userField'))
|
|
|
|
userID = if _.isObject userObjectID then userObjectID.toHexString() else userObjectID # Standardize! Use strings, not ObjectId's
|
|
|
|
|
|
|
|
earned =
|
|
|
|
user: userID
|
|
|
|
achievement: achievement._id.toHexString()
|
|
|
|
achievementName: achievement.get 'name'
|
|
|
|
earnedRewards: achievement.get 'rewards'
|
|
|
|
|
2015-01-06 21:14:19 -05:00
|
|
|
pointWorth = achievement.get('worth') ? 10
|
|
|
|
gemWorth = achievement.get('rewards')?.gems ? 0
|
2014-11-21 01:08:49 -05:00
|
|
|
earnedPoints = 0
|
2015-01-06 21:14:19 -05:00
|
|
|
earnedGems = 0
|
2015-01-07 00:32:58 -05:00
|
|
|
|
2014-11-21 01:08:49 -05:00
|
|
|
wrapUp = (earnedAchievementDoc) ->
|
|
|
|
# Update user's experience points
|
2015-01-06 21:14:19 -05:00
|
|
|
update = {$inc: {points: earnedPoints, 'earned.gems': earnedGems}}
|
2014-11-21 01:08:49 -05:00
|
|
|
for rewardType, rewards of achievement.get('rewards') ? {}
|
2015-01-06 21:14:19 -05:00
|
|
|
continue if rewardType is 'gems'
|
|
|
|
if rewards.length
|
2014-11-21 01:08:49 -05:00
|
|
|
update.$addToSet ?= {}
|
|
|
|
update.$addToSet["earned.#{rewardType}"] = $each: rewards
|
2015-01-06 21:14:19 -05:00
|
|
|
User.update {_id: mongoose.Types.ObjectId(userID)}, update, {}, (err, count) ->
|
2014-11-21 01:08:49 -05:00
|
|
|
log.error err if err?
|
|
|
|
done?(earnedAchievementDoc)
|
|
|
|
|
|
|
|
isRepeatable = achievement.get('proportionalTo')?
|
|
|
|
if isRepeatable
|
|
|
|
#log.debug 'Upserting repeatable achievement called \'' + (achievement.get 'name') + '\' for ' + userID
|
|
|
|
proportionalTo = achievement.get 'proportionalTo'
|
|
|
|
docObj = doc.toObject()
|
2015-01-07 00:32:58 -05:00
|
|
|
newAmount = util.getByPath(docObj, proportionalTo) or 0
|
2015-01-07 15:25:31 -05:00
|
|
|
if previouslyEarnedAchievement
|
|
|
|
originalAmount = previouslyEarnedAchievement.get('achievedAmount') or 0
|
2015-01-07 18:03:32 -05:00
|
|
|
else if originalDocObj # This branch could get buggy if unchangedCopy tracking isn't working.
|
2015-01-07 15:25:31 -05:00
|
|
|
originalAmount = util.getByPath(originalDocObj, proportionalTo) or 0
|
|
|
|
else
|
|
|
|
originalAmount = 0
|
2015-01-07 18:03:32 -05:00
|
|
|
#console.log 'original amount is', originalAmount, 'and new amount is', newAmount, 'for', proportionalTo, 'with doc', docObj, 'and previously earned achievement amount', previouslyEarnedAchievement?.get('achievedAmount'), 'because we had originalDocObj', originalDocObj
|
2014-11-21 01:08:49 -05:00
|
|
|
|
|
|
|
if originalAmount isnt newAmount
|
|
|
|
expFunction = achievement.getExpFunction()
|
|
|
|
earned.notified = false
|
|
|
|
earned.achievedAmount = newAmount
|
2015-01-07 15:25:31 -05:00
|
|
|
#console.log 'earnedPoints is', (expFunction(newAmount) - expFunction(originalAmount)) * pointWorth, 'was', earned.earnedPoints, earned.previouslyAchievedAmount, 'got exp function for new amount', newAmount, expFunction(newAmount), 'for original amount', originalAmount, expFunction(originalAmount), 'with point worth', pointWorth
|
2015-01-06 21:14:19 -05:00
|
|
|
earnedPoints = earned.earnedPoints = (expFunction(newAmount) - expFunction(originalAmount)) * pointWorth
|
|
|
|
earnedGems = earned.earnedGems = (expFunction(newAmount) - expFunction(originalAmount)) * gemWorth
|
2014-11-21 01:08:49 -05:00
|
|
|
earned.previouslyAchievedAmount = originalAmount
|
|
|
|
EarnedAchievement.update {achievement: earned.achievement, user: earned.user}, earned, {upsert: true}, (err) ->
|
|
|
|
return log.debug err if err?
|
|
|
|
|
|
|
|
#log.debug earnedPoints
|
2015-01-06 21:14:19 -05:00
|
|
|
wrapUp(new EarnedAchievement(earned))
|
|
|
|
else
|
|
|
|
done?()
|
2014-11-21 01:08:49 -05:00
|
|
|
|
|
|
|
else # not alreadyAchieved
|
|
|
|
#log.debug 'Creating a new earned achievement called \'' + (achievement.get 'name') + '\' for ' + userID
|
2015-01-06 21:14:19 -05:00
|
|
|
earned.earnedPoints = pointWorth
|
|
|
|
earned.earnedGems = gemWorth
|
2014-11-21 01:08:49 -05:00
|
|
|
(new EarnedAchievement(earned)).save (err, doc) ->
|
|
|
|
return log.error err if err?
|
2015-01-06 21:14:19 -05:00
|
|
|
earnedPoints = pointWorth
|
|
|
|
earnedGems = gemWorth
|
2014-11-21 13:04:50 -05:00
|
|
|
wrapUp(doc)
|
2014-11-21 01:08:49 -05:00
|
|
|
|
2014-12-04 15:57:57 -05:00
|
|
|
User.saveActiveUser userID, "achievement"
|
|
|
|
|
2014-06-03 06:40:47 -04:00
|
|
|
module.exports = EarnedAchievement = mongoose.model('EarnedAchievement', EarnedAchievementSchema)
|