JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
hide other players hand
[peach-cgt.git] / client.coffee
1 # globals
2 $table = null
3 table_width = 0
4 table_height = 0
5 card_width = 0
6 card_height = 0
7 state = null
8 server_url = null
9 top_card_z = 0 # css z-index of front-most card
10 piles = null
11
12 window.log = []
13 show_message = (txt) ->
14         window.log.push txt
15         if window.log.length > 20
16                 window.log.shift()
17         return
18
19 # timeout function with args in convenient order
20 timeout = (ms, func) -> setTimeout func, ms
21
22 unless Array::shuffle?
23         Array::shuffle = ->
24                 return if @length is 0
25                 top = @length
26
27                 while --top
28                         current = Math.floor(Math.random() * (top + 1))
29                         tmp = @[current]
30                         @[current] = @[top]
31                         @[top] = tmp
32                 return
33
34 new_button = (text) -> $ $ "<div class=\"button\">#{text}</div>"
35
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
40 transform_x = (x) ->
41         return x unless state.agent is 'p2'
42         return flip_x x
43 transform_y = (y) ->
44         return y unless state.agent is 'p2'
45         return flip_y y
46
47 next_card_z = -> return top_card_z += 1
48
49 bring_card_to_front = (card) ->
50         card.view.css "z-index": next_card_z()
51
52 new_blank_card = (x, y, css_class) ->
53         view = $ $ "<div class=\"blank_card #{css_class}\" style=\"left: #{transform_x x}px; top: #{transform_y y}px; z-index: 0\"></div>"
54         $table.append view
55         return view
56
57 find_pile = (x, y) ->
58         fudge = 40
59         for pile in piles
60                 if -fudge < pile.x - x < fudge and -fudge < pile.y - y < fudge
61                         return pile
62         return null
63
64 in_your_hand = (card) ->
65         return (not (card.pile?)) and ((transform_y card.y) < (card_height * 0.8))
66
67 uninstantiate_card = (card) ->
68         show_message "uninstantiate card #{card.number}"
69         card.view.remove()
70         delete card.view
71
72 instantiate_card = (card) ->
73         show_message "instantiate card #{card.number}"
74         if card.view
75                 die.a.horrible.death()
76
77         text = card.text
78         if card.owner is state.agent
79                 card_class = 'my_card'
80         else
81                 card_class = 'your_card'
82
83         if in_your_hand card
84                 card_class = "#{card_class} your_hand"
85
86         if card.z > top_card_z
87                 top_card_z = card.z
88
89         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>"
90         card.view = view
91         button_box = $ $ '<div/>'
92         flip_button = new_button "flip over"
93         mark_button = new_button "mark"
94         flip_button.bind 'click', ->
95                 state.flip state.agent, card.number, ! view.hasClass 'flipped'
96         mark_button.bind 'click', ->
97                 state.mark state.agent, card.number, ! view.hasClass 'marked'
98         button_box.append flip_button
99         button_box.append mark_button
100         view.append button_box
101         if card.marked
102                 view.addClass 'marked'
103         if card.flipped
104                 view.addClass 'flipped'
105         $table.append view
106         view.draggable containment: '#table', grid: [20, 20]
107         view.bind 'dragstart', (event, ui) ->
108                 view.css 'z-index': card.z = next_card_z()
109                 if card.pile?
110                         delete card.pile
111                 update_pile_views()
112         view.bind 'dragstop', (event, ui) ->
113                 p = view.position()
114                 x = transform_x(p.left)
115                 y = transform_y(p.top)
116                 pile = find_pile x, y
117                 if pile?
118                         x = pile.x
119                         y = pile.y
120                         pile = pile.key
121                         view.css {left: transform_x(x), top: transform_y(y)}
122                 state.move state.agent, card.number, x, y, card.z, pile
123
124 error_lag = 3
125
126 outgoing_messages = []
127 # message should be [agent, method, args...]
128 # don't forget the agent (state.agent)
129 tell_server = (message) ->
130         outgoing_messages.push message
131         send_updates()
132
133 send_updates = ->
134         return if outgoing_messages.length is 0
135
136         messages = outgoing_messages
137         outgoing_messages = []
138
139         $.ajax "#{server_url}/set", {
140                 cache: false
141                 data: {
142                         agent: state.agent
143                         game: 'test' # FIXME, and in the /get call too
144                         messages: JSON.stringify(messages)
145                 }
146                 type: 'POST'
147                 dataType: 'json'
148                 error: (xhr, status, error) ->
149                         show_message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
150                         for message in messages
151                                 outgoing_messages.unshift message
152                         timeout error_lag * 1000, send_updates
153                         error_lag *= 2
154                 success: (data, status, xhr) ->
155                         show_message "update sent"
156                         error_lag = 3
157         }
158
159 error_lag = 3
160 poll_for_updates = ->
161         $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
162                 cache: false
163                 type: 'GET'
164                 dataType: 'json'
165                 error: (xhr, status, error) ->
166                         show_message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
167                         timeout error_lag * 1000, poll_for_updates
168                         error_lag *= 2
169                 success: (data, status, xhr) ->
170                         state.process_messages data
171                         timeout 100, poll_for_updates
172                         error_lag = 3
173         }
174
175 n_cards = (count) ->
176         return "#{count} cards" unless count is 1
177         return "1 card"
178
179 initialize_cards = () ->
180         show_message 'initialize_cards'
181         $('.card').remove()
182         top_card_z = 0
183         # instantiate cards in play
184         for card in state.cards
185                 delete card.view
186
187         unless piles?
188                 piles = [ # global
189                         {key: 'p2_draw', x: 140, y: 20, name: "Draw Pile"}
190                         {key: 'p2_discard', x: 20, y: 20, name: "Discard Pile"}
191                         {key: 'p1_draw', x: flip_x(140), y: flip_y(20), name: "Draw Pile"}
192                         {key: 'p1_discard', x: flip_x(20), y: flip_y(20), name: "Discard Pile"}
193                 ]
194                 for pile in piles
195                         if pile.key.substr(0, 2) is state.agent
196                                 css_class = 'my_card'
197                         else
198                                 css_class = 'your_card'
199                         pile.$blank = new_blank_card pile.x, pile.y, css_class
200                         pile.$caption = $ $ "<div class=\"pile_caption\"><div>#{pile.name}:</div><div class=\"n_cards\">#{n_cards 0}</div></div>"
201
202         update_pile_views()
203
204 # also makes sure all non-piled cards are instantiated FIXME rename
205 update_pile_views = ->
206         ps = {}
207         for card in state.cards
208                 if card.pile?
209                         if ps[card.pile]?
210                                 ps[card.pile].total += 1
211                                 if card.z > ps[card.pile].top_z
212                                         if ps[card.pile].top_card.view?
213                                                 uninstantiate_card ps[card.pile].top_card
214                                         ps[card.pile].top_card = card
215                                         ps[card.pile].top_z = card.z
216                                 else if card.view
217                                         uninstantiate_card card
218                         else
219                                 ps[card.pile] = { total: 1, top_card: card, top_z: card.z }
220                 else
221                         # not in a pile
222                         instantiate_card card unless card.view?
223
224         for pile in piles
225                 # where should the caption be?
226                 if ps[pile.key]?
227                         unless ps[pile.key].top_card.view?
228                                 ps[pile.key].top_card.x = pile.x
229                                 ps[pile.key].top_card.y = pile.y
230                                 instantiate_card ps[pile.key].top_card
231                         caption_dest = ps[pile.key].top_card.view
232                 else
233                         caption_dest = pile.$blank
234                 if caption_dest isnt pile.caption_loc
235                         pile.$caption.detach()
236                         caption_dest.append pile.$caption
237                         pile.caption_loc = caption_dest
238
239                 # update caption to show correct number of cards in the pile
240                 card_count = 0
241                 card_count = ps[pile.key].total if ps[pile.key]?
242                 pile.$caption.children('.n_cards').html n_cards card_count
243
244
245 init = ->
246         if window.location.hash? and window.location.hash.length > 0
247                 me = window.location.hash.substr 1
248                 winloc = "#{window.location}"
249                 server_url = winloc.substr 0, winloc.length - window.location.hash.length
250         else
251                 me = 'p1'
252                 server_url = window.location
253
254         state = window.game_model.new me
255         state.on 'move', (agent, card, x, y, z, pile) ->
256                 update_pile_views()
257                 if state.cards[card].view? # the card is visible
258                         # show it face down if it's in the other player's hand
259                         state.cards[card].view.toggleClass 'your_hand', in_your_hand state.cards[card]
260
261                 if agent is me
262                         tell_server ['move', agent, card, x, y, z, pile]
263                 else
264                         # FIXME use the z from the server
265                         bring_card_to_front state.cards[card]
266                         state.cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
267         state.on 'mark', (agent, card, state) ->
268                 @cards[card].view.toggleClass 'marked', state
269                 if agent is me
270                         tell_server ['mark', agent, card, state]
271         state.on 'flip', (agent, card, state) ->
272                 @cards[card].view.toggleClass 'flipped', state
273                 if agent is me
274                         tell_server ['flip', agent, card, state]
275         state.on 'set_cards', (agent, cards) ->
276                 initialize_cards()
277                 if agent is me
278                         tell_server ['set_cards', agent, cards]
279
280         # timeout so browser will stop showing that we're loading
281         timeout 1, poll_for_updates
282         timeout 2, ->
283                 # ask for initial state
284                 tell_server ['send_state', state.agent]
285
286 $ ->
287         $table = $ '#table'
288         table_width = $table.width()
289         table_height = $table.height()
290         card_width = $('#loading-card').outerWidth()
291         card_height = $('#loading-card').outerHeight()
292
293         init()