Permalink
Browse files

No more camelCase, adds BracketTree::Match

  • Loading branch information...
1 parent e4170ff commit 3946c936b67de806ef8c1743de0778b5b5227dc5 @cadwallion cadwallion committed May 23, 2012
View
@@ -1,4 +1,5 @@
require 'json'
+require 'bracket_tree/match'
require 'bracket_tree/bracket'
require 'bracket_tree/template'
require 'bracket_tree/templates/double_elimination'
@@ -6,12 +6,17 @@ class NoSeedOrderError < Exception ; end
class SeedLimitExceededError < Exception ; end
include Enumerable
- attr_accessor :root, :seed_order, :insertion_order
+ attr_accessor :root, :seed_order, :matches, :insertion_order
def initialize options = {}
@insertion_order = []
+ @matches = []
- @matches = options[:matches] || []
+ if options[:matches]
+ options[:matches].each do |m|
+ @matches << Match.new(m)
+ end
+ end
end
# Adds a Node at the given position, setting the data as the payload. Maps to
@@ -112,6 +117,24 @@ def at position
alias_method :size, :count
+ # Progresses the bracket by using the stored `matches` to copy data to the winning
+ # and losing seats. This facilitates match progression without manually
+ # manipulating bracket positions
+ #
+ # @param Fixnum seat - winning seat position
+ def match_winner seat
+ match = @matches.find { |m| m.include? seat }
+ losing_seat = match.seats.find { |s| s != seat }
+
+ if match.winner_to
+ self.replace match.winner_to, seats.at(seat).payload
+ end
+
+ if match.loser_to
+ self.replace match.loser_to, seats.at(losing_seat).payload
+ end
+ end
+
def in_order(node, block)
if node
unless node.left.nil?
@@ -0,0 +1,22 @@
+module BracketTree
+ class Match
+ attr_accessor :seats, :winner_to, :loser_to
+ def initialize options = {}
+ @seats = options[:seats] || []
+ @winner_to = options[:winner_to]
+ @loser_to = options[:loser_to]
+ end
+
+ def include? seat
+ @seats.include? seat
+ end
+
+ def to_h
+ {
+ seats: @seats,
+ winner_to: @winner_to,
+ loser_to: @loser_to
+ }
+ end
+ end
+end
@@ -11,7 +11,7 @@ def by_size size
filename = File.join location, "#{size}.json"
if File.exists? filename
- from_json JSON.parse File.read filename
+ from_json JSON.parse(File.read(filename), symbolize_names: true)
else
return nil
end
@@ -23,16 +23,16 @@ def by_size size
# @return [BracketTree::Template]
def from_json json
template = new
- if json['seats']
- template.seats = json['seats'].map { |s| s['position'] }
+ if json[:seats]
+ template.seats = json[:seats].map { |s| s[:position] }
end
- if json['startingSeats']
- template.starting_seats = json['startingSeats']
+ if json[:starting_seats]
+ template.starting_seats = json[:starting_seats]
end
- if json['matches']
- template.matches = json['matches']
+ if json[:matches]
+ template.matches = json[:matches]
end
template
@@ -58,7 +58,7 @@ def initialize
#
# @return [BracketTree::Bracket] bracket
def generate_blank_bracket
- bracket = Bracket.new
+ bracket = Bracket.new matches: @matches
@seats.each do |position|
bracket.add position, {}
@@ -74,9 +74,9 @@ def generate_blank_bracket
# @return [Hash] template
def to_h
{
- 'seats' => @seats.map { |s| { 'position' => s } },
- 'startingSeats' => @starting_seats,
- 'matches' => @matches
+ seats: @seats.map { |s| { position: s } },
+ starting_seats: @starting_seats,
+ matches: @matches
}
end
end
Oops, something went wrong. Retry.

0 comments on commit 3946c93

Please sign in to comment.