Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Tweaks, a convenience method, and speculative refactor. #1

Merged
merged 5 commits into from

2 participants

@hypomodern
Owner

Just taking a look at what is going on here.

I have questions. It seems like dick-all is done with the nodes specified in the template. They look like they set up a state machine governing how winners move around but this data this thrown away by the Template and Node doesn't implement any state logic. Is this to-do, or does starleagues have it and you haven't been able to extract it yet?

Do you want or need help with implementing that, is my real question, I guess.

lib/bracket_tree/bracket.rb
@@ -77,10 +77,14 @@ def seed players
end
# This needs refactoring. Inconsistent interface
+ # Not sure if this is what you want here or not, but this now accepts hashlike input
+ # I'm also unsure about a good default position. I assume 1? - MHW
@cadwallion Collaborator

1 will never be the correct default position because the winner is the root node, which must be n+1 where n is the highest position value on the left 'half' of the tree.

@root.payload = winner is probably sufficient for now, because I still have some math to do before I can implement automatic recalculation of node positions to map to a given binary tree representation, and I think that's a bit overkill for initial version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@cadwallion
Collaborator

I would rather not assume a Hash as the node positions. I'm putting in empty hashes for the placeholders mainly because it's 1) easily serializable 2) less intense than some arbitrary object. However, I don't want to pigeon-hole the content into just a hash. SC2Battles, for example, uses a Seat object in the positions to carry extra info and to add AM::Serializer support.

Everything else looks good.

@hypomodern
Owner

There ya go.

@cadwallion
Collaborator

:thumbsup: :tada:

@cadwallion cadwallion merged commit 68be8fc into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
12 lib/bracket_tree/bracket.rb
@@ -7,7 +7,7 @@ class SeedLimitExceededError < Exception ; end
include Enumerable
attr_accessor :root, :seed_order, :insertion_order
-
+
def initialize
@insertion_order = []
end
@@ -49,7 +49,7 @@ def add position, data
# @param [Fixnum] position - the node position to replace
# @param payload - the new payload object to replace
def replace position, payload
- node = find { |n| n.position == position }
+ node = at position
if node
node.payload = payload
true
@@ -99,14 +99,16 @@ def nodes
to_a.sort_by { |node| @insertion_order.index(node.position) }
end
- def size
- @insertion_order.size
+ def at position
+ find { |n| n.position == position }
end
+ alias_method :size, :count
+
def in_order(node, block)
if node
unless node.left.nil?
- in_order(node.left, block)
+ in_order(node.left, block)
end
block.call(node)
View
8 lib/bracket_tree/template.rb
@@ -18,13 +18,13 @@ def by_size size
end
# Generates Template from JSON
- #
+ #
# @param [String] json - the bracket template in its standard data specification
# @return [BracketTree::Template]
def from_json json
template = new
if json['seats']
- template.seats = json['seats'].map { |s| s['position'] }
+ template.seats = json['seats'].map { |s| s['position'] }
end
if json['startingSeats']
@@ -68,12 +68,12 @@ def generate_blank_bracket
bracket
end
-
+
# Returns hash representation of the Template
#
# @return [Hash] template
def to_h
- hash = {
+ {
'seats' => @seats.map { |s| { 'position' => s } },
'startingSeats' => @starting_seats,
'nodes' => @nodes
View
22 spec/unit/bracket_tree/bracket_spec.rb
@@ -105,6 +105,28 @@
end
end
+ describe "#size" do
+ it "should return the number of nodes in the bracket" do
+ bracket.add 3, { foo: 'foo' }
+ bracket.add 2, { bar: 'bar' }
+ bracket.add 4, { baz: 'baz' }
+ bracket.size.should == 3
+ end
+ end
+
+ describe "#at" do
+ before do
+ bracket.add 3, { foo: 'foo' }
+ bracket.add 2, { bar: 'bar' }
+ bracket.add 1, { baz: 'baz' }
+ end
+ it "should return the node at the given position in the bracket" do
+ bracket.at(1).payload.should == { baz: 'baz'}
+ bracket.at(2).payload.should == { bar: 'bar'}
+ bracket.at(3).payload.should == { foo: 'foo'}
+ end
+ end
+
describe '#seed' do
let(:bracket) { BracketTree::Template::SingleElimination.by_size(4).generate_blank_bracket }
let(:players) do
Something went wrong with that request. Please try again.