JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix view (flip x too), arrange piles correctly
[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 uninstantiate_card = (card) ->
58         show_message "uninstantiate card #{card.number}"
59         card.view.remove()
60         delete card.view
61
62 instantiate_card = (card) ->
63         show_message "instantiate card #{card.number}"
64         if card.view
65                 die.a.horrible.death()
66
67         text = card.text
68         if card.owner is state.agent
69                 card_class = 'my_card'
70         else
71                 card_class = 'your_card'
72
73         if card.z > top_card_z
74                 top_card_z = card.z
75
76         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>"
77         card.view = view
78         button_box = $ $ '<div/>'
79         flip_button = new_button "flip over"
80         mark_button = new_button "mark"
81         flip_button.bind 'click', ->
82                 state.flip state.agent, card.number, ! view.hasClass 'flipped'
83         mark_button.bind 'click', ->
84                 state.mark state.agent, card.number, ! view.hasClass 'marked'
85         button_box.append flip_button
86         button_box.append mark_button
87         view.append button_box
88         if card.marked
89                 view.addClass 'marked'
90         if card.flipped
91                 view.addClass 'flipped'
92         $table.append view
93         view.draggable containment: '#table', grid: [20, 20]
94         view.bind 'dragstart', (event, ui) ->
95                 view.css 'z-index': card.z = next_card_z()
96                 if card.pile?
97                         delete card.pile
98                 update_pile_views()
99         view.bind 'dragstop', (event, ui) ->
100                 p = view.position()
101                 pile = null # FIXME figure out what pile we moved to
102                 state.move state.agent, card.number, transform_x(p.left), transform_y(p.top), card.z, pile
103
104 error_lag = 3
105
106 outgoing_messages = []
107 # message should be [agent, method, args...]
108 # don't forget the agent (state.agent)
109 tell_server = (message) ->
110         outgoing_messages.push message
111         send_updates()
112
113 send_updates = ->
114         return if outgoing_messages.length is 0
115
116         messages = outgoing_messages
117         outgoing_messages = []
118
119         $.ajax "#{server_url}/set", {
120                 cache: false
121                 data: {
122                         agent: state.agent
123                         game: 'test' # FIXME, and in the /get call too
124                         messages: JSON.stringify(messages)
125                 }
126                 type: 'POST'
127                 dataType: 'json'
128                 error: (xhr, status, error) ->
129                         show_message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
130                         for message in messages
131                                 outgoing_messages.unshift message
132                         timeout error_lag * 1000, send_updates
133                         error_lag *= 2
134                 success: (data, status, xhr) ->
135                         show_message "update sent"
136                         error_lag = 3
137         }
138
139 error_lag = 3
140 poll_for_updates = ->
141         $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
142                 cache: false
143                 type: 'GET'
144                 dataType: 'json'
145                 error: (xhr, status, error) ->
146                         show_message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
147                         timeout error_lag * 1000, poll_for_updates
148                         error_lag *= 2
149                 success: (data, status, xhr) ->
150                         state.process_messages data
151                         timeout 100, poll_for_updates
152                         error_lag = 3
153         }
154
155 n_cards = (count) ->
156         return "#{count} cards" unless count is 1
157         return "1 card"
158
159 initialize_cards = () ->
160         show_message 'initialize_cards'
161         $('.card').remove()
162         top_card_z = 0
163         # instantiate cards in play
164         for card in state.cards
165                 delete card.view
166                 instantiate_card card unless card.pile?
167
168         unless piles?
169                 piles = [ # global
170                         {key: 'p2_draw', x: 140, y: 20, name: "Draw Pile"}
171                         {key: 'p2_discard', x: 20, y: 20, name: "Discard Pile"}
172                         {key: 'p1_draw', x: flip_x(140), y: flip_y(20), name: "Draw Pile"}
173                         {key: 'p1_discard', x: flip_x(20), y: flip_y(20), name: "Discard Pile"}
174                 ]
175                 for pile in piles
176                         if pile.key.substr(0, 2) is state.agent
177                                 css_class = 'my_card'
178                         else
179                                 css_class = 'your_card'
180                         pile.$blank = new_blank_card pile.x, pile.y, css_class
181                         pile.$caption = $ $ "<div class=\"pile_caption\"><div>#{pile.name}:</div><div class=\"n_cards\">#{n_cards 0}</div></div>"
182
183         update_pile_views()
184
185 update_pile_views = ->
186         ps = {}
187         for card in state.cards
188                 if card.pile?
189                         if ps[card.pile]?
190                                 ps[card.pile].total += 1
191                                 if card.z > ps[card.pile].top_z
192                                         if ps[card.pile].top_card.view?
193                                                 uninstantiate_card ps[card.pile].top_card
194                                         ps[card.pile].top_card = card
195                                         ps[card.pile].top_z = card.z
196                         else
197                                 ps[card.pile] = { total: 1, top_card: card, top_z: card.z }
198
199         for pile in piles
200                 # where should the caption be?
201                 if ps[pile.key]?
202                         unless ps[pile.key].top_card.view?
203                                 ps[pile.key].top_card.x = pile.x
204                                 ps[pile.key].top_card.y = pile.y
205                                 instantiate_card ps[pile.key].top_card
206                         caption_dest = ps[pile.key].top_card.view
207                 else
208                         caption_dest = pile.$blank
209                 if caption_dest isnt pile.caption_loc
210                         pile.$caption.detach()
211                         caption_dest.append pile.$caption
212                         pile.caption_loc = caption_dest
213
214                 # update caption to show correct number of cards in the pile
215                 card_count = 0
216                 card_count = ps[pile.key].total if ps[pile.key]?
217                 pile.$caption.children('.n_cards').html n_cards card_count
218
219
220 init = ->
221         if window.location.hash? and window.location.hash.length > 0
222                 me = window.location.hash.substr 1
223                 winloc = "#{window.location}"
224                 server_url = winloc.substr 0, winloc.length - window.location.hash.length
225         else
226                 me = 'p1'
227                 server_url = window.location
228
229         state = window.game_model.new me
230         state.on 'move', (agent, card, x, y, z, pile) ->
231                 update_pile_views()
232                 if agent is me
233                         tell_server ['move', agent, card, x, y, z, pile]
234                 else
235                         # FIXME should we use the z from the server? Should p1 use odd numbers and p2 even?
236                         bring_card_to_front state.cards[card]
237                         state.cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
238         state.on 'mark', (agent, card, state) ->
239                 @cards[card].view.toggleClass 'marked', state
240                 if agent is me
241                         tell_server ['mark', agent, card, state]
242         state.on 'flip', (agent, card, state) ->
243                 @cards[card].view.toggleClass 'flipped', state
244                 if agent is me
245                         tell_server ['flip', agent, card, state]
246         state.on 'set_cards', (agent, cards) ->
247                 initialize_cards()
248                 if agent is me
249                         tell_server ['set_cards', agent, cards]
250
251         # timeout so browser will stop showing that we're loading
252         timeout 1, poll_for_updates
253         timeout 2, ->
254                 # ask for initial state
255                 tell_server ['send_state', state.agent]
256
257 $ ->
258         $table = $ '#table'
259         table_width = $table.width()
260         table_height = $table.height()
261         card_width = $('#loading-card').outerWidth()
262         card_height = $('#loading-card').outerHeight()
263
264         init()