diff --git a/app/controllers/challenges_controller.rb b/app/controllers/challenges_controller.rb index f2a964c8d..bce0ae3c2 100644 --- a/app/controllers/challenges_controller.rb +++ b/app/controllers/challenges_controller.rb @@ -149,7 +149,11 @@ class ChallengesController < ApplicationController def index uid_logger("identifier: #{params}") # 通过调试发现 这里includes(:games)性能会慢10倍 - @challenges = @shixun.challenges.fields_for_list + # @challenges = @shixun.challenges.fields_for_list + @challenges = Challenge.fields_for_list.includes(:games).where("challenges.shixun_id" => @shixun.id) + + # @challenges = Challenge.includes(:games).where("games.user_id" => 12).where(shixun_id: @shixun.id) + @editable = current_user.manager_of_shixun?(@shixun) && @shixun.status == 0 @user = current_user end @@ -247,7 +251,6 @@ class ChallengesController < ApplicationController # 关卡位置被修改,需要修改脚本 script = modify_shixun_script @shixun, @shixun.evaluate_script @shixun.update_column(:evaluate_script, script) - end def index_up diff --git a/app/models/challenge.rb b/app/models/challenge.rb index 5495dd7a3..f25ce330a 100644 --- a/app/models/challenge.rb +++ b/app/models/challenge.rb @@ -51,9 +51,8 @@ class Challenge < ApplicationRecord end # 开启挑战 - def open_game(user_id) - game = self.games.select([:status, :identifier]).where(user_id: user_id).first - shixun = self.shixun + def open_game user_id, challenge_id, shixun + game = games.select{|game| game.challenge_id = challenge_id && game.user_id == user_id}.first if game.present? shixun.task_pass || game.status != 3 ? "/tasks/#{game.identifier}" : "" else @@ -61,18 +60,31 @@ class Challenge < ApplicationRecord end end + # # 开启挑战 + # def open_game(user_id, shixun) + # + # + # game = self.games.select([:status, :identifier]).where(user_id: user_id).first + # game = self.games.select{|game| game.user_id == user_id} + # if game.present? + # shixun.task_pass || game.status != 3 ? "/tasks/#{game.identifier}" : "" + # else + # "/api/shixuns/#{shixun.identifier}/shixun_exec" + # end + # end + ## 用户关卡状态 0: 不能开启实训; 1:直接开启; 2表示已完成 def user_tpi_status user_id # todo: 以前没加索引导致相同关卡,同一用户有多个games - game = self.games.where(user_id: user_id).last - status = - if game.blank? - self.position == 1 ? 1 : 0 - elsif game.status == 2 - 2 - else - 1 - end + game = games.select{|game| game.user_id == user_id }.last + + if game.blank? + self.position == 1 ? 1 : 0 + elsif game.status == 2 + 2 + else + 1 + end end ## 选择题答案 @@ -85,12 +97,12 @@ class Challenge < ApplicationRecord # 关卡用户通关数 def user_passed_count - games.where(status: 2).count + games.select{|game| game.status == 2}.size end # 关卡用户正在挑战的人数 def playing_count - games.where(status: [0, 1]).count + games.select{|game| game.status == 0 || game.status == 1}.size end def last_challenge diff --git a/app/views/challenges/index.json.jbuilder b/app/views/challenges/index.json.jbuilder index 0b90b5ca5..dc122b8c4 100644 --- a/app/views/challenges/index.json.jbuilder +++ b/app/views/challenges/index.json.jbuilder @@ -17,7 +17,7 @@ if @challenges.present? json.passed_count challenge.user_passed_count json.playing_count challenge.playing_count json.name_url shixun_challenge_path(challenge, shixun_identifier: @shixun.identifier) - json.open_game challenge.open_game(@user.id) + json.open_game challenge.open_game(@user.id, challenge.id, @shixun) if @editable json.edit_url edit_shixun_challenge_path(challenge, shixun_identifier: @shixun.identifier) json.delete_url shixun_challenge_path(challenge, shixun_identifier: @shixun.identifier)