2014-11-28 20:49:41 -05:00
|
|
|
ModalView = require 'views/core/ModalView'
|
2014-09-17 21:56:08 -04:00
|
|
|
template = require 'templates/play/modal/play-achievements-modal'
|
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
|
|
|
Achievement = require 'models/Achievement'
|
2014-11-19 17:55:01 -05:00
|
|
|
EarnedAchievement = require 'models/EarnedAchievement'
|
|
|
|
|
2014-11-28 20:49:41 -05:00
|
|
|
utils = require 'core/utils'
|
2014-11-19 17:55:01 -05:00
|
|
|
|
|
|
|
PAGE_SIZE = 200
|
2014-09-17 21:56:08 -04:00
|
|
|
|
|
|
|
module.exports = class PlayAchievementsModal extends ModalView
|
|
|
|
className: 'modal fade play-modal'
|
|
|
|
template: template
|
|
|
|
id: 'play-achievements-modal'
|
2014-11-19 17:55:01 -05:00
|
|
|
plain: true
|
2014-11-22 23:56:46 -05:00
|
|
|
|
2014-11-19 17:55:01 -05:00
|
|
|
earnedMap: {}
|
2014-09-17 21:56:08 -04:00
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
2014-11-19 17:55:01 -05:00
|
|
|
@achievements = new Backbone.Collection()
|
|
|
|
earnedMap = {}
|
2014-11-22 23:56:46 -05:00
|
|
|
|
2014-11-19 17:55:01 -05:00
|
|
|
achievementsFetcher = new CocoCollection([], {url: '/db/achievement', model: Achievement})
|
|
|
|
achievementsFetcher.setProjection([
|
|
|
|
'name'
|
|
|
|
'description'
|
|
|
|
'icon'
|
|
|
|
'worth'
|
|
|
|
'i18n'
|
|
|
|
'rewards'
|
|
|
|
'collection'
|
2015-02-13 21:55:23 -05:00
|
|
|
'function'
|
2014-11-19 17:55:01 -05:00
|
|
|
])
|
2014-11-22 23:56:46 -05:00
|
|
|
|
2014-11-19 17:55:01 -05:00
|
|
|
earnedAchievementsFetcher = new CocoCollection([], {url: '/db/earned_achievement', model: EarnedAchievement})
|
2015-02-13 21:55:23 -05:00
|
|
|
earnedAchievementsFetcher.setProjection ['achievement', 'achievedAmount']
|
2014-11-22 23:56:46 -05:00
|
|
|
|
2014-11-19 17:55:01 -05:00
|
|
|
achievementsFetcher.skip = 0
|
2015-02-11 16:12:42 -05:00
|
|
|
achievementsFetcher.fetch cache: false, data: {skip: 0, limit: PAGE_SIZE}
|
2014-11-19 17:55:01 -05:00
|
|
|
earnedAchievementsFetcher.skip = 0
|
2015-02-11 16:12:42 -05:00
|
|
|
earnedAchievementsFetcher.fetch cache: false, data: {skip: 0, limit: PAGE_SIZE}
|
2014-11-19 17:55:01 -05:00
|
|
|
|
|
|
|
@listenTo achievementsFetcher, 'sync', @onAchievementsLoaded
|
|
|
|
@listenTo earnedAchievementsFetcher, 'sync', @onEarnedAchievementsLoaded
|
|
|
|
|
|
|
|
@supermodel.loadCollection(achievementsFetcher, 'achievement')
|
|
|
|
@supermodel.loadCollection(earnedAchievementsFetcher, 'achievement')
|
2014-11-22 23:56:46 -05:00
|
|
|
|
2014-11-19 17:55:01 -05:00
|
|
|
@onEverythingLoaded = _.after(2, @onEverythingLoaded)
|
|
|
|
|
|
|
|
onAchievementsLoaded: (fetcher) ->
|
|
|
|
needMore = fetcher.models.length is PAGE_SIZE
|
|
|
|
@achievements.add(fetcher.models)
|
|
|
|
if needMore
|
|
|
|
fetcher.skip += PAGE_SIZE
|
2015-02-11 16:12:42 -05:00
|
|
|
fetcher.fetch cache: false, data: {skip: fetcher.skip, limit: PAGE_SIZE}
|
2014-11-19 17:55:01 -05:00
|
|
|
else
|
|
|
|
@stopListening(fetcher)
|
|
|
|
@onEverythingLoaded()
|
|
|
|
|
|
|
|
onEarnedAchievementsLoaded: (fetcher) ->
|
|
|
|
needMore = fetcher.models.length is PAGE_SIZE
|
|
|
|
for earned in fetcher.models
|
|
|
|
@earnedMap[earned.get('achievement')] = earned
|
|
|
|
if needMore
|
|
|
|
fetcher.skip += PAGE_SIZE
|
2015-02-11 16:12:42 -05:00
|
|
|
fetcher.fetch cache: false, data: {skip: fetcher.skip, limit: PAGE_SIZE}
|
2014-11-19 17:55:01 -05:00
|
|
|
else
|
|
|
|
@stopListening(fetcher)
|
|
|
|
@onEverythingLoaded()
|
|
|
|
|
|
|
|
onEverythingLoaded: =>
|
|
|
|
@achievements.set(@achievements.filter((m) -> m.get('collection') isnt 'level.sessions'))
|
|
|
|
for achievement in @achievements.models
|
|
|
|
if earned = @earnedMap[achievement.id]
|
|
|
|
achievement.earned = earned
|
|
|
|
achievement.earnedDate = earned.getCreationDate()
|
2015-02-13 21:55:23 -05:00
|
|
|
expFunction = achievement.getExpFunction()
|
|
|
|
achievement.earnedGems = Math.round (achievement.get('rewards')?.gems or 0) * expFunction earned.get('achievedAmount')
|
|
|
|
achievement.earnedPoints = Math.round (achievement.get('worth', true) or 0) * expFunction earned.get('achievedAmount')
|
2014-11-19 17:55:01 -05:00
|
|
|
achievement.earnedDate ?= ''
|
|
|
|
@achievements.comparator = (m) -> m.earnedDate
|
|
|
|
@achievements.sort()
|
|
|
|
@achievements.set(@achievements.models.reverse())
|
|
|
|
for achievement in @achievements.models
|
|
|
|
achievement.name = utils.i18n achievement.attributes, 'name'
|
|
|
|
achievement.description = utils.i18n achievement.attributes, 'description'
|
|
|
|
@render()
|
2014-09-17 21:56:08 -04:00
|
|
|
|
|
|
|
getRenderData: (context={}) ->
|
|
|
|
context = super(context)
|
2014-11-19 17:55:01 -05:00
|
|
|
context.achievements = @achievements.models
|
2014-09-17 21:56:08 -04:00
|
|
|
context
|
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
return unless @supermodel.finished()
|
2014-11-26 09:58:23 -05:00
|
|
|
@playSound 'game-menu-open'
|
2014-09-17 21:56:08 -04:00
|
|
|
|
|
|
|
onHidden: ->
|
|
|
|
super()
|
2014-11-26 09:58:23 -05:00
|
|
|
@playSound 'game-menu-close'
|