class TeamImporter

Attributes

teams[R]

Public Class Methods

new() click to toggle source
# File lib/showdown-team-import.rb, line 17
def initialize
  @teams = Array.new
end

Public Instance Methods

import(team_import) click to toggle source
# File lib/showdown-team-import.rb, line 21
def import team_import
  team = Team.new(String.new, String.new, String.new)
  line_count = 0
  in_team = false

  team_import.each do |line, count|
    next if line.equal? team_import.last # I haven't found any way to make this not an error except adding more shitty checks
    if in_team
      team.content += line unless team.content.empty? && line.strip.empty? # Beginning of file

      if line.strip == ''
        line_count += 1
        if line_count == 2 # 2 empty lines = end of current team
          @teams.push(team) # add team to array and reset everything
          team = Team.new(String.new, String.new, String.new)
          in_team = false
          line_count = 0
        end
      else # we already appended line earlier, so just resetting linecount
        line_count = 0
      end
    elsif line[0..2] == "===" && line.chomp.reverse[0..2] == "===" # beginning of team
      in_team = true
      name_and_tier = line[4..-5]
      bracket_index = name_and_tier.index(']') # 10/10 code I know
      format = name_and_tier[1..bracket_index-1] if !bracket_index.nil? && bracket_index > 2
      format = "others" unless format

      if !bracket_index.nil? && !name_and_tier[bracket_index + 1..-1].strip.empty?
        team_name = name_and_tier[bracket_index + 1..-1]
      elsif !name_and_tier.strip.empty? && bracket_index.nil?
        team_name = name_and_tier
      else
        team_name = "Untitled [#{SecureRandom.hex[0..5]}]"
      end

      team.name = team_name
      team.tier = format
    else
      raise ParserError.new(line), "Error parsing backup file"
    end
  end

  @teams.push(team) if @teams.empty?
end