bracket_tree 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. data/.gitignore +4 -0
  2. data/.travis.yml +7 -0
  3. data/Gemfile +4 -0
  4. data/README.md +187 -0
  5. data/Rakefile +8 -0
  6. data/bracket_tree.gemspec +20 -0
  7. data/lib/bracket_tree.rb +9 -0
  8. data/lib/bracket_tree/bracket.rb +12 -0
  9. data/lib/bracket_tree/bracket/base.rb +228 -0
  10. data/lib/bracket_tree/bracket/double_elimination.rb +7 -0
  11. data/lib/bracket_tree/bracket/single_elimination.rb +7 -0
  12. data/lib/bracket_tree/match.rb +24 -0
  13. data/lib/bracket_tree/node.rb +23 -0
  14. data/lib/bracket_tree/template.rb +70 -0
  15. data/lib/bracket_tree/templates/double_elimination.rb +11 -0
  16. data/lib/bracket_tree/templates/double_elimination/128.json +3695 -0
  17. data/lib/bracket_tree/templates/double_elimination/16.json +107 -0
  18. data/lib/bracket_tree/templates/double_elimination/32.json +202 -0
  19. data/lib/bracket_tree/templates/double_elimination/4.json +26 -0
  20. data/lib/bracket_tree/templates/double_elimination/64.json +400 -0
  21. data/lib/bracket_tree/templates/double_elimination/8.json +50 -0
  22. data/lib/bracket_tree/templates/single_elimination.rb +11 -0
  23. data/lib/bracket_tree/templates/single_elimination/128.json +1917 -0
  24. data/lib/bracket_tree/templates/single_elimination/16.json +56 -0
  25. data/lib/bracket_tree/templates/single_elimination/2.json +11 -0
  26. data/lib/bracket_tree/templates/single_elimination/32.json +351 -0
  27. data/lib/bracket_tree/templates/single_elimination/4.json +17 -0
  28. data/lib/bracket_tree/templates/single_elimination/64.json +703 -0
  29. data/lib/bracket_tree/templates/single_elimination/8.json +29 -0
  30. data/spec/bracket_spec.rb +189 -0
  31. data/spec/double_elimination_spec.rb +5 -0
  32. data/spec/match_spec.rb +30 -0
  33. data/spec/spec_helper.rb +3 -0
  34. data/spec/template_spec.rb +58 -0
  35. metadata +112 -0
