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'
|
|
|
|
#CourseInstance = require 'models/CourseInstance'
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
loadLeague: ->
|
|
|
|
@leagueID = @leagueType = null unless @leagueType in ['clan'] #, 'course']
|
|
|
|
return unless @leagueID
|
|
|
|
modelClass = if @leagueType is 'clan' then Clan else null# else CourseInstance
|
|
|
|
resourceString = if @leagueType is 'clan' then 'clans.clan' else null# else 'courses.course'
|
|
|
|
@league = @supermodel.loadModel(new modelClass(_id: @leagueID), resourceString).model
|
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-03-27 00:05:34 -04:00
|
|
|
if tournamentEndDate = {greed: 1402444800000, 'criss-cross': 1410912000000, 'zero-sum': 1428364800000}[@levelID]
|
|
|
|
ctx.tournamentTimeLeft = moment(new Date(tournamentEndDate)).fromNow()
|
|
|
|
if tournamentStartDate = {'zero-sum': 1427472000000}[@levelID]
|
|
|
|
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))
|
|
|
|
@insertSubView(@simulateTab = new SimulateTabView(league: @league))
|
2015-02-26 20:20:27 -05:00
|
|
|
@refreshInterval = setInterval(@fetchSessionsAndRefreshViews.bind(@), 60 * 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()
|
|
|
|
@myMatchesTab.refreshMatches()
|
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
|
|
|
|
console.log humanSession, ogreSession
|
|
|
|
return unless humanSession and ogreSession
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopImmediatePropagation()
|
|
|
|
url = "/play/spectate/#{@level.get('slug')}?session-one=#{humanSession}&session-two=#{ogreSession}"
|
|
|
|
Backbone.Mediator.publish 'router:navigate', route: url
|
|
|
|
|
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()
|