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 in_your_hand = (card) ->
65 return (not (card.pile?)) and ((transform_y card.y) < (card_height * 0.8))
67 uninstantiate_card = (card) ->
68 show_message "uninstantiate card #{card.number}"
72 instantiate_card = (card) ->
73 show_message "instantiate card #{card.number}"
75 die.a.horrible.death()
78 if card.owner is state.agent
79 card_class = 'my_card'
81 card_class = 'your_card'
84 card_class = "#{card_class} your_hand"
86 if card.z > top_card_z
89 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>"
91 button_box = $ $ '<div/>'
92 flip_button = new_button "flip over"
93 mark_button = new_button "mark"
94 flip_button.bind 'click', ->
95 state.flip state.agent, card.number, ! view.hasClass 'flipped'
96 mark_button.bind 'click', ->
97 state.mark state.agent, card.number, ! view.hasClass 'marked'
98 button_box.append flip_button
99 button_box.append mark_button
100 view.append button_box
102 view.addClass 'marked'
104 view.addClass 'flipped'
106 view.draggable containment: '#table', grid: [20, 20]
107 view.bind 'dragstart', (event, ui) ->
108 view.css 'z-index': card.z = next_card_z()
112 view.bind 'dragstop', (event, ui) ->
114 x = transform_x(p.left)
115 y = transform_y(p.top)
116 pile = find_pile x, y
121 view.css {left: transform_x(x), top: transform_y(y)}
122 state.move state.agent, card.number, x, y, card.z, pile
126 outgoing_messages = []
127 # message should be [agent, method, args...]
128 # don't forget the agent (state.agent)
129 tell_server = (message) ->
130 outgoing_messages.push message
134 return if outgoing_messages.length is 0
136 messages = outgoing_messages
137 outgoing_messages = []
139 $.ajax "#{server_url}/set", {
143 game: 'test' # FIXME, and in the /get call too
144 messages: JSON.stringify(messages)
148 error: (xhr, status, error) ->
149 show_message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
150 for message in messages
151 outgoing_messages.unshift message
152 timeout error_lag * 1000, send_updates
154 success: (data, status, xhr) ->
155 show_message "update sent"
160 poll_for_updates = ->
161 $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
165 error: (xhr, status, error) ->
166 show_message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
167 timeout error_lag * 1000, poll_for_updates
169 success: (data, status, xhr) ->
170 state.process_messages data
171 timeout 100, poll_for_updates
176 return "#{count} cards" unless count is 1
179 initialize_cards = () ->
180 show_message 'initialize_cards'
183 # instantiate cards in play
184 for card in state.cards
189 {key: 'p2_draw', x: 140, y: 20, name: "Draw Pile"}
190 {key: 'p2_discard', x: 20, y: 20, name: "Discard Pile"}
191 {key: 'p1_draw', x: flip_x(140), y: flip_y(20), name: "Draw Pile"}
192 {key: 'p1_discard', x: flip_x(20), y: flip_y(20), name: "Discard Pile"}
195 if pile.key.substr(0, 2) is state.agent
196 css_class = 'my_card'
198 css_class = 'your_card'
199 pile.$blank = new_blank_card pile.x, pile.y, css_class
200 pile.$caption = $ $ "<div class=\"pile_caption\"><div>#{pile.name}:</div><div class=\"n_cards\">#{n_cards 0}</div></div>"
204 # also makes sure all non-piled cards are instantiated
205 update_pile_views = ->
207 for card in state.cards
210 ps[card.pile].total += 1
211 if card.z > ps[card.pile].top_z
212 if ps[card.pile].top_card.view?
213 uninstantiate_card ps[card.pile].top_card
214 ps[card.pile].top_card = card
215 ps[card.pile].top_z = card.z
217 uninstantiate_card card
219 ps[card.pile] = { total: 1, top_card: card, top_z: card.z }
222 instantiate_card card unless card.view?
225 # where should the caption be?
227 unless ps[pile.key].top_card.view?
228 ps[pile.key].top_card.x = pile.x
229 ps[pile.key].top_card.y = pile.y
230 instantiate_card ps[pile.key].top_card
231 caption_dest = ps[pile.key].top_card.view
233 caption_dest = pile.$blank
234 if caption_dest isnt pile.caption_loc
235 pile.$caption.detach()
236 caption_dest.append pile.$caption
237 pile.caption_loc = caption_dest
239 # update caption to show correct number of cards in the pile
241 card_count = ps[pile.key].total if ps[pile.key]?
242 pile.$caption.children('.n_cards').html n_cards card_count
246 if window.location.hash? and window.location.hash.length > 0
247 me = window.location.hash.substr 1
248 winloc = "#{window.location}"
249 server_url = winloc.substr 0, winloc.length - window.location.hash.length
252 server_url = window.location
254 state = window.game_model.new me
255 state.on 'move', (agent, card, x, y, z, pile) ->
256 update_pile_views() # ensures instantiation of all visible cards
257 if state.cards[card].view? # the card is visible
258 # show it face down if it's in the other player's hand
259 state.cards[card].view.toggleClass 'your_hand', in_your_hand state.cards[card]
262 tell_server ['move', agent, card, x, y, z, pile]
264 # FIXME use the z from the server
265 bring_card_to_front state.cards[card]
266 state.cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
267 state.on 'mark', (agent, card, state) ->
268 @cards[card].view.toggleClass 'marked', state
270 tell_server ['mark', agent, card, state]
271 state.on 'flip', (agent, card, state) ->
272 @cards[card].view.toggleClass 'flipped', state
274 tell_server ['flip', agent, card, state]
275 state.on 'set_cards', (agent, cards) ->
278 tell_server ['set_cards', agent, cards]
280 # timeout so browser will stop showing that we're loading
281 timeout 1, poll_for_updates
283 # ask for initial state
284 tell_server ['send_state', state.agent]
288 table_width = $table.width()
289 table_height = $table.height()
290 card_width = $('#loading-card').outerWidth()
291 card_height = $('#loading-card').outerHeight()