View = require 'views/kinds/ModalView' template = require 'templates/play/ladder/play_modal' ThangType = require 'models/ThangType' {me} = require 'lib/auth' LeaderboardCollection = require 'collections/LeaderboardCollection' module.exports = class LadderPlayModal extends View id: "ladder-play-modal" template: template closeButton: true startsLoading = true constructor: (options, @level, @session, @team) -> super(options) @nameMap = {} @otherTeam = if team is 'ogres' then 'humans' else 'ogres' @startLoadingChallengersMaybe() @wizardType = ThangType.wizardType # PART 1: Load challengers from the db unless some are in the matches startLoadingChallengersMaybe: -> matches = @session?.get('matches') if matches?.length then @loadNames() else @loadChallengers() loadChallengers: -> @challengers = new ChallengersData(@level, @team, @otherTeam, @session) @challengers.on 'sync', @loadNames, @ # PART 2: Loading the names of the other users loadNames: -> @challengers = @getChallengers() ids = (challenger.opponentID for challenger in _.values @challengers) success = (@nameMap) => for challenger in _.values(@challengers) challenger.opponentName = @nameMap[challenger.opponentID] or 'Anoner' @checkWizardLoaded() $.ajax('/db/user/-/names', { data: {ids: ids} type: 'POST' success: success }) # PART 3: Make sure wizard is loaded checkWizardLoaded: -> if @wizardType.loaded then @finishRendering() else @wizardType.once 'sync', @finishRendering, @ # PART 4: Render finishRendering: -> @startsLoading = false @render() getRenderData: -> ctx = super() ctx.level = @level ctx.levelID = @level.get('slug') or @level.id ctx.teamName = _.string.titleize @team ctx.teamID = @team ctx.otherTeamID = @otherTeam ctx.challengers = if not @startsLoading then @challengers else {} ctx.portraitSRC = @wizardType.getPortraitSource() ctx.myName = me.get('name') || 'Newcomer' ctx # Choosing challengers 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 addChallenger: (info, challengers, title) -> # check for duplicates first return unless info for key, value of challengers return if value.sessionID is info.sessionID challengers[title] = info challengeInfoFromSession: (session) -> # given a model from the db, return info needed for a link to the match return unless session return { sessionID: session.id opponentID: session.get('creator') } challengeInfoFromMatches: (matches) -> return unless matches?.length match = _.sample matches opponent = match.opponents[0] return { sessionID: opponent.sessionID opponentID: opponent.userID } class ChallengersData constructor: (@level, @team, @otherTeam, @session) -> _.extend @, Backbone.Events score = @session?.get('totalScore') or 25 @easyPlayer = new LeaderboardCollection(@level, {order:1, scoreOffset: score - 5, limit: 1, team: @otherTeam}) @easyPlayer.fetch() @easyPlayer.once 'sync', @challengerLoaded, @ @mediumPlayer = new LeaderboardCollection(@level, {order:1, scoreOffset: score, limit: 1, team: @otherTeam}) @mediumPlayer.fetch() @mediumPlayer.once 'sync', @challengerLoaded, @ @hardPlayer = new LeaderboardCollection(@level, {order:-1, scoreOffset: score + 5, limit: 1, team: @otherTeam}) @hardPlayer.fetch() @hardPlayer.once 'sync', @challengerLoaded, @ console.log 'fetching challengers yes' challengerLoaded: -> console.log 'challenger loaded' if @allLoaded() @loaded = true @trigger 'sync' playerIDs: -> collections = [@easyPlayer, @mediumPlayer, @hardPlayer] (c.models[0].get('creator') for c in collections when c?.models[0]) allLoaded: -> _.all [@easyPlayer.loaded, @mediumPlayer.loaded, @hardPlayer.loaded]