parent
							
								
									40e0ad626a
								
							
						
					
					
						commit
						a1796ee2c4
					
				| @ -0,0 +1,54 @@ | ||||
| module CompetitionsHelper | ||||
|   def chart_school_str user_ids | ||||
|     chart_school_name = "" | ||||
|     chart_school_name = School.where(id: UserExtension.where(user_id: user_ids).pluck(:school_id).uniq).pluck(:name).join("、") | ||||
|   end | ||||
| 
 | ||||
|   # 耗时:小时、分、秒  00:00:00 | ||||
|   # 小于1秒钟则不显示 | ||||
|   def com_spend_time time | ||||
|     hour = time / (60*60) | ||||
|     min = time % (60*60) / 60 | ||||
|     sec = time % (60*60) % 60 | ||||
|     hour_str = "00" | ||||
|     min_str = "00" | ||||
|     sec_str = "00" | ||||
|     if hour >= 1 && hour < 10 | ||||
|       hour_str = "0#{hour}" | ||||
|     elsif hour >= 10 | ||||
|       hour_str = "#{hour}" | ||||
|     end | ||||
| 
 | ||||
|     if min >= 1 && min < 10 | ||||
|       min_str = "0#{min}" | ||||
|     elsif min >= 10 | ||||
|       min_str = "#{min}" | ||||
|     end | ||||
| 
 | ||||
|     if sec >= 1 && sec < 10 | ||||
|       sec_str = "0#{sec}" | ||||
|     elsif sec >= 10 | ||||
|       sec_str = "#{sec}" | ||||
|     end | ||||
| 
 | ||||
|     time = "#{hour_str} : #{min_str} : #{sec_str}" | ||||
|     return time | ||||
|   end | ||||
| 
 | ||||
|   def chart_stages competition | ||||
|     stages = [] | ||||
|     statistic_stages = competition.competition_stages.where("rate > 0") | ||||
|     if competition.end_time && competition.end_time < Time.now && statistic_stages.size > 1 | ||||
|       stages << {id: nil, name: "总排行榜", rate: 1.0, start_time: competition.start_time, end_time: competition.end_time} | ||||
|     end | ||||
| 
 | ||||
|     statistic_stages.each do |stage| | ||||
|       if stage.max_end_time && stage.max_end_time < Time.now | ||||
|         stages << {id: stage.id, name: "#{stage.name}排行榜", rate: stage.rate, start_time: stage.min_start_time, end_time: stage.max_end_time} | ||||
|       end | ||||
|     end | ||||
| 
 | ||||
|     stages = stages.sort { |a, b| b[:end_time] <=> a[:end_time] } if stages.size > 0 | ||||
|     return stages | ||||
|   end | ||||
| end | ||||
| @ -0,0 +1,6 @@ | ||||
| class ChartRule < ApplicationRecord | ||||
|   belongs_to :competition | ||||
|   belongs_to :competition_stage, optional: true | ||||
| 
 | ||||
|   validates :content, length: { maximum: 1000 } | ||||
| end | ||||
| @ -0,0 +1,3 @@ | ||||
| class CompetitionAward < ApplicationRecord | ||||
|   belongs_to :competition | ||||
| end | ||||
| @ -1,3 +1,4 @@ | ||||
| class CompetitionModeSetting < ApplicationRecord | ||||
|   belongs_to :course | ||||
|   belongs_to :course, optional: true | ||||
|   belongs_to :competition | ||||
| end | ||||
|  | ||||
| @ -0,0 +1,6 @@ | ||||
| class CompetitionScore < ApplicationRecord | ||||
|   belongs_to :user | ||||
|   belongs_to :competition | ||||
|   belongs_to :competition_stage, optional: true | ||||
|   belongs_to :competition_team, optional: true | ||||
| end | ||||
| @ -0,0 +1,28 @@ | ||||
| total_students_count = 0 | ||||
| total_shixun_homework_count = 0 | ||||
| total_course_score = 0 | ||||
| 
 | ||||
| json.courses @courses.each do |course| | ||||
|   students_count = course.students.count | ||||
|   total_students_count += students_count | ||||
|   total_shixun_homework_count += course['shixun_homework_count'].to_i | ||||
| 
 | ||||
|   score = 500 + 5 * @course_shixun_count_map.fetch(course.id, 0) * @course_myshixun_map.fetch(course.id, 0) | ||||
|   total_course_score += score | ||||
| 
 | ||||
|   teacher = course.teachers.where(user_id: @team_user_ids).first.user | ||||
|   json.creator teacher&.real_name | ||||
|   json.creator_login teacher&.login | ||||
|   json.course_name course.name | ||||
|   json.course_id course.id | ||||
|   json.students_count students_count | ||||
|   json.shixun_homework_count course['shixun_homework_count'] | ||||
|   json.valid_count @course_myshixun_map.fetch(course.id, 0) | ||||
|   json.score score | ||||
| end | ||||
| 
 | ||||
