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
console . debug ' Created an AchievementPopup ' , @ $el
@ render ( )
calculateData: ->
currentLevel = me . level ( )
nextLevel = currentLevel + 1
currentLevelExp = User . expForLevel ( currentLevel )
nextLevelXP = User . expForLevel ( nextLevel )
totalExpNeeded = nextLevelXP - currentLevelExp
expFunction = @ achievement . getExpFunction ( )
currentXP = me . get ' points '
if @ achievement . isRepeatable ( )
achievedXP = expFunction ( @ earnedAchievement . get ( ' previouslyAchievedAmount ' ) ) * @ achievement . get ( ' worth ' ) if @ achievement . isRepeatable ( )
else
achievedXP = @ achievement . get ' worth '
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: ->
console . debug ' render achievement popup '
super ( )
2014-08-11 08:51:34 -04:00
@ container . prepend @ $el
if @ popup
@ $el . animate
left: 0
@ $el . on ' click ' , (e) =>
@ $el . animate
left: 600
, =>
@ $el . remove ( )
@ destroy ( )
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 ( )