7 top_card_z = 0 # css z-index of front-most card
10 show_message = (txt) -> txt
11 # FIXME implement chat box or something
13 # timeout function with args in convenient order
14 timeout = (ms, func) -> setTimeout func, ms
16 unless Array::shuffle?
18 return if @length is 0
22 current = Math.floor(Math.random() * (top + 1))
28 new_button = (text) -> $ $ "<div class=\"button\">#{text}</div>"
30 # transform coordinates from client-side coords to server-side coords (or back)
31 # this makes it so p2 view everything upside down (mirrored), but still sends coords rightside up
32 flip_y = (y) -> table_height - card_height - y
33 transform_x = (x) -> x
35 return y unless state.agent is 'p2'
38 next_card_z = -> return top_card_z += 1
40 bring_card_to_front = (card) ->
41 card.view.css "z-index": next_card_z()
43 new_blank_card = (x, y) ->
44 view = $ $ "<div class=\"blank_card\" style=\"left: #{transform_x x}px; top: #{transform_y y}px; z-index: 0\"></div>"
48 instantiate_card = (card) ->
50 if card.owner is state.agent
51 card_class = 'my_card'
53 card_class = 'your_card'
55 # initial card state from server has z so that stacks come out with the right layers
57 if card.z > top_card_z
61 card.z = next_card_z()
63 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>"
65 button_box = $ $ '<div/>'
66 flip_button = new_button "flip over"
67 mark_button = new_button "mark"
68 flip_button.bind 'click', ->
69 state.flip state.agent, card.number, ! view.hasClass 'flipped'
70 mark_button.bind 'click', ->
71 state.mark state.agent, card.number, ! view.hasClass 'marked'
72 button_box.append flip_button
73 button_box.append mark_button
74 view.append button_box
76 view.addClass 'marked'
78 view.addClass 'flipped'
80 view.draggable containment: '#table', grid: [20, 20]
81 view.bind 'dragstart', (event, ui) ->
82 view.css 'z-index': card.z = next_card_z()
83 view.bind 'dragstop', (event, ui) ->
85 state.move state.agent, card.number, transform_x(p.left), transform_y(p.top), card.z
90 outgoing_messages = []
91 # message should be [agent, method, args...]
92 # don't forget the agent (state.agent)
93 tell_server = (message) ->
94 outgoing_messages.push message
98 return if outgoing_messages.length is 0
100 messages = outgoing_messages
101 outgoing_messages = []
103 $.ajax "#{server_url}/set", {
107 game: 'test' # FIXME, and it the /get call too
108 messages: JSON.stringify(messages)
112 error: (xhr, status, error) ->
113 show_message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
114 for message in messages
115 outgoing_messages.unshift message
116 timeout error_lag * 1000, send_updates
118 success: (data, status, xhr) ->
119 show_message "update sent"
124 poll_for_updates = ->
125 $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
129 error: (xhr, status, error) ->
130 message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
131 timeout error_lag * 1000, poll_for_updates
133 success: (data, status, xhr) ->
134 state.process_messages data
135 timeout 100, poll_for_updates
140 return "#{count} cards" unless count is 1
143 initialize_cards = () ->
146 $('.blank_card').remove()
148 # instantiate cards in play
149 for card in state.cards
150 instantiate_card card unless card.pile
154 {key: 'p2_draw', x: transform_x(140), y: transform_y(20), name: "Draw Pile"}
155 {key: 'p2_discard', x: transform_x(20), y: transform_y(20), name: "Discard Pile"}
156 {key: 'p1_draw', x: transform_x(140), y: transform_y(flip_y(20)), name: "Draw Pile"}
157 {key: 'p1_discard', x: transform_x(20), y: transform_y(flip_y(20)), name: "Discard Pile"}
160 manage_pile = (pile) ->
161 pile.$blank = new_blank_card pile.x, pile.y
164 if state.piles[pile.key]?.length
165 count = state.piles[pile.key].length
166 top = state.piles[pile.key][0]
167 $caption = $ $ "<div class=\"pile_caption\"><div>#{pile.name}:</div><div class=\"n_cards\">#{n_cards count}</div></div>"
168 pile.$caption = $caption
180 pile.drag_handler = view.bind 'dragstart', ->
181 pile.$caption.remove()
183 state.piles[pile.key].shift()
185 # FIXME make sure this state change is sent
186 # FIXME handle this message coming in
192 if window.location.hash? and window.location.hash.length > 0
193 me = window.location.hash.substr 1
194 winloc = "#{window.location}"
195 server_url = winloc.substr 0, winloc.length - window.location.hash.length
198 server_url = window.location
200 state = window.game_model.new me
201 state.on 'move', (agent, card, x, y, z) ->
202 # FIXME add/handle pile argument
204 tell_server ['move', agent, card, x, y, z]
206 # FIXME should we use the z from the server? Should p1 use odd numbers and p2 even?
207 bring_card_to_front state.cards[card]
208 state.cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
209 state.on 'mark', (agent, card, state) ->
210 @cards[card].view.toggleClass 'marked', state
212 tell_server ['mark', agent, card, state]
213 state.on 'flip', (agent, card, state) ->
214 @cards[card].view.toggleClass 'flipped', state
216 tell_server ['flip', agent, card, state]
217 state.on 'set_cards', (agent, cards) ->
220 tell_server ['set_cards', agent, cards]
222 # timeout so browser will stop showing that we're loading
223 timeout 1, poll_for_updates
225 # ask for initial state
226 tell_server ['send_state', state.agent]
230 table_height = $table.height()
231 card_height = $('#loading-card').outerHeight()