2014-11-28 20:49:41 -05:00
|
|
|
RootView = require 'views/core/RootView'
|
2014-02-07 18:51:05 -05:00
|
|
|
Level = require 'models/Level'
|
|
|
|
LevelSession = require 'models/LevelSession'
|
2014-04-22 15:42:26 -04:00
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
2014-05-19 14:58:45 -04:00
|
|
|
{teamDataFromLevel} = require './utils'
|
2014-11-28 20:49:41 -05:00
|
|
|
{me} = require 'core/auth'
|
|
|
|
application = require 'core/application'
|
2014-03-03 12:03:44 -05:00
|
|
|
|
2014-07-23 10:02:45 -04:00
|
|
|
LadderTabView = require './LadderTabView'
|
|
|
|
MyMatchesTabView = require './MyMatchesTabView'
|
|
|
|
SimulateTabView = require './SimulateTabView'
|
|
|
|
LadderPlayModal = require './LadderPlayModal'
|
2014-11-28 20:49:41 -05:00
|
|
|
CocoClass = require 'core/CocoClass'
|
2014-04-17 15:57:08 -04:00
|
|
|
|
2015-08-15 09:45:38 -04:00
|
|
|
Clan = require 'models/Clan'
|
2015-11-18 17:02:45 -05:00
|
|
|
CourseInstance = require 'models/CourseInstance'
|
|
|
|
Course = require 'models/Course'
|
2015-08-15 09:45:38 -04:00
|
|
|
|
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'
|
2014-05-19 14:58:45 -04:00
|
|
|
template: require 'templates/play/ladder/ladder'
|
2014-10-15 16:43:26 -04:00
|
|
|
usesSocialMedia: true
|
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-06-01 01:09:41 -04:00
|
|
|
'click a:not([data-toggle])': 'onClickedLink'
|
2015-03-20 18:32:35 -04:00
|
|
|
'click .spectate-button': 'onClickSpectateButton'
|
2014-02-14 19:53:34 -05:00
|
|
|
|
2016-06-02 22:23:35 -04:00
|
|
|
initialize: (options, @levelID, @leagueType, @leagueID) ->
|
2016-02-18 11:44:40 -05:00
|
|
|
@level = @supermodel.loadModel(new Level(_id: @levelID)).model
|
2016-08-30 19:05:25 -04:00
|
|
|
onLoaded = =>
|
2016-07-13 13:04:43 -04:00
|
|
|
return if @destroyed
|
2016-06-02 22:23:35 -04:00
|
|
|
@levelDescription = marked(@level.get('description')) if @level.get('description')
|
2016-06-08 05:36:01 -04:00
|
|
|
@teams = teamDataFromLevel @level
|
2016-08-30 19:05:25 -04:00
|
|
|
|
|
|
|
if @level.loaded then onLoaded() else @level.once('sync', onLoaded)
|
2015-02-11 16:12:42 -05:00
|
|
|
@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()
|
|
|
|
|
2015-08-15 09:45:38 -04:00
|
|
|
@loadLeague()
|
|
|
|
|
|
|
|
loadLeague: ->
|
2015-11-18 17:02:45 -05:00
|
|
|
@leagueID = @leagueType = null unless @leagueType in ['clan', 'course']
|
2015-08-15 09:45:38 -04:00
|
|
|
return unless @leagueID
|
2015-11-18 17:02:45 -05:00
|
|
|
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
|
2015-11-18 17:02:45 -05:00
|
|
|
if @leagueType is 'course'
|
2015-12-01 15:23:01 -05:00
|
|
|
if @league.loaded
|
|
|
|
@onCourseInstanceLoaded @league
|
|
|
|
else
|
|
|
|
@listenToOnce @league, 'sync', @onCourseInstanceLoaded
|
2015-11-18 17:02:45 -05:00
|
|
|
|
|
|
|
onCourseInstanceLoaded: (courseInstance) ->
|
2015-12-01 15:23:01 -05:00
|
|
|
return if @destroyed
|
2015-11-18 17:02:45 -05:00
|
|
|
course = new Course({_id: courseInstance.get('courseID')})
|
2016-02-18 11:44:40 -05:00
|
|
|
@course = @supermodel.loadModel(course).model
|
2015-12-03 16:46:50 -05:00
|
|
|
@listenToOnce @course, 'sync', @render
|
2014-03-02 15:43:21 -05:00
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
2014-04-12 16:29:49 -04:00
|
|
|
return unless @supermodel.finished()
|
2015-08-15 09:45:38 -04:00
|
|
|
@insertSubView(@ladderTab = new LadderTabView({league: @league}, @level, @sessions))
|
|
|
|
@insertSubView(@myMatchesTab = new MyMatchesTabView({league: @league}, @level, @sessions))
|
2015-12-06 12:20:30 -05:00
|
|
|
@insertSubView(@simulateTab = new SimulateTabView(league: @league, level: @level, leagueID: @leagueID))
|
2015-12-05 11:18:36 -05:00
|
|
|
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)
|
2014-03-08 14:37:33 -05:00
|
|
|
hash = document.location.hash[1..] if document.location.hash
|
2014-09-18 11:12:46 -04:00
|
|
|
if hash and not (hash in ['my-matches', 'simulate', 'ladder', 'prizes', 'rules', 'winners'])
|
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-05-16 16:16:35 -04:00
|
|
|
return if @destroyed or application.userIsIdle or (new Date() - 2000 < @lastRefreshTime) or not @supermodel.finished()
|
2015-02-11 16:12:42 -05:00
|
|
|
@sessions.fetch success: @refreshViews, cache: false
|
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()
|
2015-12-05 11:18:36 -05:00
|
|
|
@myMatchesTab.refreshMatches @refreshDelay
|
2014-05-16 16:16:35 -04:00
|
|
|
@simulateTab.refresh()
|
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
|
|
|
|
2015-03-20 18:32:35 -04:00
|
|
|
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}"
|
2015-09-04 19:21:35 -04:00
|
|
|
url += '&league=' + @league.id if @league
|
|
|
|
url += '&autoplay=false' if key.command
|
2015-09-03 18:04:57 -04:00
|
|
|
window.open url, if key.command then '_blank' else 'spectate' # New tab for spectating specific matches
|
|
|
|
#Backbone.Mediator.publish 'router:navigate', route: url
|
2015-03-20 18:32:35 -04:00
|
|
|
|
2014-03-07 14:23:06 -05:00
|
|
|
showPlayModal: (teamID) ->
|
2014-03-03 12:03:44 -05:00
|
|
|
session = (s for s in @sessions.models when s.get('team') is teamID)[0]
|
2015-08-19 18:30:37 -04:00
|
|
|
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
|
|
|
|
2014-03-16 23:28:02 -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')
|
2014-05-19 22:13:16 -04:00
|
|
|
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
|
|
|
|
2014-03-07 15:15:16 -05:00
|
|
|
destroy: ->
|
|
|
|
clearInterval @refreshInterval
|
2014-04-17 15:57:08 -04:00
|
|
|
super()
|