JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
clients ask/get initial state on [re]load
authorJason Woofenden <jason@jasonwoof.com>
Thu, 27 Oct 2011 19:16:55 +0000 (15:16 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Thu, 27 Oct 2011 19:16:55 +0000 (15:16 -0400)
client.coffee
common.coffee
server.coffee

index 63fd6c6..6f8d230 100644 (file)
@@ -130,6 +130,9 @@ init = ->
 
        # 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'
index cc33785..cec2bc5 100644 (file)
@@ -58,13 +58,16 @@ class GameState
                                @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()
index 29cddb1..7e191d8 100644 (file)
@@ -171,6 +171,14 @@ new_game = (id) ->
                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