JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
snap to grid so edges line up
[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
9 show_message = (txt) -> txt
10         # FIXME implement chat box or something
11
12 # timeout function with args in convenient order
13 timeout = (ms, func) -> setTimeout func, ms
14
15 unless Array::shuffle?
16         Array::shuffle = ->
17                 return if @length is 0
18                 top = @length
19
20                 while --top
21                         current = Math.floor(Math.random() * (top + 1))
22                         tmp = @[current]
23                         @[current] = @[top]
24                         @[top] = tmp
25                 return
26
27 new_button = (text) -> $ $ "<div class=\"button\">#{text}</div>"
28
29 # transform coordinates from client-side coords to server-side coords (or back)
30 # this makes it so p2 view everything upside down (mirrored), but still sends coords rightside up
31 transform_x = (x) -> x
32 transform_y = (y) ->
33         return y unless state.agent is 'p2'
34         return table_height - card_height - y
35
36 next_card_z = -> return top_card_z += 1
37
38 bring_card_to_front = (card) ->
39         card.view.css "z-index": next_card_z()
40
41 instantiate_card = (card) ->
42         text = card.text
43         if card.owner is state.agent
44                 card_class = 'my_card'
45         else
46                 card_class = 'your_card'
47
48         view = $ $ "<div class=\"card #{card_class}\" style=\"left: #{transform_x(card.x)}px; top: #{transform_y(card.y)}px; z-index: #{next_card_z()}\"><span class=\"cardtext\">#{text}</span></div>"
49         button_box = $ $ '<div/>'
50         flip_button = new_button "flip over"
51         mark_button = new_button "mark"
52         flip_button.bind 'click', ->
53                 state.flip state.agent, card.number, ! view.hasClass 'flipped'
54         mark_button.bind 'click', ->
55                 state.mark state.agent, card.number, ! view.hasClass 'marked'
56         button_box.append flip_button
57         button_box.append mark_button
58         view.append button_box
59         if card.marked
60                 view.addClass 'marked'
61         if card.flipped
62                 view.addClass 'flipped'
63         $table.append view
64         view.draggable containment: '#table', grid: [20, 20]
65         view.bind 'dragstart', (event, ui) ->
66                 view.css 'z-index': card.z = next_card_z()
67         view.bind 'dragstop', (event, ui) ->
68                 p = view.position()
69                 state.move state.agent, card.number, transform_x(p.left), transform_y(p.top)
70         card.view = view
71
72 error_lag = 3
73
74 outgoing_messages = []
75 # message should be [agent, method, args...]
76 # don't forget the agent (state.agent)
77 tell_server = (message) ->
78         outgoing_messages.push message
79         send_updates()
80
81 send_updates = ->
82         return if outgoing_messages.length is 0
83
84         messages = outgoing_messages
85         outgoing_messages = []
86
87         $.ajax "#{server_url}/set", {
88                 cache: false
89                 data: {
90                         agent: state.agent
91                         game: 'test' # FIXME, and it the /get call too
92                         messages: JSON.stringify(messages)
93                 }
94                 type: 'POST'
95                 dataType: 'json'
96                 error: (xhr, status, error) ->
97                         show_message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
98                         for message in messages
99                                 outgoing_messages.unshift message
100                         timeout error_lag * 1000, send_updates
101                         error_lag *= 2
102                 success: (data, status, xhr) ->
103                         show_message "update sent"
104                         error_lag = 3
105         }
106
107 error_lag = 3
108 poll_for_updates = ->
109         $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
110                 cache: false
111                 type: 'GET'
112                 dataType: 'json'
113                 error: (xhr, status, error) ->
114                         message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
115                         timeout error_lag * 1000, poll_for_updates
116                         error_lag *= 2
117                 success: (data, status, xhr) ->
118                         state.process_messages data
119                         timeout 100, poll_for_updates
120                         error_lag = 3
121         }
122
123 init = ->
124         if window.location.hash? and window.location.hash.length > 0
125                 me = window.location.hash.substr 1
126                 winloc = "#{window.location}"
127                 server_url = winloc.substr 0, winloc.length - window.location.hash.length
128         else
129                 me = 'p1'
130                 server_url = window.location
131
132         state = window.game_model.new me
133         state.on 'move', (agent, card, x, y) ->
134                 # FIXME add/handle pile argument
135                 if agent is me
136                         tell_server ['move', agent, card, x, y]
137                 else
138                         bring_card_to_front state.cards[card]
139                         state.cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
140         state.on 'mark', (agent, card, state) ->
141                 @cards[card].view.toggleClass 'marked', state
142                 if agent is me
143                         tell_server ['mark', agent, card, state]
144         state.on 'flip', (agent, card, state) ->
145                 @cards[card].view.toggleClass 'flipped', state
146                 if agent is me
147                         tell_server ['flip', agent, card, state]
148         state.on 'set_cards', (agent, cards) ->
149                 # FIXME add agent arg and tell server if it's not us
150                 $('.card').remove()
151                 for card in cards
152                         instantiate_card card
153                 if agent is me
154                         tell_server ['set_cards', agent, cards]
155
156         # timeout so browser will stop showing that we're loading
157         timeout 1, poll_for_updates
158         timeout 2, ->
159                 # ask for initial state
160                 tell_server ['send_state', state.agent]
161
162 $ ->
163         $table = $ '#table'
164         table_height = $table.height()
165         card_height = $('#loading-card').outerHeight()
166
167         init()