educoder/app/controllers/challenges_controller.rb

241 lines
8.2 KiB
Ruby
Raw Normal View History

2017-04-07 10:16:13 +08:00
# encoding: utf-8
class ChallengesController < ApplicationController
layout "base_shixun"
before_filter :find_shixun, :only => [:index, :new, :create, :destroy, :challenge_build]
before_filter :find_challenge, :only => [:show, :edit, :update, :challenge_build, :index_up, :index_down, :destroy]
before_filter :build_challege_from_params, :only => [:new, :create]
2017-04-28 10:21:02 +08:00
before_filter :tpi_manager_allowed, :only => [:challenge_build, :destroy, :edit, :new, :create]
before_filter :allowed_view, :only => [:show]
2017-04-07 10:16:13 +08:00
before_filter :query_challeges, :only => [:show, :edit, :update]
before_filter :find_shixun_language, :only => [:show, :new, :edit]
include ApplicationHelper
def new
# 顶部导航
@project_menu_type = 11
respond_to do |format|
format.html { render :action => 'new', :layout => 'base_shixun' }
end
end
def create
challenge_count = @shixun.challenges.count
@challenge = Challenge.new(params[:challenge])
@challenge.position = challenge_count + 1
@challenge.shixun = @shixun
@challenge.user = User.current
ActiveRecord::Base.transaction do
begin
@challenge.save!
2017-04-16 17:20:47 +08:00
if params[:test_set][:hidden].length > 0
params[:test_set][:input].each_with_index do |input, index|
2017-04-17 15:16:42 +08:00
params[:test_set][:hidden][index].to_i == 0 ? open = 1 : open = 0
2017-04-16 17:20:47 +08:00
unless (input[index] == params[:test_set][:output][index] && input[index].nil?)
2017-04-17 15:16:42 +08:00
TestSet.create(:challenge_id => @challenge.id, :input => input, :output => params[:test_set][:output][index], :is_public => open, :position => (index + 1))
2017-04-16 17:20:47 +08:00
end
end
2017-04-07 10:16:13 +08:00
end
unless params[:knowledge].blank?
params[:knowledge][:input].each do |input|
ChallengeTag.create(:name => input, :challenge_id => @challenge.id)
end
end
# 向jenkins端发送请求构建pipeline片段实时返回结果
2017-04-16 17:20:47 +08:00
jenkins_shixuns = Redmine::Configuration['jenkins_shixuns']
2017-05-15 16:37:46 +08:00
params = {:challengeStage => "#{@challenge.position}", :challengeType => "#{@challenge.evaluation_way.to_i}", :challengeProgramName => "#{@challenge.exec_path}", :trainingID => "#{@shixun.id}", :operationEnvironment => @shixun.language}
2017-04-16 17:20:47 +08:00
uri = URI("#{jenkins_shixuns}/jenkins-exec/game/generatePipelineScriptForChallenge")
2017-05-22 16:51:44 +08:00
res = uri_exec uri, {challengeInfo: params.to_json}
if res && res['code'] == 0
@challenge.update_attribute(:pipeline_script, res['msg'])
respond_to do |format|
format.html {redirect_to shixun_challenge_path(@challenge, :shixun_id => @shixun)}
end
else
2017-04-28 17:37:18 +08:00
logger.error("res['code'] is #{res['code']}, res['msg'] is #{res['msg']}")
raise("实训云平台繁忙繁忙等级116")
2017-04-07 10:16:13 +08:00
end
rescue Exception => e
flash[:error] = "#{e.message}"
2017-04-28 17:37:18 +08:00
redirect_to new_shixun_challenge_path(:shixun_id => @shixun)
raise ActiveRecord::Rollback
2017-04-07 10:16:13 +08:00
end
end
end
# 处理新建challenge返回的pipeline片段
def fragment_from_jenkins
end
2017-04-07 10:16:13 +08:00
def index
# 顶部导航
@challenges = @shixun.challenges.order("position desc")
respond_to do |format|
format.js
format.html
end
rescue ActiveRecord::RecordNotFound
render_404
end
def show
respond_to do |format|
format.html
format.js
end
end
def edit
end
def destroy
@challenge.destroy
respond_to do |format|
format.html{redirect_to shixun_challenges_path(@shixun)}
end
end
def update
respond_to do |format|
ActiveRecord::Base.transaction do
@challenge.update_attributes(params[:challenge])
begin
2017-04-07 10:16:13 +08:00
@test_sets.delete_all unless @test_sets.blank?
2017-04-17 14:41:23 +08:00
@challenge_tags.delete_all unless @challenge_tags.blank?
if params[:test_set][:hidden].length > 0
params[:test_set][:input].each_with_index do |input, index|
unless (input[index] == params[:test_set][:output][index] && input[index].nil?)
2017-04-17 15:16:42 +08:00
params[:test_set][:hidden][index].to_i == 0 ? open = 1 : open = 0
TestSet.create(:challenge_id => @challenge.id, :input => input, :output => params[:test_set][:output][index], :is_public => open, :position => (index + 1))
2017-04-17 14:41:23 +08:00
end
2017-04-07 10:16:13 +08:00
end
end
unless params[:knowledge].blank?
params[:knowledge][:input].each do |input|
ChallengeTag.create(:name => input, :challenge_id => @challenge.id)
end
end
2017-04-21 22:17:24 +08:00
# 向jenkins端发送请求构建pipeline片段实时返回结果
jenkins_shixuns = Redmine::Configuration['jenkins_shixuns']
2017-05-15 16:37:46 +08:00
params = {:challengeStage => "#{@challenge.position}", :challengeType => "#{@challenge.evaluation_way.to_i}", :challengeProgramName => "#{@challenge.exec_path}", :trainingID => "#{@shixun.id}", :operationEnvironment => @shixun.language}
2017-04-21 22:17:24 +08:00
uri = URI("#{jenkins_shixuns}/jenkins-exec/game/generatePipelineScriptForChallenge")
res = uri_exec uri, {challengeInfo: params.to_json}
2017-05-22 16:51:44 +08:00
if res && res['code'] == 0
2017-04-21 22:42:30 +08:00
@challenge.update_attribute(:pipeline_script, res['msg'])
else
logger.error("res['code'] is #{res['code']}, res['msg'] is #{res['code']}")
raise("实训云平台繁忙繁忙等级116")
2017-04-21 22:42:30 +08:00
end
format.html {redirect_to shixun_challenge_path(@challenge, :shixun_id => @shixun), notice: '更新成功!'}
rescue Exception => e
flash[:error] = "#{e.message}"
format.html{redirect_to edit_shixun_challenge_path(@challenge, :shixun_id => @shixun)}
raise ActiveRecord::Rollback
2017-04-07 10:16:13 +08:00
end
end
end
end
def index_down
next_challenge = @challenge.next_challenge
position = @challenge.position
@challenge.update_attribute(:position, (position + 1))
next_challenge.update_attribute(:position, next_challenge.position - 1)
@challenges = @shixun.challenges
end
def index_up
position = @challenge.position
last_challenge = @challenge.last_challenge
@challenge.update_attribute(:position, (position - 1))
last_challenge.update_attribute(:position, last_challenge.position + 1)
@challenges = @shixun.challenges
end
# build job 只负责发送请求
def challenge_build
gitUrl = git_repository_url(@shixun, "Shixun")
gitUrl = Base64.encode64(gitUrl)
taskId = params[:id]
jobName = @shixun.forked_form
step = @challenge.position
jenkins_shixuns = Redmine::Configuration['jenkins_shixuns']
if @challenge.status == 0
params = {:jobName => "#{jobName}", :taskId => "#{taskId}", :step => "#{step}", :gitUrl => "#{gitUrl}"}
uri = URI("#{jenkins_shixuns}/jenkins-exec/api/buildJob")
res = uri_exec uri, params
# @challenge.update_attribute(:status, 1)
end
respond_to do |format|
format.js
end
end
private
def count_input input, output
if input.length == 0 && output.length == 0
result = 0
else
if input.length > output.length
end
end
end
def tpi_manager_allowed
unless (User.current.manager_of_shixun?(@shixun) || User.current.admin?)
render_403
end
end
2017-04-28 10:21:02 +08:00
def allowed_view
unless (allow_to_view_challenge(@challenge, @shixun) || User.current.manager_of_shixun?(@shixun))
2017-04-28 10:21:02 +08:00
render_403
end
end
2017-04-07 10:16:13 +08:00
def build_challege_from_params
if params[:id].blank?
@challenge = Challenge.new
@challenge.shixun = @shixun
else
@challenge = @shixun.challenges.find(params[:id])
end
@challenge.shixun = @shixun
@challenge.user ||= User.current
end
def query_challeges
@test_sets = @challenge.test_sets
@challenge_tags = @challenge.challenge_tags
end
def find_challenge
@challenge = Challenge.find(params[:id])
@shixun = @challenge.shixun
rescue ActiveRecord::RecordNotFound
render_404
end
# Find project of id params[:id]
def find_shixun
shixun_id = params[:shixun_id] || (params[:challenge] && params[:challenge][:shixun_id])
2017-04-25 17:15:37 +08:00
@shixun = Shixun.find_by_identifier(shixun_id)
2017-04-26 14:20:07 +08:00
if @shixun.nil?
render_404
return
end
2017-04-07 10:16:13 +08:00
rescue ActiveRecord::RecordNotFound
render_404
end
def find_shixun_language
language = @shixun.language
@language = language_switch language
end
end