diff --git a/app/controllers/challenges_controller.rb b/app/controllers/challenges_controller.rb index 103c33aab..ce7b2dd38 100644 --- a/app/controllers/challenges_controller.rb +++ b/app/controllers/challenges_controller.rb @@ -155,9 +155,8 @@ class ChallengesController < ApplicationController def index uid_logger("identifier: #{params}") - - @challenges = @shixun.challenges.fields_for_list - + @challenges = @shixun.challenges.fields_for_list.joins(:games).where(games: {user_id: current_user.id}) + .select("challenges.*, games.identifier identifier, games.status status") @editable = @shixun.status == 0 # before_action:有判断权限,如果没发布,则肯定是管理人员 @user = current_user @shixun.increment!(:visits) diff --git a/app/models/challenge.rb b/app/models/challenge.rb index bc504a5bc..6f361e380 100644 --- a/app/models/challenge.rb +++ b/app/models/challenge.rb @@ -69,10 +69,10 @@ class Challenge < ApplicationRecord end # 开启挑战 - def open_game user_id, shixun - game = self.games.where(user_id: user_id).first - if game.present? - shixun.task_pass || game.status != 3 ? "/tasks/#{game.identifier}" : "" + def open_game + identifier = self.identifier + if identifier.present? + shixun.task_pass || status != 3 ? "/tasks/#{identifier}" : "" else self.position == 1 ? "/api/shixuns/#{shixun.identifier}/shixun_exec" : "" end @@ -92,16 +92,16 @@ class Challenge < ApplicationRecord # end ## 用户关卡状态 0: 不能开启实训; 1:直接开启; 2表示已完成 - def user_tpi_status user_id + def user_tpi_status # todo: 以前没加索引导致相同关卡,同一用户有多个games # 允许跳关则直接开启 - game = games.where(user_id: user_id).take - if game.blank? - position == 1 ? 1 : 0 + identifier = self.identifier + if identifier.blank? + self.position == 1 ? 1 : 0 else - if game.status == 3 + if status == 3 shixun.task_pass ? 1 : 0 - elsif game.status == 2 + elsif status == 2 2 else 1 diff --git a/app/views/challenges/index.json.jbuilder b/app/views/challenges/index.json.jbuilder index eb7ee900d..693f3717f 100644 --- a/app/views/challenges/index.json.jbuilder +++ b/app/views/challenges/index.json.jbuilder @@ -19,7 +19,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, @shixun) + json.open_game challenge.open_game 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) @@ -27,6 +27,6 @@ if @challenges.present? json.down_url index_down_shixun_challenge_path(challenge, :shixun_identifier => @shixun.identifier) if @shixun.challenges_count != challenge.position end #json.passed challenge.has_passed?(@user.id) - json.status challenge.user_tpi_status @user.id + json.status challenge.user_tpi_status end end