JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
basic card designer, new_cards api
[peach-cgt.git] / client.coffee
index 967917d..0c54a71 100644 (file)
@@ -189,9 +189,15 @@ initialize_cards = () ->
        $('.card').remove()
        top_card_z = 0
        # instantiate cards in play
+       hide_deck_designer = false
        for card in state.cards
+               if card.owner is state.agent
+                       hide_deck_designer = true
                delete card.view
 
+       if hide_deck_designer
+               $('#deck_designer').remove()
+
        unless piles?
                piles = [ # global
                        {key: 'p2_draw', x: 140, y: 20, name: "Draw Pile"}
@@ -249,6 +255,77 @@ update_pile_views = ->
                card_count = ps[pile.key].total if ps[pile.key]?
                pile.$caption.children('.n_cards').html n_cards card_count
 
+possible_cards = {}
+
+valumenous = (val) -> return true unless val is '' or val is ' '
+
+init_possible_cards = ->
+       for card in window.cs_cards
+               text = "#{card.cardname} (#{card.faction})"
+               if valumenous card.attack or valumenous card.defense
+                       text += "  #{card.attack}/#{card.defense}"
+               text += "<br>#{card.type}"
+               if valumenous card.subtype
+                       text += " &bull; #{card.subtype}"
+               text += "<br>cost: #{card.cost} thresh: #{card.threshold}<br>"
+               text += card.rules
+
+               summary = text.replace(/<br>/g, "\n")
+
+               possible_cards[card.id] = {id: card.id, text: text, summary: summary}
+
+
+init_card_designer = ->
+       show_message 'init_card_designer'
+       cards_in_deck = {}
+       container = $ '#deck_designer'
+       init_possible_cards()
+       ul = $ $ '<ul/>'
+       for key, card of possible_cards
+               view = $ $ "<li>#{card.summary}</li>"
+               view.data 'id', card.id
+               view.bind 'click', ->
+                       $el = $ this
+                       id = $el.data 'id'
+                       if cards_in_deck[id]?
+                               delete cards_in_deck[id]
+                               value = false
+                       else
+                               value = true
+                               cards_in_deck[id] = true
+                       $el.toggleClass 'in_deck', value
+               ul.append view
+
+       container.append ul
+
+       submit = $ $ "<div style=\"border: 1px solid black\">Done</div>"
+       submit.bind 'click', ->
+               $('#deck_designer').remove()
+               show_message cards_in_deck
+               cards = []
+               for key, value of cards_in_deck
+                       card = {
+                               text: possible_cards[key].text
+                               owner: state.agent
+                               pile: "#{state.agent}_draw"
+                               x: 0
+                               y: 0
+                               z: next_card_z()
+                               flipped: true
+                       }
+                       cards.push card
+                       cards.push card
+                       cards.push card
+                       cards.push card
+
+               cards.shuffle()
+               show_message cards
+
+               # let server assign card numbers
+               tell_server ['new_cards', state.agent, cards]
+
+
+       container.append submit
 
 init = ->
        if window.location.hash? and window.location.hash.length > 0
@@ -285,11 +362,14 @@ init = ->
                if agent is me
                        tell_server ['flip', agent, card, state]
        state.on 'set_cards', (agent, cards) ->
-               initialize_cards()
                if agent is me
                        tell_server ['set_cards', agent, cards]
+               initialize_cards()
+       state.on 'new_cards', (agent, cards) ->
+               initialize_cards()
 
        # timeout so browser will stop showing that we're loading
+       timeout 1, init_card_designer
        timeout 1, poll_for_updates
        timeout 2, ->
                # ask for initial state
@@ -299,7 +379,7 @@ $ ->
        $table = $ '#table'
        table_width = $table.width()
        table_height = $table.height()
-       card_width = $('#loading-card').outerWidth()
-       card_height = $('#loading-card').outerHeight()
+       card_width = $('#loading_card').outerWidth()
+       card_height = $('#loading_card').outerHeight()
 
        init()