2 Peach CGT -- Card Game Table simulator
3 Copyright (C) 2011 Jason Woofenden
4 Lincensed under AGPLv3. Source here: https://gitorious.org/peach-cgt
15 top_card_z = 0 # css z-index of front-most card
19 show_message = (txt) ->
21 if window.log.length > 20
25 # timeout function with args in convenient order
26 timeout = (ms, func) -> setTimeout func, ms
28 unless Array::shuffle?
30 return if @length is 0
34 current = Math.floor(Math.random() * (top + 1))
40 new_button = (text) -> $ $ "<div class=\"button\">#{text}</div>"
42 # transform coordinates from client-side coords to server-side coords (or back)
43 # this makes it so p2 view everything upside down (mirrored), but still sends coords rightside up
44 flip_x = (x) -> table_width - card_width - x
45 flip_y = (y) -> table_height - card_height - y
47 return x unless state.agent is 'p2'
50 return y unless state.agent is 'p2'
55 # p1 gets even numbers, p2 gets odd numbers
56 if state.agent is 'p1'
57 top_card_z += top_card_z % 2
59 top_card_z += 1 - (top_card_z % 2)
61 show_message "new z: #{top_card_z}"
66 new_blank_card = (x, y, css_class) ->
67 view = $ $ "<div class=\"blank_card #{css_class}\" style=\"left: #{transform_x x}px; top: #{transform_y y}px; z-index: 0\"></div>"
74 if -fudge < pile.x - x < fudge and -fudge < pile.y - y < fudge
78 in_your_hand = (card) ->
79 return (not (card.pile?)) and ((transform_y card.y) < (card_height * 0.8))
81 uninstantiate_card = (card) ->
82 show_message "uninstantiate card #{card.number}"
86 instantiate_card = (card) ->
87 show_message "instantiate card #{card.number}"
89 die.a.horrible.death()
92 if card.owner is state.agent
93 card_class = 'my_card'
95 card_class = 'your_card'
98 card_class = "#{card_class} your_hand"
100 if card.z > top_card_z
103 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>"
105 button_box = $ $ '<div/>'
106 flip_button = new_button "flip over"
107 mark_button = new_button "mark"
108 flip_button.bind 'click', ->
109 state.flip state.agent, card.number, ! view.hasClass 'flipped'
110 mark_button.bind 'click', ->
111 state.mark state.agent, card.number, ! view.hasClass 'marked'
112 button_box.append flip_button
113 button_box.append mark_button
114 view.append button_box
116 view.addClass 'marked'
118 view.addClass 'flipped'
120 view.draggable grid: [20, 20]
121 view.bind 'dragstart', (event, ui) ->
122 view.css 'z-index': card.z = next_card_z()
126 view.bind 'dragstop', (event, ui) ->
128 x = transform_x(p.left)
129 y = transform_y(p.top)
130 pile = find_pile x, y
135 view.css {left: transform_x(x), top: transform_y(y)}
136 state.move state.agent, card.number, x, y, card.z, pile
140 outgoing_messages = []
141 # message should be [agent, method, args...]
142 # don't forget the agent (state.agent)
143 tell_server = (message) ->
144 outgoing_messages.push message
148 return if outgoing_messages.length is 0
150 messages = outgoing_messages
151 outgoing_messages = []
153 show_message "#{server_url}/set"
154 $.ajax "#{server_url}/set", {
159 messages: JSON.stringify(messages)
163 error: (xhr, status, error) ->
164 show_message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
165 for message in messages
166 outgoing_messages.unshift message
167 timeout error_lag * 1000, send_updates
169 success: (data, status, xhr) ->
170 show_message "update sent"
175 poll_for_updates = ->
176 $.ajax "#{server_url}/get?agent=#{state.agent}&game=#{state.slug}", {
180 error: (xhr, status, error) ->
181 show_message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
182 timeout error_lag * 1000, poll_for_updates
184 success: (data, status, xhr) ->
185 state.process_messages data
186 timeout 100, poll_for_updates
191 return "#{count} cards" unless count is 1
194 initialize_cards = () ->
195 show_message 'initialize_cards'
198 # instantiate cards in play
199 hide_deck_designer = false
200 for card in state.cards
201 if card.owner is state.agent
202 hide_deck_designer = true
205 if hide_deck_designer
206 $('#deck_designer').remove()
210 {key: 'p2_draw', x: 140, y: 20, name: "Draw Pile"}
211 {key: 'p2_discard', x: 20, y: 20, name: "Discard Pile"}
212 {key: 'p1_draw', x: flip_x(140), y: flip_y(20), name: "Draw Pile"}
213 {key: 'p1_discard', x: flip_x(20), y: flip_y(20), name: "Discard Pile"}
216 if pile.key.substr(0, 2) is state.agent
217 css_class = 'my_card'
219 css_class = 'your_card'
220 pile.$blank = new_blank_card pile.x, pile.y, css_class
221 pile.$caption = $ $ "<div class=\"pile_caption\"><div>#{pile.name}:</div><div class=\"n_cards\">#{n_cards 0}</div></div>"
225 # also makes sure all non-piled cards are instantiated
226 update_pile_views = ->
228 for card in state.cards
231 ps[card.pile].total += 1
232 if card.z > ps[card.pile].top_z
233 if ps[card.pile].top_card.view?
234 uninstantiate_card ps[card.pile].top_card
235 ps[card.pile].top_card = card
236 ps[card.pile].top_z = card.z
238 uninstantiate_card card
240 ps[card.pile] = { total: 1, top_card: card, top_z: card.z }
243 instantiate_card card unless card.view?
246 # where should the caption be?
248 unless ps[pile.key].top_card.view?
249 ps[pile.key].top_card.x = pile.x
250 ps[pile.key].top_card.y = pile.y
251 instantiate_card ps[pile.key].top_card
252 caption_dest = ps[pile.key].top_card.view
254 caption_dest = pile.$blank
255 if caption_dest isnt pile.caption_loc
256 pile.$caption.detach()
257 caption_dest.append pile.$caption
258 pile.caption_loc = caption_dest
260 # update caption to show correct number of cards in the pile
262 card_count = ps[pile.key].total if ps[pile.key]?
263 pile.$caption.children('.n_cards').html n_cards card_count
267 valumenous = (val) -> return true unless val is '' or val is ' '
269 init_possible_cards = ->
270 for card in window.cs_cards
271 text = "#{card.cardname} (#{card.faction})"
272 if valumenous card.attack or valumenous card.defense
273 text += " #{card.attack}/#{card.defense}"
274 text += "<br>#{card.type}"
275 if valumenous card.subtype
276 text += " • #{card.subtype}"
277 text += "<br>cost: #{card.cost} thresh: #{card.threshold}<br>"
280 summary = text.replace(/<br>/g, "\n")
282 possible_cards[card.id] = {id: card.id, text: text, summary: summary}
285 init_card_designer = ->
286 show_message 'init_card_designer'
288 container = $ '#deck_designer'
289 init_possible_cards()
291 for key, card of possible_cards
292 view = $ $ "<li>#{card.summary}</li>"
293 view.data 'id', card.id
294 view.bind 'click', ->
297 if cards_in_deck[id]?
298 delete cards_in_deck[id]
302 cards_in_deck[id] = true
303 $el.toggleClass 'in_deck', value
308 submit = $ $ "<div style=\"border: 1px solid black; margin: 0 auto 10px 10px; width: 40px; text-align: center\">Done</div>"
309 submit.bind 'click', ->
310 $('#deck_designer').remove()
311 show_message cards_in_deck
313 for key, value of cards_in_deck
315 text: possible_cards[key].text
317 pile: "#{state.agent}_draw"
323 cards.push $.extend {}, card # clone
324 cards.push $.extend {}, card # clone
325 cards.push $.extend {}, card # clone
327 # asign z-index in random order
330 card.z = next_card_z()
333 # let server assign card numbers
334 tell_server ['new_cards', state.agent, cards]
337 container.append submit
340 charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
342 while ret.length < 10
343 ret += charset[Math.floor(Math.random() * charset.length)]
348 if window.location.hash? and window.location.hash.length > 0
349 winloc = window.location.toString()
350 server_url = winloc.substr 0, winloc.length - window.location.hash.length
351 game_agent = window.location.hash.substr 1
352 s = game_agent.split /_/g
354 window.location = server_url
355 # in case that doesn't cause a redirect:
362 game = new_game_slug()
363 server_url = window.location.toString()
364 window.location.hash = "#{game}_#{me}" # TODO should I put a # at the beginning?
373 $('#main_header').append $ " <span id=\"other_player_url\">Here's the address for the other player in this game with you: <code>#{server_url}##{game}_#{other_player}</code></span>"
375 state = window.game_model.new game, me
376 state.on 'move', (agent, card, x, y, z, pile) ->
379 update_pile_views() # ensures instantiation of all visible cards
380 if @cards[card].view? # the card is visible
381 # show it face down if it's in the other player's hand
382 @cards[card].view.toggleClass 'your_hand', in_your_hand @cards[card]
385 tell_server ['move', agent, card, x, y, z, pile]
387 if @cards[card].view?
388 @cards[card].view.css "z-index": z
389 @cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
390 state.on 'mark', (agent, card, state) ->
391 if @cards[card].view?
392 @cards[card].view.toggleClass 'marked', state
394 tell_server ['mark', agent, card, state]
395 state.on 'flip', (agent, card, state) ->
396 if @cards[card].view?
397 @cards[card].view.toggleClass 'flipped', state
399 tell_server ['flip', agent, card, state]
400 state.on 'set_cards', (agent, cards) ->
402 tell_server ['set_cards', agent, cards]
404 state.on 'new_cards', (agent, cards) ->
407 # timeouts are so browser will stop showing that we're loading
410 tell_server ['new_game', state.slug, state.agent]
411 timeout 2, init_card_designer
412 timeout 3, poll_for_updates
414 # ask for initial state
415 tell_server ['send_state', state.agent]
419 table_width = $table.width()
420 table_height = $table.height()
421 card_width = $('#loading_card').outerWidth()
422 card_height = $('#loading_card').outerHeight()