2014-11-28 20:49:41 -05:00
|
|
|
CocoView = require 'views/core/CocoView'
|
2014-01-03 13:32:13 -05:00
|
|
|
template = require 'templates/play/level/tome/cast_button'
|
2014-11-28 20:49:41 -05:00
|
|
|
{me} = require 'core/auth'
|
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'
|
2014-09-22 17:05:13 -04:00
|
|
|
'click .submit-button': 'onCastRealTimeButtonClick'
|
2014-11-10 18:16:28 -05:00
|
|
|
'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'
|
2015-01-05 13:44:17 -05:00
|
|
|
'tome:manual-cast-denied': 'onManualCastDenied'
|
2014-01-03 13:32:13 -05:00
|
|
|
'god:new-world-created': 'onNewWorld'
|
2014-10-31 19:33:43 -04:00
|
|
|
'real-time-multiplayer:created-game': 'onJoinedRealTimeMultiplayerGame'
|
2014-08-29 18:10:04 -04:00
|
|
|
'real-time-multiplayer:joined-game': 'onJoinedRealTimeMultiplayerGame'
|
|
|
|
'real-time-multiplayer:left-game': 'onLeftRealTimeMultiplayerGame'
|
2014-09-22 17:05:13 -04:00
|
|
|
'goal-manager:new-goal-states': 'onNewGoalStates'
|
2014-09-23 21:21:27 -04:00
|
|
|
'god:goals-calculated': 'onGoalsCalculated'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
|
|
|
@spells = options.spells
|
2014-06-30 22:16:26 -04:00
|
|
|
@castShortcut = '⇧↵'
|
2015-01-05 13:44:17 -05:00
|
|
|
@updateReplayabilityInterval = setInterval @updateReplayability, 1000
|
2015-01-31 13:04:02 -05:00
|
|
|
@observing = options.session.get('creator') isnt me.id
|
2015-01-05 13:44:17 -05:00
|
|
|
|
|
|
|
destroy: ->
|
|
|
|
clearInterval @updateReplayabilityInterval
|
|
|
|
super()
|
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
|
2014-08-25 00:39:34 -04:00
|
|
|
shift = $.i18n.t 'keyboard_shortcuts.shift'
|
|
|
|
enter = $.i18n.t 'keyboard_shortcuts.enter'
|
|
|
|
castShortcutVerbose = "#{shift}+#{enter}"
|
|
|
|
castRealTimeShortcutVerbose = (if @isMac() then 'Cmd' else 'Ctrl') + '+' + castShortcutVerbose
|
2014-11-07 19:04:35 -05:00
|
|
|
context.castVerbose = castShortcutVerbose + ': ' + $.i18n.t('keyboard_shortcuts.run_code')
|
2014-08-25 00:39:34 -04:00
|
|
|
context.castRealTimeVerbose = castRealTimeShortcutVerbose + ': ' + $.i18n.t('keyboard_shortcuts.run_real_time')
|
2015-01-31 13:04:02 -05:00
|
|
|
context.observing = @observing
|
2014-01-03 13:32:13 -05:00
|
|
|
context
|
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
2014-02-12 15:41:41 -05:00
|
|
|
@castButton = $('.cast-button', @$el)
|
2015-04-18 18:52:24 -04:00
|
|
|
spell.view?.createOnCodeChangeHandlers() for spellKey, spell of @spells
|
2015-07-24 20:37:42 -04:00
|
|
|
if @options.level.get('hidesSubmitUntilRun') or @options.level.get 'hidesRealTimePlayback'
|
2014-11-10 18:16:28 -05:00
|
|
|
@$el.find('.submit-button').hide() # Hide Submit for the first few until they run it once.
|
2014-12-28 16:25:20 -05:00
|
|
|
if @options.session.get('state')?.complete and @options.level.get 'hidesRealTimePlayback'
|
2014-11-10 18:16:28 -05:00
|
|
|
@$el.find('.done-button').show()
|
2014-12-28 16:25:20 -05:00
|
|
|
if @options.level.get('slug') is 'thornbush-farm'# and not @options.session.get('state')?.complete
|
2014-11-10 18:16:28 -05:00
|
|
|
@$el.find('.submit-button').hide() # Hide submit until first win so that script can explain it.
|
2015-01-05 13:44:17 -05:00
|
|
|
@updateReplayability()
|
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) ->
|
2014-11-17 18:07:10 -05:00
|
|
|
if @inRealTimeMultiplayerSession
|
2014-08-29 18:10:04 -04:00
|
|
|
Backbone.Mediator.publish 'real-time-multiplayer:manual-cast', {}
|
2015-01-05 13:44:17 -05:00
|
|
|
else if @options.level.get('replayable') and (timeUntilResubmit = @options.session.timeUntilResubmit()) > 0
|
|
|
|
Backbone.Mediator.publish 'tome:manual-cast-denied', timeUntilResubmit: timeUntilResubmit
|
Real-time multiplayer initial commit
Simple matchmaking, synchronous multiplayer PVP, flags!
Rough matchmaking is under the game menu multiplayer tab, for ladder
games only. After creating a 2-person game there, you can exit that
modal and real-time cast to play against each other.
If you’re the first person to cast, you’ll sit at the real-time level
playback view waiting until the other player casts. When they do, you
both should start the real-time playback (and start placing flags like
crazy people).
If in a multiplayer session, the real-time simulation runs the players’
code against each other. Your multiplayer opponent’s name should be up
near the level name.
Multiplayer sessions are stored completely in Firebase for now, and
removed if both players leave the game. There’s plenty of bugs,
synchronization issues, and minimal polish to add before we push it to
master.
2014-08-29 02:34:07 -04:00
|
|
|
else
|
|
|
|
Backbone.Mediator.publish 'tome:manual-cast', {realTime: true}
|
2015-01-05 13:44:17 -05:00
|
|
|
@updateReplayability()
|
2014-08-23 17:31:38 -04:00
|
|
|
|
2014-11-10 18:16:28 -05:00
|
|
|
onDoneButtonClick: (e) ->
|
2015-07-24 20:37:42 -04:00
|
|
|
return if @options.level.hasLocalChanges() # Don't award achievements when beating level changed in level editor
|
2015-01-31 01:06:22 -05:00
|
|
|
@options.session.recordScores @world.scores, @options.level
|
2014-11-10 18:16:28 -05:00
|
|
|
Backbone.Mediator.publish 'level:show-victory', showModal: true
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
onSpellChanged: (e) ->
|
|
|
|
@updateCastButton()
|
|
|
|
|
|
|
|
onCastSpells: (e) ->
|
2014-05-11 20:42:32 -04:00
|
|
|
return if e.preload
|
2014-01-03 13:32:13 -05:00
|
|
|
@casting = true
|
2014-05-20 00:53:14 -04:00
|
|
|
if @hasStartedCastingOnce # Don't play this sound the first time
|
2015-01-05 13:44:17 -05:00
|
|
|
@playSound 'cast', 0.5
|
2014-05-20 00:53:14 -04:00
|
|
|
@hasStartedCastingOnce = true
|
2014-01-03 13:32:13 -05:00
|
|
|
@updateCastButton()
|
|
|
|
|
2015-01-05 13:44:17 -05:00
|
|
|
onManualCastDenied: (e) ->
|
|
|
|
wait = moment().add(e.timeUntilResubmit, 'ms').fromNow()
|
|
|
|
#@playSound 'manual-cast-denied', 1.0 # find some sound for this?
|
|
|
|
noty text: "You can try again #{wait}.", layout: 'center', type: 'warning', killer: false, timeout: 6000
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
onNewWorld: (e) ->
|
|
|
|
@casting = false
|
2014-05-20 00:53:14 -04:00
|
|
|
if @hasCastOnce # Don't play this sound the first time
|
2015-01-05 13:44:17 -05:00
|
|
|
@playSound 'cast-end', 0.5
|
2014-05-20 00:53:14 -04:00
|
|
|
@hasCastOnce = true
|
2014-01-03 13:32:13 -05:00
|
|
|
@updateCastButton()
|
2015-01-31 01:06:22 -05:00
|
|
|
@world = e.world
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-09-22 17:05:13 -04:00
|
|
|
onNewGoalStates: (e) ->
|
2014-11-02 17:51:01 -05:00
|
|
|
winnable = e.overallStatus is 'success'
|
|
|
|
return if @winnable is winnable
|
|
|
|
@winnable = winnable
|
2014-09-22 17:05:13 -04:00
|
|
|
@$el.toggleClass 'winnable', @winnable
|
2014-11-02 17:51:01 -05:00
|
|
|
Backbone.Mediator.publish 'tome:winnability-updated', winnable: @winnable
|
2014-12-28 16:25:20 -05:00
|
|
|
if @options.level.get 'hidesRealTimePlayback'
|
2014-11-10 18:16:28 -05:00
|
|
|
@$el.find('.done-button').toggle @winnable
|
2014-12-28 16:25:20 -05:00
|
|
|
else if @winnable and @options.level.get('slug') is 'thornbush-farm'
|
2014-11-10 18:16:28 -05:00
|
|
|
@$el.find('.submit-button').show() # Hide submit until first win so that script can explain it.
|
2014-09-22 17:05:13 -04:00
|
|
|
|
2014-09-23 21:21:27 -04:00
|
|
|
onGoalsCalculated: (e) ->
|
2014-11-10 18:16:28 -05:00
|
|
|
# When preloading, with real-time playback enabled, we highlight the submit button when we think they'll win.
|
2014-09-23 21:21:27 -04:00
|
|
|
return unless e.preload
|
2014-12-28 16:25:20 -05:00
|
|
|
return if @options.level.get 'hidesRealTimePlayback'
|
|
|
|
return if @options.level.get('slug') is 'thornbush-farm' # Don't show it until they actually win for this first one.
|
2014-09-23 21:21:27 -04:00
|
|
|
@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
|
|
|
|
2014-04-22 11:54:35 -04:00
|
|
|
async.some _.values(@spells), (spell, callback) =>
|
|
|
|
spell.hasChangedSignificantly spell.getSource(), null, callback
|
|
|
|
, (castable) =>
|
2014-05-19 13:08:12 -04:00
|
|
|
Backbone.Mediator.publish 'tome:spell-has-changed-significantly-calculation', hasChangedSignificantly: castable
|
2014-09-22 17:05:13 -04:00
|
|
|
@castButton.toggleClass('castable', castable).toggleClass('casting', @casting)
|
2014-11-22 19:50:46 -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')
|
2014-12-28 16:25:20 -05:00
|
|
|
unless @options.level.get 'hidesRunShortcut' # Hide for first few.
|
2014-11-22 19:50:46 -05:00
|
|
|
castText += ' ' + @castShortcut
|
2014-04-22 11:54:35 -04:00
|
|
|
else
|
2014-11-22 19:50:46 -05:00
|
|
|
castText = $.i18n.t('play_level.tome_cast_button_ran')
|
2014-11-06 17:44:56 -05:00
|
|
|
@castButton.text castText
|
2014-11-05 21:06:21 -05:00
|
|
|
#@castButton.prop 'disabled', not castable
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2015-01-05 13:44:17 -05:00
|
|
|
updateReplayability: =>
|
|
|
|
return if @destroyed
|
|
|
|
return unless @options.level.get 'replayable'
|
|
|
|
timeUntilResubmit = @options.session.timeUntilResubmit()
|
|
|
|
disabled = timeUntilResubmit > 0
|
|
|
|
submitButton = @$el.find('.submit-button').toggleClass('disabled', disabled)
|
|
|
|
submitAgainLabel = submitButton.find('.submit-again-time').toggleClass('secret', not disabled)
|
|
|
|
if disabled
|
|
|
|
waitTime = moment().add(timeUntilResubmit, 'ms').fromNow()
|
|
|
|
submitAgainLabel.text waitTime
|
|
|
|
|
2014-08-29 18:10:04 -04:00
|
|
|
onJoinedRealTimeMultiplayerGame: (e) ->
|
2014-11-17 18:07:10 -05:00
|
|
|
@inRealTimeMultiplayerSession = true
|
Real-time multiplayer initial commit
Simple matchmaking, synchronous multiplayer PVP, flags!
Rough matchmaking is under the game menu multiplayer tab, for ladder
games only. After creating a 2-person game there, you can exit that
modal and real-time cast to play against each other.
If you’re the first person to cast, you’ll sit at the real-time level
playback view waiting until the other player casts. When they do, you
both should start the real-time playback (and start placing flags like
crazy people).
If in a multiplayer session, the real-time simulation runs the players’
code against each other. Your multiplayer opponent’s name should be up
near the level name.
Multiplayer sessions are stored completely in Firebase for now, and
removed if both players leave the game. There’s plenty of bugs,
synchronization issues, and minimal polish to add before we push it to
master.
2014-08-29 02:34:07 -04:00
|
|
|
|
2014-08-29 18:10:04 -04:00
|
|
|
onLeftRealTimeMultiplayerGame: (e) ->
|
2014-11-17 18:07:10 -05:00
|
|
|
@inRealTimeMultiplayerSession = false
|