lib/rrschedule.rb in rrschedule-0.1.2 vs lib/rrschedule.rb in rrschedule-0.1.3
- old
+ new
@@ -1,21 +1,19 @@
# rrschedule (Round Robin Schedule generator)
# Auhtor: François Lamontagne
############################################################################################################################
-require 'rubygems'
require 'active_support'
-
module RRSchedule
class Schedule
attr_accessor :playing_surfaces, :game_times, :cycles, :wdays, :start_date, :exclude_dates, :shuffle_initial_order
attr_reader :teams, :rounds
def initialize(params={})
self.teams = params[:teams] || [1,2,3,4,5]
self.playing_surfaces = params[:playing_surfaces] || ["--"]
self.cycles = params[:cycles] || 1
- self.game_times = params[:game_times] || ["7:00 PM"]
+ self.game_times = params[:game_times] || ["Game time not specified"]
self.shuffle_initial_order = params[:shuffle_initial_order] || true
self.exclude_dates = params[:exclude_dates] || []
self.start_date = params[:start_date] || Time.now.beginning_of_day
self.wdays = Array(params[:wdays]).empty? ? [1] : Array(params[:wdays])
end
@@ -40,14 +38,20 @@
all_games << {:team_a => team_a, :team_b => team_b}
end
#round completed
current_round += 1
@rounds ||= []
- @rounds << Round.new(:round => current_round, :games => games.collect {|g| Game.new(:team_a => g[:team_a], :team_b => g[:team_b])})
+ @rounds << Round.new(
+ :round => current_round,
+ :games => games.collect { |g| Game.new(
+ :team_a => g[:team_a],
+ :team_b => g[:team_b])
+ })
- games.reject! {|g| g[:team_a] == :dummy || g[:team_b] == :dummy}
- all_games.reject! {|g| g[:team_a] == :dummy || g[:team_b] == :dummy}
+ reject_dummy = lambda {|g| g[:team_a] == :dummy || g[:team_b] == :dummy}
+ games.reject! {|g| reject_dummy.call(g)}
+ all_games.reject! {|g| reject_dummy.call(g)}
@teams = @teams.insert(1,@teams.delete_at(@teams.size-1))
#If we have completed a cycle
if @teams == initial_order
@@ -65,24 +69,23 @@
def face_to_face(team_a,team_b)
res=[]
self.gamedays.each do |gd,games|
- res << games.select{|g| (g.team_a == team_a && g.team_b == team_b) || (g.team_a == team_b && g.team_b == team_a)}
+ res << games.select {|g| (g.team_a == team_a && g.team_b == team_b) || (g.team_a == team_b && g.team_b == team_a)}
end
res.flatten
end
def to_s
res = ""
res << "#{@schedule.keys.size.to_s} gamedays\n"
@schedule.sort.each do |gd,games|
- gd_proc = lambda {gd.strftime("%Y-%m-%d")}
- res << gd_proc.call + "\n"
- res << "=" * gd_proc.call.length + "\n"
+ res << gd.strftime("%Y-%m-%d") + "\n"
+ res << "==========\n"
games.each do |g|
- res << g.team_a.to_s + " VS " + g.team_b.to_s + " on playing surface #{g.playing_surface} at #{g.game_time}\n"
+ res << "#{g.team_a.to_s} VS #{g.team_b.to_s} on playing surface #{g.playing_surface} at #{g.game_time}\n"
end
res << "\n"
end
res
end
@@ -92,11 +95,10 @@
end
def by_team(team)
gms=[]
self.gamedays.each do |gd,games|
- #games = games.each {|g| g.game_date = gd}
gms << games.select{|g| g.team_a == team || g.team_b == team}
end
gms.flatten
end
@@ -124,10 +126,15 @@
cur_gt = gt_stack.shift
res[cur_date] = []
slice.each_with_index do |g,game_index|
cur_ps = ps_stack.shift
- res[cur_date] << Game.new(:team_a => g[:team_a], :team_b => g[:team_b], :playing_surface => cur_ps, :game_time => cur_gt, :game_date => cur_date)
+ res[cur_date] << Game.new(
+ :team_a => g[:team_a],
+ :team_b => g[:team_b],
+ :playing_surface => cur_ps,
+ :game_time => cur_gt,
+ :game_date => cur_date)
cur_gt = gt_stack.shift if ps_stack.empty?
gt_stack = self.game_times.clone if gt_stack.empty?
ps_stack = self.playing_surfaces.clone if ps_stack.empty?
end
cur_date += 1.day