2014-11-28 20:49:41 -05:00
CocoView = require ' views/core/CocoView '
CocoClass = require ' core/CocoClass '
2014-04-14 21:52:32 -04:00
SimulatorsLeaderboardCollection = require ' collections/SimulatorsLeaderboardCollection '
Simulator = require ' lib/simulator/Simulator '
2014-11-28 20:49:41 -05:00
{ me } = require ' core/auth '
2014-04-14 21:52:32 -04:00
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 )
2015-02-11 16:12:42 -05:00
@simulatorsLeaderboardDataRes = @ supermodel . addModelResource ( @ simulatorsLeaderboardData , ' top_simulators ' , { cache: false } )
2014-04-19 13:06:47 -04:00
@ simulatorsLeaderboardDataRes . load ( )
2015-11-30 10:23:33 -05:00
require ' vendor/aether-javascript '
require ' vendor/aether-python '
require ' vendor/aether-coffeescript '
require ' vendor/aether-lua '
require ' vendor/aether-clojure '
require ' vendor/aether-io '
2014-04-19 13:06:47 -04:00
2014-04-14 21:52:32 -04:00
onLoaded: ->
super ( )
2014-04-19 13:06:47 -04:00
@ render ( )
2015-12-06 12:20:30 -05:00
if ( document . location . hash is ' # simulate ' or @ options . level . get ( ' type ' ) is ' course-ladder ' ) and not @ simulator
2014-12-09 12:00:34 -05:00
@ startSimulating ( )
2014-04-14 21:52:32 -04:00
getRenderData: ->
ctx = super ( )
ctx.simulationStatus = @ simulationStatus
ctx.simulatorsLeaderboardData = @ simulatorsLeaderboardData
2014-05-16 16:16:35 -04:00
ctx.numberOfGamesInQueue = @ simulatorsLeaderboardData . numberOfGamesInQueue
2014-04-14 21:52:32 -04:00
ctx._ = _
ctx
afterRender: ->
super ( )
# Simulations
onSimulateButtonClick: (e) ->
2015-02-27 19:07:41 -05:00
application . tracker ? . trackEvent ' Simulate Button Click '
2014-12-09 17:21:55 -05:00
@ startSimulating ( )
2014-12-09 12:00:34 -05:00
startSimulating: ->
2015-04-08 21:31:13 -04:00
@simulationPageRefreshTimeout = _ . delay @ refreshAndContinueSimulating , 30 * 60 * 1000
2014-12-09 12:00:34 -05:00
@ simulateNextGame ( )
2014-06-30 22:16:26 -04:00
$ ( ' # simulate-button ' ) . prop ' disabled ' , true
$ ( ' # simulate-button ' ) . text ' Simulating... '
2014-12-09 12:00:34 -05:00
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 ( )
2014-08-19 00:49:58 -04:00
simulateNextGame: ->
unless @ simulator
2015-12-06 12:20:30 -05:00
@simulator = new Simulator levelID: @ options . level . get ( ' slug ' ) , leagueID: @ options . leagueID
2014-08-19 00:49:58 -04:00
@ listenTo @ simulator , ' statusUpdate ' , @ updateSimulationStatus
# Work around simulator getting super slow on Chrome
fetchAndSimulateTaskOriginal = @ simulator . fetchAndSimulateTask
@simulator.fetchAndSimulateTask = =>
2015-04-08 21:31:13 -04:00
if @ simulator . simulatedByYou >= 20
2014-10-20 00:56:26 -04:00
console . log ' ------------------- Destroying Simulator and making a new one ----------------- '
2014-08-19 00:49:58 -04:00
@ simulator . destroy ( )
@simulator = null
@ simulateNextGame ( )
else
fetchAndSimulateTaskOriginal . apply @ simulator
2014-04-14 21:52:32 -04:00
@ simulator . fetchAndSimulateTask ( )
2014-05-16 16:16:35 -04:00
refresh: ->
2015-12-05 11:18:36 -05:00
return # Queue-based scoring is currently not active anyway, so don't keep checking this until we fix it.
2014-05-16 16:16:35 -04:00
success = (numberOfGamesInQueue) ->
2014-06-30 22:16:26 -04:00
$ ( ' # games-in-queue ' ) . text numberOfGamesInQueue
2015-02-11 16:12:42 -05:00
$ . ajax ' /queue/messagesInQueueCount ' , cache: false , success: success
2014-05-16 16:16:35 -04:00
2014-04-14 21:52:32 -04:00
updateSimulationStatus: (simulationStatus, sessions) ->
2014-08-19 00:49:58 -04:00
if simulationStatus is ' Fetching simulation data! '
@simulationMatchDescription = ' '
@simulationSpectateLink = ' '
2014-10-29 13:51:34 -04:00
@simulationStatus = _ . string . escapeHTML ( simulationStatus )
2014-04-14 21:52:32 -04:00
try
if sessions ?
2014-08-19 00:49:58 -04:00
@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 ' ) } "
2014-04-14 21:52:32 -04:00
catch e
console . log " There was a problem with the named simulation status: #{ e } "
2014-08-19 00:49:58 -04:00
link = if @ simulationSpectateLink then " <a href= #{ @ simulationSpectateLink } > #{ _ . string . escapeHTML ( @ simulationMatchDescription ) } </a> " else ' '
$ ( ' # simulation-status-text ' ) . html " <h3> #{ @ simulationStatus } </h3> #{ link } "
2014-04-14 21:52:32 -04:00
destroy: ->
2014-12-09 12:00:34 -05:00
clearTimeout @ simulationPageRefreshTimeout
2014-08-19 00:49:58 -04:00
@ simulator ? . destroy ( )
2014-04-14 21:52:32 -04:00
super ( )
class SimulatorsLeaderboardData extends CocoClass
# ##
Consolidates what you need to load for a leaderboard into a single Backbone Model - like object .
# ##
constructor: (@me) ->
super ( )
fetch: ->
2014-06-30 22:16:26 -04:00
@topSimulators = new SimulatorsLeaderboardCollection ( { order: - 1 , scoreOffset: - 1 , limit: 20 } )
2014-04-14 21:52:32 -04:00
promises = [ ]
promises . push @ topSimulators . fetch ( )
unless @ me . get ( ' anonymous ' )
score = @ me . get ( ' simulatedBy ' ) or 0
2014-05-16 16:16:35 -04:00
queueSuccess = (@numberOfGamesInQueue) =>
2015-02-11 16:12:42 -05:00
promises . push $ . ajax ' /queue/messagesInQueueCount ' , { success: queueSuccess , cache: false }
2014-06-30 22:16:26 -04:00
@playersAbove = new SimulatorsLeaderboardCollection ( { order: 1 , scoreOffset: score , limit: 4 } )
2014-04-14 21:52:32 -04:00
promises . push @ playersAbove . fetch ( )
if score
2014-06-30 22:16:26 -04:00
@playersBelow = new SimulatorsLeaderboardCollection ( { order: - 1 , scoreOffset: score , limit: 4 } )
2014-04-14 21:52:32 -04:00
promises . push @ playersBelow . fetch ( )
success = (@myRank) =>
2015-02-11 16:12:42 -05:00
promises . push $ . ajax ( " /db/user/me/simulator_leaderboard_rank?scoreOffset= #{ score } " , cache: false , success: success )
2014-05-16 16:16:35 -04:00
2014-04-14 21:52:32 -04:00
@promise = $ . when ( promises . . . )
@ promise . then @ onLoad
@ promise . fail @ onFail
@ promise
onLoad: =>
return if @ destroyed
@loaded = true
@ trigger ' sync ' , @
onFail: (resource, jqxhr) =>
return if @ destroyed
@ trigger ' error ' , @ , jqxhr
inTopSimulators: ->
return me . id in ( user . id for user in @ topSimulators . models )
nearbySimulators: ->
l = [ ]
above = @ playersAbove . models
l = l . concat ( above )
2014-04-16 18:20:59 -04:00
l . reverse ( )
2014-05-21 10:06:52 -04:00
l . push @ me
2014-04-14 21:52:32 -04:00
l = l . concat ( @ playersBelow . models ) if @ playersBelow
if @ myRank
startRank = @ myRank - 4
user.rank = startRank + i for user , i in l
l
allResources: ->
resources = [ @ topSimulators , @ playersAbove , @ playersBelow ]
return ( r for r in resources when r )