Class: BracketGraph::Seat

Inherits:
Object
  • Object
show all
Defined in:
lib/bracket_graph/seat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(position, to: nil, round: nil) ⇒ Seat

Creates a new seat for the bracket graph.

Parameters:

  • position (Fixnum)

    Indicates the Seat position in the graph and acts like an Id

  • to (BracketGraph::Match) (defaults to: nil)

    The destination match. By default it’s nil (and this node will act like the root node)



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

#fromObject (readonly)

Source match for this seat



4
5
6
# File 'lib/bracket_graph/seat.rb', line 4

def from
  @from
end

#loser_toObject

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

#payloadObject

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

#positionObject (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

#toObject

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 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

#depthObject

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

Returns:

  • (Boolean)


28
29
30
# File 'lib/bracket_graph/seat.rb', line 28

def final?
  to.nil?
end

#inspectObject



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

#roundObject

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

Returns:

  • (Boolean)


24
25
26
# File 'lib/bracket_graph/seat.rb', line 24

def starting?
  from.nil? || from.empty?
end