<commit_before>class TeamTemplate < ActiveRecord::Base
  has_many :hero_teams, dependent: :destroy

  def add_hero! **_h
    # Example: team_template.add_hero name: 'karin_6'
    hero = Hero.find_by static_name: _h[:name]
    raise ActiveRecord::RecordNotFound, "Hero #{_h[:name]} not found" if hero == nil

    ht = HeroTeam.find_or_initialize_by hero_id: hero.id, team_template_id: self.id
    ht.save!
  end

  def self.rebuild **_h
    # Example: TeamTemplate.build name: 'test_team_1', description: 'just test', heros: ['karin_6', leo_6']

    raise ArgumentError, 'Template name must be supplied' if _h[:name] == nil
    raise ArgumentError, 'Please supply at least 1 hero in a team' if _h[:heros] == nil or _h[:heros].length == 0
    raise ArgumentError, 'At most only 5 heros can be included in a team' if _h[:heros].length > 5

    t = TeamTemplate.find_or_initialize_by name: _h[:name]
    t.description = _h[:description]
    t.save!

    ActiveRecord::Base.transaction do
      HeroTeam.where(team_template_id: t.id).destroy_all
      _h[:heros].each do |_name|
        t.add_hero! name: _name
      end
    end
  end

  def list_heros _method = :object
    ht = HeroTeam.where(team_template_id: self.id)
                 .joins('INNER JOIN heros
                           ON heros.id = hero_id')
                 .select('heros.*')
                 
    case _method
    when :object
      return ht
    else
      return ht.pluck(_method)
    end
  end
end<commit_msg>Team Template importer completed.<commit_after>class TeamTemplate < ActiveRecord::Base
  has_many :hero_teams, dependent: :destroy

  def add_hero! **_h
    # Example: team_template.add_hero name: 'karin_6'
    hero = Hero.find_by static_name: _h[:name]
    raise ActiveRecord::RecordNotFound, "Hero #{_h[:name]} not found" if hero == nil

    ht = HeroTeam.find_or_initialize_by hero_id: hero.id, team_template_id: self.id
    ht.save!
  end

  def self.rebuild **_h
    # Example: TeamTemplate.build name: 'test_team_1', description: 'just test', heros: ['karin_6', leo_6']

    raise ArgumentError, 'Template name must be supplied' if _h[:name] == nil
    raise ArgumentError, 'Please supply at least 1 hero in a team' if _h[:heros] == nil or _h[:heros].length == 0
    raise ArgumentError, 'At most only 5 heros can be included in a team' if _h[:heros].length > 5

    t = TeamTemplate.find_or_initialize_by name: _h[:name]
    t.description = _h[:description]
    t.save!

    ActiveRecord::Base.transaction do
      HeroTeam.where(team_template_id: t.id).destroy_all
      _h[:heros].each do |_name|
        t.add_hero! name: _name
      end
    end
  end

  def list_heros _method = :object
    ht = HeroTeam.where(team_template_id: self.id)
                 .joins('INNER JOIN heros
                           ON heros.id = hero_id')
                 .select('heros.*')
                 
    case _method
    when :object
      return ht
    else
      return ht.pluck(_method)
    end
  end

  def self.rebuild_database!
    ActiveRecord::Base.transaction do
      Utility.shut_up do
        TeamTemplate.destroy_all

        import!
      end
    end

    return TeamTemplate.count
  end

private
  def self.import!
    RemoteTable.new('https://raw.githubusercontent.com/gbudiman/skdb/master/db/team_templates.csv').each do |r|
      heros = Array.new

      (0..4).each do |i|
        cat = r["h#{i}"]
        if cat.strip.length > 0
          heros.push cat
        end
      end

      TeamTemplate.rebuild name: r['name'],
                           heros: heros,
                           description: r['description']
    end
  end
end
{'fraction_non_alphanumeric': 0.10343878954607978, 'fraction_numerical': 0.005226960110041265, 'mean_word_length': 2.5232558139534884, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 1, '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': '6930153', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1167, 'n_words': 318}