JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
remove obsolete comment
[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         # initial card state from server has z so that stacks come out with the right layers
49         if card.z?
50                 if card.z > top_card_z
51                         top_card_z = card.z
52         else
53                 unless card.pile
54                         card.z = next_card_z()
55
56         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>"
57         button_box = $ $ '<div/>'
58         flip_button = new_button "flip over"
59         mark_button = new_button "mark"
60         flip_button.bind 'click', ->
61                 state.flip state.agent, card.number, ! view.hasClass 'flipped'
62         mark_button.bind 'click', ->
63                 state.mark state.agent, card.number, ! view.hasClass 'marked'
64         button_box.append flip_button
65         button_box.append mark_button
66         view.append button_box
67         if card.marked
68                 view.addClass 'marked'
69         if card.flipped
70                 view.addClass 'flipped'
71         $table.append view
72         view.draggable containment: '#table', grid: [20, 20]
73         view.bind 'dragstart', (event, ui) ->
74                 view.css 'z-index': card.z = next_card_z()
75         view.bind 'dragstop', (event, ui) ->
76                 p = view.position()
77                 state.move state.agent, card.number, transform_x(p.left), transform_y(p.top), card.z
78         card.view = view
79
80 error_lag = 3
81
82 outgoing_messages = []
83 # message should be [agent, method, args...]
84 # don't forget the agent (state.agent)
85 tell_server = (message) ->
86         outgoing_messages.push message
87         send_updates()
88
89 send_updates = ->
90         return if outgoing_messages.length is 0
91
92         messages = outgoing_messages
93         outgoing_messages = []
94
95         $.ajax "#{server_url}/set", {
96                 cache: false
97                 data: {
98                         agent: state.agent
99                         game: 'test' # FIXME, and it the /get call too
100                         messages: JSON.stringify(messages)
101                 }
102                 type: 'POST'
103                 dataType: 'json'
104                 error: (xhr, status, error) ->
105                         show_message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
106                         for message in messages
107                                 outgoing_messages.unshift message
108                         timeout error_lag * 1000, send_updates
109                         error_lag *= 2
110                 success: (data, status, xhr) ->
111                         show_message "update sent"
112                         error_lag = 3
113         }
114
115 error_lag = 3
116 poll_for_updates = ->
117         $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
118                 cache: false
119                 type: 'GET'
120                 dataType: 'json'
121                 error: (xhr, status, error) ->
122                         message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
123                         timeout error_lag * 1000, poll_for_updates
124                         error_lag *= 2
125                 success: (data, status, xhr) ->
126                         state.process_messages data
127                         timeout 100, poll_for_updates
128                         error_lag = 3
129         }
130
131 init = ->
132         if window.location.hash? and window.location.hash.length > 0
133                 me = window.location.hash.substr 1
134                 winloc = "#{window.location}"
135                 server_url = winloc.substr 0, winloc.length - window.location.hash.length
136         else
137                 me = 'p1'
138                 server_url = window.location
139
140         state = window.game_model.new me
141         state.on 'move', (agent, card, x, y, z) ->
142                 # FIXME add/handle pile argument
143                 if agent is me
144                         tell_server ['move', agent, card, x, y, z]
145                 else
146                         bring_card_to_front state.cards[card]
147                         state.cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
148         state.on 'mark', (agent, card, state) ->
149                 @cards[card].view.toggleClass 'marked', state
150                 if agent is me
151                         tell_server ['mark', agent, card, state]
152         state.on 'flip', (agent, card, state) ->
153                 @cards[card].view.toggleClass 'flipped', state
154                 if agent is me
155                         tell_server ['flip', agent, card, state]
156         state.on 'set_cards', (agent, cards) ->
157                 $('.card').remove()
158                 for card in cards
159                         instantiate_card card
160                 if agent is me
161                         tell_server ['set_cards', agent, cards]
162
163         # timeout so browser will stop showing that we're loading
164         timeout 1, poll_for_updates
165         timeout 2, ->
166                 # ask for initial state
167                 tell_server ['send_state', state.agent]
168
169 $ ->
170         $table = $ '#table'
171         table_height = $table.height()
172         card_height = $('#loading-card').outerHeight()
173
174         init()