Class: Contest::Driver::KattisDriver

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

Instance Attribute Summary

Attributes inherited from DriverBase

#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



49
50
51
# File 'lib/contest/driver/kattis_driver.rb', line 49

def get_desc
  "Kattis (URL: https://open.kattis.com/)"
end

#get_opts_extObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/contest/driver/kattis_driver.rb', line 28

def get_opts_ext
  define_options do
    opt(
      :contest_id,
      "Contest ID (Ex: open, kth, liu, etc...)",
      :type => :string,
      :required => false,
    )
    opt(
      :problem_id,
      "Problem ID (Ex: aaah, listgame2, etc...)",
      :type => :string,
      :required => true,
    )
  end
end

#get_problem_id(options) ⇒ Object



53
54
55
# File 'lib/contest/driver/kattis_driver.rb', line 53

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

#get_site_nameObject



45
46
47
# File 'lib/contest/driver/kattis_driver.rb', line 45

def get_site_name
  "Kattis"
end

#get_status_wait(submission_id, subdomain) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/contest/driver/kattis_driver.rb', line 140

def get_status_wait(submission_id, subdomain)
  submission_id = submission_id.to_s
  # Wait for result
  12.times do
    sleep 10
    submission_page = @client.get "https://#{subdomain}.kattis.com/submission?id=#{submission_id}"
    status = get_submission_status(submission_id, submission_page.body)
    return status unless status == 'Running'
    trigger 'retry'
  end
  trigger 'timeout'
  return 'timeout'
end

#get_submission_id(body) ⇒ Object



154
155
156
157
# File 'lib/contest/driver/kattis_driver.rb', line 154

def get_submission_id(body)
  doc = Nokogiri::HTML(body)
  return doc.xpath('//a[starts-with(@href,"submission")]')[0].inner_text().strip
end

#get_submission_status(submission_id, body) ⇒ Object



159
160
161
162
# File 'lib/contest/driver/kattis_driver.rb', line 159

def get_submission_status(submission_id, body)
  doc = Nokogiri::HTML(body)
  return doc.xpath('//td[@class="status"]/span').inner_text().strip
end

#initialize_extObject



22
23
24
25
26
# File 'lib/contest/driver/kattis_driver.rb', line 22

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

#resolve_language(label) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/contest/driver/kattis_driver.rb', line 57

def resolve_language(label)
  case label
  when 'cpp'
    return '1'
  when 'c'
    return '2'
  when 'java'
    return '3'
  when 'python2'
    return '6'
  when 'python3'
    return '8'
  when 'cs'
    return '9'
  when 'golang'
    return '10'
  when 'objc'
     return '11'
  else
    abort 'unknown language'
  end
end

#submit_extObject



80
81
82
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
# File 'lib/contest/driver/kattis_driver.rb', line 80

def submit_ext
  trigger 'start'
  problem_id = @options[:problem_id]

  if (@options[:contest_id])
    subdomain = @options[:contest_id]
  else
    subdomain = 'open'
  end

  # Login
  trigger 'before_login'
   = @client.get "https://#{subdomain}.kattis.com/login?email_login=true"
  .form_with(:action => 'login?email_login=true') do |form|
    form.user = @config['user']
    form.password = @config['password']
  end.submit
  trigger 'after_login'

  # Submit
  trigger 'before_submit', @options
  submit_page = @client.get "https://#{subdomain}.kattis.com/submit"
  res_page = submit_page.form_with(:name => 'upload') do |form|
    form.problem = problem_id
    form['lang'] = @options[:language]
    form.sub_code = File.read(@options[:source])
    # Use file name as main class for Java
    if (@options[:language] == resolve_language('java'))
      form['mainclass'] = @options[:source].rpartition('.')[0]
    end
    form.submit(form.button_with(:name => 'submit'))
  end.submit
  trigger 'after_submit'

  # Result
  trigger 'before_wait'
  user = @config['user']
  doc = Nokogiri::HTML(res_page.body)
  # Check for error messages
  error = doc.xpath('//p[@class="error"]')[0];
  if ((/Problem ID not found in database./ =~ error) ||
      (/Problem-id inte funnet i databasen./ =~ error))
    abort "Problem ID not found in database."
  end
  submissions_page = @client.get "https://#{subdomain}.kattis.com/users/#{user}?show=submissions"
  submission_id = get_submission_id(submissions_page.body)
  status = get_status_wait(submission_id, subdomain)
  trigger(
    'after_wait',
    {
      :submission_id => submission_id,
      :status => status,
      :result => get_commit_message(status),
    }
  )

  trigger 'finish'
  get_commit_message(status)
end