2014-11-28 20:49:41 -05:00
CocoView = require ' views/core/CocoView '
2014-11-29 14:39:37 -05:00
template = require ' templates/core/achievement-popup '
2014-08-11 08:11:26 -04:00
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
2014-11-10 11:31:26 -05:00
currentLevelXP = User . expForLevel ( currentLevel )
2014-08-11 08:11:26 -04:00
nextLevelXP = User . expForLevel ( nextLevel )
2014-11-10 11:31:26 -05:00
totalXPNeeded = nextLevelXP - currentLevelXP
2014-08-11 08:11:26 -04:00
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
2014-11-10 11:31:26 -05:00
leveledUp = currentXP - achievedXP < currentLevelXP
2014-08-11 08:11:26 -04:00
#console.debug 'Leveled up' if leveledUp
2014-11-10 11:31:26 -05:00
alreadyAchievedPercentage = 100 * ( previousXP - currentLevelXP ) / totalXPNeeded
2014-08-11 08:11:26 -04:00
alreadyAchievedPercentage = 0 if alreadyAchievedPercentage < 0 # In case of level up
2014-11-10 11:31:26 -05:00
newlyAchievedPercentage = if leveledUp then 100 * ( currentXP - currentLevelXP ) / totalXPNeeded else 100 * achievedXP / totalXPNeeded
2014-08-11 08:11:26 -04:00
2014-11-10 11:31:26 -05:00
#console.debug "Current level is #{currentLevel} (#{currentLevelXP} xp), next level is #{nextLevel} (#{nextLevelXP} xp)."
#console.debug "Need a total of #{nextLevelXP - currentLevelXP}, already had #{previousXP} and just now earned #{achievedXP} totalling on #{currentXP}"
2014-08-11 08:11:26 -04:00
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: n ewlyAchievedPercentage
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
2015-12-21 01:28:09 -05:00
@ $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: ->
2015-02-02 21:02:57 -05:00
$ ( ' .progress-bar ' ) . addClass ( ' has-tooltip ' ) . tooltip ( )