| json.total_course_count @courses.size | ||||
| json.total_students_count total_students_count | ||||
| json.total_shixun_homework_count total_shixun_homework_count | ||||
| json.total_valid_count @course_myshixun_map.values.reduce(:+) | ||||
| json.total_course_score total_course_score | ||||
| @ -0,0 +1,41 @@ | ||||
| total_myshixun_count = 0 | ||||
| total_forked_myshixun_count = 0 | ||||
| total_shixun_score = 0 | ||||
| 
 | ||||
| json.shixuns @shixuns.each do |shixun| | ||||
|   total_myshixun_count += shixun.myshixuns_count | ||||
|   total_forked_myshixun_count += shixun['forked_myshixun_count'].to_i | ||||
| 
 | ||||
|   valid_course_count = @course_count_map.fetch(shixun.id, 0) | ||||
|   valid_student_count = @original_myshixun_count_map.fetch(shixun.id, 0) | ||||
|   score = | ||||
|     if shixun.fork_from.blank? | ||||
|       500 + 50 * valid_course_count + 10 * valid_student_count | ||||
|     else | ||||
|       100 + 10 * valid_course_count + 5 * valid_student_count | ||||
|     end | ||||
| 
 | ||||
|   @forked_shixun_map.each do |shixun_id, fork_from_id| | ||||
|     next if fork_from_id != shixun.id | ||||
| 
 | ||||
|     score += 100 + 10 * @forked_map.fetch(shixun_id, 0) + 5 * @forked_myshixun_count_map.fetch(shixun_id, 0) | ||||
|   end | ||||
| 
 | ||||
|   total_shixun_score += score | ||||
| 
 | ||||
|   json.creator shixun.user.real_name | ||||
|   json.creator_login shixun.user.login | ||||
|   json.shixun_name shixun.name | ||||
|   json.shixun_identifier shixun.identifier | ||||
|   json.forked shixun.fork_from.present? | ||||
|   json.myshixuns_count shixun.myshixuns_count | ||||
|   json.forked_myshixun_count shixun['forked_myshixun_count'].to_i | ||||
|   json.valid_count @myshixun_count_map.fetch(shixun.id, 0) | ||||
|   json.score score | ||||
| end | ||||
| 
 | ||||
| json.shixun_count @shixuns.size | ||||
| json.total_myshixun_count total_myshixun_count | ||||
| json.total_forked_myshixun_count total_forked_myshixun_count | ||||
| json.total_valid_count @myshixun_count_map.values.reduce(:+) | ||||
| json.total_shixun_score total_shixun_score | ||||
| @ -0,0 +1,18 @@ | ||||
| 
 | ||||
| wb = xlsx_package.workbook | ||||
| # wb.use_autowidth = false | ||||
| wb.styles do |s| | ||||
|   sz_all = s.add_style :border => { :style => :thin, :color =>"000000" },:alignment => {:horizontal => :center} | ||||
|   blue_cell = s.add_style :bg_color => "FAEBDC", :sz => 10,:height => 20,:b => true, :border => { :style => :thin, :color =>"000000" },:alignment => {:horizontal => :center} | ||||
|   wb.add_worksheet(:name => "比赛成绩") do |sheet| | ||||
|     sheet.sheet_view.show_grid_lines = false | ||||
|     sheet.add_row table_columns, :style => blue_cell | ||||
|     if chart_lists.count > 0 | ||||
|       chart_lists.each do |user| | ||||
|         sheet.add_row user, :height => 20,:style => sz_all | ||||
|       end  #each_widh_index | ||||
|     end | ||||
|     sheet.column_widths *([20]*sheet.column_info.count) | ||||
|     sheet.column_info.first.width = 12 | ||||
|   end  #add_worksheet | ||||
| end | ||||
| @ -0,0 +1,5 @@ | ||||
| json.rule_contents @rule_contents.each do |rule| | ||||
|   json.(rule, :id, :content, :competition_stage_id) | ||||
| end | ||||
| 
 | ||||
| json.stages chart_stages @competition | ||||
| @ -0,0 +1,21 @@ | ||||
| json.user_ranks @user_ranks.each do |user_rank| | ||||
|   rank = @records.map(&:id).index(user_rank.id) | ||||
|   rank = rank.present? ? (rank+1) : 0 | ||||
|   json.rank rank == 0 ? "--" : user_rank.rank | ||||
|   json.team_name user_rank.name | ||||
|   json.user_name user_rank.user.real_name | ||||
|   json.cost_time rank == 0 && user_rank.cost_time ? "--" : com_spend_time(user_rank.cost_time) | ||||
|   json.score rank == 0 ? "--" : user_rank.score.round(2) | ||||
| end | ||||
| 
 | ||||
