codecombat/app/views/play/level/tome/CastButtonView.coffee

179 lines
7.3 KiB
CoffeeScript
Raw Normal View History

2014-07-17 20:20:11 -04:00
CocoView = require 'views/kinds/CocoView'
2014-01-03 13:32:13 -05:00
template = require 'templates/play/level/tome/cast_button'
{me} = require 'lib/auth'
LevelOptions = require 'lib/LevelOptions'
2014-01-03 13:32:13 -05:00
2014-07-17 20:20:11 -04:00
module.exports = class CastButtonView extends CocoView
2014-01-03 13:32:13 -05:00
id: 'cast-button-view'
template: template
2014-03-24 13:11:27 -04:00
events:
'click .cast-button': 'onCastButtonClick'
'click .submit-button': 'onCastRealTimeButtonClick'
'click .done-button': 'onDoneButtonClick'
2014-03-24 13:11:27 -04:00
2014-01-03 13:32:13 -05:00
subscriptions:
2014-06-30 22:16:26 -04:00
'tome:spell-changed': 'onSpellChanged'
2014-01-03 13:32:13 -05:00
'tome:cast-spells': 'onCastSpells'
'god:new-world-created': 'onNewWorld'
'real-time-multiplayer:created-game': 'onJoinedRealTimeMultiplayerGame'
'real-time-multiplayer:joined-game': 'onJoinedRealTimeMultiplayerGame'
'real-time-multiplayer:left-game': 'onLeftRealTimeMultiplayerGame'
'goal-manager:new-goal-states': 'onNewGoalStates'
'god:goals-calculated': 'onGoalsCalculated'
2014-01-03 13:32:13 -05:00
constructor: (options) ->
super options
@spells = options.spells
@levelID = options.levelID
2014-06-30 22:16:26 -04:00
@castShortcut = '⇧↵'
@levelOptions = LevelOptions[@options.levelID] ? {}
@initButtonTextABTest() unless @levelOptions.hidesRealTimePlayback
2014-01-03 13:32:13 -05:00
2014-02-11 17:58:45 -05:00
getRenderData: (context={}) ->
2014-01-03 13:32:13 -05:00
context = super context
shift = $.i18n.t 'keyboard_shortcuts.shift'
enter = $.i18n.t 'keyboard_shortcuts.enter'
castShortcutVerbose = "#{shift}+#{enter}"
castRealTimeShortcutVerbose = (if @isMac() then 'Cmd' else 'Ctrl') + '+' + castShortcutVerbose
context.castVerbose = castShortcutVerbose + ': ' + $.i18n.t('keyboard_shortcuts.run_code')
context.castRealTimeVerbose = castRealTimeShortcutVerbose + ': ' + $.i18n.t('keyboard_shortcuts.run_real_time')
2014-11-06 17:44:56 -05:00
# A/B test submit button text
context.testSubmitText = @testButtonsText.submit if @testGroup? and @testGroup isnt 0
2014-01-03 13:32:13 -05:00
context
afterRender: ->
super()
2014-02-12 15:41:41 -05:00
@castButton = $('.cast-button', @$el)
@castOptions = $('.autocast-delays', @$el)
2014-09-15 14:56:29 -04:00
#delay = me.get('autocastDelay') # No more autocast
delay = 90019001
2014-01-03 13:32:13 -05:00
@setAutocastDelay delay
if @levelOptions.hidesSubmitUntilRun or @levelOptions.hidesRealTimePlayback
@$el.find('.submit-button').hide() # Hide Submit for the first few until they run it once.
if @options.session.get('state')?.complete and @levelOptions.hidesRealTimePlayback
@$el.find('.done-button').show()
if @options.levelID is 'thornbush-farm'# and not @options.session.get('state')?.complete
@$el.find('.submit-button').hide() # Hide submit until first win so that script can explain it.
2014-01-03 13:32:13 -05:00
2014-01-25 18:11:29 -05:00
attachTo: (spellView) ->
@$el.detach().prependTo(spellView.toolbarView.$el).show()
2014-02-12 23:45:29 -05:00
onCastButtonClick: (e) ->
Backbone.Mediator.publish 'tome:manual-cast', {}
2014-08-23 17:31:38 -04:00
onCastRealTimeButtonClick: (e) ->
if @multiplayerSession
Backbone.Mediator.publish 'real-time-multiplayer:manual-cast', {}
# Wait for multiplayer session to be up and running
@multiplayerSession.on 'change', (e) =>
if @multiplayerSession.get('state') is 'running'
# Real-time multiplayer session is ready to go, so resume normal cast
@multiplayerSession.off 'change'
Backbone.Mediator.publish 'tome:manual-cast', {realTime: true}
else
Backbone.Mediator.publish 'tome:manual-cast', {realTime: true}
2014-08-23 17:31:38 -04:00
onDoneButtonClick: (e) ->
Backbone.Mediator.publish 'level:show-victory', showModal: true
2014-01-03 13:32:13 -05:00
onSpellChanged: (e) ->
@updateCastButton()
onCastSpells: (e) ->
return if e.preload
2014-01-03 13:32:13 -05:00
@casting = true
if @hasStartedCastingOnce # Don't play this sound the first time
Backbone.Mediator.publish 'audio-player:play-sound', trigger: 'cast', volume: 0.5
@hasStartedCastingOnce = true
2014-01-03 13:32:13 -05:00
@updateCastButton()
onNewWorld: (e) ->
@casting = false
if @hasCastOnce # Don't play this sound the first time
Backbone.Mediator.publish 'audio-player:play-sound', trigger: 'cast-end', volume: 0.5
@hasCastOnce = true
2014-01-03 13:32:13 -05:00
@updateCastButton()
onNewGoalStates: (e) ->
winnable = e.overallStatus is 'success'
return if @winnable is winnable
@winnable = winnable
@$el.toggleClass 'winnable', @winnable
Backbone.Mediator.publish 'tome:winnability-updated', winnable: @winnable
if @levelOptions.hidesRealTimePlayback
@$el.find('.done-button').toggle @winnable
else if @winnable and @options.levelID is 'thornbush-farm'
@$el.find('.submit-button').show() # Hide submit until first win so that script can explain it.
onGoalsCalculated: (e) ->
# When preloading, with real-time playback enabled, we highlight the submit button when we think they'll win.
return unless e.preload
return if @levelOptions.hidesRealTimePlayback
return if @options.levelID is 'thornbush-farm' # Don't show it until they actually win for this first one.
@onNewGoalStates e
2014-01-03 13:32:13 -05:00
updateCastButton: ->
return if _.some @spells, (spell) => not spell.loaded
2014-04-22 14:04:56 -04:00
async.some _.values(@spells), (spell, callback) =>
spell.hasChangedSignificantly spell.getSource(), null, callback
, (castable) =>
Backbone.Mediator.publish 'tome:spell-has-changed-significantly-calculation', hasChangedSignificantly: castable
@castButton.toggleClass('castable', castable).toggleClass('casting', @casting)
2014-11-06 17:44:56 -05:00
# A/B testing cast button text for en-US
unless @testGroup? and @testGroup isnt 0
2014-11-06 17:44:56 -05:00
if @casting
castText = $.i18n.t('play_level.tome_cast_button_running')
else if castable or true
castText = $.i18n.t('play_level.tome_cast_button_run')
unless @levelOptions.hidesRunShortcut # Hide for first few.
2014-11-06 17:44:56 -05:00
castText += ' ' + @castShortcut
else
castText = $.i18n.t('play_level.tome_cast_button_ran')
else
2014-11-06 17:44:56 -05:00
castText = @testButtonsText.run
@castButton.text castText
#@castButton.prop 'disabled', not castable
2014-01-03 13:32:13 -05:00
setAutocastDelay: (delay) ->
2014-06-30 22:16:26 -04:00
#console.log 'Set autocast delay to', delay
2014-01-03 13:32:13 -05:00
return unless delay
2014-09-15 14:56:29 -04:00
delay = 90019001 # No more autocast
2014-01-03 13:32:13 -05:00
@autocastDelay = delay = parseInt delay
me.set('autocastDelay', delay)
me.patch()
spell.view?.setAutocastDelay delay for spellKey, spell of @spells
2014-01-03 13:32:13 -05:00
@castOptions.find('a').each ->
$(@).toggleClass('selected', parseInt($(@).attr('data-delay')) is delay)
onJoinedRealTimeMultiplayerGame: (e) ->
@multiplayerSession = e.session
onLeftRealTimeMultiplayerGame: (e) ->
if @multiplayerSession
@multiplayerSession.off 'change'
@multiplayerSession = null
2014-11-06 17:44:56 -05:00
initButtonTextABTest: ->
return if me.isAdmin()
return unless $.i18n.lng() is 'en-US'
# A/B test buttons text
# Only testing 'en-US' for simplicity and it accounts for a significant % of users
# Test group 0 is existing behavior
# Intentionally leaving out cast shortcut for test groups for simplicity
@testGroup = me.getCastButtonTextGroup()
@testButtonsText = switch @testGroup
when 0 then run: 'Run/Running', submit: 'Submit'
when 1 then run: 'Run', submit: 'Submit'
when 2 then run: 'Test', submit: 'Submit'
when 3 then run: 'Run', submit: 'Continue'
when 4 then run: 'Test', submit: 'Continue'
when 5 then run: 'Run', submit: 'Finish'
when 6 then run: 'Test', submit: 'Finish'
application.tracker?.trackEvent 'Cast Button',
levelID: @levelID
castButtonText: @testButtonsText.run + ' ' + @testButtonsText.submit
castButtonTextGroup: @testGroup