Class: Contest::Driver::CodeforcesDriver

Inherits:
DriverBase show all
Defined in:
lib/contest/driver/codeforces_driver.rb

Instance Attribute Summary

Attributes inherited from DriverBase

#args, #config, #options

Instance Method Summary collapse

Methods inherited from DriverBase

#define_options, #get_commit_message, #get_commit_message_ext, #get_opts, #initialize, #submit

Methods inherited from DriverEvent

#initialize, #off, #on, #trigger

Constructor Details

This class inherits a constructor from Contest::Driver::DriverBase

Instance Method Details

#get_descObject



44
45
46
# File 'lib/contest/driver/codeforces_driver.rb', line 44

def get_desc
  "Codeforces (URL: http://codeforces.com/)"
end

#get_opts_extObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/contest/driver/codeforces_driver.rb', line 19

def get_opts_ext
  define_options do
    opt(
      :contest_id,
      "Contest ID (Ex: 100, 234, etc...)",
      :type => :string,
      :required => true,
    )
    opt(
      :problem_id,
      "Problem ID (Ex: A, B, etc...)",
      :type => :string,
      :required => true,
    )
  end
end

#get_problem_id(options) ⇒ Object



40
41
42
# File 'lib/contest/driver/codeforces_driver.rb', line 40

def get_problem_id(options)
  "#{options[:contest_id]}#{options[:problem_id]}"
end

#get_site_nameObject



36
37
38
# File 'lib/contest/driver/codeforces_driver.rb', line 36

def get_site_name
  "Codeforces"
end

#initialize_extObject



13
14
15
16
17
# File 'lib/contest/driver/codeforces_driver.rb', line 13

def initialize_ext
  @client = Mechanize.new {|agent|
    agent.user_agent_alias = 'Windows IE 7'
  }
end

#resolve_language(label) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/contest/driver/codeforces_driver.rb', line 48

def resolve_language(label)
  case label
  when "clang"
    return "10"
  when "cpp"
    return "1"
  when "cpp11"
    return "16"
  when "cs"
    return "9"
  when "dlang"
    return "28"
  when "golang"
    return "32"
  when "haskell"
    return "12"
  when "java"
    return "23"
  when "ocaml"
    return "19"
  when "delphi"
    return "3"
  when "pascal"
    return "4"
  when "perl"
    return "13"
  when "php"
    return "6"
  when "python2"
    return "7"
  when "python3"
    return "31"
  when "ruby"
    return "8"
  when "scala"
    return "20"
  else
    abort "unknown languag"
  end
end

#submit_extObject



89
90
91
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/contest/driver/codeforces_driver.rb', line 89

def submit_ext()
  # start
  trigger 'start'
  contest_id = @options[:contest_id]
  problem_id = @options[:problem_id]

  # login
  trigger 'before_login'
   = @client.get 'http://codeforces.com/enter'
  .form_with(:action => '') do |form|
    form.handle = @config["user"]
    form.password = @config["password"]
  end.submit
  trigger 'after_login'

  # submit
  trigger 'before_submit', @options
  # retry once
  retries = 1
  begin
    submit_page = @client.get "http://codeforces.com/contest/#{contest_id}/submit"
    res_page = submit_page.form_with(:class => 'submit-form') do |form|
      form. = problem_id
      form.programTypeId = @options[:language]
      form.source = File.read(@options[:source])
    end.submit
  rescue => e
    raise if retries == 0
    retries -= 1
    # may not be registered practice
    if /submittedProblemIndex/ =~ e.to_s
      contest_page = @client.get "http://codeforces.com/contest/#{contest_id}"
      contest_page.form_with(:action => "") do |form|
        flag_need_to_register = false
        form.field_with(:value => "registerForPractice") do |field|
          flag_need_to_register = true
        end
        form.click_button if flag_need_to_register
      end
      sleep 3
      retry
    end
    raise
  end
  trigger 'after_submit'

  # need to get the newest waiting submissionId
  submission_id = get_submission_id(res_page.body)
  trigger(
    'before_wait',
    {
      :submission_id => submission_id,
    }
  )

  # wait result
  status = get_status_wait(contest_id, submission_id)
  trigger(
    'after_wait',
    {
      :submission_id => submission_id,
      :status => status,
      :result => get_commit_message(status),
    }
  )

  trigger 'finish'
  get_commit_message(status)
end