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-04-18 14:17:13 -04:00
|
|
|
@nameMap[opponent.userID] ?= nameMap[opponent.userID].name
|
2014-03-02 15:43:21 -05:00
|
|
|
@finishRendering()
|
|
|
|
|
|
|
|
$.ajax('/db/user/-/names', {
|
|
|
|
data: {ids: ids}
|
|
|
|
type: 'POST'
|
|
|
|
success: success
|
|
|
|
})
|
|
|
|
|
|
|
|
finishRendering: ->
|
|
|
|
@startsLoading = false
|
|
|
|
@render()
|
|
|
|
|
2014-04-02 21:41:10 -04:00
|
|
|
|
|
|
|
|
2014-03-02 15:43:21 -05:00
|
|
|
getRenderData: ->
|
|
|
|
ctx = super()
|
|
|
|
ctx.level = @level
|
|
|
|
ctx.levelID = @level.get('slug') or @level.id
|
|
|
|
ctx.teams = @teams
|
|
|
|
|
2014-03-11 19:31:39 -04:00
|
|
|
convertMatch = (match, submitDate) =>
|
2014-03-02 15:43:21 -05:00
|
|
|
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
|
2014-03-11 19:31:39 -04:00
|
|
|
stale: match.date < submitDate
|
2014-03-02 15:43:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for team in @teams
|
|
|
|
team.session = (s for s in @sessions.models when s.get('team') is team.id)[0]
|
2014-05-05 19:59:12 -04:00
|
|
|
team.readyToRank = team.session?.readyToRank()
|
2014-03-11 21:06:48 -04:00
|
|
|
team.isRanking = team.session?.get('isRanking')
|
2014-03-11 19:31:39 -04:00
|
|
|
team.matches = (convertMatch(match, team.session.get('submitDate')) 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-13 22:20:22 -04:00
|
|
|
scoreHistory = team.session?.get('scoreHistory')
|
|
|
|
if scoreHistory?.length > 1
|
2014-04-02 21:41:10 -04:00
|
|
|
team.scoreHistory = scoreHistory
|
2014-03-13 22:20:22 -04:00
|
|
|
scoreHistory = _.last scoreHistory, 100 # Chart URL needs to be under 2048 characters for GET
|
2014-05-05 19:59:12 -04:00
|
|
|
|
2014-03-13 22:20:22 -04:00
|
|
|
team.currentScore = Math.round scoreHistory[scoreHistory.length - 1][1] * 100
|
2014-03-11 00:30:46 -04:00
|
|
|
team.chartColor = team.primaryColor.replace '#', ''
|
2014-03-13 22:20:22 -04:00
|
|
|
#times = (s[0] for s in scoreHistory)
|
|
|
|
#times = ((100 * (t - times[0]) / (times[times.length - 1] - times[0])).toFixed(1) for t in times)
|
|
|
|
# Let's try being independent of time.
|
|
|
|
times = (i for s, i in scoreHistory)
|
|
|
|
scores = (s[1] for s in scoreHistory)
|
2014-03-20 18:40:02 -04:00
|
|
|
lowest = _.min scores #.concat([0])
|
|
|
|
highest = _.max scores #.concat(50)
|
2014-03-11 00:30:46 -04:00
|
|
|
scores = (Math.round(100 * (s - lowest) / (highest - lowest)) for s in scores)
|
|
|
|
team.chartData = times.join(',') + '|' + scores.join(',')
|
2014-03-13 22:20:22 -04:00
|
|
|
team.minScore = Math.round(100 * lowest)
|
|
|
|
team.maxScore = Math.round(100 * highest)
|
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')
|
2014-03-15 10:56:44 -04:00
|
|
|
session = _.find @sessions.models, {id: sessionID}
|
2014-03-11 19:31:39 -04:00
|
|
|
rankingState = 'unavailable'
|
2014-05-05 19:59:12 -04:00
|
|
|
if session.readyToRank()
|
2014-03-11 19:31:39 -04:00
|
|
|
rankingState = 'rank'
|
|
|
|
else if session.get 'isRanking'
|
|
|
|
rankingState = 'ranking'
|
|
|
|
@setRankingButtonText button, rankingState
|
2014-05-05 19:59:12 -04:00
|
|
|
|
2014-04-02 21:41:10 -04:00
|
|
|
@$el.find('.score-chart-wrapper').each (i, el) =>
|
|
|
|
scoreWrapper = $(el)
|
|
|
|
team = _.find @teams, name: scoreWrapper.data('team-name')
|
2014-04-04 17:22:30 -04:00
|
|
|
@generateScoreLineChart(scoreWrapper.attr('id'), team.scoreHistory, team.name)
|
2014-03-02 15:43:21 -05:00
|
|
|
|
2014-04-04 17:22:30 -04:00
|
|
|
generateScoreLineChart: (wrapperID, scoreHistory,teamName) =>
|
2014-05-05 19:59:12 -04:00
|
|
|
margin =
|
2014-04-02 21:41:10 -04:00
|
|
|
top: 20
|
|
|
|
right: 20
|
|
|
|
bottom: 30
|
|
|
|
left: 50
|
2014-05-05 19:59:12 -04:00
|
|
|
|
2014-04-02 21:41:10 -04:00
|
|
|
width = 450 - margin.left - margin.right
|
|
|
|
height = 125
|
|
|
|
x = d3.time.scale().range([0,width])
|
|
|
|
y = d3.scale.linear().range([height,0])
|
2014-05-05 19:59:12 -04:00
|
|
|
|
2014-04-02 21:41:10 -04:00
|
|
|
xAxis = d3.svg.axis().scale(x).orient("bottom").ticks(4).outerTickSize(0)
|
|
|
|
yAxis = d3.svg.axis().scale(y).orient("left").ticks(4).outerTickSize(0)
|
2014-05-05 19:59:12 -04:00
|
|
|
|
2014-04-02 21:41:10 -04:00
|
|
|
line = d3.svg.line().x(((d) -> x(d.date))).y((d) -> y(d.close))
|
|
|
|
selector = "#" + wrapperID
|
2014-05-05 19:59:12 -04:00
|
|
|
|
2014-04-02 21:41:10 -04:00
|
|
|
svg = d3.select(selector).append("svg")
|
|
|
|
.attr("width", width + margin.left + margin.right)
|
|
|
|
.attr("height", height + margin.top + margin.bottom)
|
|
|
|
.append("g")
|
|
|
|
.attr("transform","translate(#{margin.left},#{margin.top})")
|
|
|
|
time = 0
|
|
|
|
data = scoreHistory.map (d) ->
|
|
|
|
time +=1
|
|
|
|
return {
|
|
|
|
date: time
|
|
|
|
close: d[1] * 100
|
|
|
|
}
|
2014-05-05 19:59:12 -04:00
|
|
|
|
2014-04-02 21:41:10 -04:00
|
|
|
x.domain(d3.extent(data, (d) -> d.date))
|
|
|
|
y.domain(d3.extent(data, (d) -> d.close))
|
2014-05-05 19:59:12 -04:00
|
|
|
|
2014-04-02 21:41:10 -04:00
|
|
|
svg.append("g")
|
|
|
|
.attr("class", "y axis")
|
|
|
|
.call(yAxis)
|
|
|
|
.append("text")
|
|
|
|
.attr("transform", "rotate(-90)")
|
|
|
|
.attr("y",4)
|
|
|
|
.attr("dy", ".75em")
|
|
|
|
.style("text-anchor","end")
|
|
|
|
.text("Score")
|
2014-04-04 17:22:30 -04:00
|
|
|
lineClass = "line"
|
|
|
|
if teamName.toLowerCase() is "ogres" then lineClass = "ogres-line"
|
|
|
|
if teamName.toLowerCase() is "humans" then lineClass = "humans-line"
|
2014-04-02 21:41:10 -04:00
|
|
|
svg.append("path")
|
|
|
|
.datum(data)
|
2014-04-04 17:22:30 -04:00
|
|
|
.attr("class",lineClass)
|
2014-04-02 21:41:10 -04:00
|
|
|
.attr("d",line)
|
2014-03-02 15:43:21 -05:00
|
|
|
|
|
|
|
rankSession: (e) ->
|
|
|
|
button = $(e.target).closest('.rank-button')
|
|
|
|
sessionID = button.data('session-id')
|
2014-03-15 10:56:44 -04:00
|
|
|
session = _.find @sessions.models, {id: sessionID}
|
2014-05-12 22:56:12 -04:00
|
|
|
return unless session.readyToRank()
|
2014-03-02 15:43:21 -05:00
|
|
|
|
2014-03-11 19:31:39 -04:00
|
|
|
@setRankingButtonText(button, 'submitting')
|
2014-03-27 17:55:45 -04:00
|
|
|
success = =>
|
2014-03-24 13:58:40 -04:00
|
|
|
@setRankingButtonText(button, 'submitted')
|
|
|
|
failure = (jqxhr, textStatus, errorThrown) =>
|
|
|
|
console.log jqxhr.responseText
|
|
|
|
@setRankingButtonText(button, 'failed')
|
2014-05-14 14:42:15 -04:00
|
|
|
transpiledCode = @transpileSession session
|
|
|
|
|
|
|
|
ajaxData =
|
|
|
|
session: sessionID
|
|
|
|
levelID: @level.id
|
|
|
|
originalLevelID: @level.attributes.original
|
|
|
|
levelMajorVersion: @level.attributes.version.major
|
|
|
|
transpiledCode: transpiledCode
|
2014-03-09 15:53:11 -04:00
|
|
|
|
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
|
2014-03-24 13:58:40 -04:00
|
|
|
error: failure
|
2014-03-02 15:43:21 -05:00
|
|
|
}
|
|
|
|
|
2014-05-14 14:42:15 -04:00
|
|
|
transpileSession: (session) ->
|
|
|
|
submittedCode = session.get('code')
|
|
|
|
transpiledCode = {}
|
|
|
|
for thang, spells of submittedCode
|
|
|
|
transpiledCode[thang] = {}
|
|
|
|
for spellID, spell of spells
|
|
|
|
#DRY this
|
|
|
|
aetherOptions =
|
|
|
|
problems: {}
|
|
|
|
language: "javascript"
|
|
|
|
functionName: spellID
|
|
|
|
functionParameters: []
|
|
|
|
yieldConditionally: spellID is "plan"
|
|
|
|
globals: ['Vector', '_']
|
|
|
|
protectAPI: true
|
|
|
|
includeFlow: false
|
|
|
|
if spellID is "hear" then aetherOptions["functionParameters"] = ["speaker","message","data"]
|
|
|
|
|
|
|
|
aether = new Aether aetherOptions
|
|
|
|
transpiledCode[thang][spellID] = aether.transpile spell
|
|
|
|
transpiledCode
|
|
|
|
|
2014-03-02 15:43:21 -05:00
|
|
|
setRankingButtonText: (rankButton, spanClass) ->
|
|
|
|
rankButton.find('span').addClass('hidden')
|
|
|
|
rankButton.find(".#{spanClass}").removeClass('hidden')
|
|
|
|
rankButton.toggleClass 'disabled', spanClass isnt 'rank'
|