8 # FIXME implement chat box or something
10 # timeout function with args in convenient order
11 timeout = (ms, func) -> setTimeout func, ms
13 unless Array::shuffle?
15 return if @length is 0
19 current = Math.floor(Math.random() * (top + 1))
25 new_button = (text) -> $ $ "<div class=\"button\">#{text}</div>"
27 # transform coordinates from client-side coords to server-side coords (or back)
28 # this makes it so p2 view everything upside down (mirrored), but still sends coords rightside up
29 transform_coords = (coords) ->
30 ret = {left: coords.left, top: coords.top}
31 if state.agent is 'p2'
32 ret.top = table_height - ret.top
35 instantiate_card = (card) ->
37 view_coords = transform_coords {left: card.x, top: card.y}
38 view = $ $ "<div class=\"card\" style=\"left: #{view_coords.left}px; top: #{view_coords.top}px\"><span class=\"cardtext\">#{text}</span></div>"
39 button_box = $ $ '<div/>'
40 flip_button = new_button "flip over"
41 mark_button = new_button "mark"
42 flip_button.bind 'click', ->
43 state.flip state.agent, card.number, ! view.hasClass 'flipped'
44 mark_button.bind 'click', ->
45 state.mark state.agent, card.number, ! view.hasClass 'marked'
46 button_box.append flip_button
47 button_box.append mark_button
48 view.append button_box
50 view.addClass 'marked'
52 view.addClass 'flipped'
54 view.draggable stack: '.card'
55 view.bind 'dragstop', (event, ui) ->
56 p = transform_coords view.position()
57 state.move state.agent, card.number, p.left, p.top
62 outgoing_messages = []
63 # message should be [agent, method, args...]
64 # don't forget the agent (state.agent)
65 tell_server = (message) ->
66 outgoing_messages.push message
70 return if outgoing_messages.length is 0
72 messages = outgoing_messages
73 outgoing_messages = []
75 $.ajax "#{server_url}/set", {
79 game: 'test' # FIXME, and it the /get call too
80 messages: JSON.stringify(messages)
84 error: (xhr, status, error) ->
85 message "Network error while sending, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
86 for message in messages
87 outgoing_messages.unshift message
88 timeout error_lag * 1000, send_updates
90 success: (data, status, xhr) ->
97 $.ajax "#{server_url}/get?agent=#{state.agent}&game=test", {
101 error: (xhr, status, error) ->
102 message "Network error, you might want to refresh. Trying again in #{error_lag} seconds. (Status: #{status}, Error: #{error})"
103 timeout error_lag * 1000, poll_for_updates
105 success: (data, status, xhr) ->
106 state.process_messages data
107 timeout 100, poll_for_updates
112 if window.location.hash? and window.location.hash.length > 0
113 me = window.location.hash.substr 1
114 winloc = "#{window.location}"
115 server_url = winloc.substr 0, winloc.length - window.location.hash.length
118 server_url = window.location
120 state = window.game_model.new me
121 state.on 'move', (agent, card, x, y) ->
122 # FIXME add/handle pile argument
124 tell_server ['move', agent, card, x, y]
126 coords = transform_coords {left: x, top: y}
127 state.cards[card].view.animate { left: "#{coords.left}px", top: "#{coords.top}px"}, 800
128 state.on 'mark', (agent, card, state) ->
129 @cards[card].view.toggleClass 'marked', state
131 tell_server ['mark', agent, card, state]
132 state.on 'flip', (agent, card, state) ->
133 @cards[card].view.toggleClass 'flipped', state
135 tell_server ['flip', agent, card, state]
136 state.on 'set_cards', (agent, cards) ->
137 # FIXME add agent arg and tell server if it's not us
140 instantiate_card card
142 tell_server ['set_cards', agent, cards]
144 # timeout so browser will stop showing that we're loading
145 timeout 1, poll_for_updates
147 # ask for initial state
148 tell_server ['send_state', state.agent]
152 table_height = $table.height() - 148 # FIXME auto-detect card height