educoder/app/controllers/myshixuns_controller.rb

106 lines
4.2 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]
before_filter :require_login, :except => [:training_task_status]
before_filter :find_myshixun, :only => [:show, :myshixun_reset]
include ApplicationHelper
def myshixun_reset
ActiveRecord::Base.transaction do
begin
jenkins_shixuns = Redmine::Configuration['jenkins_shixuns']
shixun = @myshixun.shixun
myshixun_job = Base64.encode64("myshixun_#{@myshixun.id}")
@myshixun.destroy
myshixun_member = @myshixun
repository = @myshixun.repository
repository.destroy
g = Gitlab.client
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
params = {:jobName => "#{myshixun_job}"}
uri = URI("#{jenkins_shixuns}/jenkins-exec/game/deleteJob")
res = uri_exec uri, params
if (res && res['code'] != 0 && res['code'] != 1)
raise("实训云平台繁忙繁忙等级89")
end
redirect_to shixun_exec_shixun_path(shixun)
rescue Exception => e
flash[:error] = "实训云平台繁忙繁忙等级89"
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
jsonTestDetails = JSON.parse(params[:jsonTestDetails])
status = jsonTestDetails['status']
game_id = jsonTestDetails['buildID']
resubmit = jsonTestDetails['resubmit']
outPut = Base64.urlsafe_decode64(jsonTestDetails['outPut'].strip().gsub(" ", "+")).force_encoding("utf-8") unless jsonTestDetails['outPut'].blank?
jenkins_testsets = jsonTestDetails['msg']
# message = Base64.decode64(params[:msg]) unless params[:msg].blank?
game = Game.find(game_id)
challenge = game.challenge
unless jenkins_testsets.blank?
jenkins_testsets.each do |j_test_set|
actual_output = Base64.urlsafe_decode64(j_test_set['output'].gsub(" ", "+")).force_encoding("utf-8") unless j_test_set['output'].blank?
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, :is_public => j_test_set['isPublic'].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, :final_score => challenge.score, :end_time => Time.now)
end
else
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