def generate(params={})
      raise "You need to specify at least 1 team" if @teams.nil? || @teams.empty?
      raise "You need to specify at least 1 rule" if @rules.nil? || @rules.empty?

      arrange_flights
      init_stats

      @gamedays = []; @rounds = []


      @flights.each_with_index do |teams,flight_id|
        current_cycle = current_round = 0
        teams = teams.sort_by{rand} if @shuffle

        #loop to generate the whole round-robin(s) for the current flight
        begin
          t = teams.clone
          games = []

          #process one round
          while !t.empty? do
            team_a = t.shift
            team_b = t.reverse!.shift
            t.reverse!

            x = (current_cycle % 2) == 0 ? [team_a,team_b] : [team_b,team_a]

            matchup = {:team_a => x[0], :team_b => x[1]}
            games << matchup
          end
          #done processing round

          current_round += 1

          #Team rotation (the first team is fixed)
          teams = teams.insert(1,teams.delete_at(teams.size-1))

          #add the round in memory
          @rounds ||= []
          @rounds[flight_id] ||= []
          @rounds[flight_id] << Round.new(
            :round => current_round,
            :cycle => current_cycle + 1,
            :round_with_cycle => current_cycle * (teams.size-1) + current_round,
            :flight => flight_id,
            :games => games.collect { |g|
              Game.new(
                :team_a => g[:team_a],
                :team_b => g[:team_b]
              )
            }
          )
          #done adding round

          #have we completed a full round-robin for the current flight?
          if current_round == teams.size-1
            current_cycle += 1
            current_round = 0 if current_cycle < self.cycles
          end

        end until current_round == teams.size-1 && current_cycle==self.cycles
      end

      dispatch_games(@rounds)
      self
    end
def generate(params={})
      raise "You need to specify at least 1 team" if @teams.nil? || @teams.empty?
      raise "You need to specify at least 1 rule" if @rules.nil? || @rules.empty?

      arrange_flights
      init_stats

      @gamedays = []; @rounds = []


      @flights.each_with_index do |teams,flight_id|
        current_cycle = current_round = 0
        teams = teams.sort_by{rand} if @shuffle

        #loop to generate the whole round-robin(s) for the current flight
        begin
          t = teams.clone
          games = []

          #process one round
          while !t.empty? do
            team_a = t.shift
            team_b = t.reverse!.shift
            t.reverse!

            x = (current_cycle % 2) == 0 ? [team_a,team_b] : [team_b,team_a]

            matchup = {:team_a => x[0], :team_b => x[1]}
            games << matchup
          end
          #done processing round

          current_round += 1

          #Team rotation (the first team is fixed)
          teams = teams.insert(1,teams.delete_at(teams.size-1))

          #add the round in memory
          @rounds ||= []
          @rounds[flight_id] ||= []
          @rounds[flight_id] << Round.new(
            :round => current_round,
            :cycle => current_cycle + 1,
            :round_with_cycle => current_cycle * (teams.size-1) + current_round,
            :flight => flight_id,
            :games => games.collect { |g|
              Game.new(
                :team_a => g[:team_a],
                :team_b => g[:team_b]
              )
            }
          )
          #done adding round

          #have we completed a full round-robin for the current flight?
          if current_round == teams.size-1
            current_cycle += 1
            current_round = 0 if current_cycle < self.cycles
          end

        end until current_round == teams.size-1 && current_cycle==self.cycles
      end

      dispatch_games(@rounds)
      self
    end
This will generate the schedule based on the various parameters