2014-02-17 20:42:41 -05:00
|
|
|
RootView = require 'views/kinds/RootView'
|
|
|
|
Level = require 'models/Level'
|
|
|
|
LevelSession = require 'models/LevelSession'
|
|
|
|
LeaderboardCollection = require 'collections/LeaderboardCollection'
|
|
|
|
|
2014-02-18 14:38:49 -05:00
|
|
|
module.exports = class LadderTeamView extends RootView
|
2014-02-17 20:42:41 -05:00
|
|
|
id: 'ladder-team-view'
|
|
|
|
template: require 'templates/play/ladder/team'
|
|
|
|
startsLoading: true
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-18 14:38:49 -05:00
|
|
|
events:
|
|
|
|
'click #rank-button': 'rankSession'
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-17 20:42:41 -05:00
|
|
|
# PART 1: Loading Level/Session
|
|
|
|
|
|
|
|
constructor: (options, @levelID, @team) ->
|
|
|
|
super(options)
|
2014-02-20 16:58:35 -05:00
|
|
|
@otherTeam = if team is 'ogres' then 'humans' else 'ogres'
|
2014-02-17 20:42:41 -05:00
|
|
|
@level = new Level(_id:@levelID)
|
|
|
|
@level.fetch()
|
|
|
|
@level.once 'sync', @onLevelLoaded, @
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-17 20:42:41 -05:00
|
|
|
url = "/db/level/#{@levelID}/session?team=#{@team}"
|
|
|
|
@session = new LevelSession()
|
|
|
|
@session.url = -> url
|
|
|
|
@session.fetch()
|
|
|
|
@session.once 'sync', @onSessionLoaded, @
|
|
|
|
|
|
|
|
onLevelLoaded: -> @startLoadingChallengersMaybe()
|
|
|
|
onSessionLoaded: -> @startLoadingChallengersMaybe()
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-17 20:42:41 -05:00
|
|
|
# PART 2: Loading some challengers if we don't have any matches yet
|
|
|
|
|
|
|
|
startLoadingChallengersMaybe: ->
|
|
|
|
return unless @level.loaded and @session.loaded
|
|
|
|
matches = @session.get('matches')
|
2014-02-18 15:54:47 -05:00
|
|
|
if matches?.length then @loadNames() else @loadChallengers()
|
2014-02-17 20:42:41 -05:00
|
|
|
|
|
|
|
loadChallengers: ->
|
2014-02-20 16:58:35 -05:00
|
|
|
@challengers = new ChallengersData(@level, @team, @otherTeam, @session)
|
2014-02-17 20:42:41 -05:00
|
|
|
@challengers.on 'sync', @loadNames, @
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-17 20:42:41 -05:00
|
|
|
# PART 3: Loading the names of the other users
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-17 20:42:41 -05:00
|
|
|
loadNames: ->
|
|
|
|
ids = []
|
|
|
|
ids.push match.opponents[0].userID for match in @session.get('matches') or []
|
|
|
|
ids = ids.concat(@challengers.playerIDs()) if @challengers
|
|
|
|
|
|
|
|
success = (@nameMap) =>
|
|
|
|
for match in @session.get('matches') or []
|
|
|
|
opponent = match.opponents[0]
|
|
|
|
opponent.userName = @nameMap[opponent.userID]
|
|
|
|
@finishRendering()
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-17 20:42:41 -05:00
|
|
|
$.ajax('/db/user/-/names', {
|
|
|
|
data: {ids: ids}
|
|
|
|
type: 'POST'
|
|
|
|
success: success
|
|
|
|
})
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-17 20:42:41 -05:00
|
|
|
# PART 4: Rendering
|
|
|
|
|
|
|
|
finishRendering: ->
|
|
|
|
@startsLoading = false
|
|
|
|
@render()
|
|
|
|
|
|
|
|
getRenderData: ->
|
|
|
|
ctx = super()
|
|
|
|
ctx.level = @level
|
|
|
|
ctx.levelID = @levelID
|
|
|
|
ctx.teamName = _.string.titleize @team
|
|
|
|
ctx.teamID = @team
|
2014-02-20 16:58:35 -05:00
|
|
|
ctx.otherTeamID = @otherTeam
|
2014-02-17 20:42:41 -05:00
|
|
|
ctx.challengers = if not @startsLoading then @getChallengers() else {}
|
2014-02-18 14:38:49 -05:00
|
|
|
ctx.readyToRank = @readyToRank()
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-18 15:27:10 -05:00
|
|
|
convertMatch = (match) =>
|
|
|
|
opponent = match.opponents[0]
|
2014-02-17 20:42:41 -05:00
|
|
|
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-02-18 15:54:47 -05:00
|
|
|
|
2014-02-17 20:42:41 -05:00
|
|
|
ctx.matches = (convertMatch(match) for match in @session.get('matches') or [])
|
2014-02-19 11:19:13 -05:00
|
|
|
ctx.matches.reverse()
|
2014-02-24 16:03:04 -05:00
|
|
|
ctx.score = (@session.get('totalScore') or 10).toFixed(2)
|
2014-02-17 20:42:41 -05:00
|
|
|
ctx
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-18 14:38:49 -05:00
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
@setRankingButtonText(if @readyToRank() then 'rank' else 'unavailable')
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-18 14:38:49 -05:00
|
|
|
readyToRank: ->
|
|
|
|
c1 = @session.get('code')
|
|
|
|
c2 = @session.get('submittedCode')
|
|
|
|
c1 and not _.isEqual(c1, c2)
|
|
|
|
|
2014-02-17 20:42:41 -05:00
|
|
|
getChallengers: ->
|
|
|
|
# make an object of challengers to everything needed to link to them
|
|
|
|
challengers = {}
|
|
|
|
if @challengers
|
|
|
|
easyInfo = @challengeInfoFromSession(@challengers.easyPlayer.models[0])
|
|
|
|
mediumInfo = @challengeInfoFromSession(@challengers.mediumPlayer.models[0])
|
|
|
|
hardInfo = @challengeInfoFromSession(@challengers.hardPlayer.models[0])
|
|
|
|
else
|
|
|
|
matches = @session.get('matches')
|
|
|
|
won = (m for m in matches when m.metrics.rank < m.opponents[0].metrics.rank)
|
|
|
|
lost = (m for m in matches when m.metrics.rank > m.opponents[0].metrics.rank)
|
|
|
|
tied = (m for m in matches when m.metrics.rank is m.opponents[0].metrics.rank)
|
|
|
|
easyInfo = @challengeInfoFromMatches(won)
|
|
|
|
mediumInfo = @challengeInfoFromMatches(tied)
|
|
|
|
hardInfo = @challengeInfoFromMatches(lost)
|
|
|
|
@addChallenger easyInfo, challengers, 'easy'
|
|
|
|
@addChallenger mediumInfo, challengers, 'medium'
|
|
|
|
@addChallenger hardInfo, challengers, 'hard'
|
|
|
|
challengers
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-17 20:42:41 -05:00
|
|
|
addChallenger: (info, challengers, title) ->
|
|
|
|
# check for duplicates first
|
2014-02-18 14:38:49 -05:00
|
|
|
return unless info
|
2014-02-17 20:42:41 -05:00
|
|
|
for key, value of challengers
|
|
|
|
return if value.sessionID is info.sessionID
|
|
|
|
challengers[title] = info
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-17 20:42:41 -05:00
|
|
|
challengeInfoFromSession: (session) ->
|
|
|
|
# given a model from the db, return info needed for a link to the match
|
|
|
|
return unless session
|
|
|
|
return {
|
|
|
|
sessionID: session.id
|
|
|
|
opponentName: @nameMap[session.get('creator')] or 'Anoner'
|
|
|
|
opponentID: session.get('creator')
|
|
|
|
}
|
|
|
|
|
|
|
|
challengeInfoFromMatches: (matches) ->
|
|
|
|
return unless matches?.length
|
|
|
|
match = _.sample matches
|
|
|
|
opponent = match.opponents[0]
|
|
|
|
return {
|
|
|
|
sessionID: opponent.sessionID
|
|
|
|
opponentName: opponent.userName or 'Anoner'
|
|
|
|
opponentID: opponent.userID
|
|
|
|
}
|
2014-02-18 14:38:49 -05:00
|
|
|
|
|
|
|
rankSession: ->
|
|
|
|
return unless @readyToRank()
|
|
|
|
@setRankingButtonText('ranking')
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-18 14:38:49 -05:00
|
|
|
success = => @setRankingButtonText('ranked')
|
|
|
|
failure = => @setRankingButtonText('failed')
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-18 14:38:49 -05:00
|
|
|
$.ajax '/queue/scoring', {
|
|
|
|
type: 'POST'
|
|
|
|
data: { session: @session.id }
|
|
|
|
success: success
|
|
|
|
failure: failure
|
|
|
|
}
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-18 14:38:49 -05:00
|
|
|
setRankingButtonText: (spanClass) ->
|
|
|
|
rankButton = $('#rank-button')
|
|
|
|
rankButton.find('span').addClass('hidden')
|
|
|
|
rankButton.find(".#{spanClass}").removeClass('hidden')
|
|
|
|
rankButton.toggleClass 'disabled', spanClass isnt 'rank'
|
2014-02-17 20:42:41 -05:00
|
|
|
|
|
|
|
class ChallengersData
|
2014-02-20 16:58:35 -05:00
|
|
|
constructor: (@level, @team, @otherTeam, @session) ->
|
2014-02-17 20:42:41 -05:00
|
|
|
_.extend @, Backbone.Events
|
|
|
|
score = @session?.get('totalScore') or 25
|
2014-02-20 16:58:35 -05:00
|
|
|
@easyPlayer = new LeaderboardCollection(@level, {order:1, scoreOffset: score - 5, limit: 1, team: @otherTeam})
|
2014-02-17 20:42:41 -05:00
|
|
|
@easyPlayer.fetch()
|
|
|
|
@easyPlayer.once 'sync', @challengerLoaded, @
|
2014-02-20 16:58:35 -05:00
|
|
|
@mediumPlayer = new LeaderboardCollection(@level, {order:1, scoreOffset: score, limit: 1, team: @otherTeam})
|
2014-02-17 20:42:41 -05:00
|
|
|
@mediumPlayer.fetch()
|
|
|
|
@mediumPlayer.once 'sync', @challengerLoaded, @
|
2014-02-20 16:58:35 -05:00
|
|
|
@hardPlayer = new LeaderboardCollection(@level, {order:-1, scoreOffset: score + 5, limit: 1, team: @otherTeam})
|
2014-02-17 20:42:41 -05:00
|
|
|
@hardPlayer.fetch()
|
|
|
|
@hardPlayer.once 'sync', @challengerLoaded, @
|
|
|
|
|
|
|
|
challengerLoaded: ->
|
|
|
|
if @allLoaded()
|
|
|
|
@loaded = true
|
|
|
|
@trigger 'sync'
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-17 20:42:41 -05:00
|
|
|
playerIDs: ->
|
|
|
|
collections = [@easyPlayer, @mediumPlayer, @hardPlayer]
|
|
|
|
(c.models[0].get('creator') for c in collections when c?.models[0])
|
2014-02-18 15:54:47 -05:00
|
|
|
|
2014-02-17 20:42:41 -05:00
|
|
|
allLoaded: ->
|
2014-02-18 15:54:47 -05:00
|
|
|
_.all [@easyPlayer.loaded, @mediumPlayer.loaded, @hardPlayer.loaded]
|