+n_cards = (count) ->
+ return "#{count} cards" unless count is 1
+ return "1 card"
+
+initialize_cards = () ->
+ show_message 'initialize_cards'
+ $('.card').remove()
+ top_card_z = 0
+ # instantiate cards in play
+ hide_deck_designer = false
+ for card in state.cards
+ if card.owner is state.agent
+ hide_deck_designer = true
+ delete card.view
+
+ if hide_deck_designer
+ $('#deck_designer').remove()
+
+ unless piles?
+ piles = [ # global
+ {key: 'p2_draw', x: 140, y: 20, name: "Draw Pile"}
+ {key: 'p2_discard', x: 20, y: 20, name: "Discard Pile"}
+ {key: 'p1_draw', x: flip_x(140), y: flip_y(20), name: "Draw Pile"}
+ {key: 'p1_discard', x: flip_x(20), y: flip_y(20), name: "Discard Pile"}
+ ]
+ for pile in piles
+ if pile.key.substr(0, 2) is state.agent
+ css_class = 'my_card'
+ else
+ css_class = 'your_card'
+ pile.$blank = new_blank_card pile.x, pile.y, css_class
+ pile.$caption = $ $ "<div class=\"pile_caption\"><div>#{pile.name}:</div><div class=\"n_cards\">#{n_cards 0}</div></div>"
+
+ update_pile_views()
+
+# also makes sure all non-piled cards are instantiated
+update_pile_views = ->
+ ps = {}
+ for card in state.cards
+ if card.pile?
+ if ps[card.pile]?
+ ps[card.pile].total += 1
+ if card.z > ps[card.pile].top_z
+ if ps[card.pile].top_card.view?
+ uninstantiate_card ps[card.pile].top_card
+ ps[card.pile].top_card = card
+ ps[card.pile].top_z = card.z
+ else if card.view
+ uninstantiate_card card
+ else
+ ps[card.pile] = { total: 1, top_card: card, top_z: card.z }
+ else
+ # not in a pile
+ instantiate_card card unless card.view?
+
+ for pile in piles
+ # where should the caption be?
+ if ps[pile.key]?
+ unless ps[pile.key].top_card.view?
+ ps[pile.key].top_card.x = pile.x
+ ps[pile.key].top_card.y = pile.y
+ instantiate_card ps[pile.key].top_card
+ caption_dest = ps[pile.key].top_card.view
+ else
+ caption_dest = pile.$blank
+ if caption_dest isnt pile.caption_loc
+ pile.$caption.detach()
+ caption_dest.append pile.$caption
+ pile.caption_loc = caption_dest
+
+ # update caption to show correct number of cards in the pile
+ card_count = 0
+ card_count = ps[pile.key].total if ps[pile.key]?
+ pile.$caption.children('.n_cards').html n_cards card_count
+
+possible_cards = {}
+
+valumenous = (val) -> return true unless val is '' or val is ' '
+
+init_possible_cards = ->
+ for card in window.cs_cards
+ text = "#{card.cardname} (#{card.faction})"
+ if valumenous card.attack or valumenous card.defense
+ text += " #{card.attack}/#{card.defense}"
+ text += "<br>#{card.type}"
+ if valumenous card.subtype
+ text += " • #{card.subtype}"
+ text += "<br>cost: #{card.cost} thresh: #{card.threshold}<br>"
+ text += card.rules
+
+ summary = text.replace(/<br>/g, "\n")
+
+ possible_cards[card.id] = {id: card.id, text: text, summary: summary}
+
+
+init_card_designer = ->
+ show_message 'init_card_designer'
+ cards_in_deck = {}
+ container = $ '#deck_designer'
+ init_possible_cards()
+ ul = $ $ '<ul/>'
+ for key, card of possible_cards
+ view = $ $ "<li>#{card.summary}</li>"
+ view.data 'id', card.id
+ view.bind 'click', ->
+ $el = $ this
+ id = $el.data 'id'
+ if cards_in_deck[id]?
+ delete cards_in_deck[id]
+ value = false
+ else
+ value = true
+ cards_in_deck[id] = true
+ $el.toggleClass 'in_deck', value
+ ul.append view
+
+ container.append ul
+
+ submit = $ $ "<div style=\"border: 1px solid black\">Done</div>"
+ submit.bind 'click', ->
+ $('#deck_designer').remove()
+ show_message cards_in_deck
+ cards = []
+ for key, value of cards_in_deck
+ card = {
+ text: possible_cards[key].text
+ owner: state.agent
+ pile: "#{state.agent}_draw"
+ x: 0
+ y: 0
+ z: next_card_z()
+ flipped: true
+ }
+ cards.push card
+ cards.push card
+ cards.push card
+ cards.push card
+
+ cards.shuffle()
+ show_message cards
+
+ # let server assign card numbers
+ tell_server ['new_cards', state.agent, cards]
+
+
+ container.append submit
+