Fixed achievement plugin creation of repeatable achievements. Fixed some bugs in recalculating repeatable achievement exp. Implemented recalculating repeatable achievement gems. Achievement tests pass again.

This commit is contained in:
Nick Winter 2015-01-07 15:03:32 -08:00
parent c54fea929e
commit 4016476c4d
3 changed files with 14 additions and 8 deletions

View file

@ -52,11 +52,11 @@ EarnedAchievementSchema.statics.createForAchievement = (achievement, doc, origin
newAmount = util.getByPath(docObj, proportionalTo) or 0 newAmount = util.getByPath(docObj, proportionalTo) or 0
if previouslyEarnedAchievement if previouslyEarnedAchievement
originalAmount = previouslyEarnedAchievement.get('achievedAmount') or 0 originalAmount = previouslyEarnedAchievement.get('achievedAmount') or 0
else if originalDocObj and not newAmount # This branch could get buggy if unchangedCopy tracking isn't working. else if originalDocObj # This branch could get buggy if unchangedCopy tracking isn't working.
originalAmount = util.getByPath(originalDocObj, proportionalTo) or 0 originalAmount = util.getByPath(originalDocObj, proportionalTo) or 0
else else
originalAmount = 0 originalAmount = 0
#console.log 'original amount is', originalAmount, 'and new amount is', newAmount, 'for', proportionalTo, 'with doc', docObj, 'and previously earned achievement amount', previouslyEarnedAchievement?.get('achievedAmount') #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
if originalAmount isnt newAmount if originalAmount isnt newAmount
expFunction = achievement.getExpFunction() expFunction = achievement.getExpFunction()

View file

@ -6,6 +6,7 @@ EarnedAchievement = require './EarnedAchievement'
User = require '../users/User' User = require '../users/User'
Handler = require '../commons/Handler' Handler = require '../commons/Handler'
LocalMongo = require '../../app/lib/LocalMongo' LocalMongo = require '../../app/lib/LocalMongo'
util = require '../../app/core/utils'
class EarnedAchievementHandler extends Handler class EarnedAchievementHandler extends Handler
modelClass: EarnedAchievement modelClass: EarnedAchievement
@ -65,8 +66,8 @@ class EarnedAchievementHandler extends Handler
return @sendNotFoundError(res, 'Could not find achievement.') return @sendNotFoundError(res, 'Could not find achievement.')
else if not trigger else if not trigger
return @sendNotFoundError(res, 'Could not find trigger.') return @sendNotFoundError(res, 'Could not find trigger.')
else if achievement.get('proportionalTo') else if achievement.get('proportionalTo') and earned
EarnedAchievement.createForAchievement(achievement, trigger, trigger.unchangedCopy, earned, (earnedAchievementDoc) => EarnedAchievement.createForAchievement(achievement, trigger, null, earned, (earnedAchievementDoc) =>
@sendCreated(res, (earnedAchievementDoc or earned)?.toObject()) @sendCreated(res, (earnedAchievementDoc or earned)?.toObject())
) )
else if earned else if earned
@ -223,13 +224,15 @@ class EarnedAchievementHandler extends Handler
notified: achievement._id in alreadyEarnedIDs notified: achievement._id in alreadyEarnedIDs
if isRepeatable if isRepeatable
earned.achievedAmount = something.get(achievement.get 'proportionalTo') earned.achievedAmount = util.getByPath(something.toObject(), achievement.get 'proportionalTo') or 0
earned.previouslyAchievedAmount = 0 earned.previouslyAchievedAmount = 0
expFunction = achievement.getExpFunction() expFunction = achievement.getExpFunction()
newPoints = expFunction(earned.achievedAmount) * achievement.get('worth') ? 10 newPoints = expFunction(earned.achievedAmount) * achievement.get('worth') ? 10
newGems = expFunction(earned.achievedAmount) * (achievement.get('rewards')?.gems ? 0)
else else
newPoints = achievement.get('worth') ? 10 newPoints = achievement.get('worth') ? 10
newGems = achievement.get('rewards')?.gems ? 0
earned.earnedPoints = newPoints earned.earnedPoints = newPoints
newTotalPoints += newPoints newTotalPoints += newPoints
@ -237,7 +240,10 @@ class EarnedAchievementHandler extends Handler
earned.earnedRewards = achievement.get('rewards') earned.earnedRewards = achievement.get('rewards')
for rewardType in ['heroes', 'items', 'levels'] for rewardType in ['heroes', 'items', 'levels']
newTotalRewards[rewardType] = newTotalRewards[rewardType].concat(achievement.get('rewards')?[rewardType] ? []) newTotalRewards[rewardType] = newTotalRewards[rewardType].concat(achievement.get('rewards')?[rewardType] ? [])
newTotalRewards.gems += achievement.get('rewards')?.gems ? 0 if isRepeatable and earned.earnedRewards
earned.earnedRewards = _.clone earned.earnedRewards
earned.earnedRewards.gems = newGems
newTotalRewards.gems += newGems
EarnedAchievement.update {achievement:earned.achievement, user:earned.user}, earned, {upsert: true}, (err) -> EarnedAchievement.update {achievement:earned.achievement, user:earned.user}, earned, {upsert: true}, (err) ->
doneWithAchievement err doneWithAchievement err

View file

@ -258,8 +258,8 @@ describe 'Recalculate Achievements', ->
unittest.getNormalJoe (joe) -> unittest.getNormalJoe (joe) ->
User.findById joe.get('id'), (err, guy) -> User.findById joe.get('id'), (err, guy) ->
expect(err).toBeNull() expect(err).toBeNull()
# TODO: fix this to properly handle recalculating proportionalTo recalculation expect(guy.get 'points').toBe unlockable.worth + 4 * repeatable.worth + (Math.log(.5 * (4 + .5)) + 1) * diminishing.worth
expect(guy.get 'points').toBe unlockable.worth + 2 * repeatable.worth + (Math.log(.5 * (4 + .5)) + 1) * diminishing.worth expect(guy.get('earned').gems).toBe 4 * repeatable.rewards.gems
done() done()
it 'cleaning up test: deleting all Achievements and related', (done) -> it 'cleaning up test: deleting all Achievements and related', (done) ->