- pile.$blank = new_blank_card pile.x, pile.y
- count = 0
- top = null
- if state.piles[pile.key]?.length
- count = state.piles[pile.key].length
- top = state.piles[pile.key][0]
- $caption = $ $ "<div class=\"pile_caption\"><div>#{pile.name}:</div><div class=\"n_cards\">#{n_cards count}</div></div>"
- pile.$caption = $caption
- if top?
- top.x = pile.x
- top.y = pile.y
- instantiate_card top
- top = top.view
- else
- top = pile.$blank
-
- top.append $caption
-
- #pile.drag_handler = top.bind 'dragstart', ->
- # FIXME
+ manage_pile = (pile) ->
+ pile.$blank = new_blank_card pile.x, pile.y
+ count = 0
+ top = null
+ if state.piles[pile.key]?.length
+ count = state.piles[pile.key].length
+ top = state.piles[pile.key][0]
+ $caption = $ $ "<div class=\"pile_caption\"><div>#{pile.name}:</div><div class=\"n_cards\">#{n_cards count}</div></div>"
+ pile.$caption = $caption
+ if top?
+ top.x = pile.x
+ top.y = pile.y
+ instantiate_card top
+ view = top.view
+ else
+ view = pile.$blank
+
+ view.append $caption
+
+ if top?
+ pile.drag_handler = view.bind 'dragstart', ->
+ pile.$caption.remove()
+ delete top.pile
+ state.piles[pile.key].shift()
+ manage_pile pile
+ # FIXME make sure this state change is sent
+ # FIXME handle this message coming in
+
+ manage_pile pile