From 2a497830bb23e1837220d22d5a0d30900ecece92 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Thu, 14 Nov 2019 15:48:50 +0800 Subject: [PATCH] =?UTF-8?q?OJ=E8=AF=84=E6=B5=8B=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hack_user_lastest_codes_controller.rb | 64 ++++++++++++++++++- app/controllers/hacks_controller.rb | 7 +- app/models/hack.rb | 2 +- app/models/hack_user_lastest_code.rb | 4 +- .../show.json.jbuilder | 4 -- config/routes.rb | 4 ++ ...us_end_time_for_hack_user_lastest_code.rb} | 1 + 7 files changed, 74 insertions(+), 12 deletions(-) rename db/migrate/{20191113061858_add_status_end_time_for_hack_user_lastest_code.rb => 20191113061859_add_status_end_time_for_hack_user_lastest_code.rb} (92%) diff --git a/app/controllers/hack_user_lastest_codes_controller.rb b/app/controllers/hack_user_lastest_codes_controller.rb index 83a9d457e..7604b0558 100644 --- a/app/controllers/hack_user_lastest_codes_controller.rb +++ b/app/controllers/hack_user_lastest_codes_controller.rb @@ -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 diff --git a/app/controllers/hacks_controller.rb b/app/controllers/hacks_controller.rb index 4370b5f3b..0922a8b8c 100644 --- a/app/controllers/hacks_controller.rb +++ b/app/controllers/hacks_controller.rb @@ -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 # 首页 diff --git a/app/models/hack.rb b/app/models/hack.rb index e6486401d..3fbe1e9f8 100644 --- a/app/models/hack.rb +++ b/app/models/hack.rb @@ -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 diff --git a/app/models/hack_user_lastest_code.rb b/app/models/hack_user_lastest_code.rb index f21fa051d..e63353bba 100644 --- a/app/models/hack_user_lastest_code.rb +++ b/app/models/hack_user_lastest_code.rb @@ -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 diff --git a/app/views/hack_user_lastest_codes/show.json.jbuilder b/app/views/hack_user_lastest_codes/show.json.jbuilder index f7a543ae4..370797750 100644 --- a/app/views/hack_user_lastest_codes/show.json.jbuilder +++ b/app/views/hack_user_lastest_codes/show.json.jbuilder @@ -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 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 1f5ecc772..af27c53e2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20191113061858_add_status_end_time_for_hack_user_lastest_code.rb b/db/migrate/20191113061859_add_status_end_time_for_hack_user_lastest_code.rb similarity index 92% rename from db/migrate/20191113061858_add_status_end_time_for_hack_user_lastest_code.rb rename to db/migrate/20191113061859_add_status_end_time_for_hack_user_lastest_code.rb index 36ff8e3c3..a9b12f7c0 100644 --- a/db/migrate/20191113061858_add_status_end_time_for_hack_user_lastest_code.rb +++ b/db/migrate/20191113061859_add_status_end_time_for_hack_user_lastest_code.rb @@ -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