Class: Contest::Driver::Codeforces

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DriverBase

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

Methods inherited from DriverEvent

#initialize, #off, #on, #trigger

Constructor Details

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

Instance Attribute Details

#client=(value) ⇒ Object (writeonly)

Sets the attribute client

Parameters:

  • value

    the value to set the attribute client to.



187
188
189
# File 'lib/contest/driver/codeforces.rb', line 187

def client=(value)
  @client = value
end

Instance Method Details

#get_descObject



38
39
40
# File 'lib/contest/driver/codeforces.rb', line 38

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

#get_opts_extObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/contest/driver/codeforces.rb', line 13

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



34
35
36
# File 'lib/contest/driver/codeforces.rb', line 34

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

#get_site_nameObject



30
31
32
# File 'lib/contest/driver/codeforces.rb', line 30

def get_site_name
  "Codeforces"
end

#resolve_language(label) ⇒ Object



42
43
44
45
46
47
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
# File 'lib/contest/driver/codeforces.rb', line 42

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_ext(config, source_path, options) ⇒ Object



83
84
85
86
87
88
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
# File 'lib/contest/driver/codeforces.rb', line 83

def submit_ext(config, source_path, options)
  # start
  trigger 'start'
  contest_id = options[:contest_id]
  problem_id = options[:problem_id]

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

  # 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(source_path)
    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
  trigger 'before_wait',
    submission_id = get_submission_id(res_page.body)

  # wait result
  status = get_status_wait(contest_id, submission_id)
  trigger(
    'after_wait',
    {
      :submission_id => submission_id,
      :status => status,
      :result => get_commit_message($config["submit_rules"]["message"], status, options),
    }
  )

  trigger 'finish'
  get_commit_message($config["submit_rules"]["message"], status, options)
end