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