7 top_card_z = 0 # css z-index of front-most card
11 show_message = (txt) ->
13 if window.log.length > 20
17 # timeout function with args in convenient order
18 timeout = (ms, func) -> setTimeout func, ms
20 unless Array::shuffle?
22 return if @length is 0
26 current = Math.floor(Math.random() * (top + 1))
32 new_button = (text) -> $ $ "<div class=\"button\">#{text}</div>"
34 # transform coordinates from client-side coords to server-side coords (or back)
35 # this makes it so p2 view everything upside down (mirrored), but still sends coords rightside up
36 flip_y = (y) -> table_height - card_height - y
37 transform_x = (x) -> x
39 return y unless state.agent is 'p2'
42 next_card_z = -> return top_card_z += 1
44 bring_card_to_front = (card) ->
45 card.view.css "z-index": next_card_z()
47 new_blank_card = (x, y) ->
48 view = $ $ "<div class=\"blank_card\" style=\"left: #{transform_x x}px; top: #{transform_y y}px; z-index: 0\"></div>"
52 uninstantiate_card = (card) ->
53 show_message "uninstantiate card #{card.number}"
57 instantiate_card = (card) ->
58 show_message "instantiate card #{card.number}"
60 die.a.horrible.death()
63 if card.owner is state.agent
64 card_class = 'my_card'
66 card_class = 'your_card'
68 if card.z > top_card_z
71 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>"
73 button_box = $ $ '<div/>'
74 flip_button = new_button "flip over"
75 mark_button = new_button "mark"
76 flip_button.bind 'click', ->
77 state.flip state.agent, card.number, ! view.hasClass 'flipped'
78 mark_button.bind 'click', ->
79 state.mark state.agent, card.number, ! view.hasClass 'marked'
80 button_box.append flip_button
81 button_box.append mark_button
82 view.append button_box
84 view.addClass 'marked'
86 view.addClass 'flipped'
88 view.draggable containment: '#table', grid: [20, 20]
89 view.bind 'dragstart', (event, ui) ->
90 view.css 'z-index': card.z = next_card_z()
94 view.bind 'dragstop', (event, ui) ->
96 pile = null # FIXME figure out what pile we moved to
97 state.move state.agent, card.number, transform_x(p.left), transform_y(p.top), card.z, pile
101 outgoing_messages = []
102 # message should be [agent, method, args...]
103 # don't forget the agent (state.agent)
104 tell_server = (message) ->
105 outgoing_messages.push message
109 return if outgoing_messages.length is 0
111 messages = outgoing_messages
112 outgoing_messages = []
114 $.ajax "#{server_url}/set", {
118 game: 'test' # FIXME, and in the /get call too
119 messages: JSON.stringify(messages)
123 error: (xhr, status, error) ->
124 show_message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
125 for message in messages
126 outgoing_messages.unshift message
127 timeout error_lag * 1000, send_updates
129 success: (data, status, xhr) ->
130 show_message "update sent"
135 poll_for_updates = ->
136 $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
140 error: (xhr, status, error) ->
141 show_message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
142 timeout error_lag * 1000, poll_for_updates
144 success: (data, status, xhr) ->
145 state.process_messages data
146 timeout 100, poll_for_updates
151 return "#{count} cards" unless count is 1
154 initialize_cards = () ->
155 show_message 'initialize_cards'
158 # instantiate cards in play
159 for card in state.cards
161 instantiate_card card unless card.pile?
165 {key: 'p2_draw', x: transform_x(140), y: transform_y(20), name: "Draw Pile"}
166 {key: 'p2_discard', x: transform_x(20), y: transform_y(20), name: "Discard Pile"}
167 {key: 'p1_draw', x: transform_x(140), y: transform_y(flip_y(20)), name: "Draw Pile"}
168 {key: 'p1_discard', x: transform_x(20), y: transform_y(flip_y(20)), name: "Discard Pile"}
171 pile.$blank = new_blank_card pile.x, pile.y
172 pile.$caption = $ $ "<div class=\"pile_caption\"><div>#{pile.name}:</div><div class=\"n_cards\">#{n_cards 0}</div></div>"
176 update_pile_views = ->
178 for card in state.cards
181 ps[card.pile].total += 1
182 if card.z > ps[card.pile].top_z
183 if ps[card.pile].top_card.view?
184 uninstantiate_card ps[card.pile].top_card
185 ps[card.pile].top_card = card
186 ps[card.pile].top_z = card.z
188 ps[card.pile] = { total: 1, top_card: card, top_z: card.z }
191 # where should the caption be?
193 unless ps[pile.key].top_card.view?
194 ps[pile.key].top_card.x = pile.x
195 ps[pile.key].top_card.y = pile.y
196 instantiate_card ps[pile.key].top_card
197 caption_dest = ps[pile.key].top_card.view
199 caption_dest = pile.$blank
200 if caption_dest isnt pile.caption_loc
201 pile.$caption.detach()
202 caption_dest.append pile.$caption
203 pile.caption_loc = caption_dest
205 # update caption to show correct number of cards in the pile
207 card_count = ps[pile.key].total if ps[pile.key]?
208 pile.$caption.children('.n_cards').html n_cards card_count
212 if window.location.hash? and window.location.hash.length > 0
213 me = window.location.hash.substr 1
214 winloc = "#{window.location}"
215 server_url = winloc.substr 0, winloc.length - window.location.hash.length
218 server_url = window.location
220 state = window.game_model.new me
221 state.on 'move', (agent, card, x, y, z, pile) ->
224 tell_server ['move', agent, card, x, y, z, pile]
226 # FIXME should we use the z from the server? Should p1 use odd numbers and p2 even?
227 bring_card_to_front state.cards[card]
228 state.cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
229 state.on 'mark', (agent, card, state) ->
230 @cards[card].view.toggleClass 'marked', state
232 tell_server ['mark', agent, card, state]
233 state.on 'flip', (agent, card, state) ->
234 @cards[card].view.toggleClass 'flipped', state
236 tell_server ['flip', agent, card, state]
237 state.on 'set_cards', (agent, cards) ->
240 tell_server ['set_cards', agent, cards]
242 # timeout so browser will stop showing that we're loading
243 timeout 1, poll_for_updates
245 # ask for initial state
246 tell_server ['send_state', state.agent]
250 table_height = $table.height()
251 card_height = $('#loading-card').outerHeight()