2014-07-15 10:15:21 -04:00
|
|
|
UserView = require 'views/kinds/UserView'
|
2014-07-29 10:50:07 -04:00
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
|
|
|
LevelSession = require 'models/LevelSession'
|
2014-08-12 07:41:14 -04:00
|
|
|
template = require 'templates/user/user_home'
|
2014-07-15 10:15:21 -04:00
|
|
|
{me} = require 'lib/auth'
|
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
|
|
|
|
|
2014-07-21 13:49:16 -04:00
|
|
|
constructor: (userID, options) ->
|
2014-07-15 10:15:21 -04:00
|
|
|
super options
|
|
|
|
|
|
|
|
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
|
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()
|
2014-08-08 13:26:24 -04:00
|
|
|
@supermodel.loadCollection @levelSessions, 'levelSessions'
|
2014-08-07 16:03:00 -04:00
|
|
|
@supermodel.loadCollection @earnedAchievements, 'earnedAchievements'
|
2014-07-29 10:50:07 -04:00
|
|
|
super()
|