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 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 hide_deck_designer = false
193 for card in state.cards
194 if card.owner is state.agent
195 hide_deck_designer = true
198 if hide_deck_designer
199 $('#deck_designer').remove()
203 {key: 'p2_draw', x: 140, y: 20, name: "Draw Pile"}
204 {key: 'p2_discard', x: 20, y: 20, name: "Discard Pile"}
205 {key: 'p1_draw', x: flip_x(140), y: flip_y(20), name: "Draw Pile"}
206 {key: 'p1_discard', x: flip_x(20), y: flip_y(20), name: "Discard Pile"}
209 if pile.key.substr(0, 2) is state.agent
210 css_class = 'my_card'
212 css_class = 'your_card'
213 pile.$blank = new_blank_card pile.x, pile.y, css_class
214 pile.$caption = $ $ "<div class=\"pile_caption\"><div>#{pile.name}:</div><div class=\"n_cards\">#{n_cards 0}</div></div>"
218 # also makes sure all non-piled cards are instantiated
219 update_pile_views = ->
221 for card in state.cards
224 ps[card.pile].total += 1
225 if card.z > ps[card.pile].top_z
226 if ps[card.pile].top_card.view?
227 uninstantiate_card ps[card.pile].top_card
228 ps[card.pile].top_card = card
229 ps[card.pile].top_z = card.z
231 uninstantiate_card card
233 ps[card.pile] = { total: 1, top_card: card, top_z: card.z }
236 instantiate_card card unless card.view?
239 # where should the caption be?
241 unless ps[pile.key].top_card.view?
242 ps[pile.key].top_card.x = pile.x
243 ps[pile.key].top_card.y = pile.y
244 instantiate_card ps[pile.key].top_card
245 caption_dest = ps[pile.key].top_card.view
247 caption_dest = pile.$blank
248 if caption_dest isnt pile.caption_loc
249 pile.$caption.detach()
250 caption_dest.append pile.$caption
251 pile.caption_loc = caption_dest
253 # update caption to show correct number of cards in the pile
255 card_count = ps[pile.key].total if ps[pile.key]?
256 pile.$caption.children('.n_cards').html n_cards card_count
260 valumenous = (val) -> return true unless val is '' or val is ' '
262 init_possible_cards = ->
263 for card in window.cs_cards
264 text = "#{card.cardname} (#{card.faction})"
265 if valumenous card.attack or valumenous card.defense
266 text += " #{card.attack}/#{card.defense}"
267 text += "<br>#{card.type}"
268 if valumenous card.subtype
269 text += " • #{card.subtype}"
270 text += "<br>cost: #{card.cost} thresh: #{card.threshold}<br>"
273 summary = text.replace(/<br>/g, "\n")
275 possible_cards[card.id] = {id: card.id, text: text, summary: summary}
278 init_card_designer = ->
279 show_message 'init_card_designer'
281 container = $ '#deck_designer'
282 init_possible_cards()
284 for key, card of possible_cards
285 view = $ $ "<li>#{card.summary}</li>"
286 view.data 'id', card.id
287 view.bind 'click', ->
290 if cards_in_deck[id]?
291 delete cards_in_deck[id]
295 cards_in_deck[id] = true
296 $el.toggleClass 'in_deck', value
301 submit = $ $ "<div style=\"border: 1px solid black; margin: 0 auto 10px 10px; width: 40px; text-align: center\">Done</div>"
302 submit.bind 'click', ->
303 $('#deck_designer').remove()
304 show_message cards_in_deck
306 for key, value of cards_in_deck
308 text: possible_cards[key].text
310 pile: "#{state.agent}_draw"
316 cards.push $.extend {}, card # clone
317 cards.push $.extend {}, card # clone
318 cards.push $.extend {}, card # clone
320 # asign z-index in random order
323 card.z = next_card_z()
326 # let server assign card numbers
327 tell_server ['new_cards', state.agent, cards]
330 container.append submit
333 if window.location.hash? and window.location.hash.length > 0
334 me = window.location.hash.substr 1
335 winloc = "#{window.location}"
336 server_url = winloc.substr 0, winloc.length - window.location.hash.length
339 server_url = window.location
341 state = window.game_model.new me
342 state.on 'move', (agent, card, x, y, z, pile) ->
345 update_pile_views() # ensures instantiation of all visible cards
346 if @cards[card].view? # the card is visible
347 # show it face down if it's in the other player's hand
348 @cards[card].view.toggleClass 'your_hand', in_your_hand @cards[card]
351 tell_server ['move', agent, card, x, y, z, pile]
353 if @cards[card].view?
354 @cards[card].view.css "z-index": z
355 @cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
356 state.on 'mark', (agent, card, state) ->
357 if @cards[card].view?
358 @cards[card].view.toggleClass 'marked', state
360 tell_server ['mark', agent, card, state]
361 state.on 'flip', (agent, card, state) ->
362 if @cards[card].view?
363 @cards[card].view.toggleClass 'flipped', state
365 tell_server ['flip', agent, card, state]
366 state.on 'set_cards', (agent, cards) ->
368 tell_server ['set_cards', agent, cards]
370 state.on 'new_cards', (agent, cards) ->
373 # timeout so browser will stop showing that we're loading
374 timeout 1, init_card_designer
375 timeout 1, poll_for_updates
377 # ask for initial state
378 tell_server ['send_state', state.agent]
382 table_width = $table.width()
383 table_height = $table.height()
384 card_width = $('#loading_card').outerWidth()
385 card_height = $('#loading_card').outerHeight()