codecombat/app/views/play/ladder/ladder_view.coffee

112 lines
3.6 KiB
CoffeeScript
Raw Normal View History

2014-02-07 18:51:05 -05:00
RootView = require 'views/kinds/RootView'
Level = require 'models/Level'
LevelSession = require 'models/LevelSession'
CocoCollection = require 'collections/CocoCollection'
{teamDataFromLevel} = require './utils'
{me} = require 'lib/auth'
application = require 'application'
LadderTabView = require './ladder_tab'
MyMatchesTabView = require './my_matches_tab'
SimulateTabView = require './simulate_tab'
LadderPlayModal = require './play_modal'
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()
@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/ladder'
2014-02-14 19:53:34 -05:00
subscriptions:
'application:idle-changed': 'onIdleChanged'
2014-02-14 19:53:34 -05:00
events:
'click .play-button': 'onClickPlayButton'
'click a:not([data-toggle])': 'onClickedLink'
2014-02-14 19:53:34 -05:00
2014-03-02 15:43:21 -05:00
constructor: (options, @levelID) ->
super(options)
@level = @supermodel.loadModel(new Level(_id:@levelID), 'level').model
@sessions = @supermodel.loadCollection(new LevelSessionsCollection(levelID), 'your_sessions').model
2014-03-02 15:43:21 -05:00
@teams = []
onLoaded: ->
2014-03-02 15:43:21 -05:00
@teams = teamDataFromLevel @level
@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
ctx.levelDescription = marked(@level.get('description')) if @level.get('description')
ctx._ = _
ctx.tournamentTimeLeft = moment(new Date(1402444800000)).fromNow()
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()
return unless @supermodel.finished()
@insertSubView(@ladderTab = new LadderTabView({}, @level, @sessions))
@insertSubView(@myMatchesTab = new MyMatchesTabView({}, @level, @sessions))
@insertSubView(@simulateTab = new SimulateTabView())
@refreshInterval = setInterval(@fetchSessionsAndRefreshViews.bind(@), 20 * 1000)
hash = document.location.hash[1..] if document.location.hash
2014-05-20 11:23:25 -04:00
if hash and not (hash in ['my-matches', 'simulate', 'ladder', 'prizes', 'rules'])
@showPlayModal(hash) if @sessions.loaded
2014-03-03 15:21:59 -05:00
2014-03-03 15:10:24 -05:00
fetchSessionsAndRefreshViews: ->
return if @destroyed or application.userIsIdle 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: =>
return if @destroyed or application.userIsIdle
2014-03-19 21:26:38 -04:00
@lastRefreshTime = new Date()
@ladderTab.refreshLadder()
@myMatchesTab.refreshMatches()
@simulateTab.refresh()
onIdleChanged: (e) ->
2014-03-20 18:40:02 -04:00
@fetchSessionsAndRefreshViews() unless e.idle
2014-03-03 15:21:59 -05:00
onClickPlayButton: (e) ->
@showPlayModal($(e.target).closest('.play-button').data('team'))
showPlayModal: (teamID) ->
return @showApologeticSignupModal() if me.get('anonymous')
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
showApologeticSignupModal: ->
SignupModal = require 'views/modal/auth_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')
e.stopPropagation()
e.preventDefault()
@showApologeticSignupModal()
2014-05-19 20:39:56 -04:00
if link and /#rules$/.test link
@$el.find('a[href="#rules"]').tab('show')
if link and /#prizes/.test link
@$el.find('a[href="#prizes"]').tab('show')
2014-03-17 01:17:56 -04:00
destroy: ->
clearInterval @refreshInterval
super()