<commit_before>class GameDaysController < ApplicationController
  BEST_TEAM_SIZE = 10

  BEST_TEAM_DEFINITION = {
    NflPlayerType::QB => {
      min: 2,
      max: 2
    },
    NflPlayerType::RB => {
      min: 2,
      max: 2
    },
    NflPlayerType::WR => {
      min: 2,
      max: 3
    },
    NflPlayerType::TE => {
      min: 1,
      max: 2
    },
    NflPlayerType::K => {
      min: 1,
      max: 2
    },
    NflPlayerType::D => {
      min: 1,
      max: 1
    }
  }

  GAME_WEEK_KEY = :game_week
  PLAYER_ID_KEY = :player_id

  def show_no_game_week
    redirect_to "/game_day/#{WithGameWeek.current_game_week}"
  end

  def show
    validate_all_parameters([GAME_WEEK_KEY], params)
    @page_game_week = params[GAME_WEEK_KEY].to_i
    @current_game_week = WithGameWeek.current_game_week
    @player_data = return_nfl_player_and_team_data
    @users = User.all.sort_by { |u| -u.team_for_game_week(@page_game_week).points }
    @best_team = find_ten_best_players(@page_game_week)
    @last_comment = get_timestamp_of_last_comment
  end

  def which_team_has_player
    validate_all_parameters([GAME_WEEK_KEY, PLAYER_ID_KEY], params)
    nfl_player = NflPlayer.find(params[PLAYER_ID_KEY])
    game_week = params[GAME_WEEK_KEY]

    respond_to do |format|
      format.json do
        render json: {
          data: form_team_data(nfl_player, game_week)
        }
      end
    end
  end

  private

  def form_team_data(nfl_player, game_week)
    game_week_team_players = GameWeekTeamPlayer.where(
      match_player: nfl_player.player_for_game_week(game_week)
    )
    return nil if game_week_team_players.empty?

    found_user = game_week_team_players.first.game_week_team.user
    {
      name: found_user.name,
      team_name: found_user.team_name,
      playing: game_week_team_players.first.playing
    }
  end

  def find_ten_best_players(game_week)
    game_week = GameWeek.find_unique_with(game_week)
    Rails.logger.info "Finding best team for game week #{game_week}"

    player_arrays = BEST_TEAM_DEFINITION.keys.reduce(best: [], remaining: []) do |player_arrays_acc, player_type|
      add_players_of_type(player_type, player_arrays_acc, game_week)
    end

    best_players = player_arrays[:best]
    remaining_players = player_arrays[:remaining]

    best_players.concat(remaining_players.sort_by(&:points).last(BEST_TEAM_SIZE - best_players.size))
  end

  def add_players_of_type(player_type, player_arrays, game_week)
    Rails.logger.info "Adding players for #{player_type}"
    best_players = player_arrays[:best]
    remaining_players = player_arrays[:remaining]

    max = BEST_TEAM_DEFINITION[player_type][:max]
    min = BEST_TEAM_DEFINITION[player_type][:min]

    best_of_type = find_top_of_type(player_type, max, game_week)
    best_players.concat(best_of_type.first(min))
    remaining_players.concat(best_of_type.last(max - min))

    Rails.logger.info "Best is now: #{best_players.map { |player| player.nfl_player.name }}"
    Rails.logger.info "Remaining is now: #{remaining_players.map { |player| player.nfl_player.name }}"
    { best: best_players, remaining: remaining_players }
  end

  def find_top_of_type(type, number, game_week)
    MatchPlayer
      .joins(:nfl_player)
      .where(game_week: game_week, nfl_players: { nfl_player_type_id: NflPlayerType.find_unique_with(type) })
      .order(points: :desc)
      .limit(number)
  end

  def find_player(users, _name, game_week)
    users.each do |user|
      user.team_for_game_week(game_week).match_players do |match_player|
        return user if match_player.nfl_player.name
      end
    end
    nil
  end<commit_msg>Closes #55 No idea why this works, but hey-ho<commit_after>class GameDaysController < ApplicationController
  BEST_TEAM_SIZE = 10

  BEST_TEAM_DEFINITION = {
    NflPlayerType::QB => {
      min: 2,
      max: 2
    },
    NflPlayerType::RB => {
      min: 2,
      max: 2
    },
    NflPlayerType::WR => {
      min: 2,
      max: 3
    },
    NflPlayerType::TE => {
      min: 1,
      max: 2
    },
    NflPlayerType::K => {
      min: 1,
      max: 2
    },
    NflPlayerType::D => {
      min: 1,
      max: 1
    }
  }

  GAME_WEEK_KEY = :game_week
  PLAYER_ID_KEY = :player_id

  def show_no_game_week
    redirect_to "/game_day/#{WithGameWeek.current_game_week}"
  end

  def show
    validate_all_parameters([GAME_WEEK_KEY], params)
    @page_game_week = params[GAME_WEEK_KEY].to_i
    @current_game_week = WithGameWeek.current_game_week
    @player_data = return_nfl_player_and_team_data
    @users = User.all.sort_by { |u| -u.team_for_game_week(@page_game_week).points }
    @best_team = find_ten_best_players(@page_game_week)
    @last_comment = get_timestamp_of_last_comment
  end

  def which_team_has_player
    validate_all_parameters([GAME_WEEK_KEY, PLAYER_ID_KEY], params)
    nfl_player = NflPlayer.find(params[PLAYER_ID_KEY])
    game_week = params[GAME_WEEK_KEY]

    respond_to do |format|
      format.json do
        render json: {
          data: form_team_data(nfl_player, game_week)
        }
      end
    end
  end

  private

  def form_team_data(nfl_player, game_week)
    game_week_team_players = GameWeekTeamPlayer.where(
      match_player: nfl_player.player_for_game_week(game_week)
    )
    return nil if game_week_team_players.empty?

    found_user = game_week_team_players.first.game_week_team.user
    {
      name: found_user.name,
      team_name: found_user.team_name,
      playing: game_week_team_players.first.playing
    }
  end

  def find_ten_best_players(game_week)
    Rails.logger.info "Finding best team for game week #{game_week}"
    game_week = GameWeek.find_unique_with(game_week)

    player_arrays = BEST_TEAM_DEFINITION.keys.reduce(best: [], remaining: []) do |player_arrays_acc, player_type|
      add_players_of_type(player_type, player_arrays_acc, game_week)
    end

    best_players = player_arrays[:best]
    remaining_players = player_arrays[:remaining]

    best_players.concat(remaining_players.sort_by(&:points).last(BEST_TEAM_SIZE - best_players.size))
  end

  def add_players_of_type(player_type, player_arrays, game_week)
    Rails.logger.info "Adding players for #{player_type}"
    best_players = player_arrays[:best]
    remaining_players = player_arrays[:remaining]

    max = BEST_TEAM_DEFINITION[player_type][:max]
    min = BEST_TEAM_DEFINITION[player_type][:min]

    best_of_type = find_top_of_type(player_type, max, game_week)
    best_players.concat(best_of_type[0 .. (min - 1)])
    remaining_players.concat(best_of_type[min .. (max - 1)])

    Rails.logger.info "Best is now: #{best_players.map { |player| player.nfl_player.name }}"
    Rails.logger.info "Remaining is now: #{remaining_players.map { |player| player.nfl_player.name }}"
    { best: best_players, remaining: remaining_players }
  end

  def find_top_of_type(type, number, game_week)
    MatchPlayer
      .joins(:nfl_player)
      .where(game_week: game_week, nfl_players: { nfl_player_type_id: NflPlayerType.find_unique_with(type) })
      .order(points: :desc)
      .limit(number)
  end

  def find_player(users, _name, game_week)
    users.each do |user|
      user.team_for_game_week(game_week).match_players do |match_player|
        return user if match_player.nfl_player.name
      end
    end
    nil
  end
{'fraction_non_alphanumeric': 0.09675208989995888, 'fraction_numerical': 0.004522406468411676, 'mean_word_length': 3.5527136618839674, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5987103', 'n_tokens_mistral': 2759, 'n_tokens_neox': 2649, 'n_words': 434}