def dispatch_games(rounds)

      rounds_copy =  Marshal.load(Marshal.dump(rounds)) #deep clone

      flat_games = []
      if group_flights
        while rounds_copy.flatten.size > 0 do
          @flights.each_with_index do |f,flight_index|
            r = rounds_copy[flight_index].shift
            flat_games << r.games if r
          end
        end          
      else
        flight_index = round_index = 0
        game_ctr = 0 
        while game_ctr < total_nbr_games
          if rounds_copy[flight_index][round_index] != nil
            game = rounds_copy[flight_index][round_index].games.shift
            if game
              flat_games << game 
              game_ctr += 1
            end
          end

          #check if round is empty
          round_empty=true
          @flights.size.times do |i| 
            round_empty = round_empty && (rounds_copy[i][round_index].nil? || rounds_copy[i][round_index].games.empty?)
          end
          
          if flight_index == @flights.size-1
            flight_index = 0
            round_index+=1 if round_empty
          else
            flight_index += 1
          end          
        end
      end
      
      flat_games.flatten!
      flat_games.each do |game|
        dispatch_game(game) unless [game.team_a,game.team_b].include?(:dummy)
      end

      #We group our schedule by gameday
      s=@schedule.group_by{|fs| fs[:gamedate]}.sort
      s.each do |gamedate,gms|
        games = []
        gms.each do |gm|
          games << Game.new(
            :team_a => gm[:team_a],
            :team_b => gm[:team_b],
            :playing_surface => gm[:ps],
            :game_time => gm [:gt]
          )
        end
        self.gamedays << Gameday.new(:date => gamedate, :games => games)
      end
    end
def dispatch_games(rounds)

      rounds_copy =  Marshal.load(Marshal.dump(rounds)) #deep clone

      flat_games = []
      if group_flights
        while rounds_copy.flatten.size > 0 do
          @flights.each_with_index do |f,flight_index|
            r = rounds_copy[flight_index].shift
            flat_games << r.games if r
          end
        end          
      else
        flight_index = round_index = 0
        game_ctr = 0 
        while game_ctr < total_nbr_games
          if rounds_copy[flight_index][round_index] != nil
            game = rounds_copy[flight_index][round_index].games.shift
            if game
              flat_games << game 
              game_ctr += 1
            end
          end

          #check if round is empty
          round_empty=true
          @flights.size.times do |i| 
            round_empty = round_empty && (rounds_copy[i][round_index].nil? || rounds_copy[i][round_index].games.empty?)
          end
          
          if flight_index == @flights.size-1
            flight_index = 0
            round_index+=1 if round_empty
          else
            flight_index += 1
          end          
        end
      end
      
      flat_games.flatten!
      flat_games.each do |game|
        dispatch_game(game) unless [game.team_a,game.team_b].include?(:dummy)
      end

      #We group our schedule by gameday
      s=@schedule.group_by{|fs| fs[:gamedate]}.sort
      s.each do |gamedate,gms|
        games = []
        gms.each do |gm|
          games << Game.new(
            :team_a => gm[:team_a],
            :team_b => gm[:team_b],
            :playing_surface => gm[:ps],
            :game_time => gm [:gt]
          )
        end
        self.gamedays << Gameday.new(:date => gamedate, :games => games)
      end
    end
Dispatch games according to available playing surfaces and game times