codecombat/app/views/ladder/LadderView.coffee

139 lines
5.6 KiB
CoffeeScript
Raw Normal View History

RootView = require 'views/core/RootView'
2014-02-07 18:51:05 -05:00
Level = require 'models/Level'
LevelSession = require 'models/LevelSession'
CocoCollection = require 'collections/CocoCollection'
{teamDataFromLevel} = require './utils'
{me} = require 'core/auth'
application = require 'core/application'
LadderTabView = require './LadderTabView'
MyMatchesTabView = require './MyMatchesTabView'
SimulateTabView = require './SimulateTabView'
LadderPlayModal = require './LadderPlayModal'
CocoClass = require 'core/CocoClass'
Clan = require 'models/Clan'
CourseInstance = require 'models/CourseInstance'
Course = require 'models/Course'
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'
usesSocialMedia: true
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'
'click .spectate-button': 'onClickSpectateButton'
2014-02-14 19:53:34 -05:00
initialize: (options, @levelID, @leagueType, @leagueID) ->
2016-02-18 11:44:40 -05:00
@level = @supermodel.loadModel(new Level(_id: @levelID)).model
@level.once 'sync', =>
return if @destroyed
@levelDescription = marked(@level.get('description')) if @level.get('description')
2016-06-08 05:36:01 -04:00
@teams = teamDataFromLevel @level
@sessions = @supermodel.loadCollection(new LevelSessionsCollection(@levelID), 'your_sessions', {cache: false}).model
2016-06-02 04:44:19 -04:00
@winners = require('./tournament_results')[@levelID]
if tournamentEndDate = {greed: 1402444800000, 'criss-cross': 1410912000000, 'zero-sum': 1428364800000, 'ace-of-coders': 1444867200000}[@levelID]
@tournamentTimeLeft = moment(new Date(tournamentEndDate)).fromNow()
if tournamentStartDate = {'zero-sum': 1427472000000, 'ace-of-coders': 1442417400000}[@levelID]
@tournamentTimeElapsed = moment(new Date(tournamentStartDate)).fromNow()
@loadLeague()
loadLeague: ->
@leagueID = @leagueType = null unless @leagueType in ['clan', 'course']
return unless @leagueID
modelClass = if @leagueType is 'clan' then Clan else CourseInstance
2016-02-18 11:44:40 -05:00
@league = @supermodel.loadModel(new modelClass(_id: @leagueID)).model
if @leagueType is 'course'
if @league.loaded
@onCourseInstanceLoaded @league
else
@listenToOnce @league, 'sync', @onCourseInstanceLoaded
onCourseInstanceLoaded: (courseInstance) ->
return if @destroyed
course = new Course({_id: courseInstance.get('courseID')})
2016-02-18 11:44:40 -05:00
@course = @supermodel.loadModel(course).model
@listenToOnce @course, 'sync', @render
2014-03-02 15:43:21 -05:00
afterRender: ->
super()
return unless @supermodel.finished()
@insertSubView(@ladderTab = new LadderTabView({league: @league}, @level, @sessions))
@insertSubView(@myMatchesTab = new MyMatchesTabView({league: @league}, @level, @sessions))
@insertSubView(@simulateTab = new SimulateTabView(league: @league, level: @level, leagueID: @leagueID))
highLoad = true
@refreshDelay = switch
when not application.isProduction() then 10 # Refresh very quickly in develompent.
when @league then 20 # Refresh quickly when looking at a league ladder.
when not highLoad then 30 # Refresh slowly when in production.
when not me.isAnonymous() then 60 # Refresh even more slowly during HoC scaling.
else 300 # Refresh super slowly if anonymous during HoC scaling.
@refreshInterval = setInterval(@fetchSessionsAndRefreshViews.bind(@), @refreshDelay * 1000)
hash = document.location.hash[1..] if document.location.hash
if hash and not (hash in ['my-matches', 'simulate', 'ladder', 'prizes', 'rules', 'winners'])
@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()
@sessions.fetch success: @refreshViews, cache: false
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 @refreshDelay
@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'))
onClickSpectateButton: (e) ->
humanSession = @ladderTab.spectateTargets?.humans
ogreSession = @ladderTab.spectateTargets?.ogres
return unless humanSession and ogreSession
e.preventDefault()
e.stopImmediatePropagation()
url = "/play/spectate/#{@level.get('slug')}?session-one=#{humanSession}&session-two=#{ogreSession}"
url += '&league=' + @league.id if @league
url += '&autoplay=false' if key.command
window.open url, if key.command then '_blank' else 'spectate' # New tab for spectating specific matches
#Backbone.Mediator.publish 'router:navigate', route: url
showPlayModal: (teamID) ->
session = (s for s in @sessions.models when s.get('team') is teamID)[0]
modal = new LadderPlayModal({league: @league}, @level, session, teamID)
2014-03-03 15:21:59 -05:00
@openModalView modal
2014-03-17 01:17:56 -04:00
onClickedLink: (e) ->
link = $(e.target).closest('a').attr('href')
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')
2015-04-07 23:56:24 -04:00
if link and /#winners/.test link
@$el.find('a[href="#winners"]').tab('show')
2014-03-17 01:17:56 -04:00
destroy: ->
clearInterval @refreshInterval
super()