2014-11-28 20:49:41 -05:00
|
|
|
UserView = require 'views/common/UserView'
|
2014-07-29 10:50:07 -04:00
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
|
|
|
LevelSession = require 'models/LevelSession'
|
2014-11-25 13:19:38 -05:00
|
|
|
template = require 'templates/user/main-user-view'
|
2014-11-28 20:49:41 -05:00
|
|
|
{me} = require 'core/auth'
|
2015-04-06 14:17:13 -04:00
|
|
|
Clan = require 'models/Clan'
|
2014-08-07 16:03:00 -04:00
|
|
|
EarnedAchievementCollection = require 'collections/EarnedAchievementCollection'
|
2014-07-15 10:15:21 -04:00
|
|
|
|
2014-07-29 10:50:07 -04:00
|
|
|
class LevelSessionsCollection extends CocoCollection
|
|
|
|
model: LevelSession
|
|
|
|
|
|
|
|
constructor: (userID) ->
|
2014-09-24 02:57:54 -04:00
|
|
|
@url = "/db/user/#{userID}/level.sessions?project=state.complete,levelID,levelName,changed,team,codeLanguage,submittedCodeLanguage,totalScore&order=-1"
|
2014-07-29 10:50:07 -04:00
|
|
|
super()
|
|
|
|
|
2014-07-21 13:49:16 -04:00
|
|
|
module.exports = class MainUserView extends UserView
|
2014-08-12 07:41:14 -04:00
|
|
|
id: 'user-home'
|
2014-07-15 10:15:21 -04:00
|
|
|
template: template
|
2015-02-11 16:12:42 -05:00
|
|
|
|
2014-11-25 13:19:38 -05:00
|
|
|
events:
|
|
|
|
'click .more-button': 'onClickMoreButton'
|
2014-07-15 10:15:21 -04:00
|
|
|
|
2014-07-21 13:49:16 -04:00
|
|
|
constructor: (userID, options) ->
|
2014-07-15 10:15:21 -04:00
|
|
|
super options
|
|
|
|
|
2015-04-06 14:17:13 -04:00
|
|
|
destroy: ->
|
|
|
|
@stopListening?()
|
|
|
|
|
2014-07-15 10:15:21 -04:00
|
|
|
getRenderData: ->
|
|
|
|
context = super()
|
2014-08-07 16:03:00 -04:00
|
|
|
if @levelSessions and @levelSessions.loaded
|
2014-07-29 10:50:07 -04:00
|
|
|
singlePlayerSessions = []
|
|
|
|
multiPlayerSessions = []
|
|
|
|
languageCounts = {}
|
|
|
|
for levelSession in @levelSessions.models
|
2014-08-03 17:58:51 -04:00
|
|
|
if levelSession.isMultiplayer()
|
2014-07-29 10:50:07 -04:00
|
|
|
multiPlayerSessions.push levelSession
|
|
|
|
else
|
|
|
|
singlePlayerSessions.push levelSession
|
2014-09-24 02:57:54 -04:00
|
|
|
language = levelSession.get('codeLanguage') or levelSession.get('submittedCodeLanguage')
|
|
|
|
if language
|
|
|
|
languageCounts[language] = (languageCounts[language] or 0) + 1
|
2014-07-29 10:50:07 -04:00
|
|
|
mostUsedCount = 0
|
|
|
|
favoriteLanguage = null
|
|
|
|
for language, count of languageCounts
|
|
|
|
if count > mostUsedCount
|
|
|
|
mostUsedCount = count
|
2014-07-29 14:11:45 -04:00
|
|
|
favoriteLanguage = language
|
2014-07-29 10:50:07 -04:00
|
|
|
context.singlePlayerSessions = singlePlayerSessions
|
|
|
|
context.multiPlayerSessions = multiPlayerSessions
|
2014-07-29 14:11:45 -04:00
|
|
|
context.favoriteLanguage = favoriteLanguage
|
2014-08-07 16:03:00 -04:00
|
|
|
if @earnedAchievements and @earnedAchievements.loaded
|
|
|
|
context.earnedAchievements = @earnedAchievements
|
2015-04-06 14:17:13 -04:00
|
|
|
if @clans and @clans.loaded
|
|
|
|
context.clans = @clans.models
|
|
|
|
context.idNameMap = @idNameMap
|
2014-07-15 10:15:21 -04:00
|
|
|
context
|
|
|
|
|
2014-07-29 10:50:07 -04:00
|
|
|
onLoaded: ->
|
2014-08-08 13:26:24 -04:00
|
|
|
if @user.loaded and not (@earnedAchievements or @levelSessions)
|
2014-08-07 16:03:00 -04:00
|
|
|
@supermodel.resetProgress()
|
2014-08-08 13:26:24 -04:00
|
|
|
@levelSessions = new LevelSessionsCollection @user.getSlugOrID()
|
2014-08-07 16:03:00 -04:00
|
|
|
@earnedAchievements = new EarnedAchievementCollection @user.getSlugOrID()
|
2015-02-11 16:12:42 -05:00
|
|
|
@supermodel.loadCollection @levelSessions, 'levelSessions', {cache: false}
|
|
|
|
@supermodel.loadCollection @earnedAchievements, 'earnedAchievements', {cache: false}
|
2015-04-06 14:17:13 -04:00
|
|
|
sortClanList = (a, b) ->
|
|
|
|
if a.get('members').length isnt b.get('members').length
|
|
|
|
if a.get('members').length < b.get('members').length then 1 else -1
|
|
|
|
else
|
|
|
|
b.id.localeCompare(a.id)
|
|
|
|
@idNameMap = {}
|
|
|
|
@clans = new CocoCollection([], { url: "/db/user/#{@userID}/clans", model: Clan, comparator: sortClanList })
|
|
|
|
@listenTo @clans, 'sync', =>
|
|
|
|
@refreshNameMap @clans?.models
|
|
|
|
@render?()
|
|
|
|
@supermodel.loadCollection(@clans, 'clans', {cache: false})
|
2014-07-29 10:50:07 -04:00
|
|
|
super()
|
2014-11-25 13:19:38 -05:00
|
|
|
|
2015-04-06 14:17:13 -04:00
|
|
|
refreshNameMap: (clans) ->
|
|
|
|
return unless clans?
|
|
|
|
options =
|
|
|
|
url: '/db/user/-/names'
|
|
|
|
method: 'POST'
|
|
|
|
data: {ids: _.map(clans, (clan) -> clan.get('ownerID'))}
|
|
|
|
success: (models, response, options) =>
|
|
|
|
@idNameMap[userID] = models[userID].name for userID of models
|
|
|
|
@render?()
|
|
|
|
@supermodel.addRequestResource('user_names', options, 0).load()
|
|
|
|
|
2014-11-25 13:19:38 -05:00
|
|
|
onClickMoreButton: (e) ->
|
|
|
|
panel = $(e.target).closest('.panel')
|
|
|
|
panel.find('tr.hide').removeClass('hide')
|
2015-02-11 16:12:42 -05:00
|
|
|
panel.find('.panel-footer').remove()
|