class BracketGraph::DoubleEliminationGraph

Attributes

loser_graph[R]
root[R]
winner_graph[R]

Public Class Methods

new(root_or_size) click to toggle source
# File lib/bracket_graph/double_elimination_graph.rb, line 6
def initialize root_or_size
  if root_or_size.is_a? Seat
    @root = root_or_size
    @winner_graph = Graph.new @root.from[0]
    @loser_graph = LoserGraph.new @root.from[1]
  else
    @winner_graph = Graph.new root_or_size
    @loser_graph = LoserGraph.new root_or_size
    sync_winner_rounds
    sync_loser_rounds
    build_final_seat
    assign_loser_links
  end
end

Public Instance Methods

[](position) click to toggle source
# File lib/bracket_graph/double_elimination_graph.rb, line 21
def [](position)
  return root if position == root.position
  if position < root.position
    winner_graph[position]
  else
    loser_graph[position]
  end
end
as_json(options={}) click to toggle source
# File lib/bracket_graph/double_elimination_graph.rb, line 60
def as_json options={}
  @root.as_json options
end
seats() click to toggle source
# File lib/bracket_graph/double_elimination_graph.rb, line 48
def seats
  [root] + winner_seats + loser_seats
end
seed(*args) click to toggle source
# File lib/bracket_graph/double_elimination_graph.rb, line 56
def seed *args
  winner_graph.seed *args
end
size() click to toggle source
# File lib/bracket_graph/double_elimination_graph.rb, line 30
def size
  winner_graph.size
end
starting_seats() click to toggle source
# File lib/bracket_graph/double_elimination_graph.rb, line 52
def starting_seats
  winner_starting_seats + loser_starting_seats
end

Private Instance Methods

build_final_seat() click to toggle source
# File lib/bracket_graph/double_elimination_graph.rb, line 66
def build_final_seat
  @root = Seat.new size * 2, round: loser_graph.root.round + 1
  @root.from.concat [winner_graph.root, loser_graph.root]
  @root.from.each { |s| s.to = @root }
end
loser_starting_seats_by_round() click to toggle source
# File lib/bracket_graph/double_elimination_graph.rb, line 102
def loser_starting_seats_by_round
  loser_graph.starting_seats.sort_by(&:position).inject({}) do |memo, seat|
    memo[seat.round] ||= []
    memo[seat.round] << seat
    memo
  end
end
sync_loser_rounds() click to toggle source
# File lib/bracket_graph/double_elimination_graph.rb, line 85
def sync_loser_rounds
  loser_graph.seats.each do |s|
    s.instance_variable_set '@round', s.round + 1
  end
end
sync_winner_rounds() click to toggle source
# File lib/bracket_graph/double_elimination_graph.rb, line 72
def sync_winner_rounds
  seats_by_round = winner_seats.inject({}) do |memo, seat|
    memo[seat.round] ||= []
    memo[seat.round] << seat
    memo
  end
  3.upto(winner_root.round) do |round|
    seats_by_round[round].each do |r|
      r.instance_variable_set '@round', 2 + (round - 2) * 2
    end
  end
end
winner_matches_by_round() click to toggle source
# File lib/bracket_graph/double_elimination_graph.rb, line 91
def winner_matches_by_round
  winner_graph.seats.
    reject(&:starting?).
    sort_by(&:position).
    inject({}) do |memo, seat|
      memo[seat.round] ||= []
      memo[seat.round] << seat
      memo
    end
end