实训详情优化

dev_forum
jingquan huang 5 years ago
parent 0946b44fad
commit d183334d91

@ -149,7 +149,11 @@ class ChallengesController < ApplicationController
def index def index
uid_logger("identifier: #{params}") uid_logger("identifier: #{params}")
# 通过调试发现 这里includes(:games)性能会慢10倍 # 通过调试发现 这里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 @editable = current_user.manager_of_shixun?(@shixun) && @shixun.status == 0
@user = current_user @user = current_user
end end
@ -247,7 +251,6 @@ class ChallengesController < ApplicationController
# 关卡位置被修改,需要修改脚本 # 关卡位置被修改,需要修改脚本
script = modify_shixun_script @shixun, @shixun.evaluate_script script = modify_shixun_script @shixun, @shixun.evaluate_script
@shixun.update_column(:evaluate_script, script) @shixun.update_column(:evaluate_script, script)
end end
def index_up def index_up

@ -51,9 +51,8 @@ class Challenge < ApplicationRecord
end end
# 开启挑战 # 开启挑战
def open_game(user_id) def open_game user_id, challenge_id, shixun
game = self.games.select([:status, :identifier]).where(user_id: user_id).first game = games.select{|game| game.challenge_id = challenge_id && game.user_id == user_id}.first
shixun = self.shixun
if game.present? if game.present?
shixun.task_pass || game.status != 3 ? "/tasks/#{game.identifier}" : "" shixun.task_pass || game.status != 3 ? "/tasks/#{game.identifier}" : ""
else else
@ -61,11 +60,24 @@ class Challenge < ApplicationRecord
end end
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表示已完成 ## 用户关卡状态 0: 不能开启实训; 1:直接开启; 2表示已完成
def user_tpi_status user_id def user_tpi_status user_id
# todo: 以前没加索引导致相同关卡,同一用户有多个games # todo: 以前没加索引导致相同关卡,同一用户有多个games
game = self.games.where(user_id: user_id).last game = games.select{|game| game.user_id == user_id }.last
status =
if game.blank? if game.blank?
self.position == 1 ? 1 : 0 self.position == 1 ? 1 : 0
elsif game.status == 2 elsif game.status == 2
@ -85,12 +97,12 @@ class Challenge < ApplicationRecord
# 关卡用户通关数 # 关卡用户通关数
def user_passed_count def user_passed_count
games.where(status: 2).count games.select{|game| game.status == 2}.size
end end
# 关卡用户正在挑战的人数 # 关卡用户正在挑战的人数
def playing_count def playing_count
games.where(status: [0, 1]).count games.select{|game| game.status == 0 || game.status == 1}.size
end end
def last_challenge def last_challenge

@ -17,7 +17,7 @@ if @challenges.present?
json.passed_count challenge.user_passed_count json.passed_count challenge.user_passed_count
json.playing_count challenge.playing_count json.playing_count challenge.playing_count
json.name_url shixun_challenge_path(challenge, shixun_identifier: @shixun.identifier) 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 if @editable
json.edit_url edit_shixun_challenge_path(challenge, shixun_identifier: @shixun.identifier) json.edit_url edit_shixun_challenge_path(challenge, shixun_identifier: @shixun.identifier)
json.delete_url shixun_challenge_path(challenge, shixun_identifier: @shixun.identifier) json.delete_url shixun_challenge_path(challenge, shixun_identifier: @shixun.identifier)

Loading…
Cancel
Save