2014-02-07 18:51:05 -05:00
|
|
|
RootView = require 'views/kinds/RootView'
|
|
|
|
Level = require 'models/Level'
|
|
|
|
LevelSession = require 'models/LevelSession'
|
2014-04-22 15:42:26 -04:00
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
2014-03-02 15:43:21 -05:00
|
|
|
{teamDataFromLevel} = require './ladder/utils'
|
2014-03-16 23:28:02 -04:00
|
|
|
{me} = require 'lib/auth'
|
2014-03-11 16:59:12 -04:00
|
|
|
application = require 'application'
|
2014-03-03 12:03:44 -05:00
|
|
|
|
2014-03-02 15:43:21 -05:00
|
|
|
LadderTabView = require './ladder/ladder_tab'
|
2014-03-02 16:24:41 -05:00
|
|
|
MyMatchesTabView = require './ladder/my_matches_tab'
|
2014-04-14 21:52:32 -04:00
|
|
|
SimulateTabView = require './ladder/simulate_tab'
|
2014-03-03 12:03:44 -05:00
|
|
|
LadderPlayModal = require './ladder/play_modal'
|
2014-04-17 15:57:08 -04:00
|
|
|
CocoClass = require 'lib/CocoClass'
|
|
|
|
|
2014-02-07 18:51:05 -05:00
|
|
|
|
|
|
|
HIGHEST_SCORE = 1000000
|
|
|
|
|
|
|
|
class LevelSessionsCollection extends CocoCollection
|
|
|
|
url: ''
|
|
|
|
model: LevelSession
|
2014-02-20 16:58:35 -05:00
|
|
|
|
2014-02-07 18:51:05 -05:00
|
|
|
constructor: (levelID) ->
|
|
|
|
super()
|
2014-03-02 16:24:41 -05:00
|
|
|
@url = "/db/level/#{levelID}/my_sessions"
|
2014-02-07 18:51:05 -05:00
|
|
|
|
|
|
|
module.exports = class LadderView extends RootView
|
|
|
|
id: 'ladder-view'
|
|
|
|
template: require 'templates/play/ladder'
|
2014-02-14 19:53:34 -05:00
|
|
|
|
2014-03-12 14:39:09 -04:00
|
|
|
subscriptions:
|
|
|
|
'application:idle-changed': 'onIdleChanged'
|
|
|
|
|
2014-02-14 19:53:34 -05:00
|
|
|
events:
|
2014-03-03 12:03:44 -05:00
|
|
|
'click .play-button': 'onClickPlayButton'
|
2014-03-16 23:28:02 -04:00
|
|
|
'click a': 'onClickedLink'
|
2014-02-14 19:53:34 -05:00
|
|
|
|
2014-03-02 15:43:21 -05:00
|
|
|
constructor: (options, @levelID) ->
|
|
|
|
super(options)
|
|
|
|
@level = new Level(_id:@levelID)
|
2014-04-17 15:57:08 -04:00
|
|
|
@levelRes = @supermodel.addModelResource(@level, 'level')
|
|
|
|
@levelRes.load()
|
2014-04-12 16:29:49 -04:00
|
|
|
|
2014-03-02 16:24:41 -05:00
|
|
|
@sessions = new LevelSessionsCollection(levelID)
|
2014-04-17 15:57:08 -04:00
|
|
|
@sessionRes = @supermodel.addModelResource(@sessions, 'level_sessions_collection')
|
|
|
|
@sessionRes.load()
|
|
|
|
|
2014-03-02 15:43:21 -05:00
|
|
|
@teams = []
|
|
|
|
|
2014-04-03 21:43:29 -04:00
|
|
|
onLoaded: ->
|
2014-03-02 15:43:21 -05:00
|
|
|
@teams = teamDataFromLevel @level
|
2014-04-19 13:06:47 -04:00
|
|
|
@render()
|
2014-03-02 15:43:21 -05:00
|
|
|
|
|
|
|
getRenderData: ->
|
|
|
|
ctx = super()
|
|
|
|
ctx.level = @level
|
|
|
|
ctx.link = "/play/level/#{@level.get('name')}"
|
|
|
|
ctx.teams = @teams
|
|
|
|
ctx.levelID = @levelID
|
2014-03-03 17:59:39 -05:00
|
|
|
ctx.levelDescription = marked(@level.get('description')) if @level.get('description')
|
2014-04-17 15:57:08 -04:00
|
|
|
ctx._ = _
|
2014-03-02 15:43:21 -05:00
|
|
|
ctx
|
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
2014-04-19 13:09:50 -04:00
|
|
|
# console.debug 'gintau', 'ladder_view-afterRender', @supermodel.finished()
|
2014-04-12 16:29:49 -04:00
|
|
|
return unless @supermodel.finished()
|
2014-03-02 16:24:41 -05:00
|
|
|
@insertSubView(@ladderTab = new LadderTabView({}, @level, @sessions))
|
|
|
|
@insertSubView(@myMatchesTab = new MyMatchesTabView({}, @level, @sessions))
|
2014-04-14 21:52:32 -04:00
|
|
|
@insertSubView(@simulateTab = new SimulateTabView())
|
2014-04-15 18:01:54 -04:00
|
|
|
@refreshInterval = setInterval(@fetchSessionsAndRefreshViews.bind(@), 20 * 1000)
|
2014-03-08 14:37:33 -05:00
|
|
|
hash = document.location.hash[1..] if document.location.hash
|
2014-03-09 14:52:05 -04:00
|
|
|
if hash and not (hash in ['my-matches', 'simulate', 'ladder'])
|
2014-03-08 14:37:33 -05:00
|
|
|
@showPlayModal(hash) if @sessions.loaded
|
2014-03-03 15:21:59 -05:00
|
|
|
|
2014-03-03 15:10:24 -05:00
|
|
|
fetchSessionsAndRefreshViews: ->
|
2014-04-12 16:29:49 -04:00
|
|
|
return if @destroyed or application.userIsIdle or @$el.find('#simulate.active').length or (new Date() - 2000 < @lastRefreshTime) or not @supermodel.finished()
|
2014-03-03 15:10:24 -05:00
|
|
|
@sessions.fetch({"success": @refreshViews})
|
2014-03-03 15:21:59 -05:00
|
|
|
|
2014-03-03 15:10:24 -05:00
|
|
|
refreshViews: =>
|
2014-03-18 14:26:15 -04:00
|
|
|
return if @destroyed or application.userIsIdle
|
2014-03-19 21:26:38 -04:00
|
|
|
@lastRefreshTime = new Date()
|
|
|
|
@ladderTab.refreshLadder()
|
|
|
|
@myMatchesTab.refreshMatches()
|
|
|
|
console.log "Refreshed sessions for ladder and matches."
|
2014-03-12 14:39:09 -04:00
|
|
|
|
|
|
|
onIdleChanged: (e) ->
|
2014-03-20 18:40:02 -04:00
|
|
|
@fetchSessionsAndRefreshViews() unless e.idle
|
2014-03-03 15:21:59 -05:00
|
|
|
|
2014-03-03 12:03:44 -05:00
|
|
|
onClickPlayButton: (e) ->
|
2014-03-07 14:23:06 -05:00
|
|
|
@showPlayModal($(e.target).closest('.play-button').data('team'))
|
2014-04-17 15:57:08 -04:00
|
|
|
|
2014-03-07 14:23:06 -05:00
|
|
|
showPlayModal: (teamID) ->
|
2014-03-16 23:28:02 -04:00
|
|
|
return @showApologeticSignupModal() if me.get('anonymous')
|
2014-03-03 12:03:44 -05:00
|
|
|
session = (s for s in @sessions.models when s.get('team') is teamID)[0]
|
|
|
|
modal = new LadderPlayModal({}, @level, session, teamID)
|
2014-03-03 15:21:59 -05:00
|
|
|
@openModalView modal
|
2014-03-17 01:17:56 -04:00
|
|
|
|
2014-03-16 23:28:02 -04:00
|
|
|
showApologeticSignupModal: ->
|
|
|
|
SignupModal = require 'views/modal/signup_modal'
|
|
|
|
@openModalView(new SignupModal({showRequiredError:true}))
|
|
|
|
|
|
|
|
onClickedLink: (e) ->
|
|
|
|
link = $(e.target).closest('a').attr('href')
|
2014-03-17 01:17:56 -04:00
|
|
|
if link?.startsWith('/play/level') and me.get('anonymous')
|
2014-03-16 23:28:02 -04:00
|
|
|
e.stopPropagation()
|
|
|
|
e.preventDefault()
|
|
|
|
@showApologeticSignupModal()
|
2014-03-17 01:17:56 -04:00
|
|
|
|
2014-03-07 15:15:16 -05:00
|
|
|
destroy: ->
|
|
|
|
clearInterval @refreshInterval
|
2014-04-17 15:57:08 -04:00
|
|
|
super()
|