Skip to content

Conversation

@spaghetticode
Copy link
Contributor

Hi there, I need to generate json templates for trees with more than 128 players. So I did some reverse engineering on existing jsons, and built the single_elimination_generator.rb file to build these jsons.

I have a problem: I can't really understand the logic behind the json "seats" attributes.

  • the first seat position, up to the 64.json file, is the double of the preceding seat (ie: 8 -> 24 for 16.json)
    . On the 128 files instead it seems to be the sum of the 2 preceeding seats (64 + 192 = 256). I don't know how to unify the 2 behaviors in code.
  • I can extrapolate the "seats" attribute order looking at the "seats" attribute in the "matches". I also have to reverse them in some ways, but the reversing strategy seems to be different for jsons below 32. Still, I'd like to unify the behavior.

The relevant code is the following, please excuse the bad quality but I wanted to be able to inspect the various steps in my specs:

      def populate_seats
        @matches_seats = matches.map {|arr| arr.map {|a| a[:seats]}}
        @ordered_seats = contenders < 32 ? @matches_seats.reverse.map {|a| a.reverse} : @matches_seats.reverse
        @flat_seats    = @ordered_seats.flatten
        @first_seat    = contenders > 64 ? @flat_seats.first + @flat_seats.second : @flat_seats.first*2
        @all_seats     = @flat_seats.unshift @first_seat
        @object_seats  = @all_seats.map! {|n| {:position => n} }
        @seats         = @object_seats
      end

The code is in lib/bracket_tree/templates/single_elimination_generator.rb and the specs are in specs/single_elimination_json_generator_spec.rb

Once the issues are solved maybe you could consider merging this code, so that anyone can generate jsons with arbitrary numbers of players. Of course if you need some clarification please contact me.

Regards

Andrea

@cadwallion
Copy link
Contributor

Thanks for the contribution! I will take a look at this today, but to clarify a few things:

  1. The concept of 'seats' is to isolate the tree structure from the progression structure. The 'seats' are the two nodes playing (first node in the seats array is top seed, second node is bottom seed.
  2. The calculation of the positions and seats uses the binary tree insertion method. So, given a standard binary tree insertion/traversal algorithm, inserting 64 nodes would get you the tree structure for a 64-player single elimination tree. To generate the progression structure of a single elimination bracket, take the parent's value and you have the winner_to value, and loser_to goes to nil.

For generation, you can utilize the BracketTree::Bracket::Base to eliminate some of the complex processing. Here's something I whipped together that is missing the winner_to calculation.

bracket = BracketTree::Bracket::SingleElimination.new
players = 64

players.times do |player|
  bracket.add player, {}
end

bracket.depth[:total].times do |round|
  bracket.round(round).all.each_slice(2) do |pair|
    seats = [pair[0].position, pair[1].position]
    parent = nil # We should really save a reference to the parent in the child
    match = Match.new({ seats: seats, winner_to: parent, loser_to: nil})
    bracket.matches << match
  end
end

# take the round one positions and set them all as seeding. NOTE: this is not a
# typical seeding structure, but an example of how it can be done.
bracket.seed_order = bracket.round(1).all.map(&:position)

bracket.to_h

I'm wondering if we need to change BracketTree::Bracket::Base#add to add the parent position to the BracketTree::Node object, as the winner would be taking pair[0].parent.position

@spaghetticode
Copy link
Contributor Author

After finding an issue in the 128.json template (fix already merged) I replaced the line

        @first_seat    = contenders > 64 ? @flat_seats.first + @flat_seats.second : @flat_seats.first*2

with

        @first_seat    =  @flat_seats.first*2

Now I need to write the double elimination generator, but don't know the logic to generate the loosers bracket seat positions starting from the winner seats positions

Base automatically changed from master to main March 11, 2021 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants