socialforge/app/controllers/challenges_controller.rb

206 lines
6.1 KiB
Ruby

# 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]
before_filter :authorize_tpi, :only => [:new, :create]
before_filter :build_challege_from_params, :only => [:new, :create]
before_filter :tpi_manager_allowed, :only => [:challenge_build]
include ApplicationHelper
def new
unless @shixun.parent_id.nil?
return render_403
end
# 顶部导航
@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
if @challenge.save
if params[:sample][:input].length > 0
params[:sample][:input].each_with_index do |value, index|
unless (value == params[:sample][:output][index] && value.blank?)
ChallengeSample.create(:challenge_id => @challenge.id, :input => value, :output => params[:sample][:output][index])
end
end
end
if params[:program][:input].length > 0
params[:program][:input].each_with_index do |value, index|
unless (value == params[:program][:output][index] && value.blank?)
TestSet.create(:challenge_id => @challenge.id, :input => value, :output => params[:program][:output][index])
end
end
end
respond_to do |format|
format.html {redirect_to shixun_challenge_path(@challenge, :shixun_id => @shixun)}
end
else
respond_to do |format|
format.html { render :action => 'new' }
end
end
end
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
@outputs = GameOutputs.all
respond_to do |format|
format.html
format.js
end
end
def edit
end
def update
end
def index_down
# @challenges = @shixun.challenges
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
if @challenge.status == 0
params = {:jobName => "#{jobName}", :taskId => "#{taskId}", :step => "#{step}", :gitUrl => "#{gitUrl}"}
uri = URI.parse("http://123.59.135.74:9999/jenkins-exec/api/buildJob")
res = uri_exec uri, params
# @challenge.update_attribute(:status, 1)
end
respond_to do |format|
format.js
end
end
def game_build_result
status = params[:status].to_i
task_id = params[:taskId]
message = Base64.decode64(params[:msg]) unless params[:msg].blank?
begin
@training_task = TrainingTask.find(task_id)
# 如果已经执行成功过,则不重复执行
return if @training_task.status == 1
original_project_id = Project.find(@training_task.project_id).try(:forked_from_project_id)
original_project = Project.find(original_project_id)
rescue
return
end
original_tasks_count = original_project.training_tasks.count
position = @training_task.try(:position)
# 测试,默认成功
if status == 0
ActiveRecord::Base.transaction do
if position < original_tasks_count
# 继续发布下一个任务
publish_training_tasks original_project_id, @training_task.project_id, position + 1, original_project.user_id
end
@training_task.update_attribute(:status, 1)
@training_task.update_attribute(:result, 1)
# 创建一条回复提醒
content = (position == original_tasks_count) ? "恭喜您,您已经完成了实训项目的所有任务" : "恭喜您,您已经完成了本任务,请继续下一任务"
add_training_task_journal(content, original_project.user_id)
end
else
content = "很抱歉,您的任务未通过,请继续加油,错误信息如下:#{message}"
add_training_task_journal(content, original_project.user_id)
# 失败的时候可以继续提交
@training_task.update_attribute(:status, 0)
@training_task.update_attribute(:result, 2)
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 (@shixun.tpi? && User.current.manager_of_shixun?(@shixun))
render_403
end
end
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 find_challenge
@challenge = Challenge.find(params[:id])
@shixun = @challenge.shixun
rescue ActiveRecord::RecordNotFound
render_404
end
# TPM:parent_id is nil
# TPI is not allowed
def authorize_tpi
render_403 if @shixun.tpi?
end
# Find project of id params[:id]
def find_shixun
shixun_id = params[:shixun_id] || (params[:challenge] && params[:challenge][:shixun_id])
@shixun = Shixun.find(shixun_id)
rescue ActiveRecord::RecordNotFound
render_404
end
end