JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
start on deck designer
[peach-cgt.git] / common.coffee
1 # this file is used by the client and server.
2
3 # work around lack of module system in the browser:
4 if exports?
5         my_exports = exports
6 else
7         window.game_model = {}
8         my_exports = window.game_model
9
10 class GameState
11         constructor: (agent) ->
12                 @agent = agent
13                 @hooks = {}
14                 @cards = []
15         on: (event, callback) ->
16                 unless @hooks[event]?
17                         @hooks[event] = []
18                 @hooks[event].push callback
19         trigger: (event, args...) ->
20                 return unless @hooks[event]?
21                 for callback in @hooks[event]
22                         callback.apply this, args
23         move: (agent, card, x, y, z, pile) ->
24                 # FIXME what to do on error?
25                 return unless @cards[card]?
26
27                 @cards[card].x = x
28                 @cards[card].y = y
29                 @cards[card].z = z
30                 if pile?
31                         @cards[card].pile = pile
32                 else if @cards[card].pile?
33                         delete @cards[card].pile
34
35                 @trigger 'move', agent, card, x, y, z, pile
36
37         mark: (agent, card, state) ->
38                 # FIXME what to do on error?
39                 return unless @cards[card]?
40                 @cards[card].marked = state
41                 @trigger 'mark', agent, card, state
42
43         flip: (agent, card, state) ->
44                 # FIXME what to do on error?
45                 return unless @cards[card]?
46                 @cards[card].flipped = state
47                 @trigger 'flip', agent, card, state
48
49         # FIXME implement set_pile(pile, card_order_array)
50
51         set_cards: (agent, cards) ->
52                 @cards = []
53                 for card in cards
54                         card.number = @cards.length unless card.number
55                         card.z = @cards.length unless card.z
56                         @cards.push card
57                 @trigger 'set_cards', agent, @cards
58
59         send_state: (agent) ->
60                 @trigger 'send_state', agent
61
62         process_messages: (messages) ->
63                 unless typeof messages is 'array' or typeof messages is 'object'
64                         # FIXME what to do on error?
65                         return typeof messages
66
67                 for message in messages
68                         unless message instanceof Array and message[0]? and message[0] in ['move', 'mark', 'flip', 'set_cards', 'send_state']
69                                 # FIXME what to do on error?
70                                 return 2
71                         method = message.shift()
72                         @[method].apply this, message
73                 return
74
75
76 my_exports.new = (agent) -> new GameState agent