CocoView = require 'views/core/CocoView' CocoClass = require 'core/CocoClass' SimulatorsLeaderboardCollection = require 'collections/SimulatorsLeaderboardCollection' Simulator = require 'lib/simulator/Simulator' {me} = require 'core/auth' module.exports = class SimulateTabView extends CocoView id: 'simulate-tab-view' template: require 'templates/play/ladder/simulate_tab' events: 'click #simulate-button': 'onSimulateButtonClick' constructor: (options) -> super(options) @simulatorsLeaderboardData = new SimulatorsLeaderboardData(me) @simulatorsLeaderboardDataRes = @supermodel.addModelResource(@simulatorsLeaderboardData, 'top_simulators', {cache: false}) @simulatorsLeaderboardDataRes.load() require 'vendor/aether-javascript' require 'vendor/aether-python' require 'vendor/aether-coffeescript' require 'vendor/aether-lua' require 'vendor/aether-java' require 'vendor/aether-clojure' require 'vendor/aether-io' onLoaded: -> super() @render() if (document.location.hash is '#simulate' or @options.level.get('type') is 'course-ladder') and not @simulator @startSimulating() 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' @startSimulating() startSimulating: -> @simulationPageRefreshTimeout = _.delay @refreshAndContinueSimulating, 30 * 60 * 1000 @simulateNextGame() $('#simulate-button').prop 'disabled', true $('#simulate-button').text 'Simulating...' refreshAndContinueSimulating: => # We refresh the page every now and again to make sure simulations haven't gotten derailed by bogus games, and that simulators don't hang on to old, stale code or data. document.location.hash = '#simulate' document.location.reload() simulateNextGame: -> unless @simulator @simulator = new Simulator levelID: @options.level.get('slug'), leagueID: @options.leagueID @listenTo @simulator, 'statusUpdate', @updateSimulationStatus # Work around simulator getting super slow on Chrome fetchAndSimulateTaskOriginal = @simulator.fetchAndSimulateTask @simulator.fetchAndSimulateTask = => return if @destroyed if @simulator.simulatedByYou >= 20 console.log '------------------- Destroying Simulator and making a new one -----------------' @simulator.destroy() @simulator = null @simulateNextGame() else fetchAndSimulateTaskOriginal.apply @simulator @simulator.fetchAndSimulateTask() refresh: -> return # Queue-based scoring is currently not active anyway, so don't keep checking this until we fix it. success = (numberOfGamesInQueue) -> $('#games-in-queue').text numberOfGamesInQueue $.ajax '/queue/messagesInQueueCount', cache: false, success: 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 "