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'
|
2016-08-25 17:30:52 -04:00
|
|
|
co = require 'co'
|
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
|
|
|
|
2016-08-25 17:30:52 -04:00
|
|
|
EarnedAchievementSchema.statics.createForAchievement = co.wrap (achievement, doc, options={}) ->
|
|
|
|
{ previouslyEarnedAchievement, originalDocObj } = options
|
|
|
|
|
|
|
|
User = require('./User')
|
2014-11-21 01:08:49 -05:00
|
|
|
userObjectID = doc.get(achievement.get('userField'))
|
2016-08-25 17:30:52 -04:00
|
|
|
userID = if _.isObject userObjectID then userObjectID.toHexString() else userObjectID # Standardize! Use ObjectIds
|
2014-11-21 01:08:49 -05:00
|
|
|
|
2016-08-25 17:30:52 -04:00
|
|
|
earnedAttrs = {
|
2014-11-21 01:08:49 -05:00
|
|
|
user: userID
|
|
|
|
achievement: achievement._id.toHexString()
|
|
|
|
achievementName: achievement.get 'name'
|
|
|
|
earnedRewards: achievement.get 'rewards'
|
2016-08-25 17:30:52 -04:00
|
|
|
}
|
2014-11-21 01:08:49 -05:00
|
|
|
|
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
|
2016-08-25 17:30:52 -04:00
|
|
|
earnedDoc = null
|
2014-11-21 01:08:49 -05:00
|
|
|
|
|
|
|
isRepeatable = achievement.get('proportionalTo')?
|
2016-08-25 17:30:52 -04:00
|
|
|
|
2014-11-21 01:08:49 -05:00
|
|
|
if isRepeatable
|
2016-08-25 17:30:52 -04:00
|
|
|
proportionalTo = achievement.get('proportionalTo')
|
2014-11-21 01:08:49 -05:00
|
|
|
docObj = doc.toObject()
|
2015-01-07 00:32:58 -05:00
|
|
|
newAmount = util.getByPath(docObj, proportionalTo) or 0
|
2015-02-13 19:33:03 -05:00
|
|
|
|
|
|
|
if proportionalTo is 'simulatedBy' and newAmount > 0 and not previouslyEarnedAchievement and Math.random() < 0.1
|
|
|
|
# Because things like simulatedBy get updated with $inc and not the post-save plugin hook,
|
|
|
|
# we (infrequently) fetch the previously earned achievement so we can really update.
|
2016-08-25 17:30:52 -04:00
|
|
|
previouslyEarnedAchievement = yield EarnedAchievement.findOne({user: earnedAttrs.user, achievement: earnedAttrs.achievement})
|
|
|
|
|
|
|
|
if previouslyEarnedAchievement
|
|
|
|
originalAmount = previouslyEarnedAchievement.get('achievedAmount') or 0
|
2015-02-13 19:33:03 -05:00
|
|
|
else if originalDocObj # This branch could get buggy if unchangedCopy tracking isn't working.
|
2016-08-25 17:30:52 -04:00
|
|
|
originalAmount = util.getByPath(originalDocObj, proportionalTo) or 0
|
2015-01-06 21:14:19 -05:00
|
|
|
else
|
2016-08-25 17:30:52 -04:00
|
|
|
originalAmount = 0
|
|
|
|
|
|
|
|
if originalAmount isnt newAmount
|
|
|
|
expFunction = achievement.getExpFunction()
|
|
|
|
earnedAttrs.notified = false
|
|
|
|
earnedAttrs.achievedAmount = newAmount
|
|
|
|
earnedPoints = earnedAttrs.earnedPoints = (expFunction(newAmount) - expFunction(originalAmount)) * pointWorth
|
|
|
|
earnedGems = earnedAttrs.earnedGems = (expFunction(newAmount) - expFunction(originalAmount)) * gemWorth ? 0
|
|
|
|
earnedAttrs.previouslyAchievedAmount = originalAmount
|
|
|
|
yield EarnedAchievement.update({achievement: earnedAttrs.achievement, user: earnedAttrs.user}, earnedAttrs, {upsert: true})
|
|
|
|
earnedDoc = new EarnedAchievement(earnedAttrs)
|
2014-11-21 01:08:49 -05:00
|
|
|
|
|
|
|
else # not alreadyAchieved
|
2016-08-25 17:30:52 -04:00
|
|
|
earnedAttrs.earnedPoints = pointWorth
|
|
|
|
earnedAttrs.earnedGems = gemWorth
|
|
|
|
earnedDoc = new EarnedAchievement(earnedAttrs)
|
|
|
|
yield earnedDoc.save()
|
|
|
|
earnedPoints = pointWorth
|
|
|
|
earnedGems = gemWorth
|
|
|
|
|
|
|
|
User.saveActiveUser(userID, "achievement")
|
|
|
|
|
|
|
|
if earnedDoc
|
|
|
|
update = {$inc: {points: earnedPoints, 'earned.gems': earnedGems}}
|
|
|
|
for rewardType, rewards of achievement.get('rewards') ? {}
|
|
|
|
continue if rewardType is 'gems'
|
|
|
|
if rewards.length
|
|
|
|
update.$addToSet ?= {}
|
|
|
|
update.$addToSet["earned.#{rewardType}"] = $each: rewards
|
|
|
|
yield User.update({_id: mongoose.Types.ObjectId(userID)}, update, {})
|
|
|
|
|
|
|
|
return earnedDoc
|
2014-12-04 15:57:57 -05:00
|
|
|
|
2014-06-03 06:40:47 -04:00
|
|
|
module.exports = EarnedAchievement = mongoose.model('EarnedAchievement', EarnedAchievementSchema)
|