2014-01-03 13:32:13 -05:00
|
|
|
View = require 'views/kinds/ModalView'
|
|
|
|
template = require 'templates/play/level/modal/victory'
|
|
|
|
{me} = require 'lib/auth'
|
|
|
|
LevelFeedback = require 'models/LevelFeedback'
|
2014-03-13 12:02:19 -04:00
|
|
|
utils = require 'lib/utils'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
module.exports = class VictoryModal extends View
|
|
|
|
id: 'level-victory-modal'
|
|
|
|
template: template
|
|
|
|
|
|
|
|
events:
|
|
|
|
'click .next-level-button': 'onPlayNextLevel'
|
2014-03-10 13:36:28 -04:00
|
|
|
'click .rank-game-button': 'onRankGame'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
# review events
|
|
|
|
'mouseover .rating i': (e) -> @showStars(@starNum($(e.target)))
|
|
|
|
'mouseout .rating i': -> @showStars()
|
|
|
|
'click .rating i': (e) ->
|
|
|
|
@setStars(@starNum($(e.target)))
|
|
|
|
@$el.find('.review').show()
|
|
|
|
'keypress .review textarea': -> @saveReviewEventually()
|
|
|
|
|
|
|
|
shortcuts:
|
|
|
|
'enter': -> 'onPlayNextLevel'
|
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
victory = options.level.get('victory')
|
2014-03-13 12:02:19 -04:00
|
|
|
body = utils.i18n(victory, 'body') or 'Sorry, this level has no victory message yet.'
|
2014-01-03 13:32:13 -05:00
|
|
|
@body = marked(body)
|
|
|
|
@level = options.level
|
|
|
|
@session = options.session
|
|
|
|
@saveReviewEventually = _.debounce(@saveReviewEventually, 2000)
|
|
|
|
@loadExistingFeedback()
|
|
|
|
super options
|
|
|
|
|
|
|
|
loadExistingFeedback: ->
|
|
|
|
url = "/db/level/#{@level.id}/feedback"
|
|
|
|
@feedback = new LevelFeedback()
|
|
|
|
@feedback.url = -> url
|
|
|
|
@feedback.fetch()
|
2014-03-25 17:55:11 -04:00
|
|
|
@listenToOnce(@feedback, 'sync', -> @onFeedbackLoaded())
|
|
|
|
@listenToOnce(@feedback, 'error', -> @onFeedbackNotFound())
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
onFeedbackLoaded: ->
|
|
|
|
@feedback.url = -> '/db/level.feedback/' + @id
|
|
|
|
@$el.find('.review textarea').val(@feedback.get('review'))
|
|
|
|
@$el.find('.review').show()
|
|
|
|
@showStars()
|
|
|
|
|
|
|
|
onFeedbackNotFound: ->
|
|
|
|
@feedback = new LevelFeedback()
|
|
|
|
@feedback.set('levelID', @level.get('slug') or @level.id)
|
|
|
|
@feedback.set('levelName', @level.get('name') or '')
|
|
|
|
@feedback.set('level', {majorVersion: @level.get('version').major, original:@level.get('original')})
|
|
|
|
@showStars()
|
|
|
|
|
|
|
|
onPlayNextLevel: ->
|
|
|
|
@saveReview() if @$el.find('.review textarea').val()
|
|
|
|
Backbone.Mediator.publish('play-next-level')
|
|
|
|
|
2014-03-10 13:36:28 -04:00
|
|
|
onRankGame: (e) ->
|
|
|
|
button = @$el.find('.rank-game-button')
|
|
|
|
button.text($.i18n.t('play_level.victory_ranking_game', defaultValue: 'Submitting...'))
|
|
|
|
button.prop 'disabled', true
|
|
|
|
ajaxData = session: @session.id, levelID: @level.id, originalLevelID: @level.get('original'), levelMajorVersion: @level.get('version').major
|
|
|
|
ladderURL = "/play/ladder/#{@level.get('slug')}#my-matches"
|
|
|
|
goToLadder = -> Backbone.Mediator.publish 'router:navigate', route: ladderURL
|
2014-03-11 22:17:58 -04:00
|
|
|
console.log "Posting game for ranking from victory modal."
|
2014-03-10 13:36:28 -04:00
|
|
|
$.ajax '/queue/scoring',
|
|
|
|
type: 'POST'
|
|
|
|
data: ajaxData
|
|
|
|
success: goToLadder
|
|
|
|
failure: (response) ->
|
|
|
|
console.error "Couldn't submit game for ranking:", response
|
|
|
|
goToLadder()
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
getRenderData: ->
|
|
|
|
c = super()
|
|
|
|
c.body = @body
|
|
|
|
c.me = me
|
|
|
|
c.hasNextLevel = _.isObject(@level.get('nextLevel')) and (@level.get('name') isnt "Mobile Artillery")
|
2014-03-13 12:02:19 -04:00
|
|
|
c.levelName = utils.i18n @level.attributes, 'name'
|
2014-03-08 00:20:09 -05:00
|
|
|
c.level = @level
|
2014-03-10 13:36:28 -04:00
|
|
|
if c.level.get('type') is 'ladder'
|
|
|
|
c1 = @session?.get('code')
|
|
|
|
c2 = @session?.get('submittedCode')
|
|
|
|
c.readyToRank = @session.get('levelID') and c1 and not _.isEqual(c1, c2)
|
2014-01-03 13:32:13 -05:00
|
|
|
if me.get 'hourOfCode'
|
|
|
|
# Show the Hour of Code "I'm Done" tracking pixel after they played for 30 minutes
|
|
|
|
elapsed = (new Date() - new Date(me.get('dateCreated')))
|
|
|
|
enough = not c.hasNextLevel or elapsed >= 30 * 60 * 1000
|
|
|
|
if enough and not me.get('hourOfCodeComplete')
|
|
|
|
$('body').append($("<img src='http://code.org/api/hour/finish_codecombat.png' style='visibility: hidden;'>"))
|
|
|
|
me.set 'hourOfCodeComplete', true
|
|
|
|
me.save()
|
|
|
|
window.tracker?.trackEvent 'Hour of Code Finish', {}
|
|
|
|
# Show the "I'm done" button if they get to the end, unless it's been over two hours
|
|
|
|
tooMuch = elapsed >= 120 * 60 * 1000
|
|
|
|
c.showHourOfCodeDoneButton = not c.hasNextLevel and not tooMuch
|
|
|
|
c
|
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
|
|
|
|
afterInsert: ->
|
|
|
|
super()
|
|
|
|
Backbone.Mediator.publish 'play-sound', trigger: "victory"
|
2014-01-09 13:48:51 -05:00
|
|
|
gapi?.plusone?.go? @$el[0]
|
|
|
|
FB?.XFBML?.parse? @$el[0]
|
|
|
|
twttr?.widgets?.load?()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
onHidden: ->
|
|
|
|
Backbone.Mediator.publish 'level:victory-hidden'
|
|
|
|
|
|
|
|
destroy: ->
|
|
|
|
@saveReview() if @$el.find('.review textarea').val()
|
2014-02-18 13:03:57 -05:00
|
|
|
@feedback.off()
|
2014-02-14 13:57:47 -05:00
|
|
|
super()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
# rating, review
|
|
|
|
|
|
|
|
starNum: (starEl) -> starEl.prevAll('i').length + 1
|
|
|
|
|
|
|
|
showStars: (num) ->
|
|
|
|
@$el.find('.rating').show()
|
|
|
|
num ?= @feedback?.get('rating') or 0
|
|
|
|
stars = @$el.find('.rating i')
|
|
|
|
stars.removeClass('icon-star').addClass('icon-star-empty')
|
|
|
|
stars.slice(0, num).removeClass('icon-star-empty').addClass('icon-star')
|
|
|
|
|
|
|
|
setStars: (num) ->
|
|
|
|
@feedback.set('rating', num)
|
|
|
|
@feedback.save()
|
|
|
|
|
|
|
|
saveReviewEventually: ->
|
|
|
|
@saveReview()
|
|
|
|
|
|
|
|
saveReview: ->
|
|
|
|
@feedback.set('review', @$el.find('.review textarea').val())
|
|
|
|
@feedback.save()
|