Class: Contest

Inherits:
Object
  • Object
show all
Defined in:
lib/atcoder_greedy/lib/contest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, **options) ⇒ Contest

Returns a new instance of Contest.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/atcoder_greedy/lib/contest.rb', line 10

def initialize(url, **options)
  if options[:language] != ''
    @language = options[:language]
  else
    @language = AtcoderGreedy.config[:language]
  end
  @url = url

  set_agent
  set_contest_info(options[:problems])
  set_directories(options[:directory])

  create_inputs unless options[:no][:input]
  create_templates(options[:template]) unless options[:no][:template]

  puts 'Set up done. Go for it!'
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



8
9
10
# File 'lib/atcoder_greedy/lib/contest.rb', line 8

def dir
  @dir
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/atcoder_greedy/lib/contest.rb', line 8

def name
  @name
end

#problemsObject

Returns the value of attribute problems.



8
9
10
# File 'lib/atcoder_greedy/lib/contest.rb', line 8

def problems
  @problems
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/atcoder_greedy/lib/contest.rb', line 8

def url
  @url
end

Instance Method Details

#create_inputsObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/atcoder_greedy/lib/contest.rb', line 67

def create_inputs
  print 'Create inputs ... '
  @problems.each do |problem|
    # take input and output params from url and save to file
    html = @agent.get(@url + problem[:path]).content.toutf8
    doc = Nokogiri::HTML.parse(html, nil, 'utf8')
    in_file = File.new(@dir + "/input_#{problem[:name]}.txt", 'w')

    params = doc.xpath('//pre')
    params.shift
    params.each_with_index do |p, i|
      if i % 2 == 0
        in_file.puts "-- Example #{i/2}"
        in_file.puts "#{p.text.gsub(/\r\n?/, "\n").strip}"
      else
        in_file.puts "-- Answer #{(i-1)/2}"
        in_file.puts "#{p.text.gsub(/\r\n?/, "\n").strip}"
      end
    end

    in_file.close
  end
  puts 'Done!'
end

#create_templates(option_template) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/atcoder_greedy/lib/contest.rb', line 92

def create_templates(option_template)
  print 'Create Templates ... '
  if option_template == ''
    # use user default or system default template
    if AtcoderGreedy.config[:default_template][:"#{@language}"] != ''
      solve_template = open(AtcoderGreedy.config[:default_template][:"#{@language}"], &:read)
    else
      solve_template = open(File.dirname(__dir__) + '/templates' + "/#{@language}/solve.#{@language}", &:read)
    end
  else
    # use option_template
    template_path = GreedyTemplate.get_template_path(option_template)
    if template_path.nil?
      raise "ERROR: Template #{option_template} doesn't found"
    else
      solve_template = open(template_path, &:read)
    end
  end

  @problems.each_with_index do |problem|
    solve_file_content = solve_template.clone
    solve_file_content.gsub!(/DATE/, Time.now.strftime('%F'))
    solve_file_content.gsub!(/CONTEST/, @name.upcase)
    solve_file_content.gsub!(/PROBLEM/, problem[:name])
    solve_file = File.new(@dir + "/#{problem[:name]}.#{@language}", 'w')
    solve_file.print solve_file_content
    solve_file.close
  end
  puts 'Done!'
end

#set_agentObject



28
29
30
31
32
# File 'lib/atcoder_greedy/lib/contest.rb', line 28

def set_agent
  atcoder = Atcoder.new
  atcoder.(@url)
  @agent = atcoder.agent
end

#set_contest_info(option_problems) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/atcoder_greedy/lib/contest.rb', line 34

def set_contest_info(option_problems)
  print 'Set contest info ... '
  @name = URI.parse(@url).host.split('.').first
  html = @agent.get(@url + '/assignments').content.toutf8
  doc = Nokogiri::HTML.parse(html, nil, 'utf8')

  all_problems = []
  task_ids = []
  doc.xpath('//tbody').each do |tbody|
    tbody.xpath('.//a[starts-with(@href,"/submit")]').each do |a|
      task_ids.push(CGI.parse(URI.parse(a.attributes['href'].value).query)['task_id'].first)
    end
    all_problems = tbody.xpath('.//a[@class="linkwrapper"]')
  end

  @problems = []
  if all_problems.nil?
    raise 'Failed to get info. Do you participate this contest?'
  else
    until all_problems.empty?
      path = all_problems[0].attributes['href'].value
      pro = all_problems.select { |l| l.attributes['href'].value == path }
      all_problems = all_problems.reject { |l| l.attributes['href'].value == path }
      name = pro[0].inner_text
      if option_problems.empty? || (!option_problems.empty? && option_problems.include?(name))
        @problems.push(name: pro[0].inner_text, path: path, task_id: task_ids.shift)
        print "#{name} "
      end
    end
  end
  puts 'Done!'
end

#set_directories(directory) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/atcoder_greedy/lib/contest.rb', line 123

def set_directories(directory)
  print 'Set contest directory ... '
  if directory == ''
    FileUtils.mkdir(@name)
    @dir = "./#{@name}"
  else
    if Dir.exists?(directory)
      @dir = directory
    else
      raise "ERROR: Directory doesn't exists:#{@dir}"
    end
  end
  puts 'Done!'
end