Class: Gameworks::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/gameworks/player.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload = {}) ⇒ Player

Returns a new instance of Player.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gameworks/player.rb', line 8

def initialize(payload={})
  unless payload.is_a?(Hash) &&
         payload.has_key?('name') &&
         payload['name'].is_a?(String)
    @invalid = true
    return
  end

  @name = payload['name']
  @score = 0

  # used for evented "block until it's my turn". the game will push a token
  # on this queue when it's the player's turn, or will push a nil when the
  # game is completed
  @em_queue = EventMachine::Queue.new
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/gameworks/player.rb', line 5

def name
  @name
end

#scoreObject

Returns the value of attribute score.



6
7
8
# File 'lib/gameworks/player.rb', line 6

def score
  @score
end

Instance Method Details

#as_json(for_player = nil) ⇒ Object



41
42
43
44
# File 'lib/gameworks/player.rb', line 41

def as_json(for_player=nil)
  { :name  => @name,
    :score => @score }
end

#disqualified?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/gameworks/player.rb', line 29

def disqualified?
  @score == 'disqualified'
end

#signal_turn(token) ⇒ Object



33
34
35
# File 'lib/gameworks/player.rb', line 33

def signal_turn(token)
  @em_queue.push(token)
end

#to_json(for_player = nil) ⇒ Object



46
47
48
# File 'lib/gameworks/player.rb', line 46

def to_json(for_player=nil)
  as_json(for_player).to_json
end

#valid?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/gameworks/player.rb', line 25

def valid?
  !@invalid
end

#wait_for_turnObject



37
38
39
# File 'lib/gameworks/player.rb', line 37

def wait_for_turn
  @em_queue.pop{ |token| yield token }
end