2014-08-11 08:11:26 -04:00
CocoView = require ' views/kinds/CocoView '
template = require ' templates/achievements/achievement-popup '
User = require ' ../../models/User '
Achievement = require ' ../../models/Achievement '
module.exports = class AchievementPopup extends CocoView
className: ' achievement-popup '
template: template
constructor: (options) ->
@achievement = options . achievement
@earnedAchievement = options . earnedAchievement
@container = options . container or @ getContainer ( )
@popup = options . container
@ popup ? = true
2014-08-11 08:51:34 -04:00
@ className += ' popup ' if @ popup
2014-08-11 08:11:26 -04:00
super options
@ render ( )
calculateData: ->
currentLevel = me . level ( )
nextLevel = currentLevel + 1
currentLevelExp = User . expForLevel ( currentLevel )
nextLevelXP = User . expForLevel ( nextLevel )
totalExpNeeded = nextLevelXP - currentLevelExp
expFunction = @ achievement . getExpFunction ( )
2014-09-02 19:48:16 -04:00
currentXP = me . get ' points ' , true
2014-08-11 08:11:26 -04:00
if @ achievement . isRepeatable ( )
achievedXP = expFunction ( @ earnedAchievement . get ( ' previouslyAchievedAmount ' ) ) * @ achievement . get ( ' worth ' ) if @ achievement . isRepeatable ( )
else
2014-09-02 19:48:16 -04:00
achievedXP = @ achievement . get ' worth ' , true
2014-08-11 08:11:26 -04:00
previousXP = currentXP - achievedXP
leveledUp = currentXP - achievedXP < currentLevelExp
#console.debug 'Leveled up' if leveledUp
alreadyAchievedPercentage = 100 * ( previousXP - currentLevelExp ) / totalExpNeeded
alreadyAchievedPercentage = 0 if alreadyAchievedPercentage < 0 # In case of level up
newlyAchievedPercentage = if leveledUp then 100 * ( currentXP - currentLevelExp ) / totalExpNeeded else 100 * achievedXP / totalExpNeeded
#console.debug "Current level is #{currentLevel} (#{currentLevelExp} xp), next level is #{nextLevel} (#{nextLevelXP} xp)."
#console.debug "Need a total of #{nextLevelXP - currentLevelExp}, already had #{previousXP} and just now earned #{achievedXP} totalling on #{currentXP}"
data =
2014-08-12 11:59:33 -04:00
title: @ achievement . i18nName ( )
2014-08-11 08:11:26 -04:00
imgURL: @ achievement . getImageURL ( )
2014-08-12 11:59:33 -04:00
description: @ achievement . i18nDescription ( )
2014-08-11 08:11:26 -04:00
level: currentLevel
currentXP: currentXP
newXP: achievedXP
leftXP: nextLevelXP - currentXP
oldXPWidth: alreadyAchievedPercentage
newXPWidth: newlyAchievedPercentage
leftXPWidth: 100 - newlyAchievedPercentage - alreadyAchievedPercentage
getRenderData: ->
c = super ( )
_ . extend c , @ calculateData ( )
c.style = @ achievement . getStyle ( )
c.popup = true
2014-08-12 07:41:14 -04:00
c.$ = $ # Allows the jade template to do i18n
2014-08-11 08:11:26 -04:00
c
render: ->
super ( )
2014-08-11 08:51:34 -04:00
@ container . prepend @ $el
if @ popup
2014-09-20 18:18:21 -04:00
hide = =>
return if @ destroyed
@ $el . animate { left: 600 } , =>
2014-08-11 08:51:34 -04:00
@ $el . remove ( )
@ destroy ( )
2014-09-20 18:18:21 -04:00
@ $el . animate left: 0
@ $el . on ' click ' , hide
_ . delay hide , 10000 unless $ ( ' # editor-achievement-edit-view ' ) . length
2014-08-11 08:51:34 -04:00
2014-08-11 08:11:26 -04:00
getContainer: ->
unless @ container
@container = $ ( ' .achievement-popup-container ' )
unless @ container . length
$ ( ' body ' ) . append ( ' <div class= " achievement-popup-container " ></div> ' )
@container = $ ( ' .achievement-popup-container ' )
@ container
afterRender: ->
super ( )
_ . delay @ initializeTooltips , 1000 # TODO this could be smoother
initializeTooltips: ->
$ ( ' .progress-bar ' ) . tooltip ( )