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.



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

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
  @disqualified = false

  # 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

#disqualifiedObject (readonly) Also known as: disqualified?

Returns the value of attribute disqualified.



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

def disqualified
  @disqualified
end

#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



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

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

#disqualify!Object



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

def disqualify!
  @disqualified = true
end

#signal_turn(token) ⇒ Object



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

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

#to_json(for_player = nil) ⇒ Object



51
52
53
# File 'lib/gameworks/player.rb', line 51

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

#valid?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/gameworks/player.rb', line 27

def valid?
  !@invalid
end

#wait_for_turnObject



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

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