class BracketGraph::Seat

Attributes

from[R]

Source match for this seat

loser_to[RW]

Destination match of this seat for the loser participant.

payload[RW]

Seat payload. It should be used to keep track of the player and of its status in this seat

position[R]

Seat position in the graph. It acts like an Id

to[RW]

Destination match of this seat.

Public Class Methods

new(position, to: nil, round: nil) click to toggle source

Creates a new seat for the bracket graph.

@param position [Fixnum] Indicates the Seat position in the graph and acts like an Id @param to [BracketGraph::Match] The destination match. By default it's nil (and this node will act like the root node)

# 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

Public Instance Methods

as_json(options = {}) click to toggle source
# File lib/bracket_graph/seat.rb, line 44
def as_json options = {}
  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() click to toggle source

Graph depth until this level. If there is no destination it will return 0, otherwise it will return the destionation depth

# File lib/bracket_graph/seat.rb, line 33
def depth
  @depth ||= to && to.depth + 1 || 0
end
final?() click to toggle source
# File lib/bracket_graph/seat.rb, line 28
def final?
  to.nil?
end
inspect() click to toggle source
# 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() click to toggle source

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

# File lib/bracket_graph/seat.rb, line 40
def round
  @round || (to ? to.round - 1 : 0)
end
starting?() click to toggle source
# File lib/bracket_graph/seat.rb, line 24
def starting?
  from.nil? || from.empty?
end