8 show_message = (txt) -> txt
9 # FIXME implement chat box or something
11 # timeout function with args in convenient order
12 timeout = (ms, func) -> setTimeout func, ms
14 unless Array::shuffle?
16 return if @length is 0
20 current = Math.floor(Math.random() * (top + 1))
26 new_button = (text) -> $ $ "<div class=\"button\">#{text}</div>"
28 # transform coordinates from client-side coords to server-side coords (or back)
29 # this makes it so p2 view everything upside down (mirrored), but still sends coords rightside up
30 transform_x = (x) -> x
32 return y unless state.agent is 'p2'
33 return table_height - card_height - y
35 instantiate_card = (card) ->
37 view = $ $ "<div class=\"card\" style=\"left: #{transform_x(card.x)}px; top: #{transform_y(card.y)}px\"><span class=\"cardtext\">#{text}</span></div>"
38 button_box = $ $ '<div/>'
39 flip_button = new_button "flip over"
40 mark_button = new_button "mark"
41 flip_button.bind 'click', ->
42 state.flip state.agent, card.number, ! view.hasClass 'flipped'
43 mark_button.bind 'click', ->
44 state.mark state.agent, card.number, ! view.hasClass 'marked'
45 button_box.append flip_button
46 button_box.append mark_button
47 view.append button_box
49 view.addClass 'marked'
51 view.addClass 'flipped'
53 view.draggable stack: '.card'
54 view.bind 'dragstop', (event, ui) ->
56 state.move state.agent, card.number, transform_x(p.left), transform_y(p.top)
61 outgoing_messages = []
62 # message should be [agent, method, args...]
63 # don't forget the agent (state.agent)
64 tell_server = (message) ->
65 outgoing_messages.push message
69 return if outgoing_messages.length is 0
71 messages = outgoing_messages
72 outgoing_messages = []
74 $.ajax "#{server_url}/set", {
78 game: 'test' # FIXME, and it the /get call too
79 messages: JSON.stringify(messages)
83 error: (xhr, status, error) ->
84 show_message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
85 for message in messages
86 outgoing_messages.unshift message
87 timeout error_lag * 1000, send_updates
89 success: (data, status, xhr) ->
90 show_message "update sent"
96 $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
100 error: (xhr, status, error) ->
101 message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
102 timeout error_lag * 1000, poll_for_updates
104 success: (data, status, xhr) ->
105 state.process_messages data
106 timeout 100, poll_for_updates
111 if window.location.hash? and window.location.hash.length > 0
112 me = window.location.hash.substr 1
113 winloc = "#{window.location}"
114 server_url = winloc.substr 0, winloc.length - window.location.hash.length
117 server_url = window.location
119 state = window.game_model.new me
120 state.on 'move', (agent, card, x, y) ->
121 # FIXME add/handle pile argument
123 tell_server ['move', agent, card, x, y]
125 state.cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
126 state.on 'mark', (agent, card, state) ->
127 @cards[card].view.toggleClass 'marked', state
129 tell_server ['mark', agent, card, state]
130 state.on 'flip', (agent, card, state) ->
131 @cards[card].view.toggleClass 'flipped', state
133 tell_server ['flip', agent, card, state]
134 state.on 'set_cards', (agent, cards) ->
135 # FIXME add agent arg and tell server if it's not us
138 instantiate_card card
140 tell_server ['set_cards', agent, cards]
142 # timeout so browser will stop showing that we're loading
143 timeout 1, poll_for_updates
145 # ask for initial state
146 tell_server ['send_state', state.agent]
150 table_height = $table.height()
151 card_height = $('#loading-card').outerHeight()