2017-04-07 10:16:13 +08:00
#coding=utf-8
class GamesController < ApplicationController
layout " base_myshixun "
skip_before_filter :verify_authenticity_token , :only = > [ :file_update ]
before_filter :find_myshixun , :only = > [ :index ]
2017-09-08 11:21:08 +08:00
before_filter :find_game , :only = > [ :show , :game_build , :entry , :next_step , :prev_step , :outputs_show , :file_edit , :file_update , :get_waiting_time ,
2017-09-30 10:42:26 +08:00
:game_status , :change_status , :answer , :minus_score , :refresh_game_list , :reset_original_code , :reset_new_code , :evaluating_choice , :sync_codes ]
2017-08-04 09:38:10 +08:00
before_filter :find_repository , :only = > [ :show , :entry , :file_edit , :file_update , :reset_original_code , :reset_new_code , :html_show ]
2017-04-07 10:16:13 +08:00
before_filter :allowd_manager
before_filter :allowd_view , :only = > [ :show ]
before_filter :find__shixun_language , :only = > [ :show , :entry ]
include ApplicationHelper
2017-09-30 10:42:26 +08:00
require 'open3'
# 同步更新最新代码
# 同步完成后, 需要更新myshixun的commit_id为实训的commit最新值
# --------------------------
# 新思路, 有冲突则则重置, 没有冲突直接pull
# --------------------------
def sync_codes
g = Gitlab . client
2017-09-30 11:30:51 +08:00
myshixun_user_login = User . where ( :id = > @myshixun . user_id ) . first . try ( :login )
myshixun_rep_identifier = Repository . where ( :myshixun_id = > @myshixun . id , :type = > " Repository::Gitlab " ) . first . try ( :identifier )
gitlab_address = Redmine :: Configuration [ 'gitlab_address' ]
gitlab_myshixun_url = git_myshixun_url ( @myshixun , @myshixun . try ( :user_id ) )
shixun = @myshixun . shixun
gitlab_shixun_url = git_shixun_url ( shixun , ( shixun . try ( :fork_from ) . blank? ? " educoder " : " eduforge " ) )
if RUBY_PLATFORM =~ / w32 /
path = " c:/test/ "
else
2017-09-30 14:33:01 +08:00
path = " /home/pdl/myshixun_shell "
2017-09-30 11:30:51 +08:00
end
script = " cd #{ path }
git clone #{gitlab_address.to_s+'/'+myshixun_user_login+'/'+ myshixun_rep_identifier}.git
cd #{myshixun_rep_identifier}/
git remote set - url origin https : / / #{shixun.fork_from.blank? ? "educoder" : "eduforge"}:xinhu1ji2qu3@testbdgit.trustie.net/#{myshixun_user_login}/#{myshixun_rep_identifier}.git
git remote add remote_origin #{gitlab_shixun_url}
2017-09-30 10:42:26 +08:00
#git pull
git pull remote_origin master
2017-09-30 14:33:01 +08:00
if [ $? - eq 0 ] ; then
2017-09-30 11:30:51 +08:00
echo - e 'git pull succssed'
2017-09-30 10:42:26 +08:00
else
2017-09-30 11:30:51 +08:00
git reset - - hard #{@myshixun.commit_id}
git pull remote_origin master
2017-09-30 14:33:01 +08:00
if [ $? - eq 0 ] ; then
2017-09-30 11:30:51 +08:00
echo - e 'git reset and pull succssed'
2017-09-30 10:42:26 +08:00
else
2017-09-30 11:30:51 +08:00
echo - e 'git reset and pull failed'
2017-09-30 10:42:26 +08:00
fi
fi
2017-09-30 11:30:51 +08:00
git push - f origin master
2017-09-30 14:33:01 +08:00
if [ $? - eq 0 ] ; then
2017-09-30 11:30:51 +08:00
echo - e 'git push succssed'
2017-09-30 14:33:01 +08:00
cd .. /
rm - rf #{myshixun_rep_identifier}/
2017-09-30 10:42:26 +08:00
else
2017-09-30 11:30:51 +08:00
echo - e 'git push failed'
fi "
aFile = File . new ( File . join ( " #{ path } " , " #{ @myshixun . identifier } .sh " ) , " w+ " )
if aFile
aFile . syswrite ( script )
else
logger . error " Unable to open file! "
end
2017-09-30 14:33:01 +08:00
system ( " sh #{ path } / #{ @myshixun . identifier } .sh " )
# stdin, stdout, stderr = Open3.popen3("sh #{path}/#{@myshixun.identifier}.sh")
2017-09-30 10:42:26 +08:00
shixun_new_commit = g . commits ( @myshixun . shixun . try ( :gpid ) ) . first . try ( :id )
@myshixun . update_column ( :commit_id , shixun_new_commit )
2017-09-30 14:33:01 +08:00
sleep ( 2 ) # g
2017-09-30 15:29:46 +08:00
render :json = > { :status = > 200 }
2017-09-30 10:42:26 +08:00
end
2017-04-07 10:16:13 +08:00
def index
@games = @myshixun . games . includes ( :challenge )
@games_count = @games . count
respond_to do | format |
format . html
format . js
end
end
# mushixun的版本库必须创建时就创建
# 首次进入版本库自动打开文件
# path: "" && path: @game.path
2017-04-14 15:47:20 +08:00
# @praise 判断是否点赞
2017-05-24 20:45:39 +08:00
# @tread 判断是否踩
2017-05-22 17:47:30 +08:00
# git_repository_url @myshixun, "Myshixun"
2017-07-31 13:54:10 +08:00
# @st 0 实践任务 1 多选任务 2 单选任务
2017-04-07 10:16:13 +08:00
def show
2017-09-30 14:57:03 +08:00
# 展示全部实训
2017-09-19 17:15:50 +08:00
@is_subject = params [ :is_subject ]
2017-04-28 23:35:27 +08:00
if @game . status == 1
# 放置页面ajax数据还没获取就关闭
@game . update_attribute ( :status , 0 )
end
2017-05-18 11:26:35 +08:00
# logger.info("#################%%%%%%#{@game.outputs.first.out_put}")
2017-05-15 17:00:52 +08:00
# @games = @myshixun.games.includes(:challenge)
2017-08-08 14:58:02 +08:00
@game_challenge = @game . challenge
2017-05-16 10:19:16 +08:00
@challenges = Challenge . where ( :shixun_id = > @myshixun . shixun_id )
2017-06-23 15:13:45 +08:00
@shixun = @myshixun . shixun
2017-09-30 10:42:26 +08:00
# 判断tpm是否修改了
@tpm_modified = tpm_is_modified ( @myshixun , @shixun . try ( :gpid ) )
# end
2017-08-08 14:58:02 +08:00
@praise_count = PraiseTread . where ( :praise_tread_object_id = > @game_challenge . id , :praise_tread_object_type = > " Challenge " ) . count
@tread_count = PraiseTread . where ( :praise_tread_object_id = > @game_challenge . id , :praise_tread_object_type = > " ChallengeTread " ) . count
2017-05-15 17:00:52 +08:00
# @games_count = @games.count
2017-07-27 14:44:27 +08:00
@st = @game_challenge . st
2017-05-14 15:28:18 +08:00
@had_done = @game . had_done
2017-04-14 15:47:20 +08:00
@praise = PraiseTread . where ( " praise_tread_object_id=? and praise_tread_object_type=? and user_id=? " , @game_challenge . id , @game_challenge . class . to_s , User . current . id ) . first
@tread = PraiseTread . where ( " praise_tread_object_id=? and praise_tread_object_type=? and user_id=? " , @game_challenge . id , " ChallengeTread " , User . current . id ) . first
2017-04-07 10:16:13 +08:00
@rev = @rev . nil? ? " master " : @rev
@type = params [ :type ]
2017-04-21 19:34:57 +08:00
challenge_path = @game_challenge . path
2017-09-15 22:48:42 +08:00
@discusses_count = @shixun . discusses . where ( :parent_id = > nil ) . count
2017-07-28 10:40:37 +08:00
# 用户选项
2017-09-13 16:01:18 +08:00
#@user_answer = @game.outputs.first.actual_output.split("") unless @st == 0 || @game.outputs.blank?
2017-04-07 10:16:13 +08:00
# 默认打开文件
2017-05-14 15:28:18 +08:00
logger . info ( " repository id is #{ @repository . try ( :id ) } , repository's shixun_id = #{ @repository . try ( :myshixun_id ) } shixun_id = #{ @repository . try ( :shixun_id ) } , project_id => #{ @repository . try ( :project_id ) } " )
2017-04-21 19:34:57 +08:00
# 验证challenge文件名的合法性
2017-04-21 20:21:52 +08:00
if @path . blank? && ! challenge_path . blank? && challenge_path . include? ( " . " ) && @type != " root "
2017-04-21 19:34:57 +08:00
@path = challenge_path . strip
2017-04-20 14:10:59 +08:00
file_content = @g . files ( @myshixun . gpid , @path , @rev ) . content
2017-04-21 19:34:57 +08:00
if file_content . blank?
# gitlab缺陷: forked完成, 短暂时间内取不了内容的, 所以做一个小轮询, 间隔0.1秒
# 超过2秒则失败, 需通过页面刷新
2017-04-21 20:21:52 +08:00
for i in 0 .. 20 do
2017-04-21 19:34:57 +08:00
sleep ( 0 . 1 )
file_content = @g . files ( @myshixun . gpid , @path , @rev ) . content
unless file_content . blank?
logger . info ( " start: file_content is # # # #{ file_content . first ( 6 ) } " )
break
end
end
2017-04-21 16:10:46 +08:00
end
2017-08-10 15:43:04 +08:00
@content = tran_base64_decode64 ( file_content )
2017-04-20 09:47:56 +08:00
end
2017-04-21 19:34:57 +08:00
if @content . nil?
2017-04-07 10:16:13 +08:00
@entries = @repository . entries ( @path , @rev )
end
2017-04-18 14:09:08 +08:00
# 页面前后端分离数据转换
test_sets = @game_challenge . test_sets
unless test_sets . blank?
2017-04-20 17:19:18 +08:00
@test_sets = test_set_static_data ( test_sets )
2017-04-07 10:16:13 +08:00
end
2017-04-18 16:14:11 +08:00
2017-04-18 14:09:08 +08:00
logger . info ( " # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #{ @public_test_sets } " )
2017-04-18 17:55:15 +08:00
@test_sets_count = test_sets . count
2017-05-05 16:14:35 +08:00
@test_sets_hidden_count = test_sets . blank? ? 0 :test_sets . select { | test_set | test_set . is_public == false } . count
@test_sets_public_count = @test_sets_count - @test_sets_hidden_count
2017-08-08 17:30:05 +08:00
had_test = Output . where ( :game_id = > @game . id , :query_index = > ( @game . query_index - 1 ) )
2017-05-05 16:14:35 +08:00
@had_test_count = had_test . count
@had_passed_testsests_error_count = had_test . blank? ? 0 : had_test . select { | had_test | had_test . result == false } . count
2017-09-04 10:52:30 +08:00
@had_passed_testsests_error_count = @had_test_count - @had_passed_testsests_error_count
2017-05-05 16:14:35 +08:00
@had_passed_testsests_hidden_count = had_test . blank? ? 0 : had_test . select { | had_test | had_test . result == true && had_test . is_public == false } . count
@had_passed_testsests_public_count = had_test . blank? ? 0 : had_test . select { | had_test | had_test . result == true && had_test . is_public == true } . count
2017-06-30 22:37:54 +08:00
@final_score = ( ( @game . answer_open? || @shixun . status < = 1 ) ? 0 : @game . final_score . to_i )
2017-09-21 17:07:21 +08:00
@gold = @myshixun . shixun . status < = 1 ? 0 : ( @game . answer_open? ? - @game_challenge . score . to_i : @game . final_score . to_i )
2017-08-08 17:30:05 +08:00
@had_done = @game . had_done
2017-08-04 09:38:10 +08:00
@language = @game_challenge . shixun . language
2017-05-05 16:14:35 +08:00
error_position = had_test . blank? ? nil : had_test . select { | had_test | had_test . result == false } . last
2017-05-12 16:19:38 +08:00
logger . info ( " latest output id is #{ error_position . id unless error_position . blank? } " )
2017-09-07 14:22:12 +08:00
@latest_output = error_position . try ( :out_put ) . gsub ( / \ n / , '<br/>' ) . gsub ( / \ t / , ' ' ) . to_json unless error_position . try ( :out_put ) . blank?
2017-05-18 11:26:35 +08:00
logger . info ( @latest_output )
2017-04-07 10:16:13 +08:00
respond_to do | format |
2017-04-18 16:14:11 +08:00
format . html
2017-04-07 10:16:13 +08:00
format . js
end
rescue Exception = > e
2017-04-21 19:34:57 +08:00
logger . error ( " game error # # # # # # # # # # # # # #{ e . message } " )
2017-06-23 15:13:45 +08:00
flash [ :error ] = e . message
2017-04-07 10:16:13 +08:00
end
# 代码预览
def entry
2017-04-21 19:34:57 +08:00
# entry_and_raw(false)
file_content = @g . files ( @myshixun . gpid , @path , @rev ) . content
2017-08-10 14:33:34 +08:00
@content = tran_base64_decode64 ( file_content )
2017-04-21 19:34:57 +08:00
# @content = @repository.cat(@path, @rev)
2017-04-07 10:16:13 +08:00
end
def file_edit
# entry_and_raw(false)
# @content = @repository.cat(@path, @rev).strip
# # respond_to do |format|
# # format.js
# end
end
def file_update
@g = Gitlab . client
2017-06-28 11:01:11 +08:00
content = params [ :content ] . try ( :strip )
2017-05-08 16:44:51 +08:00
code_file = @g . edit_file ( @myshixun . gpid , :content = > params [ :content ] , :file_path = > @path , :branch_name = > @rev , :commit_message = > " shixun exec " )
unless code_file . blank?
render :json = > { :success = > " success " }
end
2017-04-21 16:57:59 +08:00
= begin
2017-04-07 10:16:13 +08:00
respond_to do | format |
2017-04-21 16:57:59 +08:00
format . js
2017-04-07 10:16:13 +08:00
end
rescue Exception = > e
puts e . message
2017-04-21 16:57:59 +08:00
= end
2017-04-07 10:16:13 +08:00
end
# json调用, 显示左边的隐藏具体详情@game 字段 msg
# 刷新右边的输出结果 @game out_put
def outputs_show
@output = Output . find ( params [ :game_output_id ] )
respond_to do | format |
format . js
end
end
2017-08-29 17:07:11 +08:00
# 获取评测的等待的时间
# res['waitNum'] 评测等待人数
# res['waitingTime'] 评测等待时间
# res['onRun'] 0 表示在等待中 1 表示可执行
def get_waiting_time
shixun_tomcat = Redmine :: Configuration [ 'shixun_tomcat' ]
uri = " #{ shixun_tomcat } /bridge/game/getWaitingTime "
params = { :tpiID = > " #{ @myshixun . id } " }
2017-09-06 17:53:13 +08:00
resubmit = @_params [ 'resubmit' ]
2017-08-29 17:07:11 +08:00
res = uri_exec uri , params
if ( res && res [ 'code' ] != 0 )
@message = " 实训云平台繁忙( 繁忙等级: 94) "
raise ( " 实训云平台繁忙( 繁忙等级: 94) " )
end
2017-09-06 17:53:13 +08:00
render :json = > { :result = > " success " , :onRun = > res [ 'onRun' ] , :waitNum = > res [ 'waitNum' ] , :waitingTime = > res [ 'waitingTime' ] , :resubmit = > resubmit }
2017-09-30 10:42:26 +08:00
#render :json => {:result => "success", :onRun => 0, :waitNum => 12, :waitingTime => 1234 }
2017-08-29 17:07:11 +08:00
rescue Exception = > e
render :json = > { :result = > " failed " }
end
2017-04-07 10:16:13 +08:00
# status 0: 未提交测评或者提交测评失败后报错; 1: 中间状态还没返回值; 2: 返回值并成功
2017-05-15 16:37:46 +08:00
# resubmit 唯一随机码,为了区分每一次重新评测结果
CODES = %W( 1 2 3 4 5 6 7 8 9 A B C D E F G H J K L N M O P Q R S T U V W X Y Z )
2017-04-07 10:16:13 +08:00
def game_build
game_challenge = @game . challenge
2017-06-20 11:34:46 +08:00
shixun = @myshixun . shixun
2017-06-30 09:25:17 +08:00
gitUrl = git_myshixun_url_ip ( @myshixun , @myshixun . try ( :user_id ) )
2017-08-06 15:34:24 +08:00
gitUrl = Base64 . urlsafe_encode64 ( gitUrl )
2017-04-26 17:01:25 +08:00
taskId = @game . id
2017-05-15 16:37:46 +08:00
code = CODES . sample ( 8 ) . join
2017-06-22 21:35:24 +08:00
# status为2说明是重新评测
2017-05-15 16:37:46 +08:00
if @game . status == 2
resubmit = " #{ code } _ #{ @myshixun . id } "
2017-06-22 21:35:24 +08:00
else
2017-08-08 11:08:59 +08:00
# 重新评测不影响已通关的实训状态
@game . update_attributes! ( :status = > 1 ) if resubmit . nil?
2017-05-15 16:37:46 +08:00
end
2017-06-18 14:40:37 +08:00
2017-08-04 21:34:37 +08:00
gameInfo = shixun . gameInfo
2017-08-04 14:19:22 +08:00
shixun_tomcat = Redmine :: Configuration [ 'shixun_tomcat' ]
2017-05-12 19:33:26 +08:00
step = game_challenge . try ( :position )
2017-06-22 21:35:24 +08:00
# 注意:这个地方的参数写的时候不能换行
2017-08-08 11:08:59 +08:00
params = { :tpiID = > " #{ @myshixun . id } " , :buildID = > " #{ taskId } " , :instanceGitURL = > " #{ gitUrl } " , :instanceChallenge = > " #{ step } " , :resubmit = > resubmit , :operationEnvironment = > " #{ shixun . try ( :language ) } " }
2017-08-13 10:48:05 +08:00
# uri = URI("#{shixun_tomcat}/bridge/game/gameEvaluate")
uri = " #{ shixun_tomcat } /bridge/game/gameEvaluate "
2017-05-12 19:33:26 +08:00
res = uri_exec uri , params
logger . info ( " # # # # # # # # # # # # # # # # # # # # # buildJobForInstance result is #{ res } " )
2017-06-22 21:35:24 +08:00
logger . info ( " # # # # # # # # # # # # # # # # # # # # # buildJobForInstance params is #{ params } " )
2017-05-12 19:33:26 +08:00
if ( res && res [ 'code' ] != 0 )
2017-05-23 21:26:42 +08:00
@message = " 实训云平台繁忙( 繁忙等级: 86) "
raise ( " 实训云平台繁忙( 繁忙等级: 86) " )
2017-04-07 10:16:13 +08:00
end
2017-05-15 16:37:46 +08:00
render :json = > { :result = > " success " , :resubmit = > resubmit }
2017-08-10 16:29:16 +08:00
rescue Exception = > e
2017-05-15 16:37:46 +08:00
if resubmit . nil?
2017-05-14 15:28:18 +08:00
@game . update_attributes! ( :status = > 0 )
end
2017-08-10 16:29:16 +08:00
logger . error ( " # # # # # # # # # # # # # # # # # # # " + e . message )
2017-04-18 10:49:52 +08:00
#REDO Jenkins服务异常弹框抛出
2017-05-12 19:33:26 +08:00
render :json = > { :result = > " failed " }
2017-04-07 10:16:13 +08:00
end
2017-05-04 17:29:00 +08:00
# @myshixun -status 1:完成实训
2017-05-10 10:43:23 +08:00
# 全部通关奖励1000分
2017-05-15 16:37:46 +08:00
# retry_status 评测的唯一标识,重新评测对以前已产生的结果不造成影响
# params[:resubmit] 不为空表示已通过该关后重新评测, 重新评测必须是通过该关后才能执行
2017-05-14 15:28:18 +08:00
# @had_done 1 通关所有关
2017-04-07 10:16:13 +08:00
def game_status
game_challenge = @game . challenge
2017-05-14 15:28:18 +08:00
had_done = @game . had_done
2017-05-15 16:37:46 +08:00
resubmit_identifier = @game . resubmit_identifier
2017-06-23 10:38:40 +08:00
shixun = @myshixun . shixun
2017-08-11 22:03:25 +08:00
score = 0
tag_count = 0
2017-05-15 16:37:46 +08:00
logger . info ( " # # # # # resubmit_identifier is #{ resubmit_identifier } " )
if params [ :resubmit ] . blank? # 非重新评测
if had_done == 1 # 通过最后一关,即所有关卡
2017-05-10 10:43:23 +08:00
@myshixun . update_attribute ( :status , 1 )
2017-06-30 20:08:19 +08:00
end
# 通过当前一关
if @game . status == 2
@game . update_attribute ( :end_time , Time . now )
game_passed_code ( @game . id , game_challenge . try ( :path ) , @myshixun . try ( :gpid ) , 1 )
if ! @game . answer_open && shixun . status > 1
2017-08-10 10:18:28 +08:00
reward_grade ( @game . user , @game . id , 'Game' , game_challenge . score )
reward_experience ( @game . user , @game . id , 'Game' , game_challenge . score )
2017-08-11 22:03:25 +08:00
score = game_challenge . score
tag_count = game_challenge . challenge_tags . count
elsif shixun . status > 1 && @game . answer_open
score = - game_challenge . score . to_i
tag_count = 0
else
score = " +0 "
tag_count = 0
2017-05-10 10:43:23 +08:00
end
2017-05-04 17:29:00 +08:00
end
2017-05-12 21:48:06 +08:00
game_status = @game . status
2017-05-15 16:37:46 +08:00
else # 重新评测
if params [ :resubmit ] == resubmit_identifier # 本次重新评测结果已经返回并存入数据库
game_status = ( @game . retry_status == 2 ? 2 : 0 ) # retry_status 2: 成功 1: 失败
2017-05-26 09:48:06 +08:00
if game_status == 2 && had_done == 1 # 重新评测如果评测成功
had_done = 1
2017-06-23 10:34:28 +08:00
game_passed_code ( @game . id , game_challenge . try ( :path ) , @myshixun . try ( :gpid ) , 1 )
2017-05-26 09:48:06 +08:00
else
had_done = 0
end
2017-05-15 16:37:46 +08:00
else
# game_status 在前端有行为判断, 2表示通关; 0表示失败; 1表示评测中的中间状态
game_status = 1
had_done = 0
end
2017-04-07 10:16:13 +08:00
end
2017-04-18 14:09:08 +08:00
test_sets = game_challenge . test_sets
unless test_sets . blank?
2017-04-20 17:19:18 +08:00
total_test_sets = test_set_static_data ( test_sets )
2017-04-18 14:09:08 +08:00
end
2017-05-05 16:14:35 +08:00
logger . info ( " # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #{ @public_test_sets } " )
@test_sets_count = test_sets . count
@test_sets_hidden_count = test_sets . blank? ? 0 :test_sets . select { | test_set | test_set . is_public == false } . count
@test_sets_public_count = @test_sets_count - @test_sets_hidden_count
2017-08-08 17:30:05 +08:00
had_test = Output . where ( :game_id = > @game . id , :query_index = > ( @game . query_index - 1 ) )
2017-05-05 16:14:35 +08:00
@had_test_count = had_test . count
@had_passed_testsests_error_count = had_test . blank? ? 0 : had_test . select { | had_test | had_test . result == false } . count
2017-09-04 10:52:30 +08:00
@had_passed_testsests_error_count = @had_test_count - @had_passed_testsests_error_count
2017-05-05 16:14:35 +08:00
@had_passed_testsests_hidden_count = had_test . blank? ? 0 : had_test . select { | had_test | had_test . result == true && had_test . is_public == false } . count
@had_passed_testsests_public_count = had_test . blank? ? 0 : had_test . select { | had_test | had_test . result == true && had_test . is_public == true } . count
2017-06-30 22:37:54 +08:00
@final_score = ( ( @game . answer_open? || shixun . status < = 1 ) ? 0 : @game . final_score . to_i )
2017-09-21 17:07:21 +08:00
@gold = @myshixun . shixun . status < = 1 ? 0 : ( @game . answer_open? ? - game_challenge . score . to_i : @game . final_score . to_i )
2017-05-05 16:14:35 +08:00
error_position = had_test . blank? ? nil : had_test . select { | had_test | had_test . result == false } . last
2017-09-17 16:22:04 +08:00
@latest_output = error_position . try ( :out_put ) . gsub ( / \ n / , '<br/>' ) . gsub ( / \ t / , ' ' ) unless error_position . try ( :out_put ) . blank?
2017-09-07 20:35:30 +08:00
logger . info ( " @compile_success # # # # # # # # # # # # # # # # # # #{ @compile_success } " )
2017-08-04 09:38:10 +08:00
@language = game_challenge . shixun . language
2017-05-05 16:14:35 +08:00
render :json = > { test_sets : total_test_sets ,
test_sets_count : @test_sets_count ,
test_sets_hidden_count : @test_sets_hidden_count ,
test_sets_public_count : @test_sets_public_count ,
had_test_count : @had_test_count ,
had_passed_testsests_error_count : @had_passed_testsests_error_count ,
had_passed_testsests_hidden_count : @had_passed_testsests_hidden_count ,
had_passed_testsests_public_count : @had_passed_testsests_public_count ,
final_score : @final_score ,
2017-09-21 10:37:35 +08:00
gold : @gold ,
2017-05-05 16:14:35 +08:00
latest_output : @latest_output ,
2017-05-12 21:48:06 +08:00
status : game_status ,
2017-08-04 09:38:10 +08:00
had_done : had_done ,
2017-08-11 22:03:25 +08:00
language : @language ,
score : score ,
2017-09-08 11:21:08 +08:00
tag_count : tag_count ,
position : game_challenge . position
2017-09-07 14:22:12 +08:00
}
2017-04-07 10:16:13 +08:00
end
2017-07-28 10:40:37 +08:00
# 选择题评测(选择题不需要重新评测)
2017-09-14 17:30:58 +08:00
# score 获得金币数 tag_count 技能数 right用户是否全对 answer_right全部选择题的正确性
2017-07-28 10:40:37 +08:00
def evaluating_choice
2017-09-14 17:30:58 +08:00
@game_challenge = @game . challenge
@challenges = Challenge . where ( :shixun_id = > @myshixun . shixun_id )
@shixun = @myshixun . shixun
score = 0
tag_count = 0
@right = true
answer_right = [ ]
@game_challenge . challenge_chooses . each_with_index do | choose , index |
correct = ( params [ :answer ] [ index ] == choose . standard_answer ) ? true : false
2017-09-26 15:03:28 +08:00
#choose.current_choose_outputs.delete if choose.current_choose_outputs.present?
#ChooseOutputs.create(:challenge_choose_id => choose.id, :user_id => User.current.id, :answer => params[:answer][index], :correct => correct)
Output . create ( :game_id = > @game . id , :test_set_position = > choose . position , :actual_output = > params [ :answer ] [ index ] , :result = > correct , :query_index = > @game . query_index )
2017-09-14 17:30:58 +08:00
if @shixun . status > 1 && ! @game . answer_open
2017-09-13 16:01:18 +08:00
if correct
2017-09-14 17:30:58 +08:00
score += choose . score
tag_count += choose . challenge_tags . count
2017-09-13 16:01:18 +08:00
end
2017-09-14 17:30:58 +08:00
elsif @shixun . status > 1 && @game . answer_open
score -= choose . score
2017-08-09 09:01:22 +08:00
end
2017-09-14 17:30:58 +08:00
unless correct
@right = false
2017-09-13 16:01:18 +08:00
end
2017-09-14 17:30:58 +08:00
answer_right << correct
end
@had_done = @game . had_done
2017-09-25 11:16:16 +08:00
if @right
@game . update_attributes ( :status = > 2 , :end_time = > Time . now )
reward_grade ( @game . user , @game . id , 'Game' , score )
reward_experience ( @game . user , @game . id , 'Game' , score )
@game . update_attribute ( :final_score , score )
end
2017-09-14 17:30:58 +08:00
respond_to do | format |
2017-09-25 14:16:45 +08:00
format . js { redirect_to myshixun_game_path ( @game , :myshixun_id = > @myshixun , :choose = > @right ) }
2017-07-28 10:40:37 +08:00
end
end
2017-04-07 10:16:13 +08:00
def change_status
@game . update_attribute ( :status , 0 )
outputs = @game . outputs
if outputs . count == 0
outputs = " "
else
outputs = outputs . map { | result | [ result . code , result . id ] }
end
render :json = > { status : @game . status , output : " 服务器网络异常 " , results : outputs , had_done : 0 }
end
2017-06-14 16:29:26 +08:00
# 获取等待的任务数
# 如果没有等待任务页面上将不显示
# msg: -1 不存在等待的任务
# 如果该方法异常则直接返回0, 跳过等待
def waiting_info
begin
2017-08-04 14:19:22 +08:00
shixun_tomcat = Redmine :: Configuration [ 'shixun_tomcat' ]
2017-06-14 16:29:26 +08:00
params = { :buildID = > " #{ @game . id } " }
2017-08-04 14:19:22 +08:00
uri = URI ( " #{ shixun_tomcat } /jenkins-exec/api/getWaitingTime " )
2017-06-14 16:29:26 +08:00
res = uri_exec uri , params
if ( res && res [ 'code' ] != 0 )
raise ( " 获取等待任务数异常 " )
end
task_count = res [ 'msg' ] . to_i
task_count > 0 ? task_count : 0
render :json = > { task_count : task_count }
rescue
render :json = > { task_count : 0 }
end
end
2017-04-07 10:16:13 +08:00
# 自动推送下一个任务
def next_step
2017-08-30 17:30:32 +08:00
unless show_next_stage? ( @game , @myshixun . shixun . try ( :status ) )
render_403
end
2017-04-07 10:16:13 +08:00
next_game = @game . next_game
2017-04-14 10:58:25 +08:00
next_game . update_attributes ( :status = > 0 , :open_time = > Time . now ) if next_game . status == 3
2017-04-28 16:01:38 +08:00
respond_to do | format |
format . js { redirect_to myshixun_game_path ( next_game , :myshixun_id = > @myshixun ) }
end
2017-04-07 10:16:13 +08:00
end
2017-09-08 11:21:08 +08:00
def prev_step
prev_game = @game . prev_game
respond_to do | format |
format . js { redirect_to myshixun_game_path ( prev_game , :myshixun_id = > @myshixun ) }
end
end
2017-05-04 14:47:19 +08:00
# 获取答案,第一次查看需扣掉该关卡的积分
# 如果已看过,下次可以免费查看
# viewed 1: 直接查看 2: 弹框提示将要扣除积分, 确定后显示内容 3: 弹框提示分数不够
def answer
challenge = @game . challenge
2017-05-05 14:57:58 +08:00
@challenge_score = challenge . score . to_i
2017-09-14 18:45:19 +08:00
@answer = " "
2017-05-04 14:47:19 +08:00
# 已经开启过
2017-05-25 16:52:08 +08:00
@score = User . current . grade . to_i - @challenge_score
2017-09-14 18:45:19 +08:00
if ( params [ :choose ] == " true " )
challenge . challenge_chooses . each_with_index do | choose , index |
@answer += " 第 " + ( ( index + 1 ) . to_s ) + " 题:<br /> "
2017-09-26 09:00:30 +08:00
@answer += ( choose . answer . blank? ? choose . standard_answer : choose . answer )
2017-09-14 18:45:19 +08:00
@answer += " <br /><br /><br /> "
end
else
@answer = challenge . answer
end
2017-09-15 21:14:17 +08:00
if challenge . shixun . status < 2 || @game . answer_open || ( challenge . st != 0 && @game . status == 2 )
2017-05-05 14:57:58 +08:00
@viewed = 1
2017-05-04 14:47:19 +08:00
else
2017-05-05 14:57:58 +08:00
if @score > = 0
@viewed = 2
2017-05-04 14:47:19 +08:00
else
2017-05-05 14:57:58 +08:00
@viewed = 3
2017-05-04 14:47:19 +08:00
end
end
2017-05-05 14:57:58 +08:00
respond_to do | format |
format . js
2017-05-21 15:00:24 +08:00
format . html
2017-05-05 14:57:58 +08:00
end
end
def minus_score
2017-05-24 17:39:36 +08:00
@game_challenge = @game . challenge
2017-05-05 14:57:58 +08:00
user = User . current
2017-05-24 17:39:36 +08:00
challenge_score = @game_challenge . score . to_i
2017-05-25 15:49:25 +08:00
reward_grade ( user , @game . id , 'Answer' , - challenge_score )
2017-09-27 10:27:51 +08:00
@answer = " "
if @game . challenge . st == 0
@answer = @game_challenge . answer
else
@game_challenge . challenge_chooses . each_with_index do | choose , index |
@answer += " 第 " + ( ( index + 1 ) . to_s ) + " 题:<br /> "
@answer += ( choose . answer . blank? ? choose . standard_answer : choose . answer )
@answer += " <br /><br /><br /> "
end
2017-05-05 14:57:58 +08:00
end
2017-09-27 10:27:51 +08:00
@game . update_column ( :answer_open , true )
2017-05-04 14:47:19 +08:00
end
2017-05-25 10:09:53 +08:00
def refresh_game_list
myshixun_id = params [ :myshixun_id ]
@myshixun = Myshixun . find_by_identifier ( myshixun_id )
@challenges = Challenge . where ( :shixun_id = > @myshixun . shixun_id )
respond_to do | format |
format . js
end
end
2017-06-22 16:21:47 +08:00
def reset_original_code
begin
2017-07-07 22:36:48 +08:00
@shixun = @myshixun . shixun
2017-06-27 16:33:42 +08:00
@g = Gitlab . client
2017-06-22 16:21:47 +08:00
game_code = GameCode . where ( :game_id = > @game . id ) . first
content = game_code . try ( :original_code )
2017-06-27 16:33:42 +08:00
if content . nil?
2017-06-30 11:05:21 +08:00
raise ( " 初始代码为空,代码重置失败 " )
2017-06-27 16:33:42 +08:00
end
@path = params [ :path ]
@rev = @rev . nil? ? " master " : @rev
2017-06-22 16:21:47 +08:00
message = " file reset "
2017-06-27 16:33:42 +08:00
update_gitlab_file ( content , @path , @rev , message )
file_content = @g . files ( @myshixun . gpid , @path , @rev ) . content
2017-08-10 14:33:34 +08:00
@content = tran_base64_decode64 ( file_content )
2017-06-27 16:33:42 +08:00
if @content . nil?
2017-06-30 11:05:21 +08:00
raise ( " 初始代码为空,代码重置失败 " )
2017-06-27 16:33:42 +08:00
# @entries = @repository.entries(@path, @rev)
end
2017-06-22 20:14:05 +08:00
respond_to do | format |
2017-06-27 16:33:42 +08:00
format . js
2017-06-22 20:14:05 +08:00
end
2017-06-27 16:33:42 +08:00
rescue Exception = > e
@error_messages = e . message
2017-06-22 16:21:47 +08:00
end
end
def reset_new_code
begin
2017-07-07 22:36:48 +08:00
@shixun = @myshixun . shixun
2017-06-27 16:33:42 +08:00
@g = Gitlab . client
2017-06-22 16:21:47 +08:00
game_code = GameCode . where ( :game_id = > @game . id ) . first
content = game_code . try ( :new_code )
2017-06-27 16:33:42 +08:00
if content . nil?
raise ( " 示例代码为空,代码重置失败 " )
end
@path = params [ :path ]
@rev = @rev . nil? ? " master " : @rev
2017-06-22 16:21:47 +08:00
message = " file reset "
2017-06-27 16:33:42 +08:00
update_gitlab_file ( content , @path , @rev , message )
file_content = @g . files ( @myshixun . gpid , @path , @rev ) . content
2017-08-10 14:33:34 +08:00
@content = tran_base64_decode64 ( file_content )
2017-06-27 16:33:42 +08:00
if @content . nil?
raise ( " 示例代码为空,代码重置失败 " )
# @entries = @repository.entries(@path, @rev)
end
2017-06-22 20:14:05 +08:00
respond_to do | format |
2017-06-27 16:33:42 +08:00
format . js
2017-06-22 20:14:05 +08:00
end
2017-06-27 16:33:42 +08:00
rescue Exception = > e
@error_messages = e . message
2017-06-22 16:21:47 +08:00
end
end
2017-04-07 10:16:13 +08:00
private
2017-06-23 10:38:40 +08:00
def update_gitlab_file content , path , rev , message
2017-06-27 16:33:42 +08:00
code_file = @g . edit_file ( @myshixun . gpid , :content = > content , :file_path = > path , :branch_name = > rev , :commit_message = > " #{ message } " )
2017-06-22 16:21:47 +08:00
if code_file . nil?
2017-06-27 16:33:42 +08:00
raise ( " 网络异常,文件更新失败 " )
2017-06-22 16:21:47 +08:00
end
end
2017-04-18 10:49:52 +08:00
def test_set_static_data test_sets
2017-04-20 17:19:18 +08:00
test_result = [ ]
2017-04-18 10:49:52 +08:00
unless test_sets . blank?
test_sets . each do | test_set |
2017-04-20 17:19:18 +08:00
output = Output . where ( :game_id = > @game . id , :test_set_position = > test_set . position ) . first
2017-09-17 16:43:36 +08:00
actual_output = output . try ( :actual_output )
2017-04-20 17:19:18 +08:00
result = output . try ( :result )
2017-09-08 16:22:51 +08:00
compile_success = ( actual_output . blank? || ! actual_output . downcase . match ( / error / ) . blank? ) ? 0 : 1
2017-04-20 17:19:18 +08:00
public_result = { :is_public = > ( test_set . is_public ? 1 : 0 ) , :result = > result , :input = > test_set . input ,
2017-09-08 09:18:33 +08:00
:actual_output = > actual_output , :output = > test_set . output , :compile_success = > compile_success }
2017-04-20 15:12:33 +08:00
logger . info ( " 1111 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #{ public_result . to_json } " )
2017-04-20 17:19:18 +08:00
test_result << public_result . to_json
2017-04-18 10:49:52 +08:00
end
end
2017-04-20 17:19:18 +08:00
test_result = test_result . blank? ? test_result : test_result . join ( " , " )
logger . info ( " 222 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #{ test_result } " )
return test_result
2017-04-18 10:49:52 +08:00
end
2017-04-07 10:16:13 +08:00
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 ) || User . current . admin? || User . current . id == @myshixun . shixun . try ( :user_id ) )
end
# 判断成员是否允许查看
def allowd_view
2017-09-01 13:20:36 +08:00
if @myshixun . shixun . try ( :status ) == 2 && @game . status == 3 && User . current . id != @myshixun . shixun . try ( :user_id )
render_403
end
# render_403 if (@game.status == 3 && User.current.id != @myshixun.shixun.try(:user_id))
2017-04-07 10:16:13 +08:00
end
def find__shixun_language
language = @myshixun . shixun . try ( :language )
@language = language_switch ( language )
end
# Find myshixun of id params[:id]
def find_myshixun
myshixun_id = params [ :myshixun_id ] || ( params [ :game ] && params [ :game ] [ :myshixun_id ] )
2017-04-26 16:03:05 +08:00
@myshixun = Myshixun . find_by_identifier ( myshixun_id )
if @myshixun . nil?
render_404
return
end
2017-04-07 10:16:13 +08:00
rescue ActiveRecord :: RecordNotFound
render_404
end
def find_game
2017-04-26 14:20:07 +08:00
# myshixun_id = params[:myshixun_id]
@game = Game . find_by_identifier ( params [ :id ] )
if @game . nil?
render_404
return
end
2017-04-07 10:16:13 +08:00
@myshixun = @game . myshixun
rescue ActiveRecord :: RecordNotFound
render_404
end
end