116 lines
3.3 KiB
Ruby
116 lines
3.3 KiB
Ruby
class Api::QuestionsController < ApplicationController
|
|
include Judge::Helper
|
|
|
|
protect_from_forgery :except => [:create , :update]
|
|
respond_to :json
|
|
|
|
def create
|
|
args = [:title, :content, :input, :output]
|
|
@question = Question.new(params.slice(*args))
|
|
if @question.save
|
|
render :json => {id: @question.id, status: 0}
|
|
else
|
|
render :json => {status: 1, message: @question.errors.full_messages}
|
|
end
|
|
end
|
|
|
|
def update
|
|
args = [:title, :content, :input, :output]
|
|
@question = Question.find(params[:id])
|
|
if @question.update_attributes(params.slice(*args))
|
|
render :json => {id: @question.id, status: 0}
|
|
else
|
|
render :json => {status: 1, message: @question.errors.full_messages}
|
|
end
|
|
end
|
|
|
|
def test_realtime
|
|
question = Question.find(params[:id])
|
|
src = params[:src]
|
|
compile_result = compile(src, params[:language].to_i)
|
|
body = {student_work_id: 0, results: []}
|
|
unless compile_result
|
|
body[:compile_error_msg] = @compiler.errmsg
|
|
body[:status] = -2
|
|
else
|
|
result = 0
|
|
question.factors.each do |factor|
|
|
output = factor.output
|
|
input = factor.input
|
|
rs,option,time_used,memory_used = run(input)
|
|
logger.debug "rs #{rs}, option #{option} time_used #{time_used} memory_used #{memory_used}"
|
|
execute_output = @executor.result
|
|
logger.debug "result #{execute_output} output #{output} input #{input}"
|
|
if rs==0
|
|
status = -2
|
|
else
|
|
status = rs
|
|
end
|
|
|
|
if rs==0 && cmp(execute_output, output)
|
|
status = 0
|
|
else
|
|
result = -1
|
|
end
|
|
|
|
body[:results] << {input: input,
|
|
output: output,
|
|
memory_used: memory_used,
|
|
time_used: time_used,
|
|
result: execute_output,
|
|
status: status}
|
|
end
|
|
body[:status] = result
|
|
end
|
|
render :json => body
|
|
end
|
|
|
|
def realtime
|
|
src = Base64.decode64(params[:src])
|
|
src = src.force_encoding('UTF-8')
|
|
compile_result = compile(src, params[:language].to_i)
|
|
body = {student_work_id: 0, results: []}
|
|
unless compile_result
|
|
body[:compile_error_msg] = @compiler.errmsg
|
|
end
|
|
|
|
i =0
|
|
params[:input].each do |input|
|
|
output = params[:output][i]
|
|
if compile_result
|
|
rs,option,time_used,memory_used = run(input)
|
|
logger.debug "rs #{rs}, option #{option} time_used #{time_used} memory_used #{memory_used}"
|
|
execute_output = @executor.result
|
|
logger.debug "result #{execute_output} output #{output} input #{input}"
|
|
if rs==0
|
|
status = -2
|
|
else
|
|
status = rs
|
|
end
|
|
|
|
if rs==0 && cmp(execute_output, output)
|
|
status = 0
|
|
end
|
|
|
|
body[:results] << {input: input,
|
|
output: output,
|
|
memory_used: memory_used,
|
|
time_used: time_used,
|
|
result: execute_output,
|
|
status: status}
|
|
else
|
|
errmsg = @compiler.errmsg
|
|
status = -1
|
|
body[:results] << {
|
|
input: input,
|
|
output: output,
|
|
status: status
|
|
}
|
|
end
|
|
i += 1
|
|
end
|
|
render :json => body
|
|
end
|
|
|
|
end
|