codecombat/app/views/play/ladder/my_matches_tab.coffee

110 lines
3.3 KiB
CoffeeScript
Raw Normal View History

2014-03-02 15:43:21 -05:00
CocoView = require 'views/kinds/CocoView'
Level = require 'models/Level'
LevelSession = require 'models/LevelSession'
LeaderboardCollection = require 'collections/LeaderboardCollection'
{teamDataFromLevel} = require './utils'
module.exports = class MyMatchesTabView extends CocoView
id: 'my-matches-tab-view'
template: require 'templates/play/ladder/my_matches_tab'
2014-03-02 15:43:21 -05:00
startsLoading: true
events:
'click .rank-button': 'rankSession'
2014-03-02 15:43:21 -05:00
constructor: (options, @level, @sessions) ->
super(options)
2014-03-03 15:10:24 -05:00
@refreshMatches()
refreshMatches: ->
2014-03-02 15:43:21 -05:00
@teams = teamDataFromLevel @level
@nameMap = {}
2014-03-02 15:43:21 -05:00
@loadNames()
loadNames: ->
ids = []
for session in @sessions.models
ids.push match.opponents[0].userID for match in session.get('matches') or []
success = (@nameMap) =>
for session in @sessions.models
for match in session.get('matches') or []
opponent = match.opponents[0]
opponent.userName = @nameMap[opponent.userID]
2014-03-02 15:43:21 -05:00
@finishRendering()
$.ajax('/db/user/-/names', {
data: {ids: ids}
type: 'POST'
success: success
})
finishRendering: ->
@startsLoading = false
@render()
getRenderData: ->
ctx = super()
ctx.level = @level
ctx.levelID = @level.get('slug') or @level.id
ctx.teams = @teams
convertMatch = (match) =>
opponent = match.opponents[0]
state = 'win'
state = 'loss' if match.metrics.rank > opponent.metrics.rank
state = 'tie' if match.metrics.rank is opponent.metrics.rank
{
state: state
opponentName: @nameMap[opponent.userID]
opponentID: opponent.userID
when: moment(match.date).fromNow()
sessionID: opponent.sessionID
}
for team in @teams
team.session = (s for s in @sessions.models when s.get('team') is team.id)[0]
team.readyToRank = @readyToRank(team.session)
team.matches = (convertMatch(match) for match in team.session?.get('matches') or [])
2014-03-02 15:43:21 -05:00
team.matches.reverse()
team.score = (team.session?.get('totalScore') or 10).toFixed(2)
2014-03-02 15:43:21 -05:00
ctx
afterRender: ->
super()
@$el.find('.rank-button').each (i, el) =>
button = $(el)
2014-03-02 15:43:21 -05:00
sessionID = button.data('session-id')
session = _.find @sessions.models, { id: sessionID }
@setRankingButtonText button, if @readyToRank(session) then 'rank' else 'unavailable'
2014-03-02 15:43:21 -05:00
readyToRank: (session) ->
c1 = session?.get('code')
c2 = session?.get('submittedCode')
2014-03-02 15:43:21 -05:00
c1 and not _.isEqual(c1, c2)
rankSession: (e) ->
2014-03-03 15:10:24 -05:00
console.log "Clicked"
2014-03-02 15:43:21 -05:00
button = $(e.target).closest('.rank-button')
sessionID = button.data('session-id')
session = _.find @sessions.models, { id: sessionID }
return unless @readyToRank(session)
@setRankingButtonText(button, 'ranking')
success = => @setRankingButtonText(button, 'ranked')
failure = => @setRankingButtonText(button, 'failed')
2014-03-06 21:48:41 -05:00
ajaxData = { session: sessionID, levelID: @level.attributes.original, levelMajorVersion: @level.attributes.version.major }
2014-03-02 15:43:21 -05:00
$.ajax '/queue/scoring', {
type: 'POST'
2014-03-06 21:48:41 -05:00
data: ajaxData
2014-03-02 15:43:21 -05:00
success: success
failure: failure
}
setRankingButtonText: (rankButton, spanClass) ->
rankButton.find('span').addClass('hidden')
rankButton.find(".#{spanClass}").removeClass('hidden')
rankButton.toggleClass 'disabled', spanClass isnt 'rank'