JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
dragged cards always brought to front
[peach-cgt.git] / client.coffee
1 $table = null
2
3 state = {
4         card_types: [
5                 {text: "Rusty Camel"}
6                 {text: "Angry Ocelot"}
7                 {text: "Unruly Parsnip"}
8         ],
9         # values are indexes into card_types array
10         my_cards: [0, 0, 0, 1, 1, 2],
11         your_cards: [0, 1, 1, 2, 2, 2],
12         auto_shuffle: true
13 }
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 add_card = (text, x, y) ->
28         card = $ $ "<div class=\"card\" style=\"left: #{x}px; top: #{y}px\">#{text}<br><span>&nbsp;</span></div>"
29         $table.append card
30         card.draggable stack: '.card'
31         card.bind 'dragstop', (event, ui) ->
32                 p = card.position()
33                 card.children().html("(#{p.left}, #{p.top})")
34                 # FIXME tell server
35
36 init = ->
37         if state.auto_shuffle
38                 state.my_cards.shuffle()
39                 state.your_cards.shuffle() # FIXME have the server or other player do this
40                 state.auto_shuffle = false
41         left = 15
42         top = 450
43         for card in state.my_cards
44                 add_card state.card_types[card].text, left, top
45                 left += 120
46         left = 15
47         top = 250
48         for card in state.your_cards
49                 add_card state.card_types[card].text, left, top
50                 left += 120
51
52 $ ->
53         $table = $ '#table'
54         init()
55