2014-11-28 20:49:41 -05:00
|
|
|
CocoClass = require 'core/CocoClass'
|
2014-11-06 19:23:23 -05:00
|
|
|
PlayHeroesModal = require 'views/play/modal/PlayHeroesModal'
|
2014-11-29 15:46:04 -05:00
|
|
|
InventoryModal = require 'views/play/menu/InventoryModal'
|
2014-12-28 16:25:20 -05:00
|
|
|
Level = require 'models/Level'
|
2014-11-06 19:23:23 -05:00
|
|
|
LevelSession = require 'models/LevelSession'
|
|
|
|
SuperModel = require 'models/SuperModel'
|
2014-11-26 11:53:06 -05:00
|
|
|
ThangType = require 'models/ThangType'
|
|
|
|
|
|
|
|
lastHeroesEarned = me.get('earned')?.heroes ? []
|
|
|
|
lastHeroesPurchased = me.get('purchased')?.heroes ? []
|
|
|
|
|
2014-11-06 19:23:23 -05:00
|
|
|
|
|
|
|
module.exports = class LevelSetupManager extends CocoClass
|
|
|
|
|
|
|
|
constructor: (@options) ->
|
|
|
|
super()
|
2014-11-10 15:47:24 -05:00
|
|
|
@supermodel = @options.supermodel ? new SuperModel()
|
2014-11-06 19:23:23 -05:00
|
|
|
@session = @options.session
|
2014-12-28 18:18:34 -05:00
|
|
|
@loadLevel()
|
2014-11-06 19:23:23 -05:00
|
|
|
if @session
|
|
|
|
@fillSessionWithDefaults()
|
|
|
|
else
|
2014-11-10 15:47:24 -05:00
|
|
|
@loadSession()
|
2014-11-06 19:23:23 -05:00
|
|
|
|
2014-12-28 18:18:34 -05:00
|
|
|
loadLevel: ->
|
2014-12-28 16:25:20 -05:00
|
|
|
levelURL = "/db/level/#{@options.levelID}"
|
|
|
|
@level = new Level().setURL levelURL
|
|
|
|
@level = @supermodel.loadModel(@level, 'level').model
|
|
|
|
onLevelSync = ->
|
|
|
|
return if @destroyed
|
|
|
|
if @waitingToLoadModals
|
|
|
|
@waitingToLoadModals = false
|
|
|
|
@loadModals()
|
|
|
|
onLevelSync.call @ if @level.loaded
|
|
|
|
|
2014-12-28 18:18:34 -05:00
|
|
|
loadSession: ->
|
|
|
|
sessionURL = "/db/level/#{@options.levelID}/session"
|
2014-12-28 16:25:20 -05:00
|
|
|
#sessionURL += "?team=#{@team}" if @options.team # TODO: figure out how to get the teams for multiplayer PVP hero style
|
|
|
|
@session = new LevelSession().setURL sessionURL
|
2014-11-10 15:47:24 -05:00
|
|
|
onSessionSync = ->
|
2014-12-04 16:18:00 -05:00
|
|
|
return if @destroyed
|
2014-11-06 19:23:23 -05:00
|
|
|
@session.url = -> '/db/level.session/' + @id
|
|
|
|
@fillSessionWithDefaults()
|
2014-11-10 15:47:24 -05:00
|
|
|
@listenToOnce @session, 'sync', onSessionSync
|
|
|
|
@session = @supermodel.loadModel(@session, 'level_session').model
|
2014-12-28 16:25:20 -05:00
|
|
|
onSessionSync.call @ if @session.loaded
|
2014-11-06 19:23:23 -05:00
|
|
|
|
|
|
|
fillSessionWithDefaults: ->
|
|
|
|
heroConfig = _.merge {}, me.get('heroConfig'), @session.get('heroConfig')
|
|
|
|
@session.set('heroConfig', heroConfig)
|
2014-12-28 16:25:20 -05:00
|
|
|
if @level.loaded
|
|
|
|
@loadModals()
|
|
|
|
else
|
|
|
|
@waitingToLoadModals = true
|
2014-11-07 12:08:30 -05:00
|
|
|
|
2014-11-10 15:47:24 -05:00
|
|
|
loadModals: ->
|
|
|
|
# build modals and prevent them from disappearing.
|
2014-12-28 16:25:20 -05:00
|
|
|
@heroesModal = new PlayHeroesModal({supermodel: @supermodel, session: @session, confirmButtonI18N: 'play.next', level: @level, hadEverChosenHero: @options.hadEverChosenHero})
|
|
|
|
@inventoryModal = new InventoryModal({supermodel: @supermodel, session: @session, level: @level})
|
2014-11-10 15:47:24 -05:00
|
|
|
@heroesModalDestroy = @heroesModal.destroy
|
|
|
|
@inventoryModalDestroy = @inventoryModal.destroy
|
|
|
|
@heroesModal.destroy = @inventoryModal.destroy = _.noop
|
|
|
|
@listenTo @heroesModal, 'confirm-click', @onHeroesModalConfirmClicked
|
|
|
|
@listenToOnce @heroesModal, 'hero-loaded', @onceHeroLoaded
|
|
|
|
@listenTo @inventoryModal, 'choose-hero-click', @onChooseHeroClicked
|
|
|
|
@listenTo @inventoryModal, 'play-click', @onInventoryModalPlayClicked
|
2014-12-04 16:18:00 -05:00
|
|
|
@modalsLoaded = true
|
|
|
|
if @waitingToOpen
|
|
|
|
@waitingToOpen = false
|
|
|
|
@open()
|
2014-11-10 15:47:24 -05:00
|
|
|
|
2014-11-06 19:23:23 -05:00
|
|
|
open: ->
|
2014-12-04 16:18:00 -05:00
|
|
|
return @waitingToOpen = true unless @modalsLoaded
|
2014-11-07 12:08:30 -05:00
|
|
|
firstModal = if @options.hadEverChosenHero then @inventoryModal else @heroesModal
|
2014-11-26 11:53:06 -05:00
|
|
|
if (not _.isEqual(lastHeroesEarned, me.get('earned')?.heroes ? []) or
|
|
|
|
not _.isEqual(lastHeroesPurchased, me.get('purchased')?.heroes ? []))
|
|
|
|
console.log 'Showing hero picker because heroes earned/purchased has changed.'
|
|
|
|
firstModal = @heroesModal
|
2014-12-28 16:25:20 -05:00
|
|
|
else if allowedHeroSlugs = @level.get 'allowedHeroes'
|
2014-11-26 11:53:06 -05:00
|
|
|
unless _.find(allowedHeroSlugs, (slug) -> ThangType.heroes[slug] is me.get('heroConfig')?.thangType)
|
|
|
|
firstModal = @heroesModal
|
|
|
|
lastHeroesEarned = me.get('earned')?.heroes ? []
|
|
|
|
lastHeroesPurchased = me.get('purchased')?.heroes ? []
|
|
|
|
|
2014-11-06 19:23:23 -05:00
|
|
|
@options.parent.openModalView(firstModal)
|
|
|
|
# @inventoryModal.onShown() # replace?
|
2014-11-26 11:53:06 -05:00
|
|
|
@playSound 'game-menu-open'
|
2014-11-07 12:08:30 -05:00
|
|
|
|
|
|
|
|
2014-11-06 19:23:23 -05:00
|
|
|
#- Modal events
|
|
|
|
|
|
|
|
onceHeroLoaded: (e) ->
|
2014-12-04 16:18:00 -05:00
|
|
|
@inventoryModal.setHero(e.hero) if window.currentModal is @inventoryModal
|
2014-11-07 12:08:30 -05:00
|
|
|
|
2014-11-06 19:23:23 -05:00
|
|
|
onHeroesModalConfirmClicked: (e) ->
|
|
|
|
@options.parent.openModalView(@inventoryModal)
|
|
|
|
@inventoryModal.render()
|
|
|
|
@inventoryModal.didReappear()
|
|
|
|
@inventoryModal.onShown()
|
2014-12-06 12:46:37 -05:00
|
|
|
@inventoryModal.setHero(e.hero) if e.hero
|
2015-02-27 19:07:41 -05:00
|
|
|
window.tracker?.trackEvent 'Choose Inventory', category: 'Play Level'
|
2014-11-06 19:23:23 -05:00
|
|
|
|
|
|
|
onChooseHeroClicked: ->
|
|
|
|
@options.parent.openModalView(@heroesModal)
|
|
|
|
@heroesModal.render()
|
|
|
|
@heroesModal.didReappear()
|
|
|
|
@inventoryModal.endHighlight()
|
2015-02-27 19:07:41 -05:00
|
|
|
window.tracker?.trackEvent 'Change Hero', category: 'Play Level'
|
2014-11-07 12:08:30 -05:00
|
|
|
|
2014-11-06 19:23:23 -05:00
|
|
|
onInventoryModalPlayClicked: ->
|
|
|
|
@navigatingToPlay = true
|
2014-11-29 19:46:36 -05:00
|
|
|
PlayLevelView = 'views/play/level/PlayLevelView'
|
|
|
|
LadderView = 'views/ladder/LadderView'
|
2014-11-06 19:23:23 -05:00
|
|
|
viewClass = if @options.levelPath is 'ladder' then LadderView else PlayLevelView
|
|
|
|
Backbone.Mediator.publish 'router:navigate', {
|
|
|
|
route: "/play/#{@options.levelPath || 'level'}/#{@options.levelID}"
|
|
|
|
viewClass: viewClass
|
|
|
|
viewArgs: [{supermodel: @supermodel}, @options.levelID]
|
2014-11-07 12:08:30 -05:00
|
|
|
}
|
2014-11-09 19:19:18 -05:00
|
|
|
|
|
|
|
destroy: ->
|
2014-12-04 16:18:00 -05:00
|
|
|
@heroesModalDestroy?.call @heroesModal unless @heroesModal?.destroyed
|
|
|
|
@inventoryModalDestroy?.call @inventoryModal unless @inventoryModal?.destroyed
|
2014-11-09 19:19:18 -05:00
|
|
|
super()
|