codecombat/app/views/play/level/tome/ThangListView.coffee

87 lines
3.2 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
# The ThangListView lives in the code area behind the SpellView, so that when you don't have a spell, you can select any Thang.
# It just ha a bunch of ThangListEntryViews (which are mostly ThangAvatarViews) in a few sections.
2014-07-17 20:20:11 -04:00
CocoView = require 'views/kinds/CocoView'
2014-01-03 13:32:13 -05:00
template = require 'templates/play/level/tome/thang_list'
{me} = require 'lib/auth'
ThangListEntryView = require './ThangListEntryView'
2014-01-03 13:32:13 -05:00
2014-07-17 20:20:11 -04:00
module.exports = class ThangListView extends CocoView
2014-01-03 13:32:13 -05:00
className: 'thang-list-view'
id: 'thang-list-view'
template: template
subscriptions: {}
constructor: (options) ->
super options
@spells = options.spells
@thangs = _.filter options.thangs, 'isSelectable'
@sortThangs()
sortThangs: ->
@readwriteThangs = _.sortBy _.filter(@thangs, (thang) =>
return true for spellKey, spell of @spells when thang.id of spell.thangs and spell.canWrite()
false
), @sortScoreForThang
@readThangs = _.sortBy _.filter(@thangs, (thang) =>
return true for spellKey, spell of @spells when thang.id of spell.thangs and spell.canRead() and not spell.canWrite()
false
), @sortScoreForThang
@muggleThangs = _.sortBy _.without(@thangs, @readwriteThangs..., @readThangs...), @sortScoreForThang
sortScoreForThang: (t) =>
# Sort by my team, then most spells and fewest shared Thangs per spell,
# then by thang.spriteName alpha, then by thang.id alpha.
# Lower comes first
score = 0
# Thangs on my team are highest priority
score -= 9001900190019001 if t.team is me.team
# The more spells per Thang, the lower
score -= 900190019001 for spellKey, spell of @spells when t.id of spell.thangs and spell.canRead()
# The more Thangs per spell, the higher
score += 90019001 for t2 of spell.thangs for spellKey, spell of @spells when t.id of spell.thangs
alpha = (s) -> _.reduce [0 ... s.length], ((acc, i) -> acc + s.charCodeAt(i) / Math.pow(100, i)), 0
# Alpha by spriteName
score += 9001 * alpha t.spriteName
# Alpha by id
score += alpha t.id
score
afterRender: ->
super()
@addThangListEntries()
addThangListEntries: ->
2014-01-03 13:32:13 -05:00
@entries = []
for [thangs, section, permission] in [
2014-06-30 22:16:26 -04:00
[@readwriteThangs, '#readwrite-thangs', 'readwrite'] # Your Minions
[@readThangs, '#read-thangs', 'read'] # Read-Only
[@muggleThangs, '#muggle-thangs', null] # Non-Castable
2014-01-03 13:32:13 -05:00
]
section = @$el.find(section).toggle thangs.length > 0
for thang in thangs
2014-01-03 13:32:13 -05:00
spells = _.filter @spells, (s) -> thang.id of s.thangs
entry = new ThangListEntryView thang: thang, spells: spells, permission: permission, supermodel: @supermodel
2014-01-03 13:32:13 -05:00
section.find('.thang-list').append entry.el # Render after appending so that we can access parent container for popover
entry.render()
@entries.push entry
topSpellForThang: (thang) ->
for entry in @entries when entry.thang.id is thang.id
return entry.spells[0]
null
adjustThangs: (spells, thangs) ->
@spells = @options.spells = spells
for entry in @entries
entry.$el.remove()
entry.destroy()
@thangs = @options.thangs = thangs
@sortThangs()
@addThangListEntries()
2014-02-11 15:02:49 -05:00
destroy: ->
entry.destroy() for entry in @entries
super()