2014-06-03 10:14:10 -04:00
|
|
|
log = require 'winston'
|
2014-06-03 16:54:56 -04:00
|
|
|
mongoose = require 'mongoose'
|
|
|
|
async = require 'async'
|
|
|
|
Achievement = require './Achievement'
|
2014-05-18 10:51:23 -04:00
|
|
|
EarnedAchievement = require './EarnedAchievement'
|
2014-06-03 16:54:56 -04:00
|
|
|
User = require '../users/User'
|
2014-05-18 10:51:23 -04:00
|
|
|
Handler = require '../commons/Handler'
|
2014-06-03 16:54:56 -04:00
|
|
|
LocalMongo = require '../../app/lib/LocalMongo'
|
2014-05-18 10:51:23 -04:00
|
|
|
|
|
|
|
class EarnedAchievementHandler extends Handler
|
|
|
|
modelClass: EarnedAchievement
|
|
|
|
|
|
|
|
# Don't allow POSTs or anything yet
|
|
|
|
hasAccess: (req) ->
|
2014-06-03 16:54:56 -04:00
|
|
|
req.method is 'GET' # or req.user.isAdmin()
|
2014-05-18 10:51:23 -04:00
|
|
|
|
2014-06-03 10:14:10 -04:00
|
|
|
recalculate: (req, res) ->
|
2014-06-08 18:33:06 -04:00
|
|
|
onSuccess = (data) => log.debug "Finished recalculating achievements"
|
|
|
|
if 'achievements' of req.body # Support both slugs and IDs separated by commas
|
|
|
|
achievementSlugsOrIDs = req.body.achievements
|
2014-06-03 16:54:56 -04:00
|
|
|
EarnedAchievementHandler.recalculate achievementSlugsOrIDs, onSuccess
|
|
|
|
else
|
|
|
|
EarnedAchievementHandler.recalculate onSuccess
|
2014-06-24 12:14:26 -04:00
|
|
|
@sendAccepted res, {}
|
2014-06-03 16:54:56 -04:00
|
|
|
|
2014-07-03 15:20:06 -04:00
|
|
|
@recalculate: (callbackOrSlugsOrIDs, callback) ->
|
|
|
|
if _.isArray callbackOrSlugsOrIDs # slugs or ids
|
2014-06-04 14:47:32 -04:00
|
|
|
achievementSlugs = (thing for thing in callbackOrSlugsOrIDs when not Handler.isID(thing))
|
|
|
|
achievementIDs = (thing for thing in callbackOrSlugsOrIDs when Handler.isID(thing))
|
2014-07-03 15:20:06 -04:00
|
|
|
else # just a callback
|
|
|
|
callback = callbackOrSlugsOrIDs
|
|
|
|
onFinished = -> callback arguments... if callback?
|
2014-06-03 16:54:56 -04:00
|
|
|
|
|
|
|
filter = {}
|
|
|
|
filter.$or = [
|
2014-06-04 14:47:32 -04:00
|
|
|
{_id: $in: achievementIDs},
|
|
|
|
{slug: $in: achievementSlugs}
|
2014-06-03 16:54:56 -04:00
|
|
|
] if achievementSlugs? or achievementIDs?
|
|
|
|
|
2014-06-08 18:33:06 -04:00
|
|
|
# Fetch all relevant achievements
|
2014-06-03 16:54:56 -04:00
|
|
|
Achievement.find filter, (err, achievements) ->
|
2014-07-03 15:20:06 -04:00
|
|
|
log.error err if err?
|
2014-06-08 18:33:06 -04:00
|
|
|
|
|
|
|
# Fetch every single user
|
2014-06-03 16:54:56 -04:00
|
|
|
User.find {}, (err, users) ->
|
2014-07-03 15:20:06 -04:00
|
|
|
async.each users, ((user, doneWithUser) ->
|
2014-06-04 14:47:32 -04:00
|
|
|
# Keep track of a user's already achieved in order to set the notified values correctly
|
2014-06-03 16:54:56 -04:00
|
|
|
userID = user.get('_id').toHexString()
|
2014-06-08 18:33:06 -04:00
|
|
|
|
|
|
|
# Fetch all of a user's earned achievements
|
2014-06-03 16:54:56 -04:00
|
|
|
EarnedAchievement.find {user: userID}, (err, alreadyEarned) ->
|
|
|
|
alreadyEarnedIDs = []
|
|
|
|
previousPoints = 0
|
2014-07-03 15:20:06 -04:00
|
|
|
async.each alreadyEarned, ((earned, doneWithEarned) ->
|
|
|
|
if (_.find achievements, (single) -> earned.get('achievement') is single.get('_id').toHexString()) # if already earned
|
2014-06-04 14:47:32 -04:00
|
|
|
alreadyEarnedIDs.push earned.get('achievement')
|
|
|
|
previousPoints += earned.get 'earnedPoints'
|
2014-07-03 15:20:06 -04:00
|
|
|
doneWithEarned()
|
|
|
|
), -> # After checking already achieved
|
|
|
|
# TODO maybe also delete earned? Make sure you don't delete too many
|
|
|
|
|
|
|
|
newTotalPoints = 0
|
|
|
|
|
|
|
|
async.each achievements, ((achievement, doneWithAchievement) ->
|
|
|
|
isRepeatable = achievement.get('proportionalTo')?
|
|
|
|
model = mongoose.modelNameByCollection(achievement.get('collection'))
|
|
|
|
if not model?
|
|
|
|
log.error "Model with collection '#{achievement.get 'collection'}' doesn't exist."
|
|
|
|
return doneWithAchievement()
|
|
|
|
|
|
|
|
finalQuery = _.clone achievement.get 'query'
|
|
|
|
finalQuery.$or = [{}, {}] # Allow both ObjectIDs or hexa string IDs
|
|
|
|
finalQuery.$or[0][achievement.userField] = userID
|
|
|
|
finalQuery.$or[1][achievement.userField] = ObjectId userID
|
|
|
|
|
|
|
|
model.findOne finalQuery, (err, something) ->
|
|
|
|
return doneWithAchievement() if _.isEmpty something
|
|
|
|
|
|
|
|
log.debug "Matched an achievement: #{achievement.get 'name'} for #{user.get 'name'}"
|
|
|
|
|
|
|
|
earned =
|
|
|
|
user: userID
|
|
|
|
achievement: achievement._id.toHexString()
|
|
|
|
achievementName: achievement.get 'name'
|
|
|
|
notified: achievement._id in alreadyEarnedIDs
|
|
|
|
|
|
|
|
if isRepeatable
|
|
|
|
earned.achievedAmount = something.get(achievement.get 'proportionalTo')
|
|
|
|
earned.previouslyAchievedAmount = 0
|
|
|
|
|
|
|
|
expFunction = achievement.getExpFunction()
|
|
|
|
newPoints = expFunction(earned.achievedAmount) * achievement.get('worth')
|
|
|
|
else
|
|
|
|
newPoints = achievement.get 'worth'
|
|
|
|
|
|
|
|
earned.earnedPoints = newPoints
|
|
|
|
newTotalPoints += newPoints
|
|
|
|
|
|
|
|
EarnedAchievement.update {achievement:earned.achievement, user:earned.user}, earned, {upsert: true}, (err) ->
|
|
|
|
log.error err if err?
|
|
|
|
doneWithAchievement()
|
|
|
|
), saveUserPoints = ->
|
|
|
|
# In principle it is enough to deduct the old amount of points and add the new amount,
|
|
|
|
# but just to be entirely safe let's start from 0 in case we're updating all of a user's achievements
|
|
|
|
return doneWithUser() unless newTotalPoints
|
|
|
|
log.debug "Matched a total of #{newTotalPoints} new points"
|
|
|
|
if _.isEmpty filter # Completely clean
|
|
|
|
log.debug "Setting this user's score to #{newTotalPoints}"
|
|
|
|
User.update {_id: userID}, {$set: points: newTotalPoints}, {}, (err) ->
|
|
|
|
log.error err if err?
|
|
|
|
doneWithUser()
|
2014-06-03 16:54:56 -04:00
|
|
|
else
|
2014-07-03 15:20:06 -04:00
|
|
|
log.debug "Incrementing score for these achievements with #{newTotalPoints - previousPoints}"
|
|
|
|
User.update {_id: userID}, {$inc: points: newTotalPoints - previousPoints}, {}, (err) ->
|
|
|
|
log.error err if err?
|
|
|
|
doneWithUser()
|
|
|
|
), onFinished
|
2014-06-03 16:54:56 -04:00
|
|
|
|
2014-06-03 10:14:10 -04:00
|
|
|
|
2014-05-19 18:24:16 -04:00
|
|
|
module.exports = new EarnedAchievementHandler()
|