|
|
@ -55,12 +55,34 @@ class StudentWorkController < ApplicationController
|
|
|
|
render :json => resultObj
|
|
|
|
render :json => resultObj
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
$test_result = {}
|
|
|
|
#行尾空格替换成□
|
|
|
|
$test_status = {}
|
|
|
|
def space_replace_1(str)
|
|
|
|
|
|
|
|
for i in 0 .. str.size
|
|
|
|
|
|
|
|
tChar = str[i]
|
|
|
|
|
|
|
|
if tChar != ' ' && tChar != "\n"
|
|
|
|
|
|
|
|
sFlag = false
|
|
|
|
|
|
|
|
eFlag = false
|
|
|
|
|
|
|
|
elsif tChar == ' ' && sFlag == false
|
|
|
|
|
|
|
|
tStart = i
|
|
|
|
|
|
|
|
sFlag = true
|
|
|
|
|
|
|
|
elsif tChar == "\n"
|
|
|
|
|
|
|
|
tEnd = i - 1
|
|
|
|
|
|
|
|
if sFlag == true
|
|
|
|
|
|
|
|
for j in tStart .. tEnd
|
|
|
|
|
|
|
|
str[j] = "□"
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
sFlag = false
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#由于负载问题 不要使用全局变量
|
|
|
|
#根据传入的tIndex确定是第几次测试
|
|
|
|
#根据传入的tIndex确定是第几次测试
|
|
|
|
|
|
|
|
#之后如果觉得很卡 可以改成将结果传回JS再以参数形式传回来
|
|
|
|
def program_test_ex
|
|
|
|
def program_test_ex
|
|
|
|
is_test = params[:is_test] == 'true'
|
|
|
|
is_test = params[:is_test] == 'true'
|
|
|
|
resultObj = {status: 0, results: [], error_msg: '', time: Time.now.strftime('%Y-%m-%d %T'),tseq:1,tcount:1} #保存每测试一次返回的结果
|
|
|
|
resultObj = {status: 0, results: [], error_msg: '', time: Time.now.strftime('%Y-%m-%d %T'),tseq:1,tcount:1,testid:1} #保存每测试一次返回的结果
|
|
|
|
|
|
|
|
|
|
|
|
student_work = find_or_save_student_work(is_test)
|
|
|
|
student_work = find_or_save_student_work(is_test)
|
|
|
|
|
|
|
|
|
|
|
@ -77,14 +99,24 @@ class StudentWorkController < ApplicationController
|
|
|
|
|
|
|
|
|
|
|
|
#请求测试
|
|
|
|
#请求测试
|
|
|
|
result = test_realtime_ex(test, params[:src])
|
|
|
|
result = test_realtime_ex(test, params[:src])
|
|
|
|
logger.debug result
|
|
|
|
|
|
|
|
|
|
|
|
if result["status"].to_i != -2
|
|
|
|
|
|
|
|
#result["results"].first['output'] = result["results"].first['output'].gsub(" ","□")
|
|
|
|
|
|
|
|
#result["results"].first['result'] = result["results"].first['result'].gsub(" ","□")
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
space_replace_1(result["results"].first['output'])
|
|
|
|
|
|
|
|
space_replace_1(result["results"].first['result'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.debug result
|
|
|
|
|
|
|
|
|
|
|
|
#-1 默认值 0全部正确并结束 2 超时 -2 编译错误
|
|
|
|
#-1 默认值 0全部正确并结束 2 超时 -2 编译错误
|
|
|
|
resultObj[:status] = -1
|
|
|
|
resultObj[:status] = -1
|
|
|
|
resultObj[:results] = result["results"][0] #本次测试结果
|
|
|
|
resultObj[:results] = result["results"].first #本次测试结果
|
|
|
|
resultObj[:error_msg] = result["error_msg"] #编译错误时的信息
|
|
|
|
resultObj[:error_msg] = result["error_msg"] #编译错误时的信息
|
|
|
|
|
|
|
|
|
|
|
|
if result["status"].to_i == -2 #编译错误
|
|
|
|
if result["status"].to_i == -2 #编译错误
|
|
|
|
|
|
|
|
resultObj[:results] = result["error_msg"]
|
|
|
|
resultObj[:status] = -2
|
|
|
|
resultObj[:status] = -2
|
|
|
|
elsif result["results"][0]["status"].to_i == 2
|
|
|
|
elsif result["results"][0]["status"].to_i == 2
|
|
|
|
resultObj[:status] = 2
|
|
|
|
resultObj[:status] = 2
|
|
|
@ -93,25 +125,6 @@ class StudentWorkController < ApplicationController
|
|
|
|
unless student_work.save
|
|
|
|
unless student_work.save
|
|
|
|
resultObj[:status] = 200
|
|
|
|
resultObj[:status] = 200
|
|
|
|
else
|
|
|
|
else
|
|
|
|
|
|
|
|
|
|
|
|
#索引
|
|
|
|
|
|
|
|
work_id = student_work.id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#测试第一个时初始化下全局变量
|
|
|
|
|
|
|
|
if index == 1
|
|
|
|
|
|
|
|
$test_result[work_id] = [] #保存本次测试的结果 输入输出
|
|
|
|
|
|
|
|
$test_status[work_id] = 0 #保存本次测试的结果 正确还是错误
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if result["status"].to_i == -2
|
|
|
|
|
|
|
|
$test_result[work_id] = [result["error_msg"]]
|
|
|
|
|
|
|
|
$test_status[work_id] = -2
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
#存下每次的结果 只有每次都为0才全部正确
|
|
|
|
|
|
|
|
$test_status[work_id] = result["status"] != 0 ? result["status"]:$test_status[work_id]
|
|
|
|
|
|
|
|
$test_result[work_id][index - 1] = resultObj[:results]
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
student_work.name = params[:title]
|
|
|
|
student_work.name = params[:title]
|
|
|
|
student_work.description = params[:src]
|
|
|
|
student_work.description = params[:src]
|
|
|
|
|
|
|
|
|
|
|
@ -121,20 +134,33 @@ class StudentWorkController < ApplicationController
|
|
|
|
student_work.late_penalty = 0
|
|
|
|
student_work.late_penalty = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#每次从数据库取出上次的结果加上本次的结果再存入数据库
|
|
|
|
|
|
|
|
status = result["status"]
|
|
|
|
|
|
|
|
if index == 1
|
|
|
|
|
|
|
|
student_work_test = student_work.student_work_tests.build(status: status,
|
|
|
|
|
|
|
|
results: [resultObj[:results]],src: params[:src])
|
|
|
|
|
|
|
|
student_work_test.save!
|
|
|
|
|
|
|
|
resultObj[:testid] = student_work_test.id
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
#先从数据库取出result
|
|
|
|
|
|
|
|
student_work_test = StudentWorkTest.find(params[:testid])
|
|
|
|
|
|
|
|
results = student_work_test.results
|
|
|
|
|
|
|
|
results << resultObj[:results]
|
|
|
|
|
|
|
|
student_work_test.results = results
|
|
|
|
|
|
|
|
student_work_test.status = (result["status"] != 0 ? result["status"] : student_work_test.status)
|
|
|
|
|
|
|
|
student_work_test.save!
|
|
|
|
|
|
|
|
status = student_work_test.status
|
|
|
|
|
|
|
|
resultObj[:testid] = student_work_test.id
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#超时或编译错误则直接返回了并存入数据库
|
|
|
|
#超时或编译错误则直接返回了并存入数据库
|
|
|
|
if resultObj[:status] == 2 || resultObj[:status] == -2 || index == @homework.homework_tests.size
|
|
|
|
if resultObj[:status] == 2 || resultObj[:status] == -2 || index == @homework.homework_tests.size
|
|
|
|
if $test_status[work_id] == 0
|
|
|
|
if status == 0
|
|
|
|
resultObj[:status] = 0
|
|
|
|
resultObj[:status] = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
student_work.save!
|
|
|
|
student_work_test = student_work.student_work_tests.build(status: $test_status[work_id],
|
|
|
|
|
|
|
|
results: $test_result[work_id],src: params[:src])
|
|
|
|
|
|
|
|
student_work.save
|
|
|
|
|
|
|
|
resultObj[:time] = student_work_test.created_at.to_s(:db)
|
|
|
|
resultObj[:time] = student_work_test.created_at.to_s(:db)
|
|
|
|
resultObj[:index] = student_work.student_work_tests.count
|
|
|
|
resultObj[:index] = student_work.student_work_tests.count
|
|
|
|
|
|
|
|
|
|
|
|
$test_result[work_id] = nil
|
|
|
|
|
|
|
|
$test_status[work_id] = nil
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#渲染返回结果
|
|
|
|
#渲染返回结果
|
|
|
|