+ view.append button_box
+ if card.marked
+ view.addClass 'marked'
+ if card.flipped
+ view.addClass 'flipped'
+ $table.append view
+ view.draggable containment: '#table', grid: [20, 20]
+ view.bind 'dragstart', (event, ui) ->
+ view.css 'z-index': card.z = next_card_z()
+ 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
+
+error_lag = 3
+
+outgoing_messages = []
+# message should be [agent, method, args...]
+# don't forget the agent (state.agent)
+tell_server = (message) ->
+ outgoing_messages.push message
+ send_updates()
+
+send_updates = ->
+ return if outgoing_messages.length is 0
+
+ messages = outgoing_messages
+ outgoing_messages = []
+
+ $.ajax "#{server_url}/set", {
+ cache: false
+ data: {
+ agent: state.agent
+ game: 'test' # FIXME, and it the /get call too
+ messages: JSON.stringify(messages)
+ }
+ type: 'POST'
+ dataType: 'json'
+ error: (xhr, status, error) ->
+ show_message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
+ for message in messages
+ outgoing_messages.unshift message
+ timeout error_lag * 1000, send_updates
+ error_lag *= 2
+ success: (data, status, xhr) ->
+ show_message "update sent"
+ error_lag = 3
+ }
+
+error_lag = 3
+poll_for_updates = ->
+ $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
+ cache: false
+ 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})"
+ timeout error_lag * 1000, poll_for_updates
+ error_lag *= 2
+ success: (data, status, xhr) ->
+ state.process_messages data
+ timeout 100, poll_for_updates
+ error_lag = 3
+ }
+
+n_cards = (count) ->
+ return "#{count} cards" unless count is 1
+ return "1 card"
+
+initialize_cards = () ->
+ # clear everything
+ $('.card').remove()
+ $('.blank_card').remove()
+
+ # instantiate cards in play
+ for card in state.cards
+ instantiate_card card unless card.pile
+
+ # build piles
+ piles = [
+ {key: 'p2_draw', x: transform_x(160), 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(160), 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
+ top = top.view
+ else
+ top = pile.$blank
+
+ top.append $caption
+
+ #pile.drag_handler = top.bind 'dragstart', ->
+ # FIXME
+