1 # this file is used by the client and server.
3 # work around lack of module system in the browser:
8 my_exports = window.game_model
11 constructor: (agent) ->
16 on: (event, callback) ->
19 @hooks[event].push callback
20 trigger: (event, args...) ->
21 return unless @hooks[event]?
22 for callback in @hooks[event]
23 callback.apply this, args
24 move: (agent, card, x, y) -> # FIXME add pile argument
25 # FIXME what to do on error?
26 return unless @cards[card]? #?.pile?
27 #cur_pile = cards[card].pile
28 #if pile isnt cur_pile
33 @trigger 'move', agent, card, x, y # FIXME add pile argument
35 mark: (agent, card, state) ->
36 # FIXME what to do on error?
37 return unless @cards[card]?
38 @cards[card].marked = state
39 @trigger 'mark', agent, card, state
41 flip: (agent, card, state) ->
42 # FIXME what to do on error?
43 return unless @cards[card]?
44 @cards[card].flipped = state
45 @trigger 'flip', agent, card, state
47 # FIXME implement set_pile(pile, card_order_array)
49 set_cards: (agent, cards) ->
53 card.number = @cards.length
56 unless @piles[card.pile]?
57 @piles[card.pile] = []
58 @piles[card.pile].push card
59 @trigger 'set_cards', agent, @cards
61 send_state: (agent) ->
62 @trigger 'send_state', agent
64 process_messages: (messages) ->
65 unless typeof messages is 'array' or typeof messages is 'object'
66 # FIXME what to do on error?
67 return typeof messages
69 for message in messages
70 unless message instanceof Array and message[0]? and message[0] in ['move', 'mark', 'flip', 'set_cards', 'send_state']
71 # FIXME what to do on error?
73 method = message.shift()
74 @[method].apply this, message
78 my_exports.new = (agent) -> new GameState agent