7 top_card_z = 0 # css z-index of front-most card
9 show_message = (txt) -> txt
10 # FIXME implement chat box or something
12 # timeout function with args in convenient order
13 timeout = (ms, func) -> setTimeout func, ms
15 unless Array::shuffle?
17 return if @length is 0
21 current = Math.floor(Math.random() * (top + 1))
27 new_button = (text) -> $ $ "<div class=\"button\">#{text}</div>"
29 # transform coordinates from client-side coords to server-side coords (or back)
30 # this makes it so p2 view everything upside down (mirrored), but still sends coords rightside up
31 transform_x = (x) -> x
33 return y unless state.agent is 'p2'
34 return table_height - card_height - y
36 next_card_z = -> return top_card_z += 1
38 bring_card_to_front = (card) ->
39 card.view.css "z-index": next_card_z()
41 instantiate_card = (card) ->
43 view = $ $ "<div class=\"card\" style=\"left: #{transform_x(card.x)}px; top: #{transform_y(card.y)}px; z-index: #{next_card_z()}\"><span class=\"cardtext\">#{text}</span></div>"
44 button_box = $ $ '<div/>'
45 flip_button = new_button "flip over"
46 mark_button = new_button "mark"
47 flip_button.bind 'click', ->
48 state.flip state.agent, card.number, ! view.hasClass 'flipped'
49 mark_button.bind 'click', ->
50 state.mark state.agent, card.number, ! view.hasClass 'marked'
51 button_box.append flip_button
52 button_box.append mark_button
53 view.append button_box
55 view.addClass 'marked'
57 view.addClass 'flipped'
59 view.draggable stack: '.card'
60 view.bind 'dragstop', (event, ui) ->
61 top_card_z = parseInt view.css 'z-index'
63 state.move state.agent, card.number, transform_x(p.left), transform_y(p.top)
68 outgoing_messages = []
69 # message should be [agent, method, args...]
70 # don't forget the agent (state.agent)
71 tell_server = (message) ->
72 outgoing_messages.push message
76 return if outgoing_messages.length is 0
78 messages = outgoing_messages
79 outgoing_messages = []
81 $.ajax "#{server_url}/set", {
85 game: 'test' # FIXME, and it the /get call too
86 messages: JSON.stringify(messages)
90 error: (xhr, status, error) ->
91 show_message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
92 for message in messages
93 outgoing_messages.unshift message
94 timeout error_lag * 1000, send_updates
96 success: (data, status, xhr) ->
97 show_message "update sent"
102 poll_for_updates = ->
103 $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
107 error: (xhr, status, error) ->
108 message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
109 timeout error_lag * 1000, poll_for_updates
111 success: (data, status, xhr) ->
112 state.process_messages data
113 timeout 100, poll_for_updates
118 if window.location.hash? and window.location.hash.length > 0
119 me = window.location.hash.substr 1
120 winloc = "#{window.location}"
121 server_url = winloc.substr 0, winloc.length - window.location.hash.length
124 server_url = window.location
126 state = window.game_model.new me
127 state.on 'move', (agent, card, x, y) ->
128 # FIXME add/handle pile argument
130 tell_server ['move', agent, card, x, y]
132 bring_card_to_front state.cards[card]
133 state.cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
134 state.on 'mark', (agent, card, state) ->
135 @cards[card].view.toggleClass 'marked', state
137 tell_server ['mark', agent, card, state]
138 state.on 'flip', (agent, card, state) ->
139 @cards[card].view.toggleClass 'flipped', state
141 tell_server ['flip', agent, card, state]
142 state.on 'set_cards', (agent, cards) ->
143 # FIXME add agent arg and tell server if it's not us
146 instantiate_card card
148 tell_server ['set_cards', agent, cards]
150 # timeout so browser will stop showing that we're loading
151 timeout 1, poll_for_updates
153 # ask for initial state
154 tell_server ['send_state', state.agent]
158 table_height = $table.height()
159 card_height = $('#loading-card').outerHeight()