codecombat/app/views/play/level/ControlBarView.coffee

106 lines
3.5 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/control_bar'
{me} = require 'lib/auth'
2014-01-03 13:32:13 -05:00
LevelGuideModal = require './modal/LevelGuideModal'
GameMenuModal = require 'views/game-menu/GameMenuModal'
RealTimeCollection = require 'collections/RealTimeCollection'
2014-01-03 13:32:13 -05:00
2014-07-17 20:20:11 -04:00
module.exports = class ControlBarView extends CocoView
2014-06-30 22:16:26 -04:00
id: 'control-bar-view'
2014-01-03 13:32:13 -05:00
template: template
subscriptions:
'bus:player-states-changed': 'onPlayerStatesChanged'
'real-time-multiplayer:joined-game': 'onJoinedRealTimeMultiplayerGame'
'real-time-multiplayer:left-game': 'onLeftRealTimeMultiplayerGame'
2014-01-03 13:32:13 -05:00
events:
'click #docs-button': ->
window.tracker?.trackEvent 'Clicked Docs', level: @level.get('name'), label: @level.get('name')
2014-01-03 13:32:13 -05:00
@showGuideModal()
'click #next-game-button': -> Backbone.Mediator.publish 'level:next-game-pressed', {}
2014-01-03 13:32:13 -05:00
'click #game-menu-button': 'showGameMenuModal'
'click #stop-real-time-playback-button': -> Backbone.Mediator.publish 'playback:stop-real-time-playback', {}
'click': -> Backbone.Mediator.publish 'tome:focus-editor', {}
2014-01-03 13:32:13 -05:00
constructor: (options) ->
@worldName = options.worldName
@session = options.session
@level = options.level
2014-02-06 20:31:08 -05:00
@playableTeams = options.playableTeams
2014-03-12 20:51:09 -04:00
@spectateGame = options.spectateGame ? false
2014-01-03 13:32:13 -05:00
super options
setBus: (@bus) ->
onPlayerStatesChanged: (e) ->
2014-02-12 15:41:41 -05:00
# TODO: this doesn't fire any more. Replacement?
2014-01-03 13:32:13 -05:00
return unless @bus is e.bus
numPlayers = _.keys(e.players).length
return if numPlayers is @numPlayers
@numPlayers = numPlayers
text = 'Multiplayer'
text += " (#{numPlayers})" if numPlayers > 1
$('#multiplayer-button', @$el).text(text)
getRenderData: (c={}) ->
super c
c.worldName = @worldName
c.multiplayerEnabled = @session.get('multiplayer')
2014-05-05 18:33:08 -04:00
c.ladderGame = @level.get('type') is 'ladder'
2014-03-12 20:51:09 -04:00
c.spectateGame = @spectateGame
2014-05-05 18:33:08 -04:00
if @level.get('type') in ['ladder', 'ladder-tutorial']
c.homeLink = '/play/ladder/' + @level.get('slug').replace /\-tutorial$/, ''
else
c.homeLink = '/'
c.editorLink = "/editor/level/#{@level.get('slug')}"
c.multiplayerSession = @multiplayerSession if @multiplayerSession
c.multiplayerPlayers = @multiplayerPlayers if @multiplayerPlayers
c.meID = me.id
c
2014-01-03 13:32:13 -05:00
afterRender: ->
super()
@guideHighlightInterval ?= setInterval @onGuideHighlight, 5 * 60 * 1000
destroy: ->
clearInterval @guideHighlightInterval if @guideHighlightInterval
super()
onGuideHighlight: =>
return if @destroyed or @guideShownOnce
@$el.find('#docs-button').hide().show('highlight', 4000)
2014-01-03 13:32:13 -05:00
showGuideModal: ->
options = {docs: @level.get('documentation'), supermodel: @supermodel}
@openModalView(new LevelGuideModal(options))
clearInterval @guideHighlightInterval
@guideHighlightInterval = null
2014-01-03 13:32:13 -05:00
showGameMenuModal: ->
@openModalView new GameMenuModal level: @level, session: @session, playableTeams: @playableTeams
onJoinedRealTimeMultiplayerGame: (e) ->
@multiplayerSession = e.session
@multiplayerPlayers = new RealTimeCollection('multiplayer_level_sessions/' + @multiplayerSession.id + '/players')
@multiplayerPlayers.on 'add', @onRealTimeMultiplayerPlayerAdded
@multiplayerPlayers.on 'remove', @onRealTimeMultiplayerPlayerRemoved
@render()
onLeftRealTimeMultiplayerGame: (e) ->
@multiplayerSession = null
@multiplayerPlayers.off()
@multiplayerPlayers = null
@render()
onRealTimeMultiplayerPlayerAdded: (e) =>
@render()
onRealTimeMultiplayerPlayerRemoved: (e) =>
@render()