<commit_before>class SolvedChallenge < FeedItem
  validates :challenge_id, :flag, :division_id, :player, :share_number, presence: true
  validate :user_has_not_solved_challenge, :challenge_is_open, :game_is_open

  after_save :award_achievement, :open_next_challenge

  belongs_to :flag
  belongs_to :division

  def description
    %(Solved challenge "#{challenge.category.name} #{challenge.point_value}")
  end

  def icon
    'ok'
  end

  def challenge_is_open
    errors.add(:challenge, 'must be open.') unless challenge.open?(player.division)
  end

  def game_is_open
    errors.add(:base, 'The game must be open.') unless challenge.category.game.open?
  end

  def user_has_not_solved_challenge
    if player.solved_challenges.where('challenge_id = ?', challenge.id).count > 0
      errors.add(:base, 'This player has already solved this challenge.')
    end
  end

  def award_achievement
    if Game.instance.solved_challenges.all.count == 1 # if this is the first solved challenge
      Achievement.create(player: player, text: 'First Blood!')
    end
    name = challenge.achievement_name
    Achievement.create(player: player, text: name) unless name.blank?
  end

  def open_next_challenge<commit_msg>Show the challenge name instead of the category name in the solved challenge description.<commit_after>class SolvedChallenge < FeedItem
  validates :challenge_id, :flag, :division_id, :player, :share_number, presence: true
  validate :user_has_not_solved_challenge, :challenge_is_open, :game_is_open

  after_save :award_achievement, :open_next_challenge

  belongs_to :flag
  belongs_to :division

  def description
    %(Solved challenge "#{challenge.name} #{challenge.point_value}")
  end

  def icon
    'ok'
  end

  def challenge_is_open
    errors.add(:challenge, 'must be open.') unless challenge.open?(player.division)
  end

  def game_is_open
    errors.add(:base, 'The game must be open.') unless challenge.category.game.open?
  end

  def user_has_not_solved_challenge
    if player.solved_challenges.where('challenge_id = ?', challenge.id).count > 0
      errors.add(:base, 'This player has already solved this challenge.')
    end
  end

  def award_achievement
    if Game.instance.solved_challenges.all.count == 1 # if this is the first solved challenge
      Achievement.create(player: player, text: 'First Blood!')
    end
    name = challenge.achievement_name
    Achievement.create(player: player, text: name) unless name.blank?
  end

  def open_next_challenge
{'fraction_non_alphanumeric': 0.08970764917901482, 'fraction_numerical': 0.0016019223067681217, 'mean_word_length': 4.337606837606837, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '7563782', 'n_tokens_mistral': 848, 'n_tokens_neox': 773, 'n_words': 208}