2014-11-28 20:49:41 -05:00
|
|
|
CocoView = require 'views/core/CocoView'
|
2014-05-17 14:55:41 -04:00
|
|
|
template = require 'templates/play/common/ladder_submission'
|
2014-08-30 16:43:56 -04:00
|
|
|
{createAetherOptions} = require 'lib/aether_utils'
|
2015-09-08 19:30:18 -04:00
|
|
|
LevelSession = require 'models/LevelSession'
|
2014-05-17 14:55:41 -04:00
|
|
|
|
|
|
|
module.exports = class LadderSubmissionView extends CocoView
|
2014-06-30 22:16:26 -04:00
|
|
|
className: 'ladder-submission-view'
|
2014-05-17 14:55:41 -04:00
|
|
|
template: template
|
|
|
|
|
|
|
|
events:
|
|
|
|
'click .rank-button': 'rankSession'
|
2014-05-19 17:59:43 -04:00
|
|
|
'click .help-simulate': 'onHelpSimulate'
|
2014-05-17 14:55:41 -04:00
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
|
|
|
@session = options.session
|
2015-09-04 19:21:35 -04:00
|
|
|
@mirrorSession = options.mirrorSession
|
2014-05-17 14:55:41 -04:00
|
|
|
@level = options.level
|
|
|
|
|
|
|
|
getRenderData: ->
|
|
|
|
ctx = super()
|
|
|
|
ctx.readyToRank = @session?.readyToRank()
|
2014-05-19 17:59:43 -04:00
|
|
|
ctx.isRanking = @session?.get('isRanking')
|
|
|
|
ctx.simulateURL = "/play/ladder/#{@level.get('slug')}#simulate"
|
2014-05-19 22:51:18 -04:00
|
|
|
ctx.lastSubmitted = moment(submitDate).fromNow() if submitDate = @session?.get('submitDate')
|
2014-05-17 14:55:41 -04:00
|
|
|
ctx
|
2014-05-19 17:59:43 -04:00
|
|
|
|
2014-05-17 14:55:41 -04:00
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
return unless @supermodel.finished()
|
|
|
|
@rankButton = @$el.find('.rank-button')
|
|
|
|
@updateButton()
|
|
|
|
|
|
|
|
updateButton: ->
|
|
|
|
rankingState = 'unavailable'
|
|
|
|
if @session?.readyToRank()
|
|
|
|
rankingState = 'rank'
|
|
|
|
else if @session?.get 'isRanking'
|
|
|
|
rankingState = 'ranking'
|
|
|
|
@setRankingButtonText rankingState
|
|
|
|
|
|
|
|
setRankingButtonText: (spanClass) ->
|
2014-05-19 17:59:43 -04:00
|
|
|
@rankButton.find('span').hide()
|
|
|
|
@rankButton.find(".#{spanClass}").show()
|
2014-05-17 14:55:41 -04:00
|
|
|
@rankButton.toggleClass 'disabled', spanClass isnt 'rank'
|
2014-05-19 17:59:43 -04:00
|
|
|
helpSimulate = spanClass in ['submitted', 'ranking']
|
|
|
|
@$el.find('.help-simulate').toggle(helpSimulate, 'slow')
|
2014-05-19 22:51:18 -04:00
|
|
|
showLastSubmitted = not (spanClass in ['submitting'])
|
|
|
|
@$el.find('.last-submitted').toggle(showLastSubmitted)
|
2014-05-17 14:55:41 -04:00
|
|
|
|
2015-03-27 00:05:34 -04:00
|
|
|
showApologeticSignupModal: ->
|
2016-02-25 18:24:16 -05:00
|
|
|
CreateAccountModal = require 'views/core/CreateAccountModal'
|
|
|
|
@openModalView(new CreateAccountModal({showRequiredError: true}))
|
2015-03-27 00:05:34 -04:00
|
|
|
|
2014-05-17 14:55:41 -04:00
|
|
|
rankSession: (e) ->
|
|
|
|
return unless @session.readyToRank()
|
2015-03-27 00:05:34 -04:00
|
|
|
return @showApologeticSignupModal() if me.get('anonymous')
|
2014-11-26 09:58:23 -05:00
|
|
|
@playSound 'menu-button-click'
|
2014-05-17 14:55:41 -04:00
|
|
|
@setRankingButtonText 'submitting'
|
|
|
|
success = =>
|
|
|
|
@setRankingButtonText 'submitted' unless @destroyed
|
|
|
|
Backbone.Mediator.publish 'ladder:game-submitted', session: @session, level: @level
|
|
|
|
failure = (jqxhr, textStatus, errorThrown) =>
|
|
|
|
console.log jqxhr.responseText
|
|
|
|
@setRankingButtonText 'failed' unless @destroyed
|
2016-05-05 16:22:30 -04:00
|
|
|
@session.save null, success: =>
|
2015-02-12 14:59:38 -05:00
|
|
|
ajaxData =
|
|
|
|
session: @session.id
|
|
|
|
levelID: @level.id
|
|
|
|
originalLevelID: @level.get('original')
|
|
|
|
levelMajorVersion: @level.get('version').major
|
2015-09-04 19:21:35 -04:00
|
|
|
ajaxOptions =
|
2015-02-12 14:59:38 -05:00
|
|
|
type: 'POST'
|
|
|
|
data: ajaxData
|
|
|
|
success: success
|
|
|
|
error: failure
|
2015-09-07 21:00:48 -04:00
|
|
|
if @mirrorSession and @mirrorSession.get('submittedCode')
|
2015-09-04 19:21:35 -04:00
|
|
|
# Also submit the mirrorSession after the main session submits successfully.
|
|
|
|
mirrorAjaxData = _.clone ajaxData
|
|
|
|
mirrorAjaxData.session = @mirrorSession.id
|
2015-09-08 19:30:18 -04:00
|
|
|
mirrorCode = @mirrorSession.get('code')
|
2015-09-04 19:21:35 -04:00
|
|
|
if @session.get('team') is 'humans'
|
2015-09-08 19:30:18 -04:00
|
|
|
mirrorCode['hero-placeholder-1'] = @session.get('code')['hero-placeholder']
|
2015-09-04 19:21:35 -04:00
|
|
|
else
|
2015-09-08 19:30:18 -04:00
|
|
|
mirrorCode['hero-placeholder'] = @session.get('code')['hero-placeholder-1']
|
2015-09-04 19:21:35 -04:00
|
|
|
mirrorAjaxOptions = _.clone ajaxOptions
|
|
|
|
mirrorAjaxOptions.data = mirrorAjaxData
|
2015-09-08 19:30:18 -04:00
|
|
|
ajaxOptions.success = =>
|
2016-05-05 16:22:30 -04:00
|
|
|
patch = code: mirrorCode, codeLanguage: @session.get('codeLanguage')
|
2015-09-08 19:30:18 -04:00
|
|
|
tempSession = new LevelSession _id: @mirrorSession.id
|
|
|
|
tempSession.save patch, patch: true, type: 'PUT', success: ->
|
|
|
|
$.ajax '/queue/scoring', mirrorAjaxOptions
|
2015-09-04 19:21:35 -04:00
|
|
|
|
|
|
|
$.ajax '/queue/scoring', ajaxOptions
|
2014-05-17 14:55:41 -04:00
|
|
|
|
2014-05-19 17:59:43 -04:00
|
|
|
onHelpSimulate: ->
|
2014-11-26 09:58:23 -05:00
|
|
|
@playSound 'menu-button-click'
|
2014-05-19 17:59:43 -04:00
|
|
|
$('a[href="#simulate"]').tab('show')
|