# timeout so browser will stop showing that we're loading
timeout 1, poll_for_updates
+ timeout 2, ->
+ # ask for initial state
+ tell_server ['send_state', state.agent]
$ ->
$table = $ '#table'
@piles[card.pile].push card
@trigger 'set_cards', agent, @cards
+ send_state: (agent) ->
+ @trigger 'send_state', agent
+
process_messages: (messages) ->
unless typeof messages is 'array' or typeof messages is 'object'
# FIXME what to do on error?
return typeof messages
for message in messages
- unless message instanceof Array and message[0]? and message[0] in ['move', 'mark', 'flip', 'set_cards']
+ unless message instanceof Array and message[0]? and message[0] in ['move', 'mark', 'flip', 'set_cards', 'send_state']
# FIXME what to do on error?
return 2
method = message.shift()
forward_events.call this, 'flip', agent, card, state
game.on 'set_cards', (agent, cards) ->
forward_events.call this, 'set_cards', agent, cards
+ game.on 'send_state', (agent) ->
+ timeout 10, =>
+ if agent is 'p1'
+ @p1_queue.push ['set_cards', 'server', @cards]
+ answer_soon this
+ if agent is 'p2'
+ @p2_queue.push ['set_cards', 'server', @cards]
+ answer_soon this
return game