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
|
|
|
|
2015-08-15 09:45:38 -04:00
|
|
|
constructor: (options, @levelID, @leagueType, @leagueID) ->
|
2014-03-02 15:43:21 -05:00
|
|
|
super(options)
|
2014-06-30 22:16:26 -04:00
|
|
|
@level = @supermodel.loadModel(new Level(_id: @levelID), 'level').model
|
2015-02-11 16:12:42 -05:00
|
|
|
@sessions = @supermodel.loadCollection(new LevelSessionsCollection(@levelID), 'your_sessions', {cache: false}).model
|
2014-03-02 15:43:21 -05:00
|
|
|
@teams = []
|
2015-08-15 09:45:38 -04:00
|
|
|
@loadLeague()
|
2015-11-18 17:02:45 -05:00
|
|
|
@course = new Course()
|
2015-08-15 09:45:38 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
resourceString = if @leagueType is 'clan' then 'clans.clan' else 'courses.course'
|
2015-08-15 09:45:38 -04:00
|
|
|
@league = @supermodel.loadModel(new modelClass(_id: @leagueID), resourceString).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')})
|
|
|
|
@course = @supermodel.loadModel(course, 'courses.course').model
|
2015-12-03 16:46:50 -05:00
|
|
|
@listenToOnce @course, 'sync', @render
|
2014-03-02 15:43:21 -05:00
|
|
|
|
2014-04-03 21:43:29 -04:00
|
|
|
onLoaded: ->
|
2014-03-02 15:43:21 -05:00
|
|
|
@teams = teamDataFromLevel @level
|
2014-06-11 22:38:41 -04:00
|
|
|
super()
|
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')
|
2015-08-15 09:45:38 -04:00
|
|
|
ctx.leagueType = @leagueType
|
|
|
|
ctx.league = @league
|
2014-04-17 15:57:08 -04:00
|
|
|
ctx._ = _
|
2015-09-15 19:36:50 -04:00
|
|
|
if tournamentEndDate = {greed: 1402444800000, 'criss-cross': 1410912000000, 'zero-sum': 1428364800000, 'ace-of-coders': 1444867200000}[@levelID]
|
2015-03-27 00:05:34 -04:00
|
|
|
ctx.tournamentTimeLeft = moment(new Date(tournamentEndDate)).fromNow()
|
2015-09-15 19:36:50 -04:00
|
|
|
if tournamentStartDate = {'zero-sum': 1427472000000, 'ace-of-coders': 1442417400000}[@levelID]
|
2015-03-27 00:05:34 -04:00
|
|
|
ctx.tournamentTimeElapsed = moment(new Date(tournamentStartDate)).fromNow()
|
2014-11-29 19:46:36 -05:00
|
|
|
ctx.winners = require('./tournament_results')[@levelID]
|
2014-03-02 15:43:21 -05:00
|
|
|
ctx
|
|
|
|
|
|
|
|
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()
|