2014-11-28 20:49:41 -05:00
|
|
|
CocoView = require 'views/core/CocoView'
|
|
|
|
CocoClass = require 'core/CocoClass'
|
2014-03-02 15:43:21 -05:00
|
|
|
Level = require 'models/Level'
|
|
|
|
LevelSession = require 'models/LevelSession'
|
2014-04-22 15:42:26 -04:00
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
2014-04-15 18:01:54 -04:00
|
|
|
User = require 'models/User'
|
2014-03-02 15:43:21 -05:00
|
|
|
LeaderboardCollection = require 'collections/LeaderboardCollection'
|
|
|
|
{teamDataFromLevel} = require './utils'
|
2014-07-23 10:02:45 -04:00
|
|
|
ModelModal = require 'views/modal/ModelModal'
|
2014-11-29 18:31:34 -05:00
|
|
|
require 'vendor/d3'
|
2014-03-02 15:43:21 -05:00
|
|
|
|
|
|
|
HIGHEST_SCORE = 1000000
|
|
|
|
|
2014-03-03 16:21:05 -05:00
|
|
|
module.exports = class LadderTabView extends CocoView
|
2014-03-02 15:43:21 -05:00
|
|
|
id: 'ladder-tab-view'
|
2015-01-09 15:38:00 -05:00
|
|
|
template: require 'templates/play/ladder/ladder-tab-view'
|
2014-03-23 19:48:30 -04:00
|
|
|
|
2014-03-21 22:50:54 -04:00
|
|
|
events:
|
2014-03-22 00:11:51 -04:00
|
|
|
'click .connect-facebook': 'onConnectFacebook'
|
2014-03-26 17:19:05 -04:00
|
|
|
'click .connect-google-plus': 'onConnectGPlus'
|
2014-04-15 18:01:54 -04:00
|
|
|
'click .name-col-cell': 'onClickPlayerName'
|
2015-03-20 18:32:35 -04:00
|
|
|
'click .spectate-cell': 'onClickSpectateCell'
|
2014-04-15 18:01:54 -04:00
|
|
|
'click .load-more-ladder-entries': 'onLoadMoreLadderEntries'
|
2014-03-23 19:48:30 -04:00
|
|
|
|
2016-03-15 16:50:33 -04:00
|
|
|
# Refactored, to-reimplement
|
|
|
|
# subscriptions:
|
|
|
|
# 'auth:facebook-api-loaded': 'checkFriends'
|
|
|
|
# 'auth:gplus-api-loaded': 'checkFriends'
|
|
|
|
# 'auth:logged-in-with-facebook': 'onConnectedWithFacebook'
|
|
|
|
# 'auth:logged-in-with-gplus': 'onConnectedWithGPlus'
|
2014-03-02 15:43:21 -05:00
|
|
|
|
|
|
|
constructor: (options, @level, @sessions) ->
|
|
|
|
super(options)
|
|
|
|
@teams = teamDataFromLevel @level
|
|
|
|
@leaderboards = {}
|
2014-03-03 15:21:59 -05:00
|
|
|
@refreshLadder()
|
2015-09-08 09:44:26 -04:00
|
|
|
# Trying not loading the FP/G+ stuff for now to see if anyone complains they were using it so we can have just two columns.
|
|
|
|
#@socialNetworkRes = @supermodel.addSomethingResource('social_network_apis', 0)
|
|
|
|
#@checkFriends()
|
2014-03-22 12:48:36 -04:00
|
|
|
|
2014-03-21 22:50:54 -04:00
|
|
|
checkFriends: ->
|
2015-09-21 17:16:23 -04:00
|
|
|
return # Skipping for now
|
2014-03-26 17:53:09 -04:00
|
|
|
return if @checked or (not window.FB) or (not window.gapi)
|
2014-03-26 16:50:01 -04:00
|
|
|
@checked = true
|
2014-05-05 18:33:08 -04:00
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
# @addSomethingToLoad('facebook_status')
|
2014-04-12 16:29:49 -04:00
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
@fbStatusRes = @supermodel.addSomethingResource('facebook_status', 0)
|
2014-04-12 16:29:49 -04:00
|
|
|
@fbStatusRes.load()
|
|
|
|
|
2014-03-21 22:50:54 -04:00
|
|
|
FB.getLoginStatus (response) =>
|
2014-05-23 15:04:42 -04:00
|
|
|
return if @destroyed
|
2014-03-21 22:50:54 -04:00
|
|
|
@facebookStatus = response.status
|
2014-04-03 21:43:29 -04:00
|
|
|
@loadFacebookFriends() if @facebookStatus is 'connected'
|
2014-04-12 16:29:49 -04:00
|
|
|
@fbStatusRes.markLoaded()
|
2014-03-23 12:30:01 -04:00
|
|
|
|
|
|
|
if application.gplusHandler.loggedIn is undefined
|
|
|
|
@listenToOnce(application.gplusHandler, 'checked-state', @gplusSessionStateLoaded)
|
|
|
|
else
|
|
|
|
@gplusSessionStateLoaded()
|
2014-04-12 16:29:49 -04:00
|
|
|
|
|
|
|
@socialNetworkRes.markLoaded()
|
2014-03-21 22:50:54 -04:00
|
|
|
|
2014-03-23 12:30:01 -04:00
|
|
|
# FACEBOOK
|
|
|
|
|
|
|
|
onConnectFacebook: ->
|
|
|
|
@connecting = true
|
|
|
|
FB.login()
|
2014-03-23 19:48:30 -04:00
|
|
|
|
2014-03-23 12:30:01 -04:00
|
|
|
onConnectedWithFacebook: -> location.reload() if @connecting
|
2014-03-23 19:48:30 -04:00
|
|
|
|
2014-04-03 21:43:29 -04:00
|
|
|
loadFacebookFriends: ->
|
2014-06-30 22:16:26 -04:00
|
|
|
# @addSomethingToLoad('facebook_friends')
|
2014-04-12 16:29:49 -04:00
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
@fbFriendRes = @supermodel.addSomethingResource('facebook_friends', 0)
|
2014-04-12 16:29:49 -04:00
|
|
|
@fbFriendRes.load()
|
|
|
|
|
2014-04-03 21:43:29 -04:00
|
|
|
FB.api '/me/friends', @onFacebookFriendsLoaded
|
2014-05-05 18:33:08 -04:00
|
|
|
|
2014-04-03 21:43:29 -04:00
|
|
|
onFacebookFriendsLoaded: (response) =>
|
|
|
|
@facebookData = response.data
|
|
|
|
@loadFacebookFriendSessions()
|
2014-04-12 16:29:49 -04:00
|
|
|
@fbFriendRes.markLoaded()
|
2014-04-04 13:12:22 -04:00
|
|
|
|
2014-03-23 12:30:01 -04:00
|
|
|
loadFacebookFriendSessions: ->
|
2014-04-03 21:43:29 -04:00
|
|
|
levelFrag = "#{@level.get('original')}.#{@level.get('version').major}"
|
|
|
|
url = "/db/level/#{levelFrag}/leaderboard_facebook_friends"
|
2014-04-12 16:29:49 -04:00
|
|
|
|
|
|
|
@fbFriendSessionRes = @supermodel.addRequestResource('facebook_friend_sessions', {
|
|
|
|
url: url
|
|
|
|
data: { friendIDs: (f.id for f in @facebookData) }
|
|
|
|
method: 'POST'
|
|
|
|
success: @onFacebookFriendSessionsLoaded
|
|
|
|
})
|
|
|
|
@fbFriendSessionRes.load()
|
2014-03-22 12:48:36 -04:00
|
|
|
|
2014-03-23 12:30:01 -04:00
|
|
|
onFacebookFriendSessionsLoaded: (result) =>
|
2014-03-21 22:50:54 -04:00
|
|
|
friendsMap = {}
|
|
|
|
friendsMap[friend.id] = friend.name for friend in @facebookData
|
|
|
|
for friend in result
|
2014-03-26 16:50:01 -04:00
|
|
|
friend.name = friendsMap[friend.facebookID]
|
2014-03-21 22:50:54 -04:00
|
|
|
friend.otherTeam = if friend.team is 'humans' then 'ogres' else 'humans'
|
2014-03-26 16:50:01 -04:00
|
|
|
friend.imageSource = "http://graph.facebook.com/#{friend.facebookID}/picture"
|
|
|
|
@facebookFriendSessions = result
|
2014-07-04 20:32:16 -04:00
|
|
|
@render() # because the ladder tab renders before waiting for fb to finish
|
2014-05-05 18:33:08 -04:00
|
|
|
|
2014-03-23 12:30:01 -04:00
|
|
|
# GOOGLE PLUS
|
|
|
|
|
2014-03-26 17:19:05 -04:00
|
|
|
onConnectGPlus: ->
|
|
|
|
@connecting = true
|
|
|
|
@listenToOnce application.gplusHandler, 'logged-in', @onConnectedWithGPlus
|
|
|
|
application.gplusHandler.reauthorize()
|
|
|
|
|
|
|
|
onConnectedWithGPlus: -> location.reload() if @connecting
|
2014-05-05 18:33:08 -04:00
|
|
|
|
2014-03-23 12:30:01 -04:00
|
|
|
gplusSessionStateLoaded: ->
|
|
|
|
if application.gplusHandler.loggedIn
|
2014-06-30 22:16:26 -04:00
|
|
|
#@addSomethingToLoad('gplus_friends')
|
|
|
|
@gpFriendRes = @supermodel.addSomethingResource('gplus_friends', 0)
|
2014-04-12 16:29:49 -04:00
|
|
|
@gpFriendRes.load()
|
2014-03-23 12:30:01 -04:00
|
|
|
application.gplusHandler.loadFriends @gplusFriendsLoaded
|
|
|
|
|
|
|
|
gplusFriendsLoaded: (friends) =>
|
|
|
|
@gplusData = friends.items
|
2014-04-03 21:43:29 -04:00
|
|
|
@loadGPlusFriendSessions()
|
2014-04-12 16:29:49 -04:00
|
|
|
@gpFriendRes.markLoaded()
|
2014-04-04 13:12:22 -04:00
|
|
|
|
2014-04-03 21:43:29 -04:00
|
|
|
loadGPlusFriendSessions: ->
|
2014-03-23 12:30:01 -04:00
|
|
|
levelFrag = "#{@level.get('original')}.#{@level.get('version').major}"
|
|
|
|
url = "/db/level/#{levelFrag}/leaderboard_gplus_friends"
|
2014-04-19 13:06:47 -04:00
|
|
|
|
2014-04-12 16:29:49 -04:00
|
|
|
@gpFriendSessionRes = @supermodel.addRequestResource('gplus_friend_sessions', {
|
|
|
|
url: url
|
|
|
|
data: { friendIDs: (f.id for f in @gplusData) }
|
|
|
|
method: 'POST'
|
|
|
|
success: @onGPlusFriendSessionsLoaded
|
|
|
|
})
|
|
|
|
@gpFriendSessionRes.load()
|
2014-03-23 12:30:01 -04:00
|
|
|
|
|
|
|
onGPlusFriendSessionsLoaded: (result) =>
|
2014-03-26 16:50:01 -04:00
|
|
|
friendsMap = {}
|
|
|
|
friendsMap[friend.id] = friend for friend in @gplusData
|
|
|
|
for friend in result
|
|
|
|
friend.name = friendsMap[friend.gplusID].displayName
|
|
|
|
friend.otherTeam = if friend.team is 'humans' then 'ogres' else 'humans'
|
|
|
|
friend.imageSource = friendsMap[friend.gplusID].image.url
|
|
|
|
@gplusFriendSessions = result
|
2014-07-04 20:32:16 -04:00
|
|
|
@render() # because the ladder tab renders before waiting for gplus to finish
|
2014-05-05 18:33:08 -04:00
|
|
|
|
2014-03-23 12:30:01 -04:00
|
|
|
# LADDER LOADING
|
2014-03-03 15:21:59 -05:00
|
|
|
|
|
|
|
refreshLadder: ->
|
2015-12-05 11:18:36 -05:00
|
|
|
# Only do this so often if not in a league; servers cache a lot of this data for a few minutes anyway.
|
|
|
|
return if not @options.league and (new Date() - 2 * 60 * 1000 < @lastRefreshTime)
|
|
|
|
@lastRefreshTime = new Date()
|
2014-05-20 13:40:07 -04:00
|
|
|
@supermodel.resetProgress()
|
2015-11-19 18:18:53 -05:00
|
|
|
@ladderLimit ?= parseInt @getQueryVariable('top_players', if @options.league then 100 else 20)
|
2014-03-02 15:43:21 -05:00
|
|
|
for team in @teams
|
2014-05-21 18:33:28 -04:00
|
|
|
if oldLeaderboard = @leaderboards[team.id]
|
|
|
|
@supermodel.removeModelResource oldLeaderboard
|
|
|
|
oldLeaderboard.destroy()
|
2014-03-20 17:40:17 -04:00
|
|
|
teamSession = _.find @sessions.models, (session) -> session.get('team') is team.id
|
2015-08-15 09:45:38 -04:00
|
|
|
@leaderboards[team.id] = new LeaderboardData(@level, team.id, teamSession, @ladderLimit, @options.league)
|
2015-02-11 16:12:42 -05:00
|
|
|
@leaderboardRes = @supermodel.addModelResource(@leaderboards[team.id], 'leaderboard', {cache: false}, 3)
|
2014-04-12 16:29:49 -04:00
|
|
|
@leaderboardRes.load()
|
2014-03-02 15:43:21 -05:00
|
|
|
|
2014-04-04 16:38:36 -04:00
|
|
|
render: ->
|
|
|
|
super()
|
2014-05-05 18:33:08 -04:00
|
|
|
|
2014-04-04 16:38:36 -04:00
|
|
|
@$el.find('.histogram-display').each (i, el) =>
|
|
|
|
histogramWrapper = $(el)
|
|
|
|
team = _.find @teams, name: histogramWrapper.data('team-name')
|
|
|
|
histogramData = null
|
|
|
|
$.when(
|
2015-10-30 12:43:05 -04:00
|
|
|
level = "#{@level.get('original')}.#{@level.get('version').major}"
|
|
|
|
url = "/db/level/#{level}/histogram_data?team=#{team.name.toLowerCase()}"
|
2015-08-15 09:45:38 -04:00
|
|
|
url += '&leagues.leagueID=' + @options.league.id if @options.league
|
2015-12-05 11:18:36 -05:00
|
|
|
$.get url, (data) -> histogramData = data
|
2014-04-04 16:38:36 -04:00
|
|
|
).then =>
|
2014-05-05 18:33:08 -04:00
|
|
|
@generateHistogram(histogramWrapper, histogramData, team.name.toLowerCase()) unless @destroyed
|
|
|
|
|
2014-03-02 15:43:21 -05:00
|
|
|
getRenderData: ->
|
|
|
|
ctx = super()
|
|
|
|
ctx.level = @level
|
|
|
|
ctx.link = "/play/level/#{@level.get('name')}"
|
|
|
|
ctx.teams = @teams
|
|
|
|
team.leaderboard = @leaderboards[team.id] for team in @teams
|
|
|
|
ctx.levelID = @levelID
|
2014-03-26 16:50:01 -04:00
|
|
|
ctx.friends = @consolidateFriends()
|
2014-03-21 22:50:54 -04:00
|
|
|
ctx.onFacebook = @facebookStatus is 'connected'
|
2014-03-23 12:30:01 -04:00
|
|
|
ctx.onGPlus = application.gplusHandler.loggedIn
|
2014-07-23 18:10:51 -04:00
|
|
|
ctx.capitalize = _.string.capitalize
|
2015-08-19 18:30:37 -04:00
|
|
|
ctx.league = @options.league
|
|
|
|
ctx._ = _
|
2015-09-08 09:44:26 -04:00
|
|
|
ctx.moment = moment
|
2014-03-02 15:43:21 -05:00
|
|
|
ctx
|
2014-03-03 15:21:59 -05:00
|
|
|
|
2014-04-04 16:38:36 -04:00
|
|
|
generateHistogram: (histogramElement, histogramData, teamName) ->
|
|
|
|
#renders twice, hack fix
|
2014-06-30 22:16:26 -04:00
|
|
|
if $('#' + histogramElement.attr('id')).has('svg').length then return
|
2014-04-04 16:38:36 -04:00
|
|
|
histogramData = histogramData.map (d) -> d*100
|
2014-05-05 18:33:08 -04:00
|
|
|
|
2014-04-04 16:38:36 -04:00
|
|
|
margin =
|
|
|
|
top: 20
|
|
|
|
right: 20
|
|
|
|
bottom: 30
|
2015-09-08 09:44:26 -04:00
|
|
|
left: 15
|
2014-04-04 16:38:36 -04:00
|
|
|
|
2015-09-08 09:44:26 -04:00
|
|
|
width = 470 - margin.left - margin.right
|
2014-04-04 16:38:36 -04:00
|
|
|
height = 125 - margin.top - margin.bottom
|
2014-05-05 18:33:08 -04:00
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
formatCount = d3.format(',.0')
|
2014-05-05 18:33:08 -04:00
|
|
|
|
2014-08-25 00:39:34 -04:00
|
|
|
minX = Math.floor(Math.min(histogramData...) / 1000) * 1000
|
|
|
|
maxX = Math.ceil(Math.max(histogramData...) / 1000) * 1000
|
|
|
|
x = d3.scale.linear().domain([minX, maxX]).range([0, width])
|
2014-04-04 16:38:36 -04:00
|
|
|
data = d3.layout.histogram().bins(x.ticks(20))(histogramData)
|
2014-06-30 22:16:26 -04:00
|
|
|
y = d3.scale.linear().domain([0, d3.max(data, (d) -> d.y)]).range([height, 10])
|
2014-05-05 18:33:08 -04:00
|
|
|
|
2014-04-04 16:38:36 -04:00
|
|
|
#create the x axis
|
2014-06-30 22:16:26 -04:00
|
|
|
xAxis = d3.svg.axis().scale(x).orient('bottom').ticks(5).outerTickSize(0)
|
|
|
|
|
|
|
|
svg = d3.select('#' + histogramElement.attr('id')).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})")
|
|
|
|
barClass = 'bar'
|
|
|
|
if teamName.toLowerCase() is 'ogres' then barClass = 'ogres-bar'
|
|
|
|
if teamName.toLowerCase() is 'humans' then barClass = 'humans-bar'
|
|
|
|
|
|
|
|
bar = svg.selectAll('.bar')
|
2014-04-04 16:38:36 -04:00
|
|
|
.data(data)
|
2014-06-30 22:16:26 -04:00
|
|
|
.enter().append('g')
|
|
|
|
.attr('class', barClass)
|
|
|
|
.attr('transform', (d) -> "translate(#{x(d.x)}, #{y(d.y)})")
|
|
|
|
|
|
|
|
bar.append('rect')
|
|
|
|
.attr('x', 1)
|
|
|
|
.attr('width', width/20)
|
|
|
|
.attr('height', (d) -> height - y(d.y))
|
2015-08-19 18:30:37 -04:00
|
|
|
if session = @leaderboards[teamName].session
|
|
|
|
if @options.league
|
|
|
|
playerScore = (_.find(session.get('leagues'), {leagueID: @options.league.id})?.stats.totalScore or 10) * 100
|
|
|
|
else
|
|
|
|
playerScore = session.get('totalScore') * 100
|
2014-06-30 22:16:26 -04:00
|
|
|
scorebar = svg.selectAll('.specialbar')
|
2014-04-04 16:38:36 -04:00
|
|
|
.data([playerScore])
|
2014-06-30 22:16:26 -04:00
|
|
|
.enter().append('g')
|
|
|
|
.attr('class', 'specialbar')
|
2016-03-23 18:24:38 -04:00
|
|
|
.attr('transform', "translate(#{x(playerScore)}, 0)")
|
2014-06-30 22:16:26 -04:00
|
|
|
|
|
|
|
scorebar.append('rect')
|
|
|
|
.attr('x', 1)
|
|
|
|
.attr('width', 3)
|
2016-03-23 18:24:38 -04:00
|
|
|
.attr('height', height)
|
2014-06-30 22:16:26 -04:00
|
|
|
rankClass = 'rank-text'
|
|
|
|
if teamName.toLowerCase() is 'ogres' then rankClass = 'rank-text ogres-rank-text'
|
|
|
|
if teamName.toLowerCase() is 'humans' then rankClass = 'rank-text humans-rank-text'
|
2014-05-05 18:33:08 -04:00
|
|
|
|
2014-04-04 17:55:55 -04:00
|
|
|
message = "#{histogramData.length} players"
|
2014-05-20 13:40:07 -04:00
|
|
|
if @leaderboards[teamName].session?
|
2015-08-21 06:28:25 -04:00
|
|
|
if @options.league
|
|
|
|
# TODO: fix server handler to properly fetch myRank with a leagueID
|
|
|
|
message = "#{histogramData.length} players in league"
|
|
|
|
else if @leaderboards[teamName].myRank <= histogramData.length
|
|
|
|
message = "##{@leaderboards[teamName].myRank} of #{histogramData.length}"
|
2014-05-16 16:30:55 -04:00
|
|
|
else
|
2015-08-21 06:28:25 -04:00
|
|
|
message = 'Rank your session!'
|
2014-06-30 22:16:26 -04:00
|
|
|
svg.append('g')
|
|
|
|
.append('text')
|
|
|
|
.attr('class', rankClass)
|
|
|
|
.attr('y', 0)
|
|
|
|
.attr('text-anchor', 'end')
|
|
|
|
.attr('x', width)
|
2014-04-04 17:55:55 -04:00
|
|
|
.text(message)
|
2014-05-05 18:33:08 -04:00
|
|
|
|
2014-04-04 16:38:36 -04:00
|
|
|
#Translate the x-axis up
|
2014-06-30 22:16:26 -04:00
|
|
|
svg.append('g')
|
|
|
|
.attr('class', 'x axis')
|
|
|
|
.attr('transform', 'translate(0, ' + height + ')')
|
2014-04-04 16:38:36 -04:00
|
|
|
.call(xAxis)
|
2014-05-05 18:33:08 -04:00
|
|
|
|
2014-03-26 16:50:01 -04:00
|
|
|
consolidateFriends: ->
|
|
|
|
allFriendSessions = (@facebookFriendSessions or []).concat(@gplusFriendSessions or [])
|
2014-03-26 17:22:04 -04:00
|
|
|
sessions = _.uniq allFriendSessions, false, (session) -> session._id
|
2015-11-24 16:18:38 -05:00
|
|
|
if @options.league
|
|
|
|
sessions = _.sortBy sessions, (session) -> _.find(session.leagues, leagueID: @options.league.id)?.stats.totalScore ? (session.totalScore / 2)
|
|
|
|
else
|
|
|
|
sessions = _.sortBy sessions, 'totalScore'
|
2014-03-26 17:22:04 -04:00
|
|
|
sessions.reverse()
|
|
|
|
sessions
|
2014-03-26 16:50:01 -04:00
|
|
|
|
2014-04-15 18:01:54 -04:00
|
|
|
# Admin view of players' code
|
|
|
|
onClickPlayerName: (e) ->
|
|
|
|
return unless me.isAdmin()
|
|
|
|
row = $(e.target).parent()
|
|
|
|
player = new User _id: row.data 'player-id'
|
|
|
|
session = new LevelSession _id: row.data 'session-id'
|
|
|
|
@openModalView new ModelModal models: [session, player]
|
|
|
|
|
2015-03-20 18:32:35 -04:00
|
|
|
onClickSpectateCell: (e) ->
|
|
|
|
cell = $(e.target).closest '.spectate-cell'
|
|
|
|
row = cell.parent()
|
|
|
|
table = row.closest('table')
|
|
|
|
wasSelected = cell.hasClass 'selected'
|
|
|
|
table.find('.spectate-cell.selected').removeClass 'selected'
|
|
|
|
cell = $(e.target).closest('.spectate-cell').toggleClass 'selected', not wasSelected
|
|
|
|
sessionID = row.data 'session-id'
|
|
|
|
teamID = table.data 'team'
|
|
|
|
@spectateTargets ?= {}
|
|
|
|
@spectateTargets[teamID] = if wasSelected then null else sessionID
|
|
|
|
console.log @spectateTargets, cell, row, table
|
|
|
|
|
2014-04-15 18:01:54 -04:00
|
|
|
onLoadMoreLadderEntries: (e) ->
|
|
|
|
@ladderLimit ?= 100
|
|
|
|
@ladderLimit += 100
|
2015-12-05 11:18:36 -05:00
|
|
|
@lastRefreshTime = null
|
2014-04-15 18:01:54 -04:00
|
|
|
@refreshLadder()
|
|
|
|
|
2014-06-06 19:46:42 -04:00
|
|
|
module.exports.LeaderboardData = LeaderboardData = class LeaderboardData extends CocoClass
|
2014-04-03 21:43:29 -04:00
|
|
|
###
|
|
|
|
Consolidates what you need to load for a leaderboard into a single Backbone Model-like object.
|
|
|
|
###
|
2014-04-13 17:48:36 -04:00
|
|
|
|
2015-08-15 09:45:38 -04:00
|
|
|
constructor: (@level, @team, @session, @limit, @league) ->
|
2014-04-03 21:43:29 -04:00
|
|
|
super()
|
2014-04-19 13:06:47 -04:00
|
|
|
|
2015-08-15 09:45:38 -04:00
|
|
|
collectionParameters: (parameters) ->
|
|
|
|
parameters.team = @team
|
|
|
|
parameters['leagues.leagueID'] = @league.id if @league
|
|
|
|
parameters
|
|
|
|
|
2014-04-03 21:43:29 -04:00
|
|
|
fetch: ->
|
2014-06-06 19:46:42 -04:00
|
|
|
console.warn 'Already have top players on', @ if @topPlayers
|
2015-08-15 09:45:38 -04:00
|
|
|
|
|
|
|
@topPlayers = new LeaderboardCollection(@level, @collectionParameters(order: -1, scoreOffset: HIGHEST_SCORE, limit: @limit))
|
2014-03-20 17:40:17 -04:00
|
|
|
promises = []
|
2015-02-11 16:12:42 -05:00
|
|
|
promises.push @topPlayers.fetch cache: false
|
2014-03-02 15:43:21 -05:00
|
|
|
|
2014-03-20 17:40:17 -04:00
|
|
|
if @session
|
2015-08-19 18:30:37 -04:00
|
|
|
if @league
|
2015-08-21 06:28:25 -04:00
|
|
|
score = _.find(@session.get('leagues'), {leagueID: @league.id})?.stats.totalScore
|
2015-08-19 18:30:37 -04:00
|
|
|
else
|
2015-08-21 06:28:25 -04:00
|
|
|
score = @session.get('totalScore')
|
|
|
|
if score
|
|
|
|
@playersAbove = new LeaderboardCollection(@level, @collectionParameters(order: 1, scoreOffset: score, limit: 4))
|
|
|
|
promises.push @playersAbove.fetch cache: false
|
|
|
|
@playersBelow = new LeaderboardCollection(@level, @collectionParameters(order: -1, scoreOffset: score, limit: 4))
|
|
|
|
promises.push @playersBelow.fetch cache: false
|
|
|
|
level = "#{@level.get('original')}.#{@level.get('version').major}"
|
|
|
|
success = (@myRank) =>
|
|
|
|
loadURL = "/db/level/#{level}/leaderboard_rank?scoreOffset=#{score}&team=#{@team}"
|
|
|
|
loadURL += '&leagues.leagueID=' + @league.id if @league
|
|
|
|
promises.push $.ajax(loadURL, cache: false, success: success)
|
2014-03-21 22:50:54 -04:00
|
|
|
@promise = $.when(promises...)
|
|
|
|
@promise.then @onLoad
|
2014-04-03 21:43:29 -04:00
|
|
|
@promise.fail @onFail
|
2014-03-21 22:50:54 -04:00
|
|
|
@promise
|
2014-03-20 17:40:17 -04:00
|
|
|
|
|
|
|
onLoad: =>
|
2014-06-06 19:46:42 -04:00
|
|
|
return if @destroyed or not @topPlayers.loaded
|
2014-03-09 16:22:22 -04:00
|
|
|
@loaded = true
|
2014-06-06 19:46:42 -04:00
|
|
|
@loading = false
|
2014-04-03 21:43:29 -04:00
|
|
|
@trigger 'sync', @
|
2014-03-20 17:40:17 -04:00
|
|
|
# TODO: cache user ids -> names mapping, and load them here as needed,
|
|
|
|
# and apply them to sessions. Fetching each and every time is too costly.
|
2014-05-05 18:33:08 -04:00
|
|
|
|
2014-04-03 21:43:29 -04:00
|
|
|
onFail: (resource, jqxhr) =>
|
|
|
|
return if @destroyed
|
|
|
|
@trigger 'error', @, jqxhr
|
2014-03-21 11:09:08 -04:00
|
|
|
|
2014-03-20 17:40:17 -04:00
|
|
|
inTopSessions: ->
|
|
|
|
return me.id in (session.attributes.creator for session in @topPlayers.models)
|
2014-03-21 11:09:08 -04:00
|
|
|
|
2014-03-20 17:40:17 -04:00
|
|
|
nearbySessions: ->
|
2015-08-21 06:28:25 -04:00
|
|
|
if @league
|
|
|
|
score = _.find(@session?.get('leagues'), {leagueID: @league.id})?.stats.totalScore
|
|
|
|
else
|
|
|
|
score = @session?.get('totalScore')
|
|
|
|
return [] unless score
|
2014-03-20 17:40:17 -04:00
|
|
|
l = []
|
|
|
|
above = @playersAbove.models
|
|
|
|
l = l.concat(above)
|
2014-04-16 18:20:59 -04:00
|
|
|
l.reverse()
|
2014-03-20 17:40:17 -04:00
|
|
|
l.push @session
|
|
|
|
l = l.concat(@playersBelow.models)
|
|
|
|
if @myRank
|
|
|
|
startRank = @myRank - 4
|
|
|
|
session.rank = startRank + i for session, i in l
|
2014-03-21 11:09:08 -04:00
|
|
|
l
|
2014-04-03 21:43:29 -04:00
|
|
|
|
|
|
|
allResources: ->
|
|
|
|
resources = [@topPlayers, @playersAbove, @playersBelow]
|
2014-04-04 16:48:39 -04:00
|
|
|
return (r for r in resources when r)
|