9 top_card_z = 0 # css z-index of front-most card
13 show_message = (txt) ->
15 if window.log.length > 20
19 # timeout function with args in convenient order
20 timeout = (ms, func) -> setTimeout func, ms
22 unless Array::shuffle?
24 return if @length is 0
28 current = Math.floor(Math.random() * (top + 1))
34 new_button = (text) -> $ $ "<div class=\"button\">#{text}</div>"
36 # transform coordinates from client-side coords to server-side coords (or back)
37 # this makes it so p2 view everything upside down (mirrored), but still sends coords rightside up
38 flip_x = (x) -> table_width - card_width - x
39 flip_y = (y) -> table_height - card_height - y
41 return x unless state.agent is 'p2'
44 return y unless state.agent is 'p2'
47 next_card_z = -> return top_card_z += 1
49 bring_card_to_front = (card) ->
50 card.view.css "z-index": next_card_z()
52 new_blank_card = (x, y, css_class) ->
53 view = $ $ "<div class=\"blank_card #{css_class}\" style=\"left: #{transform_x x}px; top: #{transform_y y}px; z-index: 0\"></div>"
60 if -fudge < pile.x - x < fudge and -fudge < pile.y - y < fudge
64 uninstantiate_card = (card) ->
65 show_message "uninstantiate card #{card.number}"
69 instantiate_card = (card) ->
70 show_message "instantiate card #{card.number}"
72 die.a.horrible.death()
75 if card.owner is state.agent
76 card_class = 'my_card'
78 card_class = 'your_card'
80 if card.z > top_card_z
83 view = $ $ "<div class=\"card #{card_class}\" style=\"left: #{transform_x(card.x)}px; top: #{transform_y(card.y)}px; z-index: #{card.z}\"><span class=\"cardtext\">#{text}</span></div>"
85 button_box = $ $ '<div/>'
86 flip_button = new_button "flip over"
87 mark_button = new_button "mark"
88 flip_button.bind 'click', ->
89 state.flip state.agent, card.number, ! view.hasClass 'flipped'
90 mark_button.bind 'click', ->
91 state.mark state.agent, card.number, ! view.hasClass 'marked'
92 button_box.append flip_button
93 button_box.append mark_button
94 view.append button_box
96 view.addClass 'marked'
98 view.addClass 'flipped'
100 view.draggable containment: '#table', grid: [20, 20]
101 view.bind 'dragstart', (event, ui) ->
102 view.css 'z-index': card.z = next_card_z()
106 view.bind 'dragstop', (event, ui) ->
108 x = transform_x(p.left)
109 y = transform_y(p.top)
110 pile = find_pile x, y
115 view.css {left: transform_x(x), top: transform_y(y)}
116 state.move state.agent, card.number, x, y, card.z, pile
120 outgoing_messages = []
121 # message should be [agent, method, args...]
122 # don't forget the agent (state.agent)
123 tell_server = (message) ->
124 outgoing_messages.push message
128 return if outgoing_messages.length is 0
130 messages = outgoing_messages
131 outgoing_messages = []
133 $.ajax "#{server_url}/set", {
137 game: 'test' # FIXME, and in the /get call too
138 messages: JSON.stringify(messages)
142 error: (xhr, status, error) ->
143 show_message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
144 for message in messages
145 outgoing_messages.unshift message
146 timeout error_lag * 1000, send_updates
148 success: (data, status, xhr) ->
149 show_message "update sent"
154 poll_for_updates = ->
155 $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
159 error: (xhr, status, error) ->
160 show_message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
161 timeout error_lag * 1000, poll_for_updates
163 success: (data, status, xhr) ->
164 state.process_messages data
165 timeout 100, poll_for_updates
170 return "#{count} cards" unless count is 1
173 initialize_cards = () ->
174 show_message 'initialize_cards'
177 # instantiate cards in play
178 for card in state.cards
180 instantiate_card card unless card.pile?
184 {key: 'p2_draw', x: 140, y: 20, name: "Draw Pile"}
185 {key: 'p2_discard', x: 20, y: 20, name: "Discard Pile"}
186 {key: 'p1_draw', x: flip_x(140), y: flip_y(20), name: "Draw Pile"}
187 {key: 'p1_discard', x: flip_x(20), y: flip_y(20), name: "Discard Pile"}
190 if pile.key.substr(0, 2) is state.agent
191 css_class = 'my_card'
193 css_class = 'your_card'
194 pile.$blank = new_blank_card pile.x, pile.y, css_class
195 pile.$caption = $ $ "<div class=\"pile_caption\"><div>#{pile.name}:</div><div class=\"n_cards\">#{n_cards 0}</div></div>"
199 update_pile_views = ->
201 for card in state.cards
204 ps[card.pile].total += 1
205 if card.z > ps[card.pile].top_z
206 if ps[card.pile].top_card.view?
207 uninstantiate_card ps[card.pile].top_card
208 ps[card.pile].top_card = card
209 ps[card.pile].top_z = card.z
211 uninstantiate_card card
213 ps[card.pile] = { total: 1, top_card: card, top_z: card.z }
216 # where should the caption be?
218 unless ps[pile.key].top_card.view?
219 ps[pile.key].top_card.x = pile.x
220 ps[pile.key].top_card.y = pile.y
221 instantiate_card ps[pile.key].top_card
222 caption_dest = ps[pile.key].top_card.view
224 caption_dest = pile.$blank
225 if caption_dest isnt pile.caption_loc
226 pile.$caption.detach()
227 caption_dest.append pile.$caption
228 pile.caption_loc = caption_dest
230 # update caption to show correct number of cards in the pile
232 card_count = ps[pile.key].total if ps[pile.key]?
233 pile.$caption.children('.n_cards').html n_cards card_count
237 if window.location.hash? and window.location.hash.length > 0
238 me = window.location.hash.substr 1
239 winloc = "#{window.location}"
240 server_url = winloc.substr 0, winloc.length - window.location.hash.length
243 server_url = window.location
245 state = window.game_model.new me
246 state.on 'move', (agent, card, x, y, z, pile) ->
249 tell_server ['move', agent, card, x, y, z, pile]
251 # FIXME use the z from the server
252 bring_card_to_front state.cards[card]
253 state.cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
254 state.on 'mark', (agent, card, state) ->
255 @cards[card].view.toggleClass 'marked', state
257 tell_server ['mark', agent, card, state]
258 state.on 'flip', (agent, card, state) ->
259 @cards[card].view.toggleClass 'flipped', state
261 tell_server ['flip', agent, card, state]
262 state.on 'set_cards', (agent, cards) ->
265 tell_server ['set_cards', agent, cards]
267 # timeout so browser will stop showing that we're loading
268 timeout 1, poll_for_updates
270 # ask for initial state
271 tell_server ['send_state', state.agent]
275 table_width = $table.width()
276 table_height = $table.height()
277 card_width = $('#loading-card').outerWidth()
278 card_height = $('#loading-card').outerHeight()