CocoView = require 'views/kinds/CocoView' CocoClass = require 'lib/CocoClass' SimulatorsLeaderboardCollection = require 'collections/SimulatorsLeaderboardCollection' Simulator = require 'lib/simulator/Simulator' {me} = require 'lib/auth' module.exports = class SimulateTabView extends CocoView id: 'simulate-tab-view' template: require 'templates/play/ladder/simulate_tab' events: 'click #simulate-button': 'onSimulateButtonClick' 'click #simulate-all-button': 'onSimulateAllButtonClick' constructor: (options) -> super(options) @simulatorsLeaderboardData = new SimulatorsLeaderboardData(me) @simulatorsLeaderboardDataRes = @supermodel.addModelResource(@simulatorsLeaderboardData, 'top_simulators') @simulatorsLeaderboardDataRes.load() onLoaded: -> super() @render() getRenderData: -> ctx = super() ctx.simulationStatus = @simulationStatus ctx.simulatorsLeaderboardData = @simulatorsLeaderboardData ctx.numberOfGamesInQueue = @simulatorsLeaderboardData.numberOfGamesInQueue ctx._ = _ ctx afterRender: -> super() # Simulations onSimulateButtonClick: (e) -> application.tracker?.trackEvent 'Simulate Button Click', {} $('#simulate-button').prop 'disabled', true $('#simulate-button').text 'Simulating...' @simulateNextGame() simulateNextGame: -> unless @simulator @simulator = new Simulator() @listenTo @simulator, 'statusUpdate', @updateSimulationStatus # Work around simulator getting super slow on Chrome fetchAndSimulateTaskOriginal = @simulator.fetchAndSimulateTask @simulator.fetchAndSimulateTask = => if @simulator.simulatedByYou >= 5 console.log '------------------- Destroying Simulator and making a new one -----------------' @simulator.destroy() @simulator = null @simulateNextGame() else fetchAndSimulateTaskOriginal.apply @simulator @simulator.fetchAndSimulateTask() refresh: -> success = (numberOfGamesInQueue) -> $('#games-in-queue').text numberOfGamesInQueue $.ajax '/queue/messagesInQueueCount', {success} updateSimulationStatus: (simulationStatus, sessions) -> if simulationStatus is 'Fetching simulation data!' @simulationMatchDescription = '' @simulationSpectateLink = '' @simulationStatus = _.string.escapeHTML(simulationStatus) try if sessions? @simulationMatchDescription = '' @simulationSpectateLink = "/play/spectate/#{@simulator.level.get('slug')}?" for session, index in sessions # TODO: Fetch names from Redis, the creatorName is denormalized @simulationMatchDescription += "#{if index then ' vs ' else ''}#{session.creatorName or 'Anonymous'} (#{sessions[index].team})" @simulationSpectateLink += "session-#{if index then 'two' else 'one'}=#{session.sessionID}" @simulationMatchDescription += " on #{@simulator.level.get('name')}" catch e console.log "There was a problem with the named simulation status: #{e}" link = if @simulationSpectateLink then "#{_.string.escapeHTML(@simulationMatchDescription)}" else '' $('#simulation-status-text').html "