7 # FIXME implement chat box or something
9 # timeout function with args in convenient order
10 timeout = (ms, func) -> setTimeout func, ms
12 unless Array::shuffle?
14 return if @length is 0
18 current = Math.floor(Math.random() * (top + 1))
24 new_button = (text) -> $ $ "<div class=\"button\">#{text}</div>"
26 instantiate_card = (card) ->
30 view = $ $ "<div class=\"card\" style=\"left: #{x}px; top: #{y}px\"><span class=\"cardtext\">#{text}</span></div>"
31 button_box = $ $ '<div/>'
32 flip_button = new_button "flip over"
33 mark_button = new_button "mark"
34 flip_button.bind 'click', ->
35 state.flip state.agent, card.number, ! view.hasClass 'flipped'
36 mark_button.bind 'click', ->
37 state.mark state.agent, card.number, ! view.hasClass 'marked'
38 button_box.append flip_button
39 button_box.append mark_button
40 view.append button_box
42 view.addClass 'marked'
44 view.addClass 'flipped'
46 view.draggable stack: '.card'
47 view.bind 'dragstop', (event, ui) ->
49 state.move state.agent, card.number, p.left, p.top
54 outgoing_messages = []
55 # message should be [agent, method, args...]
56 # don't forget the agent (state.agent)
57 tell_server = (message) ->
58 outgoing_messages.push message
62 return if outgoing_messages.length is 0
64 messages = outgoing_messages
65 outgoing_messages = []
67 $.ajax "#{server_url}/set", {
71 game: 'test' # FIXME, and it the /get call too
72 messages: JSON.stringify(messages)
76 error: (xhr, status, error) ->
77 message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
78 for message in messages
79 outgoing_messages.unshift message
80 timeout error_lag * 1000, send_updates
82 success: (data, status, xhr) ->
89 $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
93 error: (xhr, status, error) ->
94 message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
95 timeout error_lag * 1000, poll_for_updates
97 success: (data, status, xhr) ->
98 state.process_messages data
99 timeout 100, poll_for_updates
104 if window.location.hash? and window.location.hash.length > 0
105 me = window.location.hash.substr 1
106 winloc = "#{window.location}"
107 server_url = winloc.substr 0, winloc.length - window.location.hash.length
110 server_url = window.location
112 state = window.game_model.new me
113 state.on 'move', (agent, card, x, y) ->
114 # FIXME add/handle pile argument
116 tell_server ['move', agent, card, x, y]
118 state.cards[card].view.animate { left: "#{x}px", top: "#{y}px"}, 800
119 state.on 'mark', (agent, card, state) ->
120 @cards[card].view.toggleClass 'marked', state
122 tell_server ['mark', agent, card, state]
123 state.on 'flip', (agent, card, state) ->
124 @cards[card].view.toggleClass 'flipped', state
126 tell_server ['flip', agent, card, state]
127 state.on 'set_cards', (agent, cards) ->
128 # FIXME add agent arg and tell server if it's not us
131 instantiate_card card
133 tell_server ['set_cards', agent, cards]
135 # timeout so browser will stop showing that we're loading
136 timeout 1, poll_for_updates
138 # ask for initial state
139 tell_server ['send_state', state.agent]