Class: AtcoderGreedy::Command

Inherits:
Thor
  • Object
show all
Defined in:
lib/atcoder_greedy/command.rb,
lib/atcoder_greedy/command/test.rb,
lib/atcoder_greedy/command/config.rb,
lib/atcoder_greedy/command/create.rb,
lib/atcoder_greedy/command/submit.rb,
lib/atcoder_greedy/command/destroy.rb,
lib/atcoder_greedy/command/template.rb

Instance Method Summary collapse

Instance Method Details

#configObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
# File 'lib/atcoder_greedy/command/config.rb', line 9

def config
  languages = Languages::ALL_LANGUAGES
  config_path = Dir.home + '/.atcoder_greedy'
  if Dir.exists?(config_path)
    puts "Your current user_id is #{AtcoderGreedy.config[:user_id]} and current language is [#{AtcoderGreedy.config[:language]}]."
  else
    Dir.mkdir(config_path)
    yml_path = AtcoderGreedy.get_config_path + '/settings.yml'
    File.open(yml_path, 'w').close
  end

  # user setting
  agent = Mechanize.new
  loop do
    print 'Input your user_id: '
    user_id = $stdin.gets.chomp!
    print 'Input your password: '
    password = $stdin.gets.chomp!
    break if user_id.size == 0 || password.size == 0

    print 'Doing test login ...'
    response = nil
    agent.get('http://abc032.contest.atcoder.jp/login') do |page|
      response = page.form_with(action: '/login') do |f|
        f.field_with(name: 'name').value = user_id
        f.field_with(name: 'password').value = password
      end.submit
    end

    if response.response['x-imojudge-simpleauth'] == 'Passed'
      puts 'OK!'
      AtcoderGreedy.configure(user_id: user_id)
      AtcoderGreedy.configure(password: password)
      break
    else
      puts 'Failed! Confirm input and try again.'
    end
  end

  # language setting
  puts "Choose default language from: #{languages}"
  print "Input languages: "
  loop do
    s = $stdin.gets.chomp!
    if languages.include?(s)
      AtcoderGreedy.configure(language: s)
      puts "Update Your default language to [#{AtcoderGreedy.config[:language]}]."
      break
    elsif s.size == 0
      break
    else
      puts "Invalid language. please try again:"
    end
  end
end

#create(contest_url) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/atcoder_greedy/command/create.rb', line 15

def create(contest_url)
  user_options = {
      no: {input: false, template: false},
      problems: [],
      directory: options[:select_directory],
      language: options[:select_language],
      template: options[:select_template]
  }

  user_options[:no][:input] = true if options[:no_input]
  user_options[:no][:template] = true if options[:no_templates]
  user_options[:problems] = options[:select_problem].split unless options[:select_problem].nil?

  contest = Contest.new(contest_url, user_options)
  # TODO: contest_infoが存在したときの処理
  File.open("#{contest.dir}/.contest_info.yml", 'w') do |f|
    info = {
        name: contest.name,
        url: contest.url,
        task: {}
    }
    contest.problems.each do |p|
      info[:task][:"#{p[:name]}"] = {
          id: p[:task_id]
      }
    end

    f.puts info.to_yaml
  end
end

#destroy(contest_name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/atcoder_greedy/command/destroy.rb', line 8

def destroy(contest_name)
  puts "Destroy ./#{contest_name} [y/n]?"
  s = $stdin.gets
  if s == 'y' || s == 'yes'
    if system("rm -r ./#{contest_name}")
      puts 'deleted.'
    else
      raise 'Runtime Error'
    end
  end
end

#submit(submit_file) ⇒ Object

TODO: 提出言語のオプション



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/atcoder_greedy/command/submit.rb', line 23

def submit(submit_file)
  print "Submit [#{submit_file}] ... "
  contest_info = YAML.load_file("./.contest_info.yml")
  problem = File.basename(submit_file, '.*')
  if contest_info[:task].include?(:"#{problem}")
    task_id = contest_info[:task][:"#{problem}"][:id]
  else
    raise "Unknown problem: #{problem}"
  end

  atcoder = Atcoder.new
  atcoder.(contest_info[:url])

  submit_url = contest_info[:url] + "/submit?task_id=#{task_id}"
  atcoder.agent.get(submit_url) do |page|
    p = page.form_with(action: "/submit?task_id=#{task_id}") do |f|
      f.field_with(name: 'source_code').value = File.open(submit_file).read
      f.field_with(name: 'task_id').value = task_id
      f.field_with(name: "language_id_#{task_id}").value = get_language_id(File.extname(submit_file))
    end.submit
    puts 'Done!'
    Launchy.open p.uri
  end
end

#templateObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/atcoder_greedy/command/template.rb', line 12

def template
  temp = GreedyTemplate.new
  if options[:add]
    temp.add(options[:add])
  elsif options[:list]
    temp.list
  elsif options[:set_default]
    temp.set_default(options[:set_default])
  elsif options[:delete]
    temp.delete(options[:delete])
  end
end

#test(problem_name) ⇒ Object



9
10
11
# File 'lib/atcoder_greedy/command/test.rb', line 9

def test(problem_name)
  TestCase.new(problem_name).validate
end