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 '
' click # simulate-all-button ' : ' onSimulateAllButtonClick '
constructor: (options) ->
super ( options )
@simulatorsLeaderboardData = new SimulatorsLeaderboardData ( me )
2014-04-19 13:06:47 -04:00
@simulatorsLeaderboardDataRes = @ supermodel . addModelResource ( @ simulatorsLeaderboardData , ' top_simulators ' )
@ simulatorsLeaderboardDataRes . load ( )
2014-12-04 11:33:26 -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 ( )
2014-12-09 12:00:34 -05:00
if document . location . hash is ' # simulate ' and not @ simulator
@ 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) ->
2014-05-15 18:10:50 -04: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: ->
@simulationPageRefreshTimeout = _ . delay @ refreshAndContinueSimulating , 20 * 60 * 1000
@ 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
@simulator = new Simulator ( )
@ listenTo @ simulator , ' statusUpdate ' , @ updateSimulationStatus
# Work around simulator getting super slow on Chrome
fetchAndSimulateTaskOriginal = @ simulator . fetchAndSimulateTask
@simulator.fetchAndSimulateTask = =>
if @ simulator . simulatedByYou >= 5
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: ->
success = (numberOfGamesInQueue) ->
2014-06-30 22:16:26 -04:00
$ ( ' # games-in-queue ' ) . text numberOfGamesInQueue
$ . ajax ' /queue/messagesInQueueCount ' , { 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
resimulateAllSessions: ->
postData =
originalLevelID: @ level . get ( ' original ' )
levelMajorVersion: @ level . get ( ' version ' ) . major
console . log postData
$ . ajax
url: ' /queue/scoring/resimulateAllSessions '
method: ' POST '
data: postData
complete: (jqxhr) ->
console . log jqxhr . responseText
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) =>
2014-06-30 22:16:26 -04:00
promises . push $ . ajax ' /queue/messagesInQueueCount ' , { success: queueSuccess }
@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) =>
2014-05-16 16:16:35 -04:00
2014-04-14 21:52:32 -04:00
promises . push $ . ajax " /db/user/me/simulator_leaderboard_rank?scoreOffset= #{ score } " , { 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 )