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