diff --git a/app/controllers/competitions_controller.rb b/app/controllers/competitions_controller.rb index 24f02037..e6112539 100644 --- a/app/controllers/competitions_controller.rb +++ b/app/controllers/competitions_controller.rb @@ -316,6 +316,92 @@ class CompetitionsController < ApplicationController rank = @records.map(&:id).index(team.id) team[:rank] = rank.present? ? (rank+1) : 0 end + elsif @competition.identifier == 'gcc-task-2019' + if @type == "预赛" || @type == "决赛" + 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, 3014] + 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 => ['92b7vt8x','a7fxenvc','wt2xfzny']).pluck(:id) + shixun2_id = Shixun.where(:identifier => ['xa4m9cng','tng6heyf','am5o73er']).pluck(:id) + shixun3_id = Shixun.where(:identifier => ['9fla2zry','fzp3iu4w','qlsy6xb4']).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 = 0.5 + result1 = chart_exp_score_pre user_ids, s1_time, e1_time, challenges_1, challenge_rate1 + 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 + 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 + else + @records = CompetitionTeam.find_by_sql("SELECT teams.*, cs.score AS s_score, cs.cost_time AS s_spend_time FROM competition_teams teams, competition_scores cs WHERE cs.competition_team_id = teams.`id` AND cs.competition_stage_id = '#{@stage.id}'") + end + elsif @type == "总排行榜" + pre_stage = @competition.competition_stages.where(:name => "预赛").first + final_stage = @competition.competition_stages.where(:name => "决赛").first + @records = @competition.competition_teams.includes(:user => [:user_extensions => [:school]]) + @records.each do |team| + # 决赛记录 + f_score = team.competition_scores.where(:competition_stage_id => final_stage.try(:id)).first + # 预赛记录 + p_score = team.competition_scores.where(:competition_stage_id => pre_stage.try(:id)).first + team[:s_score] = (f_score.try(:score).to_f * 0.85 + p_score.try(:score).to_f * 0.15).try(:round, 2) + team[:s_spend_time] = f_score.try(:cost_time).to_i + p_score.try(:cost_time).to_i + end + end + + @records = @records.sort do |a, b| + [b[:s_score], a[:s_spend_time]] <=> [a[:s_score], b[:s_spend_time]] + end + current_team_ids = @competition.team_members.where(:user_id => User.current.id).pluck(:competition_team_id).uniq + @user_ranks = @records.select{|com_team| current_team_ids.include?(com_team.id)} + @records = @records.select{|record| record[:s_score] > 0} + @user_ranks.each do |team| + rank = @records.map(&:id).index(team.id) + team[:rank] = rank.present? ? (rank+1) : 0 + end + records_length = 97 + @records = @records[0..records_length] end else render_403