codecombat/app/views/play/ladder_view.coffee

144 lines
5 KiB
CoffeeScript
Raw Normal View History

2014-02-07 18:51:05 -05:00
RootView = require 'views/kinds/RootView'
Level = require 'models/Level'
LevelSession = require 'models/LevelSession'
CocoCollection = require 'models/CocoCollection'
HIGHEST_SCORE = 1000000
class LevelSessionsCollection extends CocoCollection
url: ''
model: LevelSession
constructor: (levelID) ->
super()
@url = "/db/level/#{levelID}/all_sessions"
class LeaderboardCollection extends CocoCollection
url: ''
model: LevelSession
constructor: (level, options) ->
super()
options ?= {}
2014-02-13 18:51:18 -05:00
@url = "/db/level/#{level.get('original')}.#{level.get('version').major}/leaderboard?#{$.param(options)}"
2014-02-13 18:14:08 -05:00
#@url = "/db/level/#{level.get('original')}/leaderboard?#{$.param(options)}"
2014-02-13 18:02:49 -05:00
2014-02-07 18:51:05 -05:00
module.exports = class LadderView extends RootView
id: 'ladder-view'
template: require 'templates/play/ladder'
startsLoading: true
constructor: (options, levelID) ->
super(options)
@level = new Level(_id:levelID)
@level.fetch()
@level.once 'sync', @onLevelLoaded, @
2014-02-13 18:09:40 -05:00
# @sessions = new LevelSessionsCollection(levelID)
# @sessions.fetch({})
# @sessions.once 'sync', @onMySessionsLoaded, @
2014-02-07 18:51:05 -05:00
onLevelLoaded: -> @startLoadingPhaseTwoMaybe()
2014-02-13 18:02:49 -05:00
onMySessionsLoaded: ->
@startLoadingPhaseTwoMaybe()
2014-02-07 18:51:05 -05:00
startLoadingPhaseTwoMaybe: ->
2014-02-13 18:09:40 -05:00
return unless @level.loaded # and @sessions.loaded
2014-02-07 18:51:05 -05:00
@loadPhaseTwo()
loadPhaseTwo: ->
alliedSystem = _.find @level.get('systems'), (value) -> value.config?.teams?
teams = []
for teamName, teamConfig of alliedSystem.config.teams
continue unless teamConfig.playable
teams.push teamName
@teams = teams
@leaderboards = {}
@challengers = {}
for team in teams
2014-02-13 18:51:18 -05:00
# teamSession = _.find @sessions.models, (session) -> session.get('team') is team
2014-02-13 18:09:40 -05:00
teamSession = null
2014-02-13 18:02:49 -05:00
console.log "Team session: #{JSON.stringify teamSession}"
2014-02-07 18:51:05 -05:00
@leaderboards[team] = new LeaderboardData(@level, team, teamSession)
@leaderboards[team].once 'sync', @onLeaderboardLoaded, @
2014-02-13 18:09:40 -05:00
# @challengers[team] = new ChallengersData(@level, team, teamSession)
# @challengers[team].once 'sync', @onChallengersLoaded, @
2014-02-07 18:51:05 -05:00
onChallengersLoaded: -> @renderMaybe()
onLeaderboardLoaded: -> @renderMaybe()
renderMaybe: ->
2014-02-13 18:09:40 -05:00
loaders = _.values(@leaderboards) # .concat(_.values(@challengers))
2014-02-07 18:51:05 -05:00
return unless _.every loaders, (loader) -> loader.loaded
@startsLoading = false
@render()
getRenderData: ->
ctx = super()
ctx.level = @level
description = @level.get('description')
ctx.description = if description then marked(description) else ''
ctx.link = "/play/level/#{@level.get('name')}"
ctx.teams = []
for team in @teams or []
ctx.teams.push({
id: team
name: _.string.titleize(team)
leaderboard: @leaderboards[team]
2014-02-13 18:09:40 -05:00
# easyChallenger: @challengers[team].easyPlayer.models[0]
# mediumChallenger: @challengers[team].mediumPlayer.models[0]
# hardChallenger: @challengers[team].hardPlayer.models[0]
2014-02-07 18:51:05 -05:00
})
ctx
afterRender: ->
super()
@$el.find('#leaderboard-column .nav a:first').tab('show')
class LeaderboardData
constructor: (@level, @team, @session) ->
2014-02-13 18:09:40 -05:00
console.log 'creating leaderboard data', @level, @team, @session
2014-02-07 18:51:05 -05:00
_.extend @, Backbone.Events
@topPlayers = new LeaderboardCollection(@level, {order:-1, scoreOffset: HIGHEST_SCORE, team: @team, limit: if @session then 10 else 20})
@topPlayers.fetch()
@topPlayers.once 'sync', @leaderboardPartLoaded, @
2014-02-13 18:09:40 -05:00
# if @session
# score = @session.get('totalScore') or 25
2014-02-13 18:09:40 -05:00
# @playersAbove = new LeaderboardCollection(@level, {order:1, scoreOffset: score, limit: 4, team: @team})
# @playersAbove.fetch()
# @playersAbove.once 'sync', @leaderboardPartLoaded, @
# @playersBelow = new LeaderboardCollection(@level, {order:-1, scoreOffset: score, limit: 4, team: @team})
# @playersBelow.fetch()
# @playersBelow.once 'sync', @leaderboardPartLoaded, @
2014-02-07 18:51:05 -05:00
leaderboardPartLoaded: ->
if @session
2014-02-13 18:09:40 -05:00
if @topPlayers.loaded # and @playersAbove.loaded and @playersBelow.loaded
2014-02-07 18:51:05 -05:00
@loaded = true
@trigger 'sync'
else
@loaded = true
@trigger 'sync'
class ChallengersData
constructor: (@level, @team, @session) ->
_.extend @, Backbone.Events
2014-02-13 18:02:49 -05:00
score = @session?.get('totalScore') or 25
2014-02-07 18:51:05 -05:00
@easyPlayer = new LeaderboardCollection(@level, {order:1, scoreOffset: score - 5, limit: 1, team: @team})
@easyPlayer.fetch()
@easyPlayer.once 'sync', @challengerLoaded, @
@mediumPlayer = new LeaderboardCollection(@level, {order:1, scoreOffset: score, limit: 1, team: @team})
@mediumPlayer.fetch()
@mediumPlayer.once 'sync', @challengerLoaded, @
@hardPlayer = new LeaderboardCollection(@level, {order:-1, scoreOffset: score + 5, limit: 1, team: @team})
@hardPlayer.fetch()
@hardPlayer.once 'sync', @challengerLoaded, @
challengerLoaded: ->
if @easyPlayer.loaded and @mediumPlayer.loaded and @hardPlayer.loaded
@loaded = true
@trigger 'sync'