214 lines
6.6 KiB
Ruby
214 lines
6.6 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, :destroy]
|
|
before_filter :build_challege_from_params, :only => [:new, :create]
|
|
before_filter :tpi_manager_allowed, :only => [:challenge_build, :destroy, :show, :edit, :new, :create]
|
|
before_filter :query_challeges, :only => [:show, :edit, :update]
|
|
before_filter :find_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
|
|
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
|
|
unless params[:knowledge].blank?
|
|
params[:knowledge][:input].each do |input|
|
|
ChallengeTag.create(:name => input, :challenge_id => @challenge.id)
|
|
end
|
|
end
|
|
respond_to do |format|
|
|
format.html {redirect_to shixun_challenge_path(@challenge, :shixun_id => @shixun), notice: '创建成功!'}
|
|
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
|
|
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|
|
|
if @challenge.update_attributes(params[:challenge])
|
|
ActiveRecord::Base.transaction do
|
|
@challenge_samples.delete_all unless @challenge_samples.blank?
|
|
@test_sets.delete_all unless @test_sets.blank?
|
|
@challenge_tags.delete_all unless @challenge_tags.blank?
|
|
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
|
|
unless params[:knowledge].blank?
|
|
params[:knowledge][:input].each do |input|
|
|
ChallengeTag.create(:name => input, :challenge_id => @challenge.id)
|
|
end
|
|
end
|
|
end
|
|
format.html {redirect_to shixun_challenge_path(@challenge, :shixun_id => @shixun), notice: '更新成功!'}
|
|
format.json { head :no_content }
|
|
else
|
|
format.html { render action: "edit" }
|
|
format.json { render json: @challenge.errors }
|
|
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
|
|
|
|
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
|
|
@challenge_samples = @challenge.challenge_samples
|
|
@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])
|
|
@shixun = Shixun.find(shixun_id)
|
|
rescue ActiveRecord::RecordNotFound
|
|
render_404
|
|
end
|
|
|
|
def find_language
|
|
language = @shixun.language
|
|
@language = language_switch language
|
|
end
|
|
|
|
end
|