2014-02-07 18:51:05 -05:00
|
|
|
RootView = require 'views/kinds/RootView'
|
|
|
|
Level = require 'models/Level'
|
2014-02-20 11:06:11 -05:00
|
|
|
Simulator = require 'lib/simulator/Simulator'
|
2014-02-07 18:51:05 -05:00
|
|
|
LevelSession = require 'models/LevelSession'
|
|
|
|
CocoCollection = require 'models/CocoCollection'
|
2014-03-02 15:43:21 -05:00
|
|
|
{teamDataFromLevel} = require './ladder/utils'
|
2014-03-11 16:59:12 -04:00
|
|
|
application = require 'application'
|
2014-03-03 12:03:44 -05:00
|
|
|
|
2014-03-02 15:43:21 -05:00
|
|
|
LadderTabView = require './ladder/ladder_tab'
|
2014-03-02 16:24:41 -05:00
|
|
|
MyMatchesTabView = require './ladder/my_matches_tab'
|
2014-03-03 12:03:44 -05:00
|
|
|
LadderPlayModal = require './ladder/play_modal'
|
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'
|
|
|
|
template: require 'templates/play/ladder'
|
|
|
|
startsLoading: true
|
2014-02-14 19:53:34 -05:00
|
|
|
|
|
|
|
events:
|
|
|
|
'click #simulate-button': 'onSimulateButtonClick'
|
2014-02-20 11:06:11 -05:00
|
|
|
'click #simulate-all-button': 'onSimulateAllButtonClick'
|
2014-03-03 12:03:44 -05:00
|
|
|
'click .play-button': 'onClickPlayButton'
|
2014-02-14 19:53:34 -05:00
|
|
|
|
2014-03-02 15:43:21 -05:00
|
|
|
constructor: (options, @levelID) ->
|
|
|
|
super(options)
|
|
|
|
@level = new Level(_id:@levelID)
|
2014-03-12 01:01:38 -04:00
|
|
|
p1 = @level.fetch()
|
2014-03-02 16:24:41 -05:00
|
|
|
@sessions = new LevelSessionsCollection(levelID)
|
2014-03-12 01:01:38 -04:00
|
|
|
p2 = @sessions.fetch({})
|
2014-03-02 15:43:21 -05:00
|
|
|
@simulator = new Simulator()
|
|
|
|
@simulator.on 'statusUpdate', @updateSimulationStatus, @
|
|
|
|
@teams = []
|
2014-03-12 01:01:38 -04:00
|
|
|
$.when(p1, p2).then @onLoaded
|
2014-03-02 15:43:21 -05:00
|
|
|
|
2014-03-12 01:01:38 -04:00
|
|
|
onLoaded: =>
|
2014-03-02 15:43:21 -05:00
|
|
|
@teams = teamDataFromLevel @level
|
|
|
|
@startsLoading = false
|
|
|
|
@render()
|
|
|
|
|
|
|
|
getRenderData: ->
|
|
|
|
ctx = super()
|
|
|
|
ctx.level = @level
|
|
|
|
ctx.link = "/play/level/#{@level.get('name')}"
|
|
|
|
ctx.simulationStatus = @simulationStatus
|
|
|
|
ctx.teams = @teams
|
|
|
|
ctx.levelID = @levelID
|
2014-03-03 17:59:39 -05:00
|
|
|
ctx.levelDescription = marked(@level.get('description')) if @level.get('description')
|
2014-03-02 15:43:21 -05:00
|
|
|
ctx
|
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
return if @startsLoading
|
2014-03-02 16:24:41 -05:00
|
|
|
@insertSubView(@ladderTab = new LadderTabView({}, @level, @sessions))
|
|
|
|
@insertSubView(@myMatchesTab = new MyMatchesTabView({}, @level, @sessions))
|
2014-03-10 23:22:25 -04:00
|
|
|
@refreshInterval = setInterval(@fetchSessionsAndRefreshViews.bind(@), 10 * 1000)
|
2014-03-08 14:37:33 -05:00
|
|
|
hash = document.location.hash[1..] if document.location.hash
|
2014-03-09 14:52:05 -04:00
|
|
|
if hash and not (hash in ['my-matches', 'simulate', 'ladder'])
|
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: ->
|
|
|
|
@sessions.fetch({"success": @refreshViews})
|
2014-03-03 15:21:59 -05:00
|
|
|
|
2014-03-03 15:10:24 -05:00
|
|
|
refreshViews: =>
|
2014-03-11 16:59:12 -04:00
|
|
|
return if @destroyed or application.userIsIdle
|
2014-03-03 15:21:59 -05:00
|
|
|
@ladderTab.refreshLadder()
|
2014-03-03 15:10:24 -05:00
|
|
|
@myMatchesTab.refreshMatches()
|
|
|
|
console.log "refreshed views!"
|
2014-03-03 15:21:59 -05:00
|
|
|
|
2014-03-02 15:43:21 -05:00
|
|
|
# Simulations
|
|
|
|
|
2014-02-20 11:06:11 -05:00
|
|
|
onSimulateAllButtonClick: (e) ->
|
2014-03-02 15:43:21 -05:00
|
|
|
submitIDs = _.pluck @leaderboards[@teams[0].id].topPlayers.models, "id"
|
2014-02-14 19:53:34 -05:00
|
|
|
for ID in submitIDs
|
|
|
|
$.ajax
|
|
|
|
url: '/queue/scoring'
|
|
|
|
method: 'POST'
|
|
|
|
data:
|
|
|
|
session: ID
|
2014-02-20 13:40:16 -05:00
|
|
|
$("#simulate-all-button").prop "disabled", true
|
|
|
|
$("#simulate-all-button").text "Submitted all!"
|
2014-02-14 19:53:34 -05:00
|
|
|
|
2014-02-20 11:06:11 -05:00
|
|
|
onSimulateButtonClick: (e) ->
|
2014-02-20 13:40:16 -05:00
|
|
|
$("#simulate-button").prop "disabled",true
|
|
|
|
$("#simulate-button").text "Simulating..."
|
|
|
|
|
2014-02-20 11:06:11 -05:00
|
|
|
@simulator.fetchAndSimulateTask()
|
|
|
|
|
|
|
|
updateSimulationStatus: (simulationStatus, sessions)->
|
|
|
|
@simulationStatus = simulationStatus
|
|
|
|
try
|
|
|
|
if sessions?
|
|
|
|
#TODO: Fetch names from Redis, the creatorName is denormalized
|
|
|
|
creatorNames = (session.creatorName for session in sessions)
|
|
|
|
@simulationStatus = "Simulating game between "
|
|
|
|
for index in [0...creatorNames.length]
|
|
|
|
unless creatorNames[index]
|
|
|
|
creatorNames[index] = "Anonymous"
|
|
|
|
@simulationStatus += " and " + creatorNames[index]
|
|
|
|
@simulationStatus += "..."
|
|
|
|
catch e
|
|
|
|
console.log "There was a problem with the named simulation status: #{e}"
|
2014-02-20 16:58:35 -05:00
|
|
|
$("#simulation-status-text").text @simulationStatus
|
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-03-08 00:20:09 -05: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]
|
|
|
|
modal = new LadderPlayModal({}, @level, session, teamID)
|
2014-03-03 15:21:59 -05:00
|
|
|
@openModalView modal
|
2014-03-08 00:20:09 -05:00
|
|
|
|
2014-03-07 15:15:16 -05:00
|
|
|
destroy: ->
|
|
|
|
clearInterval @refreshInterval
|
|
|
|
@simulator.destroy()
|
|
|
|
super()
|