From 17254bb57943dda8793cf80c37fb65db472de0ea Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Thu, 27 Oct 2011 15:16:55 -0400 Subject: [PATCH] clients ask/get initial state on [re]load --- client.coffee | 3 +++ common.coffee | 5 ++++- server.coffee | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/client.coffee b/client.coffee index 63fd6c6..6f8d230 100644 --- a/client.coffee +++ b/client.coffee @@ -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' diff --git a/common.coffee b/common.coffee index cc33785..cec2bc5 100644 --- a/common.coffee +++ b/common.coffee @@ -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() diff --git a/server.coffee b/server.coffee index 29cddb1..7e191d8 100644 --- a/server.coffee +++ b/server.coffee @@ -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 -- 1.7.10.4