codecombat/app/views/play/ladder_view.coffee

198 lines
6.7 KiB
CoffeeScript
Raw Normal View History

2014-02-07 18:51:05 -05:00
RootView = require 'views/kinds/RootView'
Level = require 'models/Level'
Simulator = require 'lib/simulator/Simulator'
2014-02-07 18:51:05 -05:00
LevelSession = require 'models/LevelSession'
CocoCollection = require 'models/CocoCollection'
2014-02-17 20:42:41 -05:00
LeaderboardCollection = require 'collections/LeaderboardCollection'
{hslToHex} = require 'lib/utils'
2014-02-07 18:51:05 -05:00
HIGHEST_SCORE = 1000000
class LevelSessionsCollection extends CocoCollection
url: ''
model: LevelSession
2014-02-20 16:58:35 -05:00
2014-02-07 18:51:05 -05:00
constructor: (levelID) ->
super()
@url = "/db/level/#{levelID}/all_sessions"
module.exports = class LadderView extends RootView
id: 'ladder-view'
template: require 'templates/play/ladder'
startsLoading: true
2014-02-14 19:53:34 -05:00
events:
'click #simulate-button': 'onSimulateButtonClick'
'click #simulate-all-button': 'onSimulateAllButtonClick'
2014-02-14 19:53:34 -05:00
onSimulateAllButtonClick: (e) ->
2014-02-14 19:53:34 -05:00
submitIDs = _.pluck @leaderboards[@teams[0]].topPlayers.models, "id"
for ID in submitIDs
$.ajax
url: '/queue/scoring'
method: 'POST'
data:
session: ID
$("#simulate-all-button").prop "disabled", true
$("#simulate-all-button").text "Submitted all!"
2014-02-14 19:53:34 -05:00
onSimulateButtonClick: (e) ->
$("#simulate-button").prop "disabled",true
$("#simulate-button").text "Simulating..."
@simulator.fetchAndSimulateTask()
updateSimulationStatus: (simulationStatus, sessions)->
@simulationStatus = simulationStatus
try
if sessions?
#TODO: Fetch names from Redis, the creatorName is denormalized
creatorNames = (session.creatorName for session in sessions)
@simulationStatus = "Simulating game between "
for index in [0...creatorNames.length]
unless creatorNames[index]
creatorNames[index] = "Anonymous"
@simulationStatus += " and " + creatorNames[index]
@simulationStatus += "..."
catch e
console.log "There was a problem with the named simulation status: #{e}"
2014-02-20 16:58:35 -05:00
$("#simulation-status-text").text @simulationStatus
2014-02-17 20:42:41 -05:00
constructor: (options, @levelID) ->
2014-02-07 18:51:05 -05:00
super(options)
2014-02-17 20:42:41 -05:00
@level = new Level(_id:@levelID)
2014-02-07 18:51:05 -05:00
@level.fetch()
@level.once 'sync', @onLevelLoaded, @
@simulator = new Simulator()
@simulator.on 'statusUpdate', @updateSimulationStatus, @
2014-02-20 16:58:35 -05:00
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()
2014-02-20 16:58:35 -05:00
2014-02-07 18:51:05 -05:00
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
2014-02-17 20:42:41 -05:00
@teamConfigs = alliedSystem.config.teams
2014-02-20 16:58:35 -05:00
2014-02-07 18:51:05 -05:00
@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-20 16:58:35 -05:00
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()
2014-02-20 16:58:35 -05:00
2014-02-07 18:51:05 -05:00
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.simulationStatus = @simulationStatus
2014-02-07 18:51:05 -05:00
ctx.teams = []
2014-02-17 20:42:41 -05:00
ctx.levelID = @levelID
2014-02-07 18:51:05 -05:00
for team in @teams or []
otherTeam = if team is 'ogres' then 'humans' else 'ogres'
2014-02-17 20:42:41 -05:00
color = @teamConfigs[team].color
bgColor = hslToHex([color.hue, color.saturation, color.lightness + (1 - color.lightness) * 0.5])
primaryColor = hslToHex([color.hue, 0.5, 0.5])
2014-02-07 18:51:05 -05:00
ctx.teams.push({
id: team
name: _.string.titleize(team)
leaderboard: @leaderboards[team]
otherTeam: otherTeam
2014-02-17 20:42:41 -05:00
bgColor: bgColor
primaryColor: primaryColor
2014-02-07 18:51:05 -05:00
})
ctx
2014-02-20 16:58:35 -05:00
2014-02-07 18:51:05 -05:00
class LeaderboardData
constructor: (@level, @team, @session) ->
_.extend @, Backbone.Events
@topPlayers = new LeaderboardCollection(@level, {order:-1, scoreOffset: HIGHEST_SCORE, team: @team, limit: if @session then 10 else 20})
@topPlayers.fetch()
@topPlayers.comparator = (model) ->
return -model.get('totalScore')
@topPlayers.sort()
2014-02-07 18:51:05 -05:00
@topPlayers.once 'sync', @leaderboardPartLoaded, @
2014-02-20 16:58:35 -05:00
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
2014-02-17 20:42:41 -05:00
@fetchNames()
2014-02-07 18:51:05 -05:00
else
@loaded = true
2014-02-17 20:42:41 -05:00
@fetchNames()
2014-02-20 16:58:35 -05:00
2014-02-17 20:42:41 -05:00
fetchNames: ->
sessionCollections = [@topPlayers, @playersAbove, @playersBelow]
sessionCollections = (s for s in sessionCollections when s)
ids = []
for collection in sessionCollections
ids.push model.get('creator') for model in collection.models
2014-02-20 16:58:35 -05:00
2014-02-17 20:42:41 -05:00
success = (nameMap) =>
for collection in sessionCollections
session.set('creatorName', nameMap[session.get('creator')]) for session in collection.models
2014-02-07 18:51:05 -05:00
@trigger 'sync'
2014-02-20 16:58:35 -05:00
2014-02-17 20:42:41 -05:00
$.ajax('/db/user/-/names', {
data: {ids: ids}
type: 'POST'
success: success
})
2014-02-07 18:51:05 -05:00
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'