diff --git a/app/controllers/admins/competition_stages_controller.rb b/app/controllers/admins/competition_stages_controller.rb index b02d5ae8c..1e68b3f50 100644 --- a/app/controllers/admins/competition_stages_controller.rb +++ b/app/controllers/admins/competition_stages_controller.rb @@ -16,7 +16,85 @@ class Admins::CompetitionStagesController < Admins::BaseController def calculate_stage_score if current_stage.max_end_time && current_stage.max_end_time < Time.now + current_stage.competition_scores.destroy_all + if current_stage.competition_stage_sections.size > 0 + # 是否只有老师参赛 + only_teacher = current_competition.member_staff.blank? + stage_sections = current_stage.competition_stage_sections.includes(:competition_entries) + shixuns = Shixun.where(identifier: current_stage.competition_entries.pluck(:shixun_identifier).reject(&:blank?)) + + + # 计算每个战队的成绩 + current_competition.competition_teams.each do |team| + totoal_score = 0 + total_time = 0 + user_ids = only_teacher ? team.team_members.pluck(:user_id) : team.team_members.where(is_teacher: 0).pluck(:user_id) + + stage_sections.each do |section| + shixun_identifiers = section.competition_entries.pluck(:shixun_identifier).reject(&:blank?) + shixun_ids = shixuns.select{ |shixun| shixun_identifiers.include?(shixun.identifier) }.map(&:id) + + end + end + end + + if @type == "预赛" + # 'nyog9r7c','yugrij4n','48flws5g','bfgau7s6','mfv6zrj7','f398leqr','qwaffs2p','ose7482b','y5wh2ofx' + # 预赛的实训id 第一阶段:1185,1197,1195 第二阶段:1202, 1210, 1207 第三阶段:1254,1255,1243 + shixun1_id = [2303] + shixun2_id = [2994, 3012, 3024] + shixun3_id = [2944, 2938, 2943] + elsif @type == "决赛" + # '92b7vt8x','a7fxenvc','wt2xfzny','xa4m9cng','tng6heyf','am5o73er','9fla2zry','fzp3iu4w','qlsy6xb4' + # 预赛的实训id 第一阶段:1289,1373,1256 第二阶段:1488, 1453, 1487 第三阶段:1470, 1473, 1408 + shixun1_id = Shixun.where(:identifier => ['ftlc4x38']).pluck(:id) + shixun2_id = Shixun.where(:identifier => ['y9npgih2','ucqt7fw3','2p7ouzwk']).pluck(:id) + shixun3_id = Shixun.where(:identifier => ['fj49r7xv','gf2cvxfh','cmoxhtbs']).pluck(:id) + end + + if @competition.competition_scores.where(:competition_stage_id => @stage.id).count == 0 + # 三个阶段的开始时间和结束时间 + s1_time = @stage.competition_stage_sections[0].start_time + e1_time = @stage.competition_stage_sections[0].end_time + s2_time = @stage.competition_stage_sections[1].start_time + e2_time = @stage.competition_stage_sections[1].end_time + s3_time = @stage.competition_stage_sections[2].start_time + e3_time = @stage.competition_stage_sections[2].end_time + + @records = @competition.competition_teams.includes(:user => [:user_extensions => [:school]]) + @records.each do |team| + user_ids = team.team_members.where(:is_teacher => 0).pluck(:user_id) + # 第一阶段的得分和耗时 + challenges_1 = Challenge.where(:shixun_id => shixun1_id) + challenge_rate1 = 1.0 + result1 = chart_exp_score_pre user_ids, s1_time, e1_time, challenges_1, challenge_rate1, 2 + score1 = result1[0] + time1 = result1[1] + + # 第二阶段的得分和耗时 + challenge_rate2 = 1.0 + challenges_2 = Challenge.where(:shixun_id => shixun2_id) + result2 = chart_exp_score_pre user_ids, s2_time, e2_time, challenges_2, challenge_rate2, 2 + score2 = result2[0] + time2 = result2[1] + + + # 第三阶段的得分和耗时 + challenges_3 = Challenge.where(:shixun_id => shixun3_id) + result3 = chart_exp_score_third user_ids, s3_time, e3_time, challenges_3 + score3 = result3[0] + time3 = result3[1] + + team[:s_score] = (score1 + score2 + score3).try(:round, 2) + team[:s_spend_time] = time1 + time2 + time3 + + # 比赛已截止且未有分数纪录 则创建 + if Time.now > e3_time && team.competition_scores.where(:competition_id => @competition.id, :competition_stage_id => @stage.id).count == 0 + CompetitionScore.create(:user_id => team.user_id, :competition_team_id => team.id, :competition_id => @competition.id, :competition_stage_id => @stage.id, :score => team[:s_score], :cost_time => team[:s_spend_time]) + end + end + end else render_error("#{current_stage.name}还未结束") end @@ -55,4 +133,58 @@ class Admins::CompetitionStagesController < Admins::BaseController params.permit(:stage_name, :score_rate, stage: [:start_time, :end_time, :mission_count, :entry, :score_source, identifiers: []]) end + + # challenges 每阶段的所有关卡 + # s_time 阶段开始时间 e_time 阶段结束时间 + # rate 关卡经验值与分数的比值 + # challenge_count 每个实训的关卡数 + # 对三个实训的所有关卡循环: 找到在比赛时间内通关的最低耗时 + def chart_stage_score user_ids, s_time, e_time, challenges, s_rate, challenge_count + total_score = 0 + total_time = 0 + length = challenge_count #每个实训的关卡数 + for i in 1..length + score = 0 + time = 0 + challenges.where(:position => i).each do |challenge| + Game.where(:challenge_id => challenge.id, :user_id => user_ids, :status => 2).select{|game| game.open_time >= s_time && game.end_time <= e_time }.each do |game| + game_score = challenge.score * s_rate + cost_time = (game.end_time.to_i - s_time.to_i) > 0 ? (game.end_time.to_i - s_time.to_i) : 0 + if score < game_score + score = game_score + time = cost_time + elsif score == game_score + time = cost_time > time ? time : cost_time + end + end + end + total_score += score + total_time += time + end + return [total_score, total_time] + end + + def chart_stage_rate user_ids, s_time, e_time, challenges + # 第三阶段的得分和耗时 + score3 = 0 + time3 = 0 + + challenges.each do |challenge| + Game.where(:challenge_id => challenge.id, :user_id => user_ids, :status => 2).select{|game| game.open_time >= s_time && game.end_time <= e_time }.each do |game| + outputs = game.outputs.select{|output| !output.text_scor.nil? && output.created_at <= e_time } + if outputs.present? + outputs = outputs.sort { |a, b| b[:text_scor].to_f <=> a[:text_scor].to_f } + myshixun_score = outputs.first.text_scor.to_f + myshixun_time = outputs.first.created_at.to_i - s_time.to_i + if score3 < myshixun_score + score3 = myshixun_score + time3 = myshixun_time + elsif score3 == myshixun_score + time3 = myshixun_time > time3 ? time3 : myshixun_time + end + end + end + end + return [score3, time3] + end end \ No newline at end of file diff --git a/app/forms/competitions/save_team_form.rb b/app/forms/competitions/save_team_form.rb index df7690ac6..be07a6365 100644 --- a/app/forms/competitions/save_team_form.rb +++ b/app/forms/competitions/save_team_form.rb @@ -16,14 +16,13 @@ class Competitions::SaveTeamForm validate :check_teachers_enrollable def check_teachers_enrollable if competition.teacher_enroll_forbidden? # 禁止老师报名 + return if creator.is_teacher? # 因为创建者有单独校验,所以这里跳过 return if teacher_ids.blank? if teacher_ids.present? errors.add(:teacher_ids, :enroll_forbidden) return end - - return if creator.is_teacher? # 因为创建者有单独校验,所以这里跳过 end self.teacher_ids = teacher_ids.map(&:to_i) @@ -40,9 +39,9 @@ class Competitions::SaveTeamForm # 存在已报名老师 enrolled_teacher_members = competition.team_members.where(user_id: all_teachers) - .where.not(competition_team_id: team.id).includes(:user) + .where.not(competition_team_id: team.id).pluck(:user_id) if enrolled_teacher_members.present? - errors.add(:teacher_ids, :enrolled, names: enrolled_teacher_members.map { |m| m.user.real_name }.join(',')) + errors.add(:teacher_ids, :enrolled, names: User.where(id: enrolled_teacher_members).map(&:real_name).join(',')) return end end @@ -50,14 +49,13 @@ class Competitions::SaveTeamForm validate :check_members_enrollable def check_members_enrollable if competition.member_enroll_forbidden? # 禁止学生报名 + return unless creator.is_teacher? # 因为创建者有单独校验,所以这里跳过 return if member_ids.blank? if member_ids.present? errors.add(:member_ids, :enroll_forbidden) return end - - return unless creator.is_teacher? # 因为创建者有单独校验,所以这里跳过 end self.member_ids = member_ids.map(&:to_i) @@ -74,9 +72,9 @@ class Competitions::SaveTeamForm # 存在已报名成员 enrolled_members = competition.team_members.where(user_id: all_members) - .where.not(competition_team_id: team.id).includes(:user) + .where.not(competition_team_id: team.id).pluck(:user_id) if enrolled_members.present? - errors.add(:member_ids, :enrolled, names: enrolled_members.map { |m| m.user.real_name }.join(',')) + errors.add(:member_ids, :enrolled, names: User.where(id: enrolled_members).map(&:real_name).join(',')) return end end diff --git a/app/models/competition.rb b/app/models/competition.rb index a0eb88857..47be8f9d9 100644 --- a/app/models/competition.rb +++ b/app/models/competition.rb @@ -50,7 +50,7 @@ class Competition < ApplicationRecord def competition_status if !status com_status = "nearly_published" - elsif end_time > Time.now + elsif end_time.nil? || end_time > Time.now com_status = "progressing" else com_status = "ended" diff --git a/app/views/admins/competitions/shared/_list.html.erb b/app/views/admins/competitions/shared/_list.html.erb index 6e9620a39..2618c8739 100644 --- a/app/views/admins/competitions/shared/_list.html.erb +++ b/app/views/admins/competitions/shared/_list.html.erb @@ -7,7 +7,7 @@
+ { + teacher_staff === undefined || teacher_staff === null ? + "" : + +
* - 导师: -
- - {/*该老师已添加
+ : + } - /> - - {/*该老师已添加
- : - ++ { + member_staff === undefined || member_staff === null ? + "" : +
* - 队员: -
- - -该队员已添加
+ : + } - /> - - -该队员已添加
- : - +