JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
register coffeescript compiler
[peach-cgt.git] / common.coffee
1 ###
2 Peach CGT -- Card Game Table simulator
3 Copyright (C) 2011  Jason Woofenden
4 Lincensed under AGPLv3. Source here: https://gitorious.org/peach-cgt
5 ###
6
7 # this file is used by the client and server.
8
9 # work around lack of module system in the browser:
10 if exports?
11         my_exports = exports
12 else
13         window.game_model = {}
14         my_exports = window.game_model
15
16 class GameState
17         constructor: (slug, agent) ->
18                 @slug = slug
19                 @agent = agent
20                 @hooks = {}
21                 @cards = []
22         on: (event, callback) ->
23                 unless @hooks[event]?
24                         @hooks[event] = []
25                 @hooks[event].push callback
26         trigger: (event, args...) ->
27                 return unless @hooks[event]?
28                 for callback in @hooks[event]
29                         callback.apply this, args
30         move: (agent, card, x, y, z, pile) ->
31                 # FIXME what to do on error?
32                 return unless @cards[card]?
33
34                 @cards[card].x = x
35                 @cards[card].y = y
36                 @cards[card].z = z
37                 if pile?
38                         @cards[card].pile = pile
39                 else if @cards[card].pile?
40                         delete @cards[card].pile
41
42                 @trigger 'move', agent, card, x, y, z, pile
43
44         mark: (agent, card, state) ->
45                 # FIXME what to do on error?
46                 return unless @cards[card]?
47                 @cards[card].marked = state
48                 @trigger 'mark', agent, card, state
49
50         flip: (agent, card, state) ->
51                 # FIXME what to do on error?
52                 return unless @cards[card]?
53                 @cards[card].flipped = state
54                 @trigger 'flip', agent, card, state
55
56         # FIXME implement set_pile(pile, card_order_array)
57
58         set_cards: (agent, cards) ->
59                 @cards = []
60                 for card in cards
61                         card.number = @cards.length unless card.number
62                         card.z = @cards.length unless card.z
63                         @cards.push card
64                 @trigger 'set_cards', agent, @cards
65
66         new_cards: (agent, cards) ->
67                 for card in cards
68                         card.number = @cards.length unless card.number
69                         card.z = @cards.length unless card.z
70                         @cards.push card
71                 @trigger 'new_cards', agent, cards
72
73         send_state: (agent) ->
74                 @trigger 'send_state', agent
75
76         process_messages: (messages) ->
77                 unless typeof messages is 'array' or typeof messages is 'object'
78                         # FIXME what to do on error?
79                         return typeof messages
80
81                 for message in messages
82                         unless message instanceof Array and message[0]? and message[0] in ['move', 'mark', 'flip', 'set_cards', 'send_state', 'new_cards']
83                                 if console?.log?
84                                         console.log "Got unrecognized message: #{JSON.stringify message}"
85                                 # FIXME what to do on error?
86                                 return 2
87                         method = message.shift()
88                         @[method].apply this, message
89                 return
90
91
92 my_exports.new = (slug, agent) -> new GameState slug, agent