2015-04-01 19:00:39 -04:00
|
|
|
app = require 'core/application'
|
|
|
|
AuthModal = require 'views/core/AuthModal'
|
2015-03-31 16:28:57 -04:00
|
|
|
RootView = require 'views/core/RootView'
|
|
|
|
template = require 'templates/clans/clan-details'
|
2015-04-02 20:00:28 -04:00
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
2015-04-01 14:56:48 -04:00
|
|
|
Clan = require 'models/Clan'
|
2015-04-03 14:05:37 -04:00
|
|
|
EarnedAchievement = require 'models/EarnedAchievement'
|
2015-04-03 18:52:16 -04:00
|
|
|
LevelSession = require 'models/LevelSession'
|
2015-04-03 19:44:35 -04:00
|
|
|
ThangType = require 'models/ThangType'
|
2015-04-02 20:00:28 -04:00
|
|
|
User = require 'models/User'
|
2015-03-31 16:28:57 -04:00
|
|
|
|
2015-04-02 14:44:18 -04:00
|
|
|
# TODO: Message for clan not found
|
2015-04-01 19:00:39 -04:00
|
|
|
# TODO: join/leave mostly duped from clans view
|
|
|
|
|
2015-03-31 16:28:57 -04:00
|
|
|
module.exports = class ClanDetailsView extends RootView
|
|
|
|
id: 'clan-details-view'
|
|
|
|
template: template
|
|
|
|
|
2015-04-01 19:00:39 -04:00
|
|
|
events:
|
2015-04-02 14:44:18 -04:00
|
|
|
'click .delete-clan-btn': 'onDeleteClan'
|
2015-04-01 19:00:39 -04:00
|
|
|
'click .join-clan-btn': 'onJoinClan'
|
|
|
|
'click .leave-clan-btn': 'onLeaveClan'
|
2015-04-02 14:01:37 -04:00
|
|
|
'click .remove-member-btn': 'onRemoveMember'
|
2015-04-01 19:00:39 -04:00
|
|
|
|
2015-03-31 16:28:57 -04:00
|
|
|
constructor: (options, @clanID) ->
|
|
|
|
super options
|
2015-04-03 12:52:25 -04:00
|
|
|
@initData()
|
2015-03-31 16:28:57 -04:00
|
|
|
|
2015-04-02 20:00:28 -04:00
|
|
|
destroy: ->
|
|
|
|
@stopListening?()
|
|
|
|
|
2015-04-03 12:52:25 -04:00
|
|
|
initData: ->
|
2015-04-03 14:05:37 -04:00
|
|
|
@stats = {}
|
|
|
|
|
2015-04-03 12:52:25 -04:00
|
|
|
@clan = new Clan _id: @clanID
|
2015-04-03 15:22:44 -04:00
|
|
|
@members = new CocoCollection([], { url: "/db/clan/#{@clanID}/members", model: User, comparator:'slug' })
|
2015-04-03 14:05:37 -04:00
|
|
|
@memberAchievements = new CocoCollection([], { url: "/db/clan/#{@clanID}/member_achievements", model: EarnedAchievement, comparator:'_id' })
|
2015-04-03 18:52:16 -04:00
|
|
|
@memberSessions = new CocoCollection([], { url: "/db/clan/#{@clanID}/member_sessions", model: LevelSession, comparator:'_id' })
|
2015-04-03 14:05:37 -04:00
|
|
|
|
2015-04-03 12:52:25 -04:00
|
|
|
@listenTo me, 'sync', => @render?()
|
2015-04-03 18:52:16 -04:00
|
|
|
@listenTo @clan, 'sync', @onClanSync
|
|
|
|
@listenTo @members, 'sync', @onMembersSync
|
|
|
|
@listenTo @memberAchievements, 'sync', @onMemberAchievementsSync
|
|
|
|
@listenTo @memberSessions, 'sync', @onMemberSessionsSync
|
|
|
|
|
|
|
|
@supermodel.loadModel @clan, 'clan', cache: false
|
|
|
|
@supermodel.loadCollection(@members, 'members', {cache: false})
|
|
|
|
@supermodel.loadCollection(@memberAchievements, 'member_achievements', {cache: false})
|
|
|
|
@supermodel.loadCollection(@memberSessions, 'member_sessions', {cache: false})
|
2015-04-03 12:52:25 -04:00
|
|
|
|
2015-04-03 19:44:35 -04:00
|
|
|
getRenderData: ->
|
|
|
|
context = super()
|
|
|
|
context.clan = @clan
|
|
|
|
if application.isProduction()
|
|
|
|
context.joinClanLink = "https://codecombat.com/clans/#{@clanID}"
|
|
|
|
else
|
|
|
|
context.joinClanLink = "http://localhost:3000/clans/#{@clanID}"
|
|
|
|
context.owner = @owner
|
|
|
|
context.memberAchievementsMap = @memberAchievementsMap
|
|
|
|
context.memberLanguageMap = @memberLanguageMap
|
|
|
|
context.members = @members?.models
|
|
|
|
context.isOwner = @clan.get('ownerID') is me.id
|
|
|
|
context.isMember = @clanID in (me.get('clans') ? [])
|
|
|
|
context.stats = @stats
|
|
|
|
context
|
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
@updateHeroIcons()
|
|
|
|
|
2015-04-03 14:05:37 -04:00
|
|
|
refreshData: ->
|
|
|
|
me.fetch cache: false
|
|
|
|
@members.fetch cache: false
|
|
|
|
@memberAchievements.fetch cache: false
|
|
|
|
|
2015-04-03 19:44:35 -04:00
|
|
|
updateHeroIcons: ->
|
|
|
|
return unless @members?.models?
|
|
|
|
for member in @members.models
|
|
|
|
continue unless hero = member.get('heroConfig')?.thangType
|
|
|
|
for slug, original of ThangType.heroes when original is hero
|
|
|
|
@$el.find(".player-hero-icon[data-memberID=#{member.id}]").removeClass('.player-hero-icon').addClass('player-hero-icon ' + slug)
|
|
|
|
|
2015-04-03 18:52:16 -04:00
|
|
|
onClanSync: ->
|
|
|
|
unless @owner?
|
|
|
|
@owner = new User _id: @clan.get('ownerID')
|
|
|
|
@listenTo @owner, 'sync', => @render?()
|
|
|
|
@supermodel.loadModel @owner, 'owner', cache: false
|
|
|
|
@render?()
|
|
|
|
|
|
|
|
onMembersSync: ->
|
|
|
|
@stats.averageLevel = Math.round(@members.reduce(((sum, member) -> sum + member.level()), 0) / @members.length)
|
|
|
|
@render?()
|
|
|
|
|
|
|
|
onMemberAchievementsSync: ->
|
|
|
|
@stats.totalAchievements = @memberAchievements.models.length
|
|
|
|
@memberAchievementsMap = {}
|
|
|
|
for achievement in @memberAchievements.models
|
|
|
|
user = achievement.get('user')
|
|
|
|
@memberAchievementsMap[user] ?= []
|
|
|
|
@memberAchievementsMap[user].push achievement
|
|
|
|
@render?()
|
|
|
|
|
|
|
|
onMemberSessionsSync: ->
|
|
|
|
@memberSessionMap = {}
|
|
|
|
for levelSession in @memberSessions.models
|
|
|
|
user = levelSession.get('creator')
|
|
|
|
@memberSessionMap[user] ?= []
|
|
|
|
@memberSessionMap[user].push levelSession
|
|
|
|
@memberLanguageMap = {}
|
|
|
|
for user of @memberSessionMap
|
|
|
|
languageCounts = {}
|
|
|
|
for levelSession in @memberSessionMap[user]
|
|
|
|
language = levelSession.get('codeLanguage') or levelSession.get('submittedCodeLanguage')
|
|
|
|
languageCounts[language] = (languageCounts[language] or 0) + 1 if language
|
|
|
|
mostUsedCount = 0
|
|
|
|
for language, count of languageCounts
|
|
|
|
if count > mostUsedCount
|
|
|
|
mostUsedCount = count
|
|
|
|
@memberLanguageMap[user] = language
|
|
|
|
@render?()
|
|
|
|
|
2015-04-02 14:44:18 -04:00
|
|
|
onDeleteClan: (e) ->
|
|
|
|
return @openModalView(new AuthModal()) if me.isAnonymous()
|
|
|
|
options =
|
|
|
|
url: "/db/clan/#{@clanID}"
|
|
|
|
method: 'DELETE'
|
|
|
|
error: (model, response, options) =>
|
|
|
|
console.error 'Error joining clan', response
|
|
|
|
success: (model, response, options) =>
|
|
|
|
app.router.navigate "/clans"
|
|
|
|
window.location.reload()
|
|
|
|
@supermodel.addRequestResource( 'delete_clan', options).load()
|
|
|
|
|
2015-04-01 19:00:39 -04:00
|
|
|
onJoinClan: (e) ->
|
|
|
|
return @openModalView(new AuthModal()) if me.isAnonymous()
|
2015-04-02 14:44:18 -04:00
|
|
|
options =
|
|
|
|
url: "/db/clan/#{@clanID}/join"
|
|
|
|
method: 'PUT'
|
|
|
|
error: (model, response, options) =>
|
|
|
|
console.error 'Error joining clan', response
|
2015-04-03 14:05:37 -04:00
|
|
|
success: (model, response, options) => @refreshData()
|
2015-04-02 14:44:18 -04:00
|
|
|
@supermodel.addRequestResource( 'join_clan', options).load()
|
2015-04-01 19:00:39 -04:00
|
|
|
|
|
|
|
onLeaveClan: (e) ->
|
2015-04-02 14:44:18 -04:00
|
|
|
options =
|
|
|
|
url: "/db/clan/#{@clanID}/leave"
|
|
|
|
method: 'PUT'
|
|
|
|
error: (model, response, options) =>
|
|
|
|
console.error 'Error leaving clan', response
|
2015-04-03 14:05:37 -04:00
|
|
|
success: (model, response, options) => @refreshData()
|
2015-04-02 14:44:18 -04:00
|
|
|
@supermodel.addRequestResource( 'leave_clan', options).load()
|
2015-04-02 14:01:37 -04:00
|
|
|
|
|
|
|
onRemoveMember: (e) ->
|
|
|
|
if memberID = $(e.target).data('id')
|
|
|
|
options =
|
|
|
|
url: "/db/clan/#{@clanID}/remove/#{memberID}"
|
|
|
|
method: 'PUT'
|
|
|
|
error: (model, response, options) =>
|
|
|
|
console.error 'Error removing clan member', response
|
2015-04-03 14:05:37 -04:00
|
|
|
success: (model, response, options) => @refreshData()
|
2015-04-02 14:01:37 -04:00
|
|
|
@supermodel.addRequestResource( 'remove_member', options).load()
|
|
|
|
else
|
|
|
|
console.error "No member ID attached to remove button."
|