OJ评测模式

dev_daiao
daiao 5 years ago
parent 2a38bed1ad
commit 2a497830bb

@ -1,15 +1,73 @@
class HackUserLastestCodesController < ApplicationController
before_action :require_login
before_action :find_user_hack, only: [:show]
before_action :require_login, except: [:listen_result]
before_action :find_user_hack, only: [:show, :code_debug]
def show
def show;end
# 调试代码
def code_debug
exec_mode = "debug"
error_status = 501
error_msg = "debug_error"
oj_evaluate exec_mode, error_status, error_msg
render_ok
end
# 提交
def code_submit
exec_mode = "all"
error_status = 502
error_msg = "submit_error"
oj_evaluate exec_mode, error_status, error_msg
render_ok
end
# 接收中间件返回结果接口
def listen_result
logger.info("###########listen_result#{params}")
# debug模式
if params[:execMode] == "debug"
msg = JSON.parse(params[:msg])
if msg.present?
HackUserDebug.create(input: msg['input'], output: msg['output'], error_line: msg['output'])
end
# 只有编译出错时,才正则匹配错误行数
if params[:status] == "-4"
language = Hack.find_by_identifier(params[:tpiID]).language
regular_match_error_line params[:outPut], language
end
end
end
private
def find_user_hack
@my_hack = HackUserLastestCode.find_by(identifier: params[:identifier])
@hack = @my_hack.hack
end
def oj_evaluate exec_mode, error_status, error_msg
request_url = "#{edu_setting('cloud_bridge')}/bridge/ojs/evaluate"
testCases = Base64.urlsafe_encode64({input: params[:input]}.to_json)
debug_params = {execMode: exec_mode,
tpiID: @my_hack.identifier,
testCases: testCases,
platform: @my_hack.language,
codeFileContent: @my_hack.code,
timeLimit: @hack.time_limit, sec_key: Time.now.to_s}
interface_post request_url, debug_params, error_status, error_msg
end
# 正则错误行数
def regular_match_error_line content, language
case language
when 'Java'
when 'C'
when 'C++'
when 'Python'
end
end
end

@ -8,15 +8,16 @@ class HacksController < ApplicationController
def start
user_hack = @hack.hack_user_lastest_codes.mine(current_user.id)
identifier =
if user_hack
if user_hack.present?
user_hack.identifier
else
user_identifier = generate_identifier HackUserLastestCode, 12
user_code = {user_id: current_user.id, code: @hack.code, identifier: user_identifier}
user_code = {user_id: current_user.id, code: @hack.code,
identifier: user_identifier, language: @hack.language}
@hack.hack_user_lastest_codes.create!(user_code)
user_identifier
end
render_ok({identifier: identifier})
render_ok(data: {identifier: identifier})
end
# 首页

@ -19,7 +19,7 @@ class Hack < ApplicationRecord
if hack_codes.count == 1
hack_codes.first.language
else
hack_codes.pluck(:language)
hack_codes.pluck(:language).first
end
end

@ -1,8 +1,10 @@
class HackUserLastestCode < ApplicationRecord
# passed 用户之前评测是否通过
# status: 最新评测状态 -1测试用例结果不匹配; 0: 未评测; 1评测通过;2 评测超时;3 创建pod失败; 4 编译失败;5 执行失败
# 编程题最新代码
belongs_to :hack, counter_cache: true
scope :mine, ->(author_id){ find_by(user_id: author_id)}
scope :mine, ->(author_id){ find_by(user_id: author_id) }
scope :passed, -> {where(status: 1)}
end

@ -9,8 +9,4 @@ end
json.test_case do
json.input @hack.input_test_case
end
json.user_code do
json.code @my_hack.code
end

@ -39,6 +39,10 @@ Rails.application.routes.draw do
end
member do
post :publish
get :start
get :code_debug
get :code_submit
match :listen_result, :via => [:get, :post]
end
end

@ -4,6 +4,7 @@ class AddStatusEndTimeForHackUserLastestCode < ActiveRecord::Migration[5.2]
add_column :hack_user_lastest_codes, :pass_time, :timestamp
add_column :hack_user_lastest_codes, :status, :integer, :default => 0
add_column :hack_user_lastest_codes, :identifier, :string
add_column :hack_user_lastest_codes, :language, :string
add_column :hacks, :hack_user_lastest_codes_count, :integer, :default => 0
add_column :hacks, :pass_num, :integer, :default => 0
add_column :hacks, :submit_num, :integer, :default => 0
Loading…
Cancel
Save