state = null
server_url = null
top_card_z = 0 # css z-index of front-most card
-$pile_captions = {}
+piles = null
-show_message = (txt) -> txt
- # FIXME implement chat box or something
+window.log = []
+show_message = (txt) ->
+ window.log.push txt
+ if window.log.length > 20
+ window.log.shift()
+ return
# timeout function with args in convenient order
timeout = (ms, func) -> setTimeout func, ms
$table.append view
return view
+uninstantiate_card = (card) ->
+ show_message "uninstantiate card #{card.number}"
+ card.view.remove()
+ delete card.view
+
instantiate_card = (card) ->
+ show_message "instantiate card #{card.number}"
+ if card.view
+ die.a.horrible.death()
+
text = card.text
if card.owner is state.agent
card_class = 'my_card'
else
card_class = 'your_card'
- # initial card state from server has z so that stacks come out with the right layers
- if card.z?
- if card.z > top_card_z
- top_card_z = card.z
- else
- unless card.pile
- card.z = next_card_z()
+ if card.z > top_card_z
+ top_card_z = card.z
view = $ $ "<div class=\"card #{card_class}\" style=\"left: #{transform_x(card.x)}px; top: #{transform_y(card.y)}px; z-index: #{card.z}\"><span class=\"cardtext\">#{text}</span></div>"
- view.data card.number
+ card.view = view
button_box = $ $ '<div/>'
flip_button = new_button "flip over"
mark_button = new_button "mark"
view.draggable containment: '#table', grid: [20, 20]
view.bind 'dragstart', (event, ui) ->
view.css 'z-index': card.z = next_card_z()
+ if card.pile?
+ delete card.pile
+ update_pile_views()
view.bind 'dragstop', (event, ui) ->
p = view.position()
- state.move state.agent, card.number, transform_x(p.left), transform_y(p.top), card.z
- card.view = view
+ pile = null # FIXME figure out what pile we moved to
+ state.move state.agent, card.number, transform_x(p.left), transform_y(p.top), card.z, pile
error_lag = 3
cache: false
data: {
agent: state.agent
- game: 'test' # FIXME, and it the /get call too
+ game: 'test' # FIXME, and in the /get call too
messages: JSON.stringify(messages)
}
type: 'POST'
type: 'GET'
dataType: 'json'
error: (xhr, status, error) ->
- message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
+ show_message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
timeout error_lag * 1000, poll_for_updates
error_lag *= 2
success: (data, status, xhr) ->
return "1 card"
initialize_cards = () ->
- # clear everything
+ show_message 'initialize_cards'
$('.card').remove()
- $('.blank_card').remove()
-
+ top_card_z = 0
# instantiate cards in play
for card in state.cards
- instantiate_card card unless card.pile
-
- # build piles
- piles = [
- {key: 'p2_draw', x: transform_x(140), y: transform_y(20), name: "Draw Pile"}
- {key: 'p2_discard', x: transform_x(20), y: transform_y(20), name: "Discard Pile"}
- {key: 'p1_draw', x: transform_x(140), y: transform_y(flip_y(20)), name: "Draw Pile"}
- {key: 'p1_discard', x: transform_x(20), y: transform_y(flip_y(20)), name: "Discard Pile"}
- ]
- for pile in piles
- manage_pile = (pile) ->
+ delete card.view
+ instantiate_card card unless card.pile?
+
+ unless piles?
+ piles = [ # global
+ {key: 'p2_draw', x: transform_x(140), y: transform_y(20), name: "Draw Pile"}
+ {key: 'p2_discard', x: transform_x(20), y: transform_y(20), name: "Discard Pile"}
+ {key: 'p1_draw', x: transform_x(140), y: transform_y(flip_y(20)), name: "Draw Pile"}
+ {key: 'p1_discard', x: transform_x(20), y: transform_y(flip_y(20)), name: "Discard Pile"}
+ ]
+ for pile in piles
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
+ pile.$caption = $ $ "<div class=\"pile_caption\"><div>#{pile.name}:</div><div class=\"n_cards\">#{n_cards 0}</div></div>"
+
+ update_pile_views()
- view.append $caption
+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
+ ps[card.pile] = { total: 1, top_card: card, top_z: card.z }
- 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
+ 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
- manage_pile pile
+ # 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
init = ->
server_url = window.location
state = window.game_model.new me
- state.on 'move', (agent, card, x, y, z) ->
- # FIXME add/handle pile argument
+ state.on 'move', (agent, card, x, y, z, pile) ->
+ update_pile_views()
if agent is me
- tell_server ['move', agent, card, x, y, z]
+ tell_server ['move', agent, card, x, y, z, pile]
else
# FIXME should we use the z from the server? Should p1 use odd numbers and p2 even?
bring_card_to_front state.cards[card]