dev_daiao
daiao 5 years ago
parent 2a497830bb
commit cf1fe0aab3

@ -1,9 +1,16 @@
class HackUserLastestCodesController < ApplicationController
before_action :require_login, except: [:listen_result]
before_action :find_user_hack, only: [:show, :code_debug]
before_action :find_user_hack, only: [:show, :code_debug, :update_code, :listen_result]
before_action :update_user_hack_status, only: [:code_debug, :code_submit]
before_action :require_auth_identity, only: [:update_code]
before_action :require_manager_identity, only: [:update_code]
def show;end
def update_code
@my_hack.update_attribute(:code, params[:code])
end
# 调试代码
def code_debug
exec_mode = "debug"
@ -15,7 +22,7 @@ class HackUserLastestCodesController < ApplicationController
# 提交
def code_submit
exec_mode = "all"
exec_mode = "normal"
error_status = 502
error_msg = "submit_error"
oj_evaluate exec_mode, error_status, error_msg
@ -23,24 +30,35 @@ class HackUserLastestCodesController < ApplicationController
end
# 接收中间件返回结果接口
# 调试模式: status 0 表示评测无错误,其他 表示错误(如编译出错,执行出错,超时等)
def listen_result
logger.info("###########listen_result#{params}")
# debug模式
if params[:execMode] == "debug"
begin
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
error_line=
if params[:status] == "-4"
regular_match_error_line params[:outPut], @my_hack.hack.language
end
# debug 与submit 公用的参数
ds_params = {input: msg['input'], output: msg['output'], hack_id: @hack.id, user_id: @my_hack.user_id,
error_line: error_line, status: params[:status], error_msg: params[:outPut],
execute_time: params[:executeTime], execute_memory: params[:executeMem]}
ActiveRecord::Base.transaction do
# debug模式与submit模式
if params[:execMode] == "debug"
save_debug_data ds_params
elsif params[:execMode] == "normal"
save_submit_data ds_params
end
# 评测完成后,还原评测中的状态
@my_hack.update_attribute(:submit_status, 0)
end
rescue Exception => e
end
end
end
private
def find_user_hack
@ -58,15 +76,65 @@ class HackUserLastestCodesController < ApplicationController
codeFileContent: @my_hack.code,
timeLimit: @hack.time_limit, sec_key: Time.now.to_s}
interface_post request_url, debug_params, error_status, error_msg
# 每次评测提交数增加
@hack.increment!(:submit_num)
end
# 正则错误行数
def regular_match_error_line content, language
case language
when 'Java'
when 'C'
when 'C++'
content.scan(/.java.\d+/).map{|s| s.match(/\d+/)[0].to_i}.min
when 'C', 'C++'
content.scan(/\d:\d+: error/).map{|s| s.match(/\d+/)[0]}.min
when 'Python'
content.scan(/line \d+/).map{|s| s.match(/\d+/)[0].to_i}.min
end
end
# 存储debug数据
def save_debug_data debug_params
if @my_hack.hack_user_debug.present?
@my_hack.hack_user_debug.update_attributes!(debug_params)
else
@my_hack.hack_user_debug.create!(debug_params)
end
end
# 存储submit数据
def save_submit_data submit_params
# 通关
if submit_params[:status] == "0"
# 编程题已经发布,且之前未通关奖励积分
if @hack.status == 1 && !@my_hack.passed?
reward_attrs = { container_id: game.id, container_type: 'Hack', score: @hack.score }
RewardGradeService.call(@my_hack.user, reward_attrs)
RewardExperienceService.call(@my_hack.user, reward_attrs)
# 评测完成更新通过数
@hack.increment!(:pass_num)
@my_hack.update_attribute(:passed, true)
end
end
# 创建用户评测记录
@my_hack.hack_user_codes.create!(submit_params)
end
# 调试或提交改变状态
def update_user_hack_status
@my_hack.update_attribute(:submit_status, 1)
end
# 只有自己才能改动代码
def require_identity
if @my_hack.user_id != current_user.id
tip_exception(403, "..")
end
end
# 老师、自己、管理可以评测他人的编程题
def require_manager_identity
unless current_user.certification_teacher? || admin_or_business? || @my_hack.user_id == current_user.id
tip_exception(403, "..")
end
end

@ -90,12 +90,16 @@ class HacksController < ApplicationController
private
# 实名认证老师,管理员与运营人员权限
def require_teacher_identity
current_user.certification_teacher? || admin_or_business?
unless current_user.certification_teacher? || admin_or_business?
tip_exception(403, "..")
end
end
# 只有自己,或者管理员才能更新
def require_auth_identity
@hack.user_id == current_user.id || admin_or_business?
unless @hack.user_id == current_user.id || admin_or_business?
tip_exception(403, "..")
end
end
def find_hack

@ -7,7 +7,6 @@ class Hack < ApplicationRecord
has_many :hack_sets, ->{order("position asc")}, :dependent => :destroy
# 代码
has_many :hack_codes, :dependent => :destroy
has_many :hack_user_codes, :dependent => :destroy
has_many :hack_user_lastest_codes, :dependent => :destroy
belongs_to :user

@ -1,2 +1,4 @@
class HackUserDebug < ApplicationRecord
belongs_to :hack
belongs_to :hack_user_lastest_code
end

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

@ -149,6 +149,7 @@ class User < ApplicationRecord
belongs_to :partner, optional: true
# OJ编程题
has_many :hacks, dependent: :destroy
has_many :hack_user_lastest_codes, dependent: :destroy
# Groups and active users
scope :active, lambda { where(status: STATUS_ACTIVE) }

@ -47,6 +47,9 @@ Rails.application.routes.draw do
end
resources :hack_user_lastest_codes, path: :myproblems, param: :identifier do
member do
post :update_code
end
end

@ -1,15 +0,0 @@
class AddStatusEndTimeForHackUserLastestCode < ActiveRecord::Migration[5.2]
def change
add_column :hack_user_lastest_codes, :passed, :boolean, :default => false
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
add_index :hack_user_lastest_codes, :user_id, name: "user_index"
add_index :hack_user_lastest_codes, :hack_id, name: "hack_index"
add_index :hack_user_lastest_codes, :identifier, unique: true
end
end

@ -0,0 +1,27 @@
class AddStatusEndTimeForHackUserLastestCode < ActiveRecord::Migration[5.2]
def change
# add_column :hack_user_lastest_codes, :passed, :boolean, :default => false
# add_column :hack_user_lastest_codes, :pass_time, :timestamp
# add_column :hack_user_lastest_codes, :identifier, :string
# add_column :hack_user_lastest_codes, :language, :string
# add_column :hack_user_debugs, :hack_user_lastest_code_id, :integer
# 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
# add_column :hack_user_codes, :input, :text
# add_index :hack_user_lastest_codes, :user_id, name: "user_index"
# add_index :hack_user_lastest_codes, :hack_id, name: "hack_index"
# add_index :hack_user_lastest_codes, :identifier, unique: true
# add_index :hack_user_debugs, :hack_user_lastest_code_id, unique: true
# add_column :hack_user_lastest_codes, :submit_status, :integer, :default => 0
# add_column :hack_user_lastest_codes, :status, :integer
# add_column :hack_user_lastest_codes, :passed_time, :timestamp
# add_column :hack_user_debugs, :status, :integer
add_column :hack_user_codes, :execute_time, :integer
add_column :hack_user_codes, :execute_memory, :integer
add_column :hack_user_debugs, :execute_time, :integer
add_column :hack_user_debugs, :execute_memory, :integer
end
end
Loading…
Cancel
Save