JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
start on deck designer
[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 = ->
48         top_card_z += 1
49         # p1 gets even numbers, p2 gets odd numbers
50         if state.agent is 'p1'
51                 top_card_z += top_card_z % 2
52         else
53                 top_card_z += 1 - (top_card_z % 2)
54
55         show_message "new z: #{top_card_z}"
56
57         return top_card_z
58
59
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>"
62         $table.append view
63         return view
64
65 find_pile = (x, y) ->
66         fudge = 40
67         for pile in piles
68                 if -fudge < pile.x - x < fudge and -fudge < pile.y - y < fudge
69                         return pile
70         return null
71
72 in_your_hand = (card) ->
73         return (not (card.pile?)) and ((transform_y card.y) < (card_height * 0.8))
74
75 uninstantiate_card = (card) ->
76         show_message "uninstantiate card #{card.number}"
77         card.view.remove()
78         delete card.view
79
80 instantiate_card = (card) ->
81         show_message "instantiate card #{card.number}"
82         if card.view
83                 die.a.horrible.death()
84
85         text = card.text
86         if card.owner is state.agent
87                 card_class = 'my_card'
88         else
89                 card_class = 'your_card'
90
91         if in_your_hand card
92                 card_class = "#{card_class} your_hand"
93
94         if card.z > top_card_z
95                 top_card_z = card.z
96
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>"
98         card.view = view
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
109         if card.marked
110                 view.addClass 'marked'
111         if card.flipped
112                 view.addClass 'flipped'
113         $table.append view
114         view.draggable containment: '#table', grid: [20, 20]
115         view.bind 'dragstart', (event, ui) ->
116                 view.css 'z-index': card.z = next_card_z()
117                 if card.pile?
118                         delete card.pile
119                 update_pile_views()
120         view.bind 'dragstop', (event, ui) ->
121                 p = view.position()
122                 x = transform_x(p.left)
123                 y = transform_y(p.top)
124                 pile = find_pile x, y
125                 if pile?
126                         x = pile.x
127                         y = pile.y
128                         pile = pile.key
129                         view.css {left: transform_x(x), top: transform_y(y)}
130                 state.move state.agent, card.number, x, y, card.z, pile
131
132 error_lag = 3
133
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
139         send_updates()
140
141 send_updates = ->
142         return if outgoing_messages.length is 0
143
144         messages = outgoing_messages
145         outgoing_messages = []
146
147         $.ajax "#{server_url}/set", {
148                 cache: false
149                 data: {
150                         agent: state.agent
151                         game: 'test' # FIXME, and in the /get call too
152                         messages: JSON.stringify(messages)
153                 }
154                 type: 'POST'
155                 dataType: 'json'
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
161                         error_lag *= 2
162                 success: (data, status, xhr) ->
163                         show_message "update sent"
164                         error_lag = 3
165         }
166
167 error_lag = 3
168 poll_for_updates = ->
169         $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
170                 cache: false
171                 type: 'GET'
172                 dataType: 'json'
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
176                         error_lag *= 2
177                 success: (data, status, xhr) ->
178                         state.process_messages data
179                         timeout 100, poll_for_updates
180                         error_lag = 3
181         }
182
183 n_cards = (count) ->
184         return "#{count} cards" unless count is 1
185         return "1 card"
186
187 initialize_cards = () ->
188         show_message 'initialize_cards'
189         $('.card').remove()
190         top_card_z = 0
191         # instantiate cards in play
192         for card in state.cards
193                 delete card.view
194
195         unless piles?
196                 piles = [ # global
197                         {key: 'p2_draw', x: 140, y: 20, name: "Draw Pile"}
198                         {key: 'p2_discard', x: 20, y: 20, name: "Discard Pile"}
199                         {key: 'p1_draw', x: flip_x(140), y: flip_y(20), name: "Draw Pile"}
200                         {key: 'p1_discard', x: flip_x(20), y: flip_y(20), name: "Discard Pile"}
201                 ]
202                 for pile in piles
203                         if pile.key.substr(0, 2) is state.agent
204                                 css_class = 'my_card'
205                         else
206                                 css_class = 'your_card'
207                         pile.$blank = new_blank_card pile.x, pile.y, css_class
208                         pile.$caption = $ $ "<div class=\"pile_caption\"><div>#{pile.name}:</div><div class=\"n_cards\">#{n_cards 0}</div></div>"
209
210         update_pile_views()
211
212 # also makes sure all non-piled cards are instantiated
213 update_pile_views = ->
214         ps = {}
215         for card in state.cards
216                 if card.pile?
217                         if ps[card.pile]?
218                                 ps[card.pile].total += 1
219                                 if card.z > ps[card.pile].top_z
220                                         if ps[card.pile].top_card.view?
221                                                 uninstantiate_card ps[card.pile].top_card
222                                         ps[card.pile].top_card = card
223                                         ps[card.pile].top_z = card.z
224                                 else if card.view
225                                         uninstantiate_card card
226                         else
227                                 ps[card.pile] = { total: 1, top_card: card, top_z: card.z }
228                 else
229                         # not in a pile
230                         instantiate_card card unless card.view?
231
232         for pile in piles
233                 # where should the caption be?
234                 if ps[pile.key]?
235                         unless ps[pile.key].top_card.view?
236                                 ps[pile.key].top_card.x = pile.x
237                                 ps[pile.key].top_card.y = pile.y
238                                 instantiate_card ps[pile.key].top_card
239                         caption_dest = ps[pile.key].top_card.view
240                 else
241                         caption_dest = pile.$blank
242                 if caption_dest isnt pile.caption_loc
243                         pile.$caption.detach()
244                         caption_dest.append pile.$caption
245                         pile.caption_loc = caption_dest
246
247                 # update caption to show correct number of cards in the pile
248                 card_count = 0
249                 card_count = ps[pile.key].total if ps[pile.key]?
250                 pile.$caption.children('.n_cards').html n_cards card_count
251
252
253 init = ->
254         if window.location.hash? and window.location.hash.length > 0
255                 me = window.location.hash.substr 1
256                 winloc = "#{window.location}"
257                 server_url = winloc.substr 0, winloc.length - window.location.hash.length
258         else
259                 me = 'p1'
260                 server_url = window.location
261
262         state = window.game_model.new me
263         state.on 'move', (agent, card, x, y, z, pile) ->
264                 if z > top_card_z
265                         top_card_z = z
266                 update_pile_views() # ensures instantiation of all visible cards
267                 if @cards[card].view? # the card is visible
268                         # show it face down if it's in the other player's hand
269                         @cards[card].view.toggleClass 'your_hand', in_your_hand @cards[card]
270
271                 if agent is me
272                         tell_server ['move', agent, card, x, y, z, pile]
273                 else
274                         if @cards[card].view?
275                                 @cards[card].view.css "z-index": z
276                                 @cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
277         state.on 'mark', (agent, card, state) ->
278                 if @cards[card].view?
279                         @cards[card].view.toggleClass 'marked', state
280                 if agent is me
281                         tell_server ['mark', agent, card, state]
282         state.on 'flip', (agent, card, state) ->
283                 if @cards[card].view?
284                         @cards[card].view.toggleClass 'flipped', state
285                 if agent is me
286                         tell_server ['flip', agent, card, state]
287         state.on 'set_cards', (agent, cards) ->
288                 initialize_cards()
289                 if agent is me
290                         tell_server ['set_cards', agent, cards]
291
292         # timeout so browser will stop showing that we're loading
293         timeout 1, poll_for_updates
294         timeout 2, ->
295                 # ask for initial state
296                 tell_server ['send_state', state.agent]
297
298 $ ->
299         $table = $ '#table'
300         table_width = $table.width()
301         table_height = $table.height()
302         card_width = $('#loading-card').outerWidth()
303         card_height = $('#loading-card').outerHeight()
304
305         init()