133 lines
4.3 KiB
Ruby
133 lines
4.3 KiB
Ruby
class GamesController < ApplicationController
|
||
layout "base_myshixun"
|
||
before_filter :find_myshixun, :only => [:index]
|
||
before_filter :find_game, :only => [:show, :game_build, :entry,:next_step]
|
||
before_filter :find_repository, :only => [:show, :entry]
|
||
before_filter :allowd_manager, :only => [:game_build]
|
||
include ApplicationHelper
|
||
|
||
def index
|
||
@games = @myshixun.games
|
||
end
|
||
|
||
# mushixun的版本库必须创建时就创建
|
||
def show
|
||
@git_url = git_repository_url(@myshixun, "Myshixun")
|
||
@entries = @repository.entries(@path, @rev)
|
||
@outputs =
|
||
respond_to do |format|
|
||
format.html
|
||
format.js
|
||
end
|
||
end
|
||
|
||
# 代码预览
|
||
def entry
|
||
entry_and_raw(false)
|
||
@content = @repository.cat(@path, @rev)
|
||
if is_entry_text_data?(@content, @path)
|
||
render :layout => 'base_myshixun'
|
||
end
|
||
end
|
||
|
||
# status 0: 未提交测评或者提交测评失败后报错;1:中间状态还没返回值;2:返回值并成功
|
||
def game_build
|
||
gitUrl = git_repository_url(@myshixun, "Myshixun")
|
||
gitUrl = Base64.encode64(gitUrl)
|
||
taskId = params[:id]
|
||
jobName = @myshixun.forked_from
|
||
step = @game.stage
|
||
if @game.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
|
||
@game.update_attribute(:status, 1)
|
||
@outputs = @game.game_outputses
|
||
end
|
||
respond_to do |format|
|
||
format.js
|
||
end
|
||
end
|
||
|
||
# 自动推送下一个任务
|
||
def next_step
|
||
shixun = @myshixun.parent
|
||
position = @game.stage + 1
|
||
challenge = Challenge.where(:shixun_id => shixun, :position => position).first
|
||
render_404 if challenge.blank?
|
||
next_game = publish_games challenge, @myshixun.id, position
|
||
respond_to do |format|
|
||
format.html{redirect_to myshixun_game_path(next_game, :myshixun_id => @myshixun.id)}
|
||
end
|
||
end
|
||
|
||
private
|
||
def entry_and_raw(is_raw)
|
||
@entry = @repository.entry(@path, @rev)
|
||
(show_error_not_found; return) unless @entry
|
||
|
||
# If the entry is a dir, show the browser
|
||
(show; return) if @entry.is_dir?
|
||
|
||
@content = @repository.cat(@path, @rev)
|
||
(show_error_not_found; return) unless @content
|
||
if is_raw || (@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) || !is_entry_text_data?(@content, @path)
|
||
# Force the download
|
||
send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) }
|
||
send_type = Redmine::MimeType.of(@path)
|
||
send_opt[:type] = send_type.to_s if send_type
|
||
send_opt[:disposition] = (Redmine::MimeType.is_type?('image', @path) && !is_raw ? 'inline' : 'attachment')
|
||
send_data @content, send_opt
|
||
else
|
||
# Prevent empty lines when displaying a file with Windows style eol
|
||
# TODO: UTF-16
|
||
# Is this needs? AttachmentsController reads file simply.
|
||
@content.gsub!("\r\n", "\n")
|
||
@changeset = @repository.find_changeset_by_name(@rev)
|
||
end
|
||
end
|
||
|
||
def is_entry_text_data?(ent, path)
|
||
# UTF-16 contains "\x00".
|
||
# It is very strict that file contains less than 30% of ascii symbols
|
||
# in non Western Europe.
|
||
return true if Redmine::MimeType.is_type?('text', path)
|
||
# Ruby 1.8.6 has a bug of integer divisions.
|
||
# http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F
|
||
return false if ent.is_binary_data?
|
||
true
|
||
end
|
||
|
||
def find_repository
|
||
@repository = @myshixun.repository
|
||
render_404 if @myshixun.gpid.nil?
|
||
@path = params[:path].is_a?(Array) ? params[:path].join('/') : params[:path].to_s
|
||
@g = Gitlab.client
|
||
@g_project = @g.project(@myshixun.gpid)
|
||
@g_default_branch = @g_project.default_branch
|
||
# gitlab端获取默认分支
|
||
@rev = params[:rev].blank? ? @g_default_branch : params[:rev].to_s.strip
|
||
rescue ActiveRecord::RecordNotFound
|
||
render_404
|
||
end
|
||
|
||
def allowd_manager
|
||
render_403 unless User.current.manager_of_myshixun?(@myshixun)
|
||
end
|
||
|
||
# Find myshixun of id params[:id]
|
||
def find_myshixun
|
||
myshixun_id = params[:myshixun_id] || (params[:game] && params[:game][:myshixun_id])
|
||
@myshixun = Myshixun.find(myshixun_id)
|
||
rescue ActiveRecord::RecordNotFound
|
||
render_404
|
||
end
|
||
|
||
def find_game
|
||
@game = Game.find(params[:id])
|
||
@myshixun = @game.myshixun
|
||
rescue ActiveRecord::RecordNotFound
|
||
render_404
|
||
end
|
||
end
|