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 = (model) ->
30 card = $ $ "<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, model.number, ! card.hasClass 'flipped'
36 mark_button.bind 'click', ->
37 state.mark state.agent, model.number, ! card.hasClass 'marked'
38 button_box.append flip_button
39 button_box.append mark_button
40 card.append button_box
42 card.draggable stack: '.card'
43 card.bind 'dragstop', (event, ui) ->
45 state.move state.agent, model.number, p.left, p.top
50 outgoing_messages = []
51 # message should be [agent, method, args...]
52 # don't forget the agent (state.agent)
53 tell_server = (message) ->
54 outgoing_messages.push message
58 return if outgoing_messages.length is 0
60 messages = outgoing_messages
61 outgoing_messages = []
63 $.ajax "#{server_url}/set", {
67 game: 'test' # FIXME, and it the /get call too
68 messages: JSON.stringify(messages)
72 error: (xhr, status, error) ->
73 message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
74 for message in messages
75 outgoing_messages.unshift message
76 timeout error_lag * 1000, send_updates
78 success: (data, status, xhr) ->
85 $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
89 error: (xhr, status, error) ->
90 message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
91 timeout error_lag * 1000, poll_for_updates
93 success: (data, status, xhr) ->
94 state.process_messages data
95 timeout 100, poll_for_updates
100 if window.location.hash? and window.location.hash.length > 0
101 me = window.location.hash.substr 1
102 winloc = "#{window.location}"
103 server_url = winloc.substr 0, winloc.length - window.location.hash.length
106 server_url = window.location
108 state = window.game_model.new me
109 state.on 'move', (agent, card, x, y) ->
110 # FIXME add/handle pile argument
112 tell_server ['move', agent, card, x, y]
114 state.cards[card].view.animate { left: "#{x}px", top: "#{y}px"}, 800
115 state.on 'mark', (agent, card, state) ->
116 @cards[card].view.toggleClass 'marked', state
118 tell_server ['mark', agent, card, state]
119 state.on 'flip', (agent, card, state) ->
120 @cards[card].view.toggleClass 'flipped', state
122 tell_server ['flip', agent, card, state]
123 state.on 'set_cards', (cards) ->
124 # FIXME add agent arg and tell server if it's not us
127 instantiate_card card
129 # timeout so browser will stop showing that we're loading
130 timeout 1, poll_for_updates