@@ -0,0 +1,29 @@
1
+ {
2
+ "matches":[
3
+ { "seats":[1,3], "winner_to":2, "loser_to":null },
4
+ { "seats":[5,7], "winner_to":6, "loser_to":null },
5
+ { "seats":[9,11], "winner_to":10, "loser_to":null },
6
+ { "seats":[13,15], "winner_to":14, "loser_to":null },
7
+ { "seats":[2,6], "winner_to":4, "loser_to":null },
8
+ { "seats":[10,14], "winner_to":12, "loser_to":null },
9
+ { "seats":[4,12], "winner_to":null, "loser_to":null }
10
+ ],
11
+ "seats":[
12
+ { "position": 8 },
13
+ { "position": 4 },
14
+ { "position": 12 },
15
+ { "position": 10 },
16
+ { "position": 14 },
17
+ { "position": 2 },
18
+ { "position": 6 },
19
+ { "position": 13 },
20
+ { "position": 15 },
21
+ { "position": 9 },
22
+ { "position": 11 },
23
+ { "position": 5 },
24
+ { "position": 7 },
25
+ { "position": 1 },
26
+ { "position": 3 }
27
+ ],
28
+ "starting_seats":[1,3,5,7,9,11,13,15]
29
+ }
@@ -0,0 +1,189 @@
1
+ require 'spec_helper'
2
+
3
+ describe BracketTree::Bracket::Base do
4
+ let(:bracket) { BracketTree::Bracket::Base.new }
5
+
6
+ describe '#initialize' do
7
+ it 'should create an array of Match objects if matches are passed' do
8
+ matches = [
9
+ { seats: [1,3], winner_to: 2, loser_to: nil },
10
+ { seats: [5,7], winner_to: 4, loser_to: nil },
11
+ { seats: [2,4], winner_to: 6, loser_to: nil }
12
+ ]
13
+ bracket = BracketTree::Bracket::Base.new matches: matches
14
+
15
+ bracket.matches.should be_a Array
16
+ bracket.matches.map(&:class).should == [BracketTree::Match, BracketTree::Match, BracketTree::Match]
17
+ end
18
+
19
+ it 'should create an empty if matches are not passed' do
20
+ bracket.matches.should be_a Array
21
+ bracket.matches.should == []
22
+ end
23
+
24
+ it 'should assign the seed order if seed_order is passed' do
25
+ expected = [1,3,5,7]
26
+ bracket = BracketTree::Bracket::Base.new seed_order: expected
27
+ bracket.seed_order.should == expected
28
+ end
29
+ end
30
+
31
+ describe '#add' do
32
+ let(:root_payload) { { bar: 'bar' } }
33
+ let(:payload) { { foo: 'foo' } }
34
+
35
+ context 'no nodes present' do
36
+ before do
37
+ bracket.root.should be_nil
38
+ bracket.add 4, root_payload
39
+ end
40
+
41
+ it 'should create a new Node at root' do
42
+ bracket.root.should be_a_kind_of BracketTree::Node
43
+ end
44
+
45
+ it 'should set the payload of the new node to the passed object' do
46
+ bracket.root.payload.should == root_payload
47
+ end
48
+
49
+ it 'should set the position of the root node to the passed position' do
50
+ bracket.root.position.should == 4
51
+ end
52
+ end
53
+
54
+ context 'root node present' do
55
+ before do
56
+ bracket.add 4, root_payload
57
+ bracket.add 2, payload
58
+ end
59
+
60
+ it 'should add a node to the left position of root' do
61
+ bracket.root.left.should be_a_kind_of BracketTree::Node
62
+ end
63
+
64
+ it 'should have the payload of the new node' do
65
+ bracket.root.left.payload.should == payload
66
+ end
67
+
68
+ it 'should have the position of the new node' do
69
+ bracket.root.left.position.should == 2
70
+ end
71
+ end
72
+
73
+ context 'two nodes present' do
74
+ let(:new_payload) { { baz: 'baz' } }
75
+ before do
76
+ bracket.add 4, root_payload
77
+ bracket.add 2, payload
78
+
79
+ bracket.root.left.right.should be_nil
80
+ bracket.add 3, new_payload
81
+ end
82
+
83
+ it 'should add the new node to the left node right' do
84
+ bracket.root.left.right.should be_a_kind_of BracketTree::Node
85
+ end
86
+
87
+ it 'should add the payload of the new node' do
88
+ bracket.root.left.right.payload.should == new_payload
89
+ end
90
+
91
+ it 'should add the position of the new node' do
92
+ bracket.root.left.right.position.should == 3
93
+ end
94
+ end
95
+ end
96
+
97
+ describe '#seats' do
98
+ let(:bracket) { BracketTree::Bracket::SingleElimination.by_size 4 }
99
+
100
+ it 'should return 7 seats' do
101
+ bracket.seats.should have(7).seats
102
+ end
103
+
104
+ it 'should return them in insertion order' do
105
+ bracket.seats.map { |n| n.position }.should == bracket.insertion_order
106
+ end
107
+ end
108
+
109
+ describe '#replace' do
110
+ before do
111
+ bracket.add 3, { foo: 'foo' }
112
+ bracket.add 2, { bar: 'bar' }
113
+ bracket.add 4, { baz: 'baz' }
114
+ end
115
+
116
+ it 'replaces the payload at a given position with new payload' do
117
+ bracket.replace 3, { winner: 'me' }
118
+
119
+ bracket.root.payload.should == { winner: 'me' }
120
+ bracket.root.left.payload.should == { bar: 'bar' }
121
+ bracket.root.right.payload.should == { baz: 'baz' }
122
+ end
123
+ end
124
+
125
+ describe "#size" do
126
+ it "should return the number of nodes in the bracket" do
127
+ bracket.add 3, { foo: 'foo' }
128
+ bracket.add 2, { bar: 'bar' }
129
+ bracket.add 4, { baz: 'baz' }
130
+ bracket.size.should == 3
131
+ end
132
+ end
133
+
134
+ describe "#at" do
135
+ before do
136
+ bracket.add 3, { foo: 'foo' }
137
+ bracket.add 2, { bar: 'bar' }
138
+ bracket.add 1, { baz: 'baz' }
139
+ end
140
+ it "should return the node at the given position in the bracket" do
141
+ bracket.at(1).payload.should == { baz: 'baz'}
142
+ bracket.at(2).payload.should == { bar: 'bar'}
143
+ bracket.at(3).payload.should == { foo: 'foo'}
144
+ end
145
+ end
146
+
147
+ describe '#seed' do
148
+ let(:bracket) { BracketTree::Bracket::SingleElimination.by_size 4 }
149
+ let(:players) do
150
+ [
151
+ { name: 'player4' },
152
+ { name: 'player1' },
153
+ { name: 'player3' },
154
+ { name: 'player2' }
155
+ ]
156
+ end
157
+
158
+ it 'should place the players in the bracket by seed order' do
159
+ bracket.seed players
160
+
161
+ bracket.at(1).payload.should == { name: 'player4' }
162
+ bracket.at(3).payload.should == { name: 'player1' }
163
+ bracket.at(5).payload.should == { name: 'player3' }
164
+ bracket.at(7).payload.should == { name: 'player2' }
165
+ end
166
+
167
+ it 'should raise a NoSeedOrderError if seed order is not present in bracket' do
168
+ bracket.seed_order = nil
169
+
170
+ expect { bracket.seed players }.to raise_error(BracketTree::Bracket::NoSeedOrderError)
171
+ end
172
+
173
+ it 'should raise a SeedLimitExceededError if player count exceeds seed count' do
174
+ players << { name: 'player5' }
175
+ expect { bracket.seed players }.to raise_error(BracketTree::Bracket::SeedLimitExceededError)
176
+ end
177
+ end
178
+
179
+ describe '#match_winner' do
180
+ let(:bracket) { BracketTree::Bracket::DoubleElimination.by_size 4 }
181
+
182
+ it 'copies the seat data to the seat specified in the match winner_to' do
183
+ bracket.at(1).payload[:seed_value] = 1
184
+
185
+ bracket.match_winner 1
186
+ bracket.at(2).payload.should == bracket.at(1).payload
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe BracketTree::Template::DoubleElimination do
4
+
5
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe BracketTree::Match do
4
+ it 'should log the seats if passed' do
5
+ match = BracketTree::Match.new seats: [1,2]
6
+ match.seats.should == [1,2]
7
+ end
8
+
9
+ it 'should log the winner_to if passed' do
10
+ match = BracketTree::Match.new winner_to: 1
11
+ match.winner_to.should == 1
12
+ end
13
+
14
+ it 'should log the loser_to if passed' do
15
+ match = BracketTree::Match.new loser_to: 1
16
+ match.loser_to.should == 1
17
+ end
18
+
19
+ describe '#include?' do
20
+ it 'should return true if seat is part of Match' do
21
+ match = BracketTree::Match.new seats: [1,2]
22
+ match.include?(1).should be_true
23
+ end
24
+
25
+ it 'should return false if seats is not part of Match' do
26
+ match = BracketTree::Match.new seats: [1,2]
27
+ match.include?(3).should be_false
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
2
+
3
+ require 'bracket_tree'
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ class TestTemplate < BracketTree::Template::Base
4
+ def self.location
5
+ File.join File.dirname(__FILE__), '../', 'lib/bracket_tree/templates/single_elimination'
6
+ end
7
+ end
8
+
9
+ describe BracketTree::Template::Base do
10
+ def template_json
11
+ {
12
+ starting_seats: [1,3,5,7],
13
+ seats: [
14
+ { position: 4 },
15
+ { position: 2 },
16
+ { position: 6 },
17
+ { position: 1 },
18
+ { position: 3 },
19
+ { position: 5 },
20
+ { position: 7 }
21
+ ],
22
+ matches: []
23
+ }
24
+ end
25
+
26
+ describe '#by_size' do
27
+ it 'should return the Template based on the player size passed' do
28
+ template = TestTemplate.by_size 4
29
+
30
+ template.should be_a TestTemplate
31
+ template.seats.should have(7).seats
32
+ template.matches.should have(3).matches
33
+ end
34
+
35
+ it 'should return nil if template JSON does not exist' do
36
+ template = TestTemplate.by_size 5
37
+
38
+ template.should be_nil
39
+ end
40
+ end
41
+
42
+ describe '#from_json' do
43
+ it 'should generate the Template from the JSON template' do
44
+ template = TestTemplate.from_json template_json
45
+
46
+ template.should be_a TestTemplate
47
+ template.seats.should have(7).seats
48
+ template.starting_seats.should == [1,3,5,7]
49
+ end
50
+ end
51
+
52
+ describe '#to_h' do
53
+ it' should return a hash containing the bracket template' do
54
+ template = TestTemplate.from_json template_json
55
+ template.to_h.should == template_json
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bracket_tree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Nordman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70354606951940 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70354606951940
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70354606967860 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70354606967860
36
+ description: Binary Tree based bracketing system
37
+ email:
38
+ - anordman@majorleaguegaming.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .travis.yml
45
+ - Gemfile
46
+ - README.md
47
+ - Rakefile
48
+ - bracket_tree.gemspec
49
+ - lib/bracket_tree.rb
50
+ - lib/bracket_tree/bracket.rb
51
+ - lib/bracket_tree/bracket/base.rb
52
+ - lib/bracket_tree/bracket/double_elimination.rb
53
+ - lib/bracket_tree/bracket/single_elimination.rb
54
+ - lib/bracket_tree/match.rb
55
+ - lib/bracket_tree/node.rb
56
+ - lib/bracket_tree/template.rb
57
+ - lib/bracket_tree/templates/double_elimination.rb
58
+ - lib/bracket_tree/templates/double_elimination/128.json
59
+ - lib/bracket_tree/templates/double_elimination/16.json
60
+ - lib/bracket_tree/templates/double_elimination/32.json
61
+ - lib/bracket_tree/templates/double_elimination/4.json
62
+ - lib/bracket_tree/templates/double_elimination/64.json
63
+ - lib/bracket_tree/templates/double_elimination/8.json
64
+ - lib/bracket_tree/templates/single_elimination.rb
65
+ - lib/bracket_tree/templates/single_elimination/128.json
66
+ - lib/bracket_tree/templates/single_elimination/16.json
67
+ - lib/bracket_tree/templates/single_elimination/2.json
68
+ - lib/bracket_tree/templates/single_elimination/32.json
69
+ - lib/bracket_tree/templates/single_elimination/4.json
70
+ - lib/bracket_tree/templates/single_elimination/64.json
71
+ - lib/bracket_tree/templates/single_elimination/8.json
72
+ - spec/bracket_spec.rb
73
+ - spec/double_elimination_spec.rb
74
+ - spec/match_spec.rb
75
+ - spec/spec_helper.rb
76
+ - spec/template_spec.rb
77
+ homepage: https://github.com/agoragames/bracket_tree
78
+ licenses: []
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ segments:
90
+ - 0
91
+ hash: 1286311622881667081
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ segments:
99
+ - 0
100
+ hash: 1286311622881667081
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.17
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Binary Tree based bracketing system
107
+ test_files:
108
+ - spec/bracket_spec.rb
109
+ - spec/double_elimination_spec.rb
110
+ - spec/match_spec.rb
111
+ - spec/spec_helper.rb
112
+ - spec/template_spec.rb