rrschedule 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -86,10 +86,10 @@ and will start a new round right after.
86
86
  end
87
87
 
88
88
  === Iterate through schedule
89
- schedule.gamedays.each do |gd,games|
90
- puts gd
89
+ schedule.gamedays.each do |gd|
90
+ puts gd.date
91
91
  puts "===================="
92
- games.each do |g|
92
+ gd.games.each do |g|
93
93
  puts g.team_a.to_s + " Vs " + g.team_b.to_s + " on playing surface ##{g.playing_surface} at #{g.game_time}"
94
94
  end
95
95
  puts "\n"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
data/lib/rrschedule.rb CHANGED
@@ -5,15 +5,16 @@ require 'active_support'
5
5
  module RRSchedule
6
6
  class Schedule
7
7
  attr_accessor :playing_surfaces, :game_times, :cycles, :wdays, :start_date, :exclude_dates, :shuffle_initial_order
8
- attr_reader :teams, :rounds
8
+ attr_reader :teams, :rounds, :gamedays
9
9
 
10
10
  def initialize(params={})
11
+ @gamedays = []
11
12
  self.teams = params[:teams] || [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
12
13
  self.playing_surfaces = Array(params[:playing_surfaces]).empty? ? ["Surface A", "Surface B"] : Array(params[:playing_surfaces])
13
14
  self.cycles = params[:cycles] || 1
14
15
 
15
16
  self.game_times = Array(params[:game_times]).empty? ? ["7:00 PM", "9:00 PM"] : Array(params[:game_times])
16
- self.game_times.collect! do |gt|
17
+ self.game_times.collect! do |gt|
17
18
  begin
18
19
  DateTime.parse(gt)
19
20
  rescue
@@ -21,7 +22,7 @@ module RRSchedule
21
22
  end
22
23
  end
23
24
 
24
- self.shuffle_initial_order = params[:shuffle_initial_order] || true
25
+ self.shuffle_initial_order = params[:shuffle_initial_order].nil? ? true : params[:shuffle_initial_order]
25
26
  self.exclude_dates = params[:exclude_dates] || []
26
27
  self.start_date = params[:start_date] || Time.now.beginning_of_day
27
28
  self.wdays = Array(params[:wdays]).empty? ? [1] : Array(params[:wdays])
@@ -83,41 +84,37 @@ module RRSchedule
83
84
 
84
85
  def face_to_face(team_a,team_b)
85
86
  res=[]
86
- self.gamedays.each do |gd,games|
87
- res << games.select {|g| (g.team_a == team_a && g.team_b == team_b) || (g.team_a == team_b && g.team_b == team_a)}
87
+ self.gamedays.each do |gd|
88
+ res << gd.games.select {|g| (g.team_a == team_a && g.team_b == team_b) || (g.team_a == team_b && g.team_b == team_a)}
88
89
  end
89
90
  res.flatten
90
91
  end
91
92
 
92
93
  def to_s
93
94
  res = ""
94
- res << "#{@schedule.keys.size.to_s} gamedays\n"
95
- @schedule.sort.each do |gd,games|
96
- res << gd.strftime("%Y-%m-%d") + "\n"
95
+ res << "#{self.gamedays.size.to_s} gamedays\n"
96
+ self.gamedays.each do |gd|
97
+ res << gd.date.strftime("%Y-%m-%d") + "\n"
97
98
  res << "==========\n"
98
- games.each do |g|
99
+ gd.games.each do |g|
99
100
  res << "#{g.team_a.to_s} VS #{g.team_b.to_s} on playing surface #{g.playing_surface} at #{g.game_time.strftime("%I:%M %p")}\n"
100
101
  end
101
102
  res << "\n"
102
103
  end
103
104
  res
104
105
  end
105
-
106
- def gamedays
107
- @schedule.sort
108
- end
109
-
110
- def by_team(team)
106
+
107
+ #TODO: should return either a Schedule instance or a TeamSchedule instance (this class doesn't exist yet)
108
+ def by_team(team)
111
109
  gms=[]
112
- self.gamedays.each do |gd,games|
113
- gms << games.select{|g| g.team_a == team || g.team_b == team}
110
+ self.gamedays.each do |gd|
111
+ gms << gd.games.select{|g| g.team_a == team || g.team_b == team}
114
112
  end
115
113
  gms.flatten
116
114
  end
117
115
 
118
- #TODO: returns true if the generated schedule is a valid round-robin (for testing purpose)
119
- def round_robin?
120
-
116
+ #returns true if the generated schedule is a valid round-robin (for testing purpose)
117
+ def round_robin?
121
118
  #each round-robin round should contains n-1 games where n is the nbr of teams (:dummy included if odd)
122
119
  return false if self.rounds.size != (@teams.size*self.cycles)-self.cycles
123
120
 
@@ -130,48 +127,48 @@ module RRSchedule
130
127
  return true
131
128
  end
132
129
 
133
- private
134
-
135
130
  def teams=(arr)
136
131
  @teams = arr.clone
137
132
  raise ":dummy is a reserved team name. Please use something else" if @teams.member?(:dummy)
138
133
  raise "at least 2 teams are required" if @teams.size == 1
139
134
  @teams << :dummy if @teams.size.odd?
140
135
  end
141
-
142
- #Let's slice our games according to our physical constraints
136
+
137
+ private
138
+ #Slice games according to playing surfaces and game times
143
139
  def slice(games)
144
- res={}
145
140
  slices = games.each_slice(games_per_day)
146
141
  wdays_stack = self.wdays.clone
147
142
  cur_date = self.start_date
148
143
  slices.each_with_index do |slice,i|
149
- gt_stack = self.game_times.clone
150
- ps_stack = self.playing_surfaces.clone
144
+ gt_stack = self.game_times.clone.sort_by{rand}
145
+ ps_stack = self.playing_surfaces.clone.sort_by{rand}
151
146
  wdays_stack = self.wdays.clone if wdays_stack.empty?
152
147
 
153
148
  cur_wday = wdays_stack.shift
154
149
  cur_date = next_game_date(cur_date,cur_wday)
155
150
  cur_gt = gt_stack.shift
156
151
 
157
- res[cur_date] = []
152
+ gameday = Gameday.new(:date => cur_date)
153
+
158
154
  slice.each_with_index do |g,game_index|
159
155
  cur_ps = ps_stack.shift
160
- res[cur_date] << Game.new(
156
+ gameday.games << Game.new(
161
157
  :team_a => g[:team_a],
162
158
  :team_b => g[:team_b],
163
159
  :playing_surface => cur_ps,
164
160
  :game_time => cur_gt,
165
161
  :game_date => cur_date)
162
+
166
163
  cur_gt = gt_stack.shift if ps_stack.empty?
167
164
  gt_stack = self.game_times.clone if gt_stack.empty?
168
165
  ps_stack = self.playing_surfaces.clone if ps_stack.empty?
169
166
  end
170
167
 
171
- res[cur_date] = res[cur_date].sort_by {|g| [g.game_time,g.playing_surface]}
168
+ gameday.games = gameday.games.sort_by {|g| [g.game_time,g.playing_surface]}
169
+ self.gamedays << gameday
172
170
  cur_date += 1.day
173
171
  end
174
- @schedule = res
175
172
  end
176
173
 
177
174
  def next_game_date(dt,wday)
@@ -183,7 +180,17 @@ module RRSchedule
183
180
  self.playing_surfaces.size * self.game_times.size
184
181
  end
185
182
  end
186
-
183
+
184
+ class Gameday
185
+ attr_accessor :date, :games
186
+
187
+ def initialize(params)
188
+ self.date = params[:date]
189
+ self.games = params[:games] || []
190
+ end
191
+
192
+ end
193
+
187
194
  class Game
188
195
  attr_accessor :team_a, :team_b, :playing_surface, :game_time, :game_date
189
196
 
@@ -201,7 +208,7 @@ module RRSchedule
201
208
 
202
209
  def initialize(params={})
203
210
  self.round = params[:round]
204
- self.games = params[:games]
211
+ self.games = params[:games] || []
205
212
  end
206
213
  end
207
214
  end
@@ -15,7 +15,7 @@ class TestRrschedule < Test::Unit::TestCase
15
15
  assert schedule.exclude_dates.empty?
16
16
  end
17
17
 
18
- should "have a dummy team when team number is odd" do
18
+ should "have a dummy team when number of teams is odd" do
19
19
  schedule = RRSchedule::Schedule.new(
20
20
  :teams => Array(1..9)
21
21
  )
@@ -24,7 +24,7 @@ class TestRrschedule < Test::Unit::TestCase
24
24
  assert schedule.teams.member?(:dummy), "There should always be a :dummy team when the nbr of teams is odd"
25
25
  end
26
26
 
27
- should "not have a dummy team when team number is even" do
27
+ should "not have a dummy team when number of teams is even" do
28
28
  schedule = RRSchedule::Schedule.new(
29
29
  :teams => Array(1..6)
30
30
  )
@@ -69,7 +69,7 @@ class TestRrschedule < Test::Unit::TestCase
69
69
  assert_equal ["the only one"], schedule.playing_surfaces
70
70
  end
71
71
 
72
- should "have at least one team specified" do
72
+ should "have at least two teams" do
73
73
  assert_raise RuntimeError do
74
74
  schedule = RRSchedule::Schedule.new(:teams => [1])
75
75
  end
@@ -81,6 +81,25 @@ class TestRrschedule < Test::Unit::TestCase
81
81
  end
82
82
  end
83
83
 
84
+ context "Any valid schedule" do
85
+ setup do
86
+ @s = RRSchedule::Schedule.new(
87
+ :teams => %w(a b c d e f g h i j l m),
88
+ :playing_surfaces => %w(one two),
89
+ :game_times => ["10:00 AM", "13:00 PM"]
90
+ )
91
+ end
92
+
93
+ should "have gamedays that respect the wdays attribute" do
94
+ @s.wdays = [3,5]
95
+ @s.generate
96
+
97
+ @s.gamedays.each do |gd|
98
+ assert [3,5].include?(gd.date.wday), "wday is #{gd.date.wday.to_s} but should be 3 or 5"
99
+ end
100
+ end
101
+ end
102
+
84
103
  context "A generated schedule with an odd number of teams" do
85
104
  setup do
86
105
  @s = RRSchedule::Schedule.new(
@@ -94,8 +113,8 @@ class TestRrschedule < Test::Unit::TestCase
94
113
  assert @s.round_robin?
95
114
  end
96
115
 
97
- should "not have any :dummy teams in the schedule" do
98
- assert @s.gamedays.collect{|gd,games| games}.flatten.select{
116
+ should "not have any :dummy teams in the final schedule" do
117
+ assert @s.gamedays.collect{|gd| gd.games}.flatten.select{
99
118
  |g| [g.team_a,g.team_b].include?(:dummy)
100
119
  }.size == 0
101
120
  end
@@ -114,10 +133,10 @@ class TestRrschedule < Test::Unit::TestCase
114
133
  assert @s.round_robin?
115
134
  end
116
135
 
117
- should "not have any :dummy teams in the schedule" do
118
- assert @s.gamedays.collect{|gd,games| games}.flatten.select{
136
+ should "not have any :dummy teams in the final schedule" do
137
+ assert @s.gamedays.collect{|gd| gd.games}.flatten.select{
119
138
  |g| [g.team_a,g.team_b].include?(:dummy)
120
139
  }.size == 0
121
- end
122
- end
140
+ end
141
+ end
123
142
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rrschedule
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 4
10
- version: 0.1.4
9
+ - 5
10
+ version: 0.1.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - flamontagne
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-09 00:00:00 -05:00
18
+ date: 2010-11-10 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -100,5 +100,5 @@ signing_key:
100
100
  specification_version: 3
101
101
  summary: Round-Robin schedule generator
102
102
  test_files:
103
- - test/helper.rb
104
103
  - test/test_rrschedule.rb
104
+ - test/helper.rb