trustieplus/app/controllers/myshixuns_controller.rb

183 lines
7.9 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# encoding: utf-8
class MyshixunsController < ApplicationController
layout 'base_myshixun'
skip_before_filter :verify_authenticity_token, :only => [:training_task_status, :close_webssh]
before_filter :require_login, :except => [:training_task_status, :close_webssh]
before_filter :check_authentication, :except => [:training_task_status, :close_webssh]
before_filter :find_myshixun, :only => [:show, :myshixun_reset, :open_webssh]
include ApplicationHelper
# 将代码重置到开启状态
# 方法使用文件更新非Git重置
def code_reset
end
def code_reset_success
end
# 连接webssh
def open_webssh
username = Redmine::Configuration['webssh_username']
password = Redmine::Configuration['webssh_password']
begin
shixun_tomcat = Redmine::Configuration['shixun_tomcat']
uri = "#{shixun_tomcat}/bridge/webssh/getConnectInfo"
user_id = User.current.id
params = {tpiID:@myshixun.id, operationEnvironment:@myshixun.shixun.try(:language)}
res = uri_exec uri, params
if res && res['code'].to_i != 0
raise("实训云平台繁忙繁忙等级92")
end
render :json => {:host => res['address'], :port => res['port'], :username => username, :password => password, :game_id => @myshixun.id}
rescue Exception => e
logger.error(e)
render :json => {:error => e.try(:message)}
# @message = e.try(:message)
end
end
# 断开webssh连接
def close_webssh
username = Redmine::Configuration['webssh_username']
password = Redmine::Configuration['webssh_password']
begin
shixun_tomcat = Redmine::Configuration['shixun_tomcat']
uri = "#{shixun_tomcat}/bridge/webssh/deleteSSH"
user_id = User.current.id
logger.info("#############################")
logger.info("game_id:" + @_params[:id])
params = {tpiID:@_params[:id]} # @_params[:id] 是webssh传过来的gameId
res = uri_exec uri, params
if res && res['code'].to_i != 0
raise("实训云平台繁忙繁忙等级93")
end
render :jsonp => {:host => res['address'], :port => res['port'], :username => username, :password => password}
rescue Exception => e
logger.error(e)
@message = e.try(:message)
end
end
# 实训重置只更新脚本
def myshixun_reset
ActiveRecord::Base.transaction do
begin
g = Gitlab.client
shixun_tomcat = Redmine::Configuration['shixun_tomcat']
shixun = @myshixun.shixun
@challenges = shixun.challenges
# 删除选择题用户记录
unless @challenges.blank?
@challenges.each do |challenge|
if challenge.st != 0
challenge.challenge_chooses.each do |choose|
user_output = choose.choose_outputs
unless user_output.blank?
user_output.delete
end
end
end
end
end
#end
myshixun_job = Base64.urlsafe_encode64("myshixun_#{@myshixun.id}")
StudentWork.where(:myshixun_id => @myshixun.id).update_all(:myshixun_id => nil, :work_status => 0)
@myshixun.destroy
myshixun_member = @myshixun
repository = @myshixun.repository
repository.destroy
g_project = g.project(@myshixun.gpid)
unless g_project.id.nil?
git_rep = g.delete_project(@myshixun.gpid)
logger.info("### delete repository result is #{git_rep}")
if git_rep != true
raise("实训云平台繁忙繁忙等级87")
end
end
redirect_to shixun_exec_shixun_path(shixun, :type => 1, :is_subject => params[:is_subject])
rescue Exception => e
flash[:error] = "实训云平台繁忙繁忙等级89"
g.delete_project(@myshixun.gpid) if !g_project.try(:id).nil?
redirect_to myshixun_path(@myshixun)
logger.error("myshixun reset failed #{e}")
raise ActiveRecord::Rollback
end
end
end
# taskId 即返回的game id
# 返回结果params [:stauts] 0 表示成功,其它则失败
# msg 错误信息
# output 为测试用户编译输出结果
# myshixun:status 1为完成实训
# @jenkins: caseId对应test_set的positionpassed: 1表示成功0表示失败
# resubmit 1表示已通关后重新评测0表示非重新评测
# retry_status 0初始值1重新评测失败2重新评测成功
def training_task_status
#params[:jsonTestDetails] = '{"buildID":"1","compileSuccess":"1","msg":[{"caseId":"1","expectedOutput":"56ysMOS9jeWQjOWtpueahOaAu+WIhuS4ujoyNDcK56ysMeS9jeWQjOWtpueahOaAu+WIhuS4ujoz MjIK","input":"Mg0KMjMsNTYsNzgsOTANCjIxLDU2LDc4LDc2LDkx","output":"56ysMOS9jeWQjOWtpueahOaAu+WIhuS4ujoyNDcK56ysMeS9jeWQjOWtpueahOaAu+WIhuS4ujoz MjIK","passed":"1"},{"caseId":"2","expectedOutput":"56ysMOS9jeWQjOWtpueahOaAu+WIhuS4ujoyMjgK56ysMeS9jeWQjOWtpueahOaAu+WIhuS4ujoy MzUK56ysMuS9jeWQjOWtpueahOaAu+WIhuS4ujoyMDYK","input":"Mw0KOTgsNzYsNTQNCjkwLDU0LDg3LDQNCjc4LDI2LDEyLDkw","output":"56ysMOS9jeWQjOWtpueahOaAu+WIhuS4ujoyMjgK56ysMeS9jeWQjOWtpueahOaAu+WIhuS4ujoy MzUK56ysMuS9jeWQjOWtpueahOaAu+WIhuS4ujoyMDYK","passed":"1"}],"outPut":"Y29tcGlsZSBzdWNjZXNzZnVsbHk","resubmit":"57D9COBR_5806","status":"0"}'
jsonTestDetails = JSON.parse(params[:jsonTestDetails])
status = jsonTestDetails['status']
game_id = jsonTestDetails['buildID']
resubmit = jsonTestDetails['resubmit']
outPut = tran_base64_decode64(jsonTestDetails['outPut'])
jenkins_testsets = jsonTestDetails['msg']
compile_success = jsonTestDetails['compileSuccess']
# message = Base64.decode64(params[:msg]) unless params[:msg].blank?
logger.info(outPut)
game = Game.find(game_id)
challenge = game.challenge
unless jenkins_testsets.blank?
jenkins_testsets.each do |j_test_set|
logger.info("j_test_set: ############## #{j_test_set['output']}")
actual_output = tran_base64_decode64(j_test_set['output'])
logger.info "actual_output:################################################# #{actual_output}"
game_outputs = Output.create(:code => status, :game_id => game_id, :out_put => outPut, :test_set_position => j_test_set['caseId'],
:actual_output => actual_output, :result => j_test_set['passed'].to_i, :query_index => game.query_index, :compile_success => compile_success.to_i)
# output = Output.where(:game_id => game.id, :test_set_position => j_test_set['caseId'].to_i).first
# logger.info("#############{outPut}")
# if output.nil?
# game_outputs = Output.create(:code => status, :game_id => game_id, :out_put => outPut, :test_set_position => j_test_set['caseId'],
# :actual_output => actual_output, :result => j_test_set['passed'].to_i)
# else
# output.update_attributes(:code => status, :out_put => outPut, :actual_output => actual_output, :result => j_test_set['passed'].to_i)
# end
end
end
if status == "0"
if !resubmit.blank?
game.update_attributes(:retry_status => 2, :resubmit_identifier => resubmit)
else
game.update_attributes(:status => 2, :end_time => Time.now)
unless game.answer_open? # 如果没有查看答案,则获得该关卡得分
game.update_attribute(:final_score, challenge.score)
end
end
else # status == "-1" 表示返回结果错误
if !resubmit.blank?
game.update_attributes(:retry_status => 1, :resubmit_identifier => resubmit)
else
game.update_attribute(:status, 0)
end
end
render :json => {:data => "success"}
end
def show
respond_to do |format|
format.html{redirect_to myshixun_game_path(@myshixun.current_task, :myshixun_id => @myshixun)}
end
end
private
# Find myshixun of id params[:id]
def find_myshixun
@myshixun = Myshixun.find_by_identifier(params[:id])
rescue ActiveRecord::RecordNotFound
render_404
end
end