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'
|
|
|
|
|
2014-03-02 16:24:41 -05:00
|
|
|
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:
|
2014-03-02 16:24:41 -05:00
|
|
|
'click .rank-button': 'rankSession'
|
2014-03-02 15:43:21 -05:00
|
|
|
|
|
|
|
constructor: (options, @level, @sessions) ->
|
|
|
|
super(options)
|
2014-03-09 16:22:22 -04:00
|
|
|
@nameMap = {}
|
2014-03-03 15:10:24 -05:00
|
|
|
@refreshMatches()
|
2014-03-09 15:53:11 -04:00
|
|
|
|
2014-03-03 15:10:24 -05:00
|
|
|
refreshMatches: ->
|
2014-03-02 15:43:21 -05:00
|
|
|
@teams = teamDataFromLevel @level
|
|
|
|
@loadNames()
|
|
|
|
|
|
|
|
loadNames: ->
|
2014-03-09 16:22:22 -04:00
|
|
|
# Only fetch the names for the userIDs we don't already have in @nameMap
|
2014-03-02 15:43:21 -05:00
|
|
|
ids = []
|
|
|
|
for session in @sessions.models
|
2014-03-09 16:22:22 -04:00
|
|
|
for match in (session.get('matches') or [])
|
|
|
|
id = match.opponents[0].userID
|
|
|
|
ids.push id unless @nameMap[id]
|
|
|
|
|
|
|
|
return @finishRendering() unless ids.length
|
2014-03-02 15:43:21 -05:00
|
|
|
|
2014-03-09 16:22:22 -04:00
|
|
|
success = (nameMap) =>
|
2014-03-02 16:24:41 -05:00
|
|
|
for session in @sessions.models
|
|
|
|
for match in session.get('matches') or []
|
|
|
|
opponent = match.opponents[0]
|
2014-03-09 16:22:22 -04:00
|
|
|
@nameMap[opponent.userID] = 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)
|
2014-03-03 12:20:14 -05:00
|
|
|
team.matches = (convertMatch(match) for match in team.session?.get('matches') or [])
|
2014-03-02 15:43:21 -05:00
|
|
|
team.matches.reverse()
|
2014-03-03 12:20:14 -05:00
|
|
|
team.score = (team.session?.get('totalScore') or 10).toFixed(2)
|
2014-03-09 21:46:11 -04:00
|
|
|
team.wins = _.filter(team.matches, {state: 'win'}).length
|
|
|
|
team.ties = _.filter(team.matches, {state: 'tie'}).length
|
|
|
|
team.losses = _.filter(team.matches, {state: 'loss'}).length
|
2014-03-10 23:22:25 -04:00
|
|
|
team.scoreHistory = team.session.get('scoreHistory')
|
|
|
|
team.chartColor = team.primaryColor.replace '#', ''
|
|
|
|
times = (s[0] for s in team.scoreHistory)
|
|
|
|
times = (100 * (t - times[0]) / (times[times.length - 1] - times[0]) for t in times)
|
|
|
|
scores = (s[1] for s in team.scoreHistory)
|
|
|
|
lowest = _.min scores
|
|
|
|
highest = _.max scores
|
|
|
|
scores = (100 * (s - lowest) / highest for s in scores)
|
|
|
|
team.chartData = times.join(',') + '|' + scores.join(',')
|
2014-03-09 15:53:11 -04:00
|
|
|
|
2014-03-02 15:43:21 -05:00
|
|
|
ctx
|
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
@$el.find('.rank-button').each (i, el) =>
|
2014-03-02 16:24:41 -05:00
|
|
|
button = $(el)
|
2014-03-02 15:43:21 -05:00
|
|
|
sessionID = button.data('session-id')
|
|
|
|
session = _.find @sessions.models, { id: sessionID }
|
2014-03-02 16:24:41 -05:00
|
|
|
@setRankingButtonText button, if @readyToRank(session) then 'rank' else 'unavailable'
|
2014-03-02 15:43:21 -05:00
|
|
|
|
|
|
|
readyToRank: (session) ->
|
2014-03-09 15:53:11 -04:00
|
|
|
return false unless session?.get('levelID') # If it hasn't been denormalized, then it's not ready.
|
2014-03-03 12:20:14 -05:00
|
|
|
c1 = session?.get('code')
|
|
|
|
c2 = session?.get('submittedCode')
|
2014-03-02 15:43:21 -05:00
|
|
|
c1 and not _.isEqual(c1, c2)
|
|
|
|
|
|
|
|
rankSession: (e) ->
|
|
|
|
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-09 15:53:11 -04:00
|
|
|
|
2014-03-07 21:16:48 -05:00
|
|
|
ajaxData = { session: sessionID, levelID: @level.id, originalLevelID: @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'
|