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