From 1ec306d877b2b6a1cb1c5acb936aa3c3e0753332 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Tue, 10 Mar 2020 01:16:57 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E6=B3=A8=E9=87=8A=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/challenges_controller.rb | 13 ++++--- app/models/challenge.rb | 49 ++++++++++++++++-------- app/views/challenges/index.json.jbuilder | 12 +++--- 3 files changed, 47 insertions(+), 27 deletions(-) diff --git a/app/controllers/challenges_controller.rb b/app/controllers/challenges_controller.rb index c69f5bbfb..1d68e3a6e 100644 --- a/app/controllers/challenges_controller.rb +++ b/app/controllers/challenges_controller.rb @@ -155,15 +155,16 @@ class ChallengesController < ApplicationController def index uid_logger("identifier: #{params}") - base_columns = "challenges.id, challenges.subject, challenges.st, challenges.score, challenges.position, - challenges.shixun_id, games.identifier, games.status" - join_sql = "LEFT JOIN games ON games.challenge_id = challenges.id AND games.user_id = #{current_user.id}" + #base_columns = "challenges.id, challenges.subject, challenges.st, challenges.score, challenges.position, + # challenges.shixun_id, games.identifier, games.status" + #join_sql = "LEFT JOIN games ON games.challenge_id = challenges.id AND games.user_id = #{current_user.id}" # 下面2个参数是为了解决列表获取通关人数与正在游玩人数的问题 - @pass_games_map = @shixun.challenges.joins(:games).where(games: {status:2}).group(:challenge_id).reorder(nil).count - @play_games_map = @shixun.challenges.joins(:games).where(games: {status:[0,1]}).group(:challenge_id).reorder(nil).count + #@pass_games_map = @shixun.challenges.joins(:games).where(games: {status:2}).group(:challenge_id).reorder(nil).count + #@play_games_map = @shixun.challenges.joins(:games).where(games: {status:[0,1]}).group(:challenge_id).reorder(nil).count - @challenges = @shixun.challenges.joins(join_sql).select(base_columns) + #@challenges = @shixun.challenges.joins(join_sql).select(base_columns) + @challenges = @shixun.challenges.fields_for_list @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 5646da363..ca88efbb3 100644 --- a/app/models/challenge.rb +++ b/app/models/challenge.rb @@ -69,15 +69,16 @@ class Challenge < ApplicationRecord end # 开启挑战 - def open_game shixun - # 这里的identifier,status是关联了games取了games的identifier,status - identifier = self.identifier - if identifier.present? - shixun.task_pass || self.status != 3 ? "/tasks/#{identifier}" : "" - else - self.position == 1 ? "/api/shixuns/#{shixun.identifier}/shixun_exec" : "" - end - end + # def open_game shixun + # # 这里的identifier,status是关联了games取了games的identifier,status + # identifier = self.identifier + # if identifier.present? + # shixun.task_pass || self.status != 3 ? "/tasks/#{identifier}" : "" + # else + # self.position == 1 ? "/api/shixuns/#{shixun.identifier}/shixun_exec" : "" + # end + # end + # # 开启挑战 # def open_game(user_id, shixun) @@ -93,16 +94,16 @@ class Challenge < ApplicationRecord # end ## 用户关卡状态 0: 不能开启实训; 1:直接开启; 2表示已完成 - def user_tpi_status shixun + def user_tpi_status shixun, user_id # todo: 以前没加索引导致相同关卡,同一用户有多个games # 允许跳关则直接开启 - identifier = self.identifier - if identifier.blank? - self.position == 1 ? 1 : 0 + game = games.where(user_id: user_id).take + if game.blank? + position == 1 ? 1 : 0 else - if status == 3 + if game.status == 3 shixun.task_pass ? 1 : 0 - elsif status == 2 + elsif game.status == 2 2 else 1 @@ -110,6 +111,24 @@ class Challenge < ApplicationRecord end end + # ## 用户关卡状态 0: 不能开启实训; 1:直接开启; 2表示已完成 + # def user_tpi_status shixun + # # todo: 以前没加索引导致相同关卡,同一用户有多个games + # # 允许跳关则直接开启 + # identifier = self.identifier + # if identifier.blank? + # self.position == 1 ? 1 : 0 + # else + # if status == 3 + # shixun.task_pass ? 1 : 0 + # elsif status == 2 + # 2 + # else + # 1 + # end + # end + # end + def tags_show if self.challenge_tags.nil? "--" diff --git a/app/views/challenges/index.json.jbuilder b/app/views/challenges/index.json.jbuilder index d4f2a7f59..409514643 100644 --- a/app/views/challenges/index.json.jbuilder +++ b/app/views/challenges/index.json.jbuilder @@ -16,12 +16,12 @@ if @challenges.present? json.st challenge.st json.name challenge.subject json.score challenge.score - json.passed_count @pass_games_map.fetch(challenge.id, 0) - #json.passed_count challenge.user_passed_count - json.playing_count @play_games_map.fetch(challenge.id, 0) - #json.playing_count challenge.playing_count + #json.passed_count @pass_games_map.fetch(challenge.id, 0) + json.passed_count challenge.user_passed_count + #json.playing_count @play_games_map.fetch(challenge.id, 0) + json.playing_count challenge.playing_count json.name_url shixun_challenge_path(challenge, shixun_identifier: @shixun.identifier) - json.open_game challenge.open_game(@shixun) + #json.open_game challenge.open_game(@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) @@ -29,6 +29,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(@shixun) + json.status challenge.user_tpi_status(@shixun, @user.id) end end From 1ee1984ca839f198600bad49dddbd94442b04434 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Tue, 10 Mar 2020 01:25:33 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/challenge.rb | 21 ++++++++------------- app/views/challenges/index.json.jbuilder | 1 + 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/app/models/challenge.rb b/app/models/challenge.rb index ca88efbb3..68d55a582 100644 --- a/app/models/challenge.rb +++ b/app/models/challenge.rb @@ -79,19 +79,14 @@ 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 + 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}" : "" + else + self.position == 1 ? "/api/shixuns/#{shixun.identifier}/shixun_exec" : "" + end + end ## 用户关卡状态 0: 不能开启实训; 1:直接开启; 2表示已完成 def user_tpi_status shixun, user_id diff --git a/app/views/challenges/index.json.jbuilder b/app/views/challenges/index.json.jbuilder index 409514643..2eb8093a4 100644 --- a/app/views/challenges/index.json.jbuilder +++ b/app/views/challenges/index.json.jbuilder @@ -22,6 +22,7 @@ if @challenges.present? json.playing_count challenge.playing_count json.name_url shixun_challenge_path(challenge, shixun_identifier: @shixun.identifier) #json.open_game challenge.open_game(@shixun) + json.open_game challenge.open_game(@shixun, @user.id) 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) From 518ee3aa324bc01a07e013c5529cb0a2cc885f0c Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Tue, 10 Mar 2020 01:27:17 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/challenge.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/challenge.rb b/app/models/challenge.rb index 68d55a582..19a3bf1bb 100644 --- a/app/models/challenge.rb +++ b/app/models/challenge.rb @@ -79,7 +79,7 @@ class Challenge < ApplicationRecord # end # end - def open_game user_id, shixun + def open_game shixun, user_id game = self.games.where(user_id: user_id).first if game.present? shixun.task_pass || game.status != 3 ? "/tasks/#{game.identifier}" : "" From 3cf27da3dc19cb5aecb26847e2e95e3137f9531e Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Tue, 10 Mar 2020 01:32:25 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/challenges_controller.rb | 2 +- app/models/challenge.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/challenges_controller.rb b/app/controllers/challenges_controller.rb index 1d68e3a6e..9b86bb87d 100644 --- a/app/controllers/challenges_controller.rb +++ b/app/controllers/challenges_controller.rb @@ -164,7 +164,7 @@ class ChallengesController < ApplicationController #@challenges = @shixun.challenges.joins(join_sql).select(base_columns) - @challenges = @shixun.challenges.fields_for_list + @challenges = @shixun.challenges.includes(:games).fields_for_list @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 19a3bf1bb..e948a9bcc 100644 --- a/app/models/challenge.rb +++ b/app/models/challenge.rb @@ -142,12 +142,12 @@ class Challenge < ApplicationRecord # 关卡用户通关数 def user_passed_count - games.where(status: 2).count + games.map{|g| g.status == 2}.count end # 关卡用户正在挑战的人数 def playing_count - games.where(status: [0, 1]).count + games.map{|g| g.status == 0 || g.status == 1}.count end def last_challenge From 50d856fa1cde1e3062fefc3eb354e331ccbd3e7b Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Tue, 10 Mar 2020 01:33:23 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E4=BC=98=E5=8C=96N+1=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/challenge.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/challenge.rb b/app/models/challenge.rb index e948a9bcc..7c3c31ff9 100644 --- a/app/models/challenge.rb +++ b/app/models/challenge.rb @@ -80,7 +80,7 @@ class Challenge < ApplicationRecord # end def open_game shixun, user_id - game = self.games.where(user_id: user_id).first + game = self.games.map{|g| g.user_id == user_id}.first if game.present? shixun.task_pass || game.status != 3 ? "/tasks/#{game.identifier}" : "" else From cb6acf22fea9ac3807c3c1974a9ce6ed04e69207 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Tue, 10 Mar 2020 01:36:20 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/challenges_controller.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/challenges_controller.rb b/app/controllers/challenges_controller.rb index 9b86bb87d..47c8fc68c 100644 --- a/app/controllers/challenges_controller.rb +++ b/app/controllers/challenges_controller.rb @@ -155,16 +155,16 @@ class ChallengesController < ApplicationController def index uid_logger("identifier: #{params}") - #base_columns = "challenges.id, challenges.subject, challenges.st, challenges.score, challenges.position, - # challenges.shixun_id, games.identifier, games.status" - #join_sql = "LEFT JOIN games ON games.challenge_id = challenges.id AND games.user_id = #{current_user.id}" + base_columns = "challenges.id, challenges.subject, challenges.st, challenges.score, challenges.position, + challenges.shixun_id, games.identifier, games.status" + join_sql = "LEFT JOIN games ON games.challenge_id = challenges.id AND games.user_id = #{current_user.id}" # 下面2个参数是为了解决列表获取通关人数与正在游玩人数的问题 #@pass_games_map = @shixun.challenges.joins(:games).where(games: {status:2}).group(:challenge_id).reorder(nil).count #@play_games_map = @shixun.challenges.joins(:games).where(games: {status:[0,1]}).group(:challenge_id).reorder(nil).count - #@challenges = @shixun.challenges.joins(join_sql).select(base_columns) + @challenges = @shixun.challenges.joins(join_sql).select(base_columns) - @challenges = @shixun.challenges.includes(:games).fields_for_list + #@challenges = @shixun.challenges.fields_for_list @editable = @shixun.status == 0 # before_action:有判断权限,如果没发布,则肯定是管理人员 @user = current_user @shixun.increment!(:visits) From c9157cdf4265ef528f3cc717edfc00a8be7386ae Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Tue, 10 Mar 2020 01:40:21 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/challenge.rb | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/app/models/challenge.rb b/app/models/challenge.rb index 7c3c31ff9..50528b327 100644 --- a/app/models/challenge.rb +++ b/app/models/challenge.rb @@ -69,25 +69,25 @@ class Challenge < ApplicationRecord end # 开启挑战 - # def open_game shixun - # # 这里的identifier,status是关联了games取了games的identifier,status - # identifier = self.identifier - # if identifier.present? - # shixun.task_pass || self.status != 3 ? "/tasks/#{identifier}" : "" - # else - # self.position == 1 ? "/api/shixuns/#{shixun.identifier}/shixun_exec" : "" - # end - # end - - def open_game shixun, user_id - game = self.games.map{|g| g.user_id == user_id}.first - if game.present? - shixun.task_pass || game.status != 3 ? "/tasks/#{game.identifier}" : "" + def open_game shixun + # 这里的identifier,status是关联了games取了games的identifier,status + identifier = self.identifier + if identifier.present? + shixun.task_pass || self.status != 3 ? "/tasks/#{identifier}" : "" else self.position == 1 ? "/api/shixuns/#{shixun.identifier}/shixun_exec" : "" end end + # def open_game shixun, user_id + # game = self.games.map{|g| g.user_id == user_id}.first + # if game.present? + # shixun.task_pass || game.status != 3 ? "/tasks/#{game.identifier}" : "" + # else + # self.position == 1 ? "/api/shixuns/#{shixun.identifier}/shixun_exec" : "" + # end + # end + ## 用户关卡状态 0: 不能开启实训; 1:直接开启; 2表示已完成 def user_tpi_status shixun, user_id # todo: 以前没加索引导致相同关卡,同一用户有多个games @@ -142,12 +142,14 @@ class Challenge < ApplicationRecord # 关卡用户通关数 def user_passed_count - games.map{|g| g.status == 2}.count + #games.map{|g| g.status == 2}.count + self.games.where(status: 1).count end # 关卡用户正在挑战的人数 def playing_count - games.map{|g| g.status == 0 || g.status == 1}.count + #games.map{|g| g.status == 0 || g.status == 1}.count + self.games.where(status: [0,1]).count end def last_challenge From d79f182daf066dcc78af2624ac00b69bad9e532a Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Tue, 10 Mar 2020 01:40:43 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/challenges/index.json.jbuilder | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/challenges/index.json.jbuilder b/app/views/challenges/index.json.jbuilder index 2eb8093a4..c86acc95b 100644 --- a/app/views/challenges/index.json.jbuilder +++ b/app/views/challenges/index.json.jbuilder @@ -21,8 +21,8 @@ if @challenges.present? #json.playing_count @play_games_map.fetch(challenge.id, 0) json.playing_count challenge.playing_count json.name_url shixun_challenge_path(challenge, shixun_identifier: @shixun.identifier) - #json.open_game challenge.open_game(@shixun) - json.open_game challenge.open_game(@shixun, @user.id) + json.open_game challenge.open_game(@shixun) + #json.open_game challenge.open_game(@shixun, @user.id) 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) From 9eb33593222501db22a6d4aeddca172700f1013a Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Tue, 10 Mar 2020 01:43:58 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/challenge.rb | 48 ++++++++++++------------ app/views/challenges/index.json.jbuilder | 3 +- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/app/models/challenge.rb b/app/models/challenge.rb index 50528b327..31a683760 100644 --- a/app/models/challenge.rb +++ b/app/models/challenge.rb @@ -89,34 +89,16 @@ class Challenge < ApplicationRecord # end ## 用户关卡状态 0: 不能开启实训; 1:直接开启; 2表示已完成 - def user_tpi_status shixun, user_id - # todo: 以前没加索引导致相同关卡,同一用户有多个games - # 允许跳关则直接开启 - game = games.where(user_id: user_id).take - if game.blank? - position == 1 ? 1 : 0 - else - if game.status == 3 - shixun.task_pass ? 1 : 0 - elsif game.status == 2 - 2 - else - 1 - end - end - end - - # ## 用户关卡状态 0: 不能开启实训; 1:直接开启; 2表示已完成 - # def user_tpi_status shixun + # def user_tpi_status shixun, user_id # # todo: 以前没加索引导致相同关卡,同一用户有多个games # # 允许跳关则直接开启 - # identifier = self.identifier - # if identifier.blank? - # self.position == 1 ? 1 : 0 + # game = games.where(user_id: user_id).take + # if game.blank? + # position == 1 ? 1 : 0 # else - # if status == 3 + # if game.status == 3 # shixun.task_pass ? 1 : 0 - # elsif status == 2 + # elsif game.status == 2 # 2 # else # 1 @@ -124,6 +106,24 @@ class Challenge < ApplicationRecord # end # end + # ## 用户关卡状态 0: 不能开启实训; 1:直接开启; 2表示已完成 + def user_tpi_status shixun + # todo: 以前没加索引导致相同关卡,同一用户有多个games + # 允许跳关则直接开启 + identifier = self.identifier + if identifier.blank? + self.position == 1 ? 1 : 0 + else + if status == 3 + shixun.task_pass ? 1 : 0 + elsif status == 2 + 2 + else + 1 + end + end + end + def tags_show if self.challenge_tags.nil? "--" diff --git a/app/views/challenges/index.json.jbuilder b/app/views/challenges/index.json.jbuilder index c86acc95b..6613dd888 100644 --- a/app/views/challenges/index.json.jbuilder +++ b/app/views/challenges/index.json.jbuilder @@ -30,6 +30,7 @@ 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(@shixun, @user.id) + #json.status challenge.user_tpi_status(@shixun, @user.id) + json.status challenge.user_tpi_status(@shixun) end end From 86adedc617ca2b7dead9902307c24b212058c0f6 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Tue, 10 Mar 2020 01:56:39 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=85=B3=E5=8D=A1?= =?UTF-8?q?=E4=BA=BA=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/challenges/index.json.jbuilder | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/views/challenges/index.json.jbuilder b/app/views/challenges/index.json.jbuilder index 6613dd888..eae72dad2 100644 --- a/app/views/challenges/index.json.jbuilder +++ b/app/views/challenges/index.json.jbuilder @@ -17,9 +17,10 @@ if @challenges.present? json.name challenge.subject json.score challenge.score #json.passed_count @pass_games_map.fetch(challenge.id, 0) - json.passed_count challenge.user_passed_count + user_passed_count = challenge.user_passed_count + json.passed_count user_passed_count #json.playing_count @play_games_map.fetch(challenge.id, 0) - json.playing_count challenge.playing_count + json.playing_count (challenge.games.count - user_passed_count) json.name_url shixun_challenge_path(challenge, shixun_identifier: @shixun.identifier) json.open_game challenge.open_game(@shixun) #json.open_game challenge.open_game(@shixun, @user.id)