JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
colored card areas (deck, hand, etc)
[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 new_button = (text) -> $ $ "<div class=\"button\">#{text}</div>"
28
29 add_card = (text, x, y) ->
30         card = $ $ "<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                 card.toggleClass 'flipped'
36                 # FIXME tell server
37         mark_button.bind 'click', ->
38                 card.toggleClass 'marked'
39                 # FIXME tell server
40         button_box.append flip_button
41         button_box.append mark_button
42         card.append button_box
43         $table.append card
44         card.draggable stack: '.card'
45         card.bind 'dragstop', (event, ui) ->
46                 p = card.position()
47                 #card.children().html("(#{p.left}, #{p.top})")
48
49 init = ->
50         if state.auto_shuffle
51                 state.my_cards.shuffle()
52                 state.your_cards.shuffle() # FIXME have the server or other player do this
53                 state.auto_shuffle = false
54         left = 15
55         top = 450
56         for card in state.my_cards
57                 add_card state.card_types[card].text, left, top
58                 left += 120
59         left = 15
60         top = 250
61         for card in state.your_cards
62                 add_card state.card_types[card].text, left, top
63                 left += 120
64
65 $ ->
66         $table = $ '#table'
67         init()
68