diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb
index 61dcc454d..cc930f7b1 100644
--- a/app/controllers/games_controller.rb
+++ b/app/controllers/games_controller.rb
@@ -3,7 +3,7 @@ class GamesController < ApplicationController
layout "base_myshixun"
skip_before_filter :verify_authenticity_token, :only => [:file_update]
before_filter :find_myshixun, :only => [:index]
- before_filter :find_game, :only => [:show, :game_build, :entry,:next_step, :outputs_show, :file_edit, :file_update, :game_status, :change_status, :answer]
+ before_filter :find_game, :only => [:show, :game_build, :entry,:next_step, :outputs_show, :file_edit, :file_update, :game_status, :change_status, :answer, :minus_score]
before_filter :find_repository, :only => [:show, :entry, :file_edit, :file_update]
before_filter :allowd_manager
before_filter :allowd_view, :only => [:show]
@@ -69,23 +69,19 @@ class GamesController < ApplicationController
@test_sets = test_set_static_data(test_sets)
end
- @consume_time = (@game.status == 2 ? game_spend_time(@game.open_time, @game.updated_at) : game_spend_time(@game.open_time, Time.now))
logger.info("#################################{@public_test_sets}")
@test_sets_count = test_sets.count
- @test_sets_hidden_count = test_sets.where(:is_public => false).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
+ had_test = @game.outputs
+ @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
+ @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
@final_score = @game.final_score.to_i
- # @had_tested如果已评测过,output中至少会有一条记录
- @had_tested = Output.where(:game_id => @game.id).count
- @had_passed_testsests = Output.where(:game_id => @game.id, :result => true).count
- # 出错的测试用例序号
- error_position = Output.where(:game_id => @game.id, :result => false).last
- @error_position = error_position.blank? ? 0 : error_position.try(:test_set_position).to_i
- # @had_done 1 已通关
- (@myshixun.games.count == @game_challenge.position && @game.status ==2) ? @had_done = 1 : @had_done = 0
- # 异常结果
-
+ @had_done = (@myshixun.games.where(:status => 2).count == @shixun.game.count ? 1 : 0)
+ error_position = had_test.blank? ? nil : had_test.select{|had_test| had_test.result == false}.last
@latest_output = error_position.try(:out_put).gsub("\r\n", "
") unless error_position.try(:out_put).blank?
- # @latest_output = @game.latest_output.try(:expect_output)
respond_to do |format|
format.html
format.js
@@ -161,34 +157,64 @@ class GamesController < ApplicationController
redirect_to myshixun_game_path(@game, :myshixun_id => @myshixun)
end
+ # @myshixun -status 1:完成实训
def game_status
game_challenge = @game.challenge
# @had_done 1 已通关
(@myshixun.games.count == game_challenge.position && @game.status ==2) ? had_done = 1 : had_done = 0
- # 下一关自动开启
- if had_done != 1 && @game.status == 2
- @game.next_game.update_attribute(:status, 0)
+ # 全部通关奖励1000分
+ if had_done == 1
+ reward_grade(@game.user, @game.id, @game.class, 1000)
@myshixun.update_attribute(:status, 1)
+ else
+ # 通过当前一关
+ if @game.status == 2
+ @game.next_game.update_attribute(:status, 0)
+ reward_grade(@game.user, @game.id, @game.class, game_challenge.score)
+ end
end
test_sets = game_challenge.test_sets
unless test_sets.blank?
total_test_sets = test_set_static_data(test_sets)
end
- test_sets_count = test_sets.count
- test_sets_hidden_count = test_sets.where(:is_public => false).count
- final_score = @game.final_score.to_i
- # @had_tested如果已评测过,output中至少会有一条记录
- had_tested = Output.where(:game_id => @game.id).count
- had_passed_testsests = Output.where(:game_id => @game.id, :result => true).count
- # 出错的测试用例序号
- error_position = Output.where(:game_id => @game.id, :result => false).last
- static_error_position = error_position.blank? ? 0 : error_position.try(:test_set_position).to_i
- consume_time = @game.status == 2 ? game_spend_time(@game.open_time, @game.updated_at) : game_spend_time(@game.open_time, Time.now)
+
+ 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
+ had_test = @game.outputs
+ @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
+ @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
+ @final_score = @game.final_score.to_i
+ @had_done = (@myshixun.games.where(:status => 2).count == @myshixun.games.count ? 1 : 0)
+ error_position = had_test.blank? ? nil : had_test.select{|had_test| had_test.result == false}.last
+ @latest_output = error_position.try(:out_put).gsub("\r\n", "
") unless error_position.try(:out_put).blank?
+ # test_sets_count = test_sets.count
+ # test_sets_hidden_count = test_sets.where(:is_public => false).count
+ # final_score = @game.final_score.to_i
+ # # @had_tested如果已评测过,output中至少会有一条记录
+ # had_tested = Output.where(:game_id => @game.id).count
+ # had_passed_testsests = Output.where(:game_id => @game.id, :result => true).count
+ # # 出错的测试用例序号
+ # error_position = Output.where(:game_id => @game.id, :result => false).last
+ # static_error_position = error_position.blank? ? 0 : error_position.try(:test_set_position).to_i
+ # consume_time = @game.status == 2 ? game_spend_time(@game.open_time, @game.updated_at) : game_spend_time(@game.open_time, Time.now)
# 异常结果
latest_output = error_position.try(:out_put).gsub("\r\n", "
").html_safe unless error_position.try(:out_put).blank?
- render :json => {test_sets: total_test_sets, status: @game.status, had_done: had_done, latest_output: latest_output, error_position: static_error_position,
- had_passed_testsests:had_passed_testsests, test_sets_count:test_sets_count, test_sets_hidden_count:test_sets_hidden_count,
- final_score:final_score, had_tested:had_tested, consume_time: consume_time}
+ 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,
+ latest_output: @latest_output,
+ status: @game.status,
+ had_done: had_done}
end
def change_status
@@ -216,21 +242,40 @@ class GamesController < ApplicationController
# 如果已看过,下次可以免费查看
# viewed 1: 直接查看 2:弹框提示将要扣除积分,确定后显示内容 3:弹框提示分数不够
def answer
+ challenge = @game.challenge
+ user = User.current
+ @challenge_score = challenge.score.to_i
+ user_score = user.grade.to_i
+ @score = user_score - @challenge_score
+ # 已经开启过
+ @answer = challenge.answer
+ if @game.answer_open
+ @viewed = 1
+ else
+ if @score >= 0
+ @viewed = 2
+ else
+ @viewed = 3
+ end
+ end
+ respond_to do |format|
+ format.js
+ end
+ end
+
+ def minus_score
challenge = @game.challenge
user = User.current
challenge_score = challenge.score.to_i
user_score = user.grade.to_i
@score = user_score - challenge_score
- # 已经开启过
- if @game.answer_open
- render :json => {:answer => challenge.answer, :viewed => 1}
- else
- if @score > 0
- user.update_column(:grade, @score)
- render :json => {:answer => challenge.answer, :viewed => 2}
- else
- render :json => {:answer => false, :viewed => 3}
- end
+ @answer = challenge.answer
+
+ user.update_column(:grade, @score)
+ @game.update_column(:answer_open, true)
+
+ respond_to do |format|
+ format.js
end
end
diff --git a/app/controllers/myshixuns_controller.rb b/app/controllers/myshixuns_controller.rb
index d291ff729..fe64be14f 100644
--- a/app/controllers/myshixuns_controller.rb
+++ b/app/controllers/myshixuns_controller.rb
@@ -27,16 +27,14 @@ class MyshixunsController < ApplicationController
output = Output.where(:game_id => game.id, :test_set_position => j_test_set['caseId'].to_i).first
if output.nil?
game_outputs = Output.create(:code => status, :game_id => game_id, :out_put => outPut, :test_set_position => j_test_set['caseId'],
- :actual_output => actual_output, :result => j_test_set['passed'].to_i)
+ :actual_output => actual_output, :result => j_test_set['passed'].to_i, :is_public => j_test_set['isPublic'].to_i)
else
- output.update_attributes(:code => status, :out_put => outPut, :actual_output => actual_output.force_encoding("utf-8"), :result => j_test_set['passed'].to_i)
+ output.update_attributes(:code => status, :out_put => outPut, :actual_output => actual_output, :result => j_test_set['passed'].to_i)
end
-
end
end
if status == "0"
- game.update_attribute(:status, 2)
- game.update_attribute(:final_score, challenge.score)
+ game.update_attributes!(:status => 2, :final_score => challenge.score, :end_time => Time.now)
else
game.update_attribute(:status, 0)
end
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 383651ffa..65bfb0a05 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -37,7 +37,7 @@ module ApplicationHelper
def reward_grade(user, container_id, container_type, score)
grade = Grade.where(:user_id => user.id, :container_id => container_id, :container_type => container_type).first
unless grade.nil?
- user.update_column(:grade, (500 + user.grade.to_i))
+ user.update_column(:grade, (score.to_i + user.grade.to_i))
Grade.create!(:user_id => user.id, :container_id => container_id, :container_type => container_type, :score => score)
end
end
diff --git a/app/models/game.rb b/app/models/game.rb
index 0bed87484..79f4426fe 100644
--- a/app/models/game.rb
+++ b/app/models/game.rb
@@ -1,7 +1,7 @@
class Game < ActiveRecord::Base
# stauts 0: can exe 1:doing 2:successed 3:locked
default_scope :order => 'created_at desc'
- attr_accessible :myshixun_id, :user_id, :status, :final_score, :challenge_id, :open_time, :identifier
+ attr_accessible :myshixun_id, :user_id, :status, :final_score, :challenge_id, :open_time, :identifier, :answer_open, :end_time
belongs_to :myshixun,:touch=> true
belongs_to :user
belongs_to :challenge
@@ -14,7 +14,6 @@ class Game < ActiveRecord::Base
identifier
end
-
def last_game
challenge = self.challenge
last_challenge_id = challenge.last_challenge
diff --git a/app/views/challenges/_form.html.erb b/app/views/challenges/_form.html.erb
index 62cb6a08e..ba39f7da9 100644
--- a/app/views/challenges/_form.html.erb
+++ b/app/views/challenges/_form.html.erb
@@ -104,11 +104,11 @@
- / + /
@@ -60,10 +60,10 @@
- / + /
@@ -74,8 +74,8 @@