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.



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

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

  @id = SecureRandom.uuid.to_s.split('-').first
  @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.



9
10
11
# File 'lib/gameworks/player.rb', line 9

def disqualified
  @disqualified
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#scoreObject

Returns the value of attribute score.



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

def score
  @score
end

Instance Method Details

#as_json(for_player = nil) ⇒ Object



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

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

#disqualify!Object



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

def disqualify!
  @disqualified = true
end

#signal_turn(token) ⇒ Object



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

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

#to_json(for_player = nil) ⇒ Object



55
56
57
# File 'lib/gameworks/player.rb', line 55

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

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  !@invalid
end

#wait_for_turnObject



44
45
46
# File 'lib/gameworks/player.rb', line 44

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