Class: BracketGraph::Seat
- Inherits:
-
Object
- Object
- BracketGraph::Seat
- Defined in:
- lib/bracket_graph/seat.rb
Instance Attribute Summary collapse
-
#from ⇒ Object
readonly
Source match for this seat.
-
#loser_to ⇒ Object
Destination match of this seat for the loser participant.
-
#payload ⇒ Object
Seat payload.
-
#position ⇒ Object
readonly
Seat position in the graph.
-
#to ⇒ Object
Destination match of this seat.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
-
#depth ⇒ Object
Graph depth until this level.
- #final? ⇒ Boolean
-
#initialize(position, to: nil, round: nil) ⇒ Seat
constructor
Creates a new seat for the bracket graph.
- #inspect ⇒ Object
-
#round ⇒ Object
Round is the opposite of depth.
- #starting? ⇒ Boolean
Constructor Details
#initialize(position, to: nil, round: nil) ⇒ Seat
Creates a new seat for the bracket graph.
18 19 20 21 22 |
# File 'lib/bracket_graph/seat.rb', line 18 def initialize position, to: nil, round: nil round ||= to.round - 1 if to @position, @to, @round = position, to, round @from = [] end |
Instance Attribute Details
#from ⇒ Object (readonly)
Source match for this seat
4 5 6 |
# File 'lib/bracket_graph/seat.rb', line 4 def from @from end |
#loser_to ⇒ Object
Destination match of this seat for the loser participant.
12 13 14 |
# File 'lib/bracket_graph/seat.rb', line 12 def loser_to @loser_to end |
#payload ⇒ Object
Seat payload. It should be used to keep track of the player and of its status in this seat
8 9 10 |
# File 'lib/bracket_graph/seat.rb', line 8 def payload @payload end |
#position ⇒ Object (readonly)
Seat position in the graph. It acts like an Id
6 7 8 |
# File 'lib/bracket_graph/seat.rb', line 6 def position @position end |
#to ⇒ Object
Destination match of this seat.
10 11 12 |
# File 'lib/bracket_graph/seat.rb', line 10 def to @to end |
Instance Method Details
#as_json(options = {}) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/bracket_graph/seat.rb', line 44 def as_json = {} data = { position: position, round: round } data.update payload: payload if payload data.update loser_to: loser_to.position if loser_to from && data.update(from: from.map(&:as_json)) || data end |
#depth ⇒ Object
Graph depth until this level. If there is no destination it will return 0, otherwise it will return the destionation depth
33 34 35 |
# File 'lib/bracket_graph/seat.rb', line 33 def depth @depth ||= to && to.depth + 1 || 0 end |
#final? ⇒ Boolean
28 29 30 |
# File 'lib/bracket_graph/seat.rb', line 28 def final? to.nil? end |
#inspect ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/bracket_graph/seat.rb', line 51 def inspect """#<BracketGraph::Seat:#{position} @from=#{from.map(&:position).inspect} @round=#{round} @to=#{(to && to.position || nil).inspect} @loser_to=#{(loser_to && loser_to.position || nil).inspect} @payload=#{payload.inspect}>""" end |
#round ⇒ Object
Round is the opposite of depth. While depth is 0 in the root node and Math.log2(size) at the lower level round is 0 at the lower level and Math.log2(size) in the root node While depth is memoized, round is calculated each time. If the seat has a source, it’s the source round + 1, otherwise it’s 0
40 41 42 |
# File 'lib/bracket_graph/seat.rb', line 40 def round @round || (to ? to.round - 1 : 0) end |
#starting? ⇒ Boolean
24 25 26 |
# File 'lib/bracket_graph/seat.rb', line 24 def starting? from.nil? || from.empty? end |