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'
49 # p1 gets even numbers, p2 gets odd numbers
50 if state.agent is 'p1'
51 top_card_z += top_card_z % 2
53 top_card_z += 1 - (top_card_z % 2)
55 show_message "new z: #{top_card_z}"
60 new_blank_card = (x, y, css_class) ->
61 view = $ $ "<div class=\"blank_card #{css_class}\" style=\"left: #{transform_x x}px; top: #{transform_y y}px; z-index: 0\"></div>"
68 if -fudge < pile.x - x < fudge and -fudge < pile.y - y < fudge
72 in_your_hand = (card) ->
73 return (not (card.pile?)) and ((transform_y card.y) < (card_height * 0.8))
75 uninstantiate_card = (card) ->
76 show_message "uninstantiate card #{card.number}"
80 instantiate_card = (card) ->
81 show_message "instantiate card #{card.number}"
83 die.a.horrible.death()
86 if card.owner is state.agent
87 card_class = 'my_card'
89 card_class = 'your_card'
92 card_class = "#{card_class} your_hand"
94 if card.z > top_card_z
97 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>"
99 button_box = $ $ '<div/>'
100 flip_button = new_button "flip over"
101 mark_button = new_button "mark"
102 flip_button.bind 'click', ->
103 state.flip state.agent, card.number, ! view.hasClass 'flipped'
104 mark_button.bind 'click', ->
105 state.mark state.agent, card.number, ! view.hasClass 'marked'
106 button_box.append flip_button
107 button_box.append mark_button
108 view.append button_box
110 view.addClass 'marked'
112 view.addClass 'flipped'
114 view.draggable containment: '#table', grid: [20, 20]
115 view.bind 'dragstart', (event, ui) ->
116 view.css 'z-index': card.z = next_card_z()
120 view.bind 'dragstop', (event, ui) ->
122 x = transform_x(p.left)
123 y = transform_y(p.top)
124 pile = find_pile x, y
129 view.css {left: transform_x(x), top: transform_y(y)}
130 state.move state.agent, card.number, x, y, card.z, pile
134 outgoing_messages = []
135 # message should be [agent, method, args...]
136 # don't forget the agent (state.agent)
137 tell_server = (message) ->
138 outgoing_messages.push message
142 return if outgoing_messages.length is 0
144 messages = outgoing_messages
145 outgoing_messages = []
147 $.ajax "#{server_url}/set", {
151 game: 'test' # FIXME, and in the /get call too
152 messages: JSON.stringify(messages)
156 error: (xhr, status, error) ->
157 show_message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
158 for message in messages
159 outgoing_messages.unshift message
160 timeout error_lag * 1000, send_updates
162 success: (data, status, xhr) ->
163 show_message "update sent"
168 poll_for_updates = ->
169 $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
173 error: (xhr, status, error) ->
174 show_message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
175 timeout error_lag * 1000, poll_for_updates
177 success: (data, status, xhr) ->
178 state.process_messages data
179 timeout 100, poll_for_updates
184 return "#{count} cards" unless count is 1
187 initialize_cards = () ->
188 show_message 'initialize_cards'
191 # instantiate cards in play
192 for card in state.cards
197 {key: 'p2_draw', x: 140, y: 20, name: "Draw Pile"}
198 {key: 'p2_discard', x: 20, y: 20, name: "Discard Pile"}
199 {key: 'p1_draw', x: flip_x(140), y: flip_y(20), name: "Draw Pile"}
200 {key: 'p1_discard', x: flip_x(20), y: flip_y(20), name: "Discard Pile"}
203 if pile.key.substr(0, 2) is state.agent
204 css_class = 'my_card'
206 css_class = 'your_card'
207 pile.$blank = new_blank_card pile.x, pile.y, css_class
208 pile.$caption = $ $ "<div class=\"pile_caption\"><div>#{pile.name}:</div><div class=\"n_cards\">#{n_cards 0}</div></div>"
212 # also makes sure all non-piled cards are instantiated
213 update_pile_views = ->
215 for card in state.cards
218 ps[card.pile].total += 1
219 if card.z > ps[card.pile].top_z
220 if ps[card.pile].top_card.view?
221 uninstantiate_card ps[card.pile].top_card
222 ps[card.pile].top_card = card
223 ps[card.pile].top_z = card.z
225 uninstantiate_card card
227 ps[card.pile] = { total: 1, top_card: card, top_z: card.z }
230 instantiate_card card unless card.view?
233 # where should the caption be?
235 unless ps[pile.key].top_card.view?
236 ps[pile.key].top_card.x = pile.x
237 ps[pile.key].top_card.y = pile.y
238 instantiate_card ps[pile.key].top_card
239 caption_dest = ps[pile.key].top_card.view
241 caption_dest = pile.$blank
242 if caption_dest isnt pile.caption_loc
243 pile.$caption.detach()
244 caption_dest.append pile.$caption
245 pile.caption_loc = caption_dest
247 # update caption to show correct number of cards in the pile
249 card_count = ps[pile.key].total if ps[pile.key]?
250 pile.$caption.children('.n_cards').html n_cards card_count
254 if window.location.hash? and window.location.hash.length > 0
255 me = window.location.hash.substr 1
256 winloc = "#{window.location}"
257 server_url = winloc.substr 0, winloc.length - window.location.hash.length
260 server_url = window.location
262 state = window.game_model.new me
263 state.on 'move', (agent, card, x, y, z, pile) ->
266 update_pile_views() # ensures instantiation of all visible cards
267 if @cards[card].view? # the card is visible
268 # show it face down if it's in the other player's hand
269 @cards[card].view.toggleClass 'your_hand', in_your_hand @cards[card]
272 tell_server ['move', agent, card, x, y, z, pile]
274 if @cards[card].view?
275 @cards[card].view.css "z-index": z
276 @cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
277 state.on 'mark', (agent, card, state) ->
278 if @cards[card].view?
279 @cards[card].view.toggleClass 'marked', state
281 tell_server ['mark', agent, card, state]
282 state.on 'flip', (agent, card, state) ->
283 if @cards[card].view?
284 @cards[card].view.toggleClass 'flipped', state
286 tell_server ['flip', agent, card, state]
287 state.on 'set_cards', (agent, cards) ->
290 tell_server ['set_cards', agent, cards]
292 # timeout so browser will stop showing that we're loading
293 timeout 1, poll_for_updates
295 # ask for initial state
296 tell_server ['send_state', state.agent]
300 table_width = $table.width()
301 table_height = $table.height()
302 card_width = $('#loading-card').outerWidth()
303 card_height = $('#loading-card').outerHeight()