| json.teams @records.each do |record| | ||||
|   record_user = record.user | ||||
|   json.team_name record.name | ||||
|   json.record_user_name record_user.real_name | ||||
|   json.user_image url_to_avatar(record_user) | ||||
|   json.user_login record_user.login | ||||
|   school_name = chart_school_str record.team_members.select{|member| !member.is_teacher}.pluck(:user_id) | ||||
|   json.school_name school_name | ||||
|   json.score record&.score&.round(2) | ||||
|   json.spend_time record.cost_time ? com_spend_time(record.cost_time) : "--" | ||||
| end | ||||
| @ -0,0 +1,6 @@ | ||||
| json.informs @informs.each do |inform| | ||||
|   json.(inform, :id, :name, :description) | ||||
|   json.attachments inform.attachments do |atta| | ||||
|     json.partial! "attachments/attachment_simple", locals: {attachment: atta} | ||||
|   end | ||||
| end | ||||
| @ -0,0 +1,4 @@ | ||||
| json.(@md_content, :id, :name, :content) | ||||
| json.attachments @md_content.attachments do |atta| | ||||
|   json.partial! "attachments/attachment_simple", locals: {attachment: atta} | ||||
| end | ||||
| @ -0,0 +1,7 @@ | ||||
| class AddCompetitionIdToModeSetting < ActiveRecord::Migration[5.2] | ||||
|   def change | ||||
|     add_column :competition_mode_settings, :competition_id, :integer | ||||
| 
 | ||||
|     add_index :competition_mode_settings, :competition_id | ||||
|   end | ||||
| end | ||||
| @ -0,0 +1,29 @@ | ||||
| class MigrateCompetitionScore < ActiveRecord::Migration[5.2] | ||||
|   def change | ||||
|     CompetitionModule.where(name: "排行榜", hidden: 0).each do |com_module| | ||||
|       competition = com_module.competition | ||||
|       if competition.present? | ||||
|         puts competition.id | ||||
|         if competition.identifier == 'hn' || competition.identifier == 'gcc-task-2019' | ||||
|           p_rate = 0.2 | ||||
|           f_rate = 0.8 | ||||
|         else | ||||
|           p_rate = 0.15 | ||||
|           f_rate = 0.85 | ||||
|         end | ||||
|         pre_stage = competition.competition_stages.where(:name => "预赛").first | ||||
|         final_stage = competition.competition_stages.where(:name => "决赛").first | ||||
| 
 | ||||
|         competition.competition_teams.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 | ||||
|           s_score = (f_score.try(:score).to_f * f_rate + p_score.try(:score).to_f * p_rate).try(:round, 2) | ||||
|           s_spend_time = f_score.try(:cost_time).to_i + p_score.try(:cost_time).to_i | ||||
|           CompetitionScore.create(:user_id => team.user_id, :competition_team_id => team.id, :competition_id => competition.id, | ||||
|                                   :competition_stage_id => 0, :score => s_score, :cost_time => s_spend_time) | ||||
|         end | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| end | ||||
| @ -0,0 +1,12 @@ | ||||
| class CreateCompetitionAwards < ActiveRecord::Migration[5.2] | ||||
|   def change | ||||
|     create_table :competition_awards do |t| | ||||
|       t.references :competition, index: true | ||||
|       t.string :name | ||||
|       t.integer :num, default: 0 | ||||
|       t.integer :award_type, default: 0 | ||||
| 
 | ||||
|       t.timestamps | ||||
|     end | ||||
|   end | ||||
| end | ||||
| @ -0,0 +1,5 @@ | ||||
| class AddRateToCompetitionStage < ActiveRecord::Migration[5.2] | ||||
|   def change | ||||
|     add_column :competition_stages, :rate, :float, default: 1.0 | ||||
|   end | ||||
| end | ||||
| @ -0,0 +1,5 @@ | ||||
| require 'rails_helper' | ||||
| 
 | ||||
| RSpec.describe ChartRule, type: :model do | ||||
|   pending "add some examples to (or delete) #{__FILE__}" | ||||
| end | ||||
| @ -0,0 +1,5 @@ | ||||
| require 'rails_helper' | ||||
| 
 | ||||
| RSpec.describe CompetitionAward, type: :model do | ||||
|   pending "add some examples to (or delete) #{__FILE__}" | ||||
| end | ||||
| @ -0,0 +1,5 @@ | ||||
| require 'rails_helper' | ||||
| 
 | ||||
| RSpec.describe CompetitionScore, type: :model do | ||||
|   pending "add some examples to (or delete) #{__FILE__}" | ||||
| end | ||||
					Loading…
					
					
				
		Reference in new issue