diff --git a/app/controllers/competitions/competition_teams_controller.rb b/app/controllers/competitions/competition_teams_controller.rb index e03810b61..f504b226e 100644 --- a/app/controllers/competitions/competition_teams_controller.rb +++ b/app/controllers/competitions/competition_teams_controller.rb @@ -1,4 +1,63 @@ class Competitions::CompetitionTeamsController < Competitions::BaseController + before_action :tech_mode, only: [:shixun_detail, :course_detail] + + def shixun_detail + start_time = current_competition.competition_mode_setting&.start_time || current_competition.start_time + end_time = current_competition.competition_mode_setting&.end_time || current_competition.end_time + + team_user_ids = @team.team_members.pluck(:user_id) + shixuns = Shixun.where(user_id: team_user_ids, status: 2) + .where('shixuns.created_at > ? && shixuns.created_at <= ?', start_time, end_time) + shixuns = shixuns.joins('left join shixuns forked_shixuns on forked_shixuns.fork_from = shixuns.id and forked_shixuns.status = 2') + shixuns = shixuns.select('shixuns.id, shixuns.identifier, shixuns.user_id, shixuns.myshixuns_count, shixuns.name, shixuns.fork_from, sum(forked_shixuns.myshixuns_count) forked_myshixun_count') + @shixuns = shixuns.group('shixuns.id').order('shixuns.myshixuns_count desc').includes(:user) + + shixun_ids = @shixuns.map(&:id) + @myshixun_count_map = get_valid_myshixun_count(shixun_ids) + @original_myshixun_count_map = @myshixun_count_map.clone + # forked shixun valid myshixun count + forked_shixun_map = Shixun.where(status: 2, fork_from: shixun_ids).select('id, fork_from') + forked_shixun_map = forked_shixun_map.each_with_object({}) { |sx, obj| obj[sx.id] = sx.fork_from } + @forked_myshixun_count_map = get_valid_myshixun_count(forked_shixun_map.keys) + @forked_myshixun_count_map.each { |k, v| @myshixun_count_map[forked_shixun_map[k]] += v } + + @course_count_map = get_valid_course_count(shixun_ids) + @forked_map = get_valid_course_count(forked_shixun_map.keys) + @forked_course_count_map = {} + @forked_map.each do |forked_id, course_count| + @forked_course_count_map[forked_shixun_map[forked_id]] ||= 0 + @forked_course_count_map[forked_shixun_map[forked_id]] += course_count + end + @forked_shixun_map = forked_shixun_map + end + + def course_detail + start_time = current_competition.competition_mode_setting&.start_time || current_competition.start_time + end_time = current_competition.competition_mode_setting&.end_time || current_competition.end_time + @team_user_ids = @team.team_members.pluck(:user_id) + + student_count_subquery = CourseMember.where('course_id = courses.id AND role = 4').select('count(*)').to_sql + subquery = StudentWork.where('homework_common_id = hcs.id') + .select('sum(compelete_status !=0 ) as finish, count(*) as total') + .having('total != 0 and finish >= (total / 2)').to_sql + course_ids = Course.where('courses.created_at > ?', start_time) + .where('courses.created_at <= ?', end_time) + .where("(#{student_count_subquery}) >= 3") + .where("exists(select 1 from homework_commons hcs where hcs.course_id = courses.id and hcs.publish_time is not null and hcs.publish_time < NOW() and hcs.homework_type = 4 and exists(#{subquery}))") + .joins('join course_members on course_members.course_id = courses.id and course_members.role in (1,2,3)') + .where(course_members: { user_id: @team_user_ids }).pluck(:id) + courses = Course.where(id: course_ids).joins(:practice_homeworks).where('homework_commons.publish_time < now()') + @courses = courses.select('courses.id, courses.name, courses.members_count, count(*) shixun_homework_count') + .group('courses.id').order('shixun_homework_count desc').having('shixun_homework_count > 0') + + course_ids = @courses.map(&:id) + @course_myshixun_map = Myshixun.joins(student_works: :homework_common) + .where(homework_commons: { course_id: course_ids }) + .where('exists(select 1 from games where games.myshixun_id = myshixuns.id and games.status = 2)') + .group('homework_commons.course_id').count + @course_shixun_count_map = get_valid_shixun_count(course_ids) + end + def index admin_or_business? ? all_competition_teams : user_competition_teams end @@ -67,4 +126,37 @@ class Competitions::CompetitionTeamsController < Competitions::BaseController def save_params params.permit(:name, teacher_ids: [], member_ids: []) end + + def tech_mode + # render_not_found if current_competition.mode != 3 + @team = current_competition.competition_teams.find_by(id: params[:id]) + end + + def get_valid_myshixun_count(ids) + Myshixun.where(shixun_id: ids) + .where('exists(select 1 from games where games.myshixun_id = myshixuns.id and games.status = 2)') + .group('shixun_id').count + end + + def get_valid_course_count(ids) + percentage_sql = StudentWork.where('homework_common_id = homework_commons.id and homework_commons.publish_time is not null and homework_commons.publish_time < NOW()') + .select('sum(compelete_status !=0 ) as finish, count(*) as total') + .having('total != 0 and finish >= (total / 2)').to_sql + + Course.joins(practice_homeworks: :homework_commons_shixun) + .where('shixun_id in (?)', ids) + .where("exists (#{percentage_sql})") + .group('shixun_id').count + end + + def get_valid_shixun_count(ids) + percentage_sql = StudentWork.where('homework_common_id = homework_commons.id and homework_commons.publish_time is not null and homework_commons.publish_time < NOW()') + .select('sum(compelete_status !=0 ) as finish, count(*) as total') + .having('total != 0 and finish >= (total / 2)').to_sql + Shixun.joins(homework_commons_shixuns: :homework_common) + .where(homework_commons: { homework_type: 4 }) + .where('course_id in (?)', ids) + .where("exists (#{percentage_sql})") + .group('course_id').count + end end diff --git a/app/controllers/competitions/competitions_controller.rb b/app/controllers/competitions/competitions_controller.rb index 30df949f6..03a276952 100644 --- a/app/controllers/competitions/competitions_controller.rb +++ b/app/controllers/competitions/competitions_controller.rb @@ -1,7 +1,10 @@ class Competitions::CompetitionsController < Competitions::BaseController + include CompetitionsHelper + skip_before_action :require_login before_action :allow_visit, except: [:index] - before_action :require_admin, only: [:update] + before_action :require_admin, only: [:update, :update_inform] + before_action :chart_visible, only: [:charts, :chart_rules] def index # 已上架 或者 即将上架 @@ -36,6 +39,98 @@ class Competitions::CompetitionsController < Competitions::BaseController def common_header @competition = current_competition + @competition_modules = @competition.unhidden_competition_modules + @user = current_user + end + + def informs + status = params[:status] || 1 + @informs = current_competition.informs.where(status: status) + end + + def update_inform + tip_exception("标题和内容不能为空") if params[:name].blank? || params[:description].blank? + tip_exception("status参数有误") if params[:status].blank? || ![1, 2].include?(params[:status].to_i) + ActiveRecord::Base.transaction do + if params[:inform_id] + inform = current_competition.informs.find_by!(id: params[:inform_id]) + inform.update_attributes!(name: params[:name], description: params[:description]) + else + inform = current_competition.informs.create!(name: params[:name], description: params[:description], status: params[:status]) + end + Attachment.associate_container(params[:attachment_ids], inform.id, inform.class) if params[:attachment_ids] + normal_status("更新成功") + end + end + + def md_content + @md_content = CompetitionModuleMdContent.find_by!(id: params[:md_content_id]) + end + + def update_md_content + tip_exception("标题和内容不能为空") if params[:name].blank? || params[:content].blank? + ActiveRecord::Base.transaction do + if params[:md_content_id] + md_content = CompetitionModuleMdContent.find_by!(id: params[:md_content_id]) + md_content.update_attributes!(name: params[:name], content: params[:content]) + else + md_content = CompetitionModuleMdContent.create!(name: params[:name], content: params[:content]) + end + Attachment.associate_container(params[:attachment_ids], md_content.id, md_content.class) if params[:attachment_ids] + normal_status("更新成功") + end + end + + def chart_rules + @competition = current_competition + @stages = @competition.competition_stages + @rule_contents = @competition.chart_rules + end + + def update_chart_rules + tip_exception("内容不能为空") if params[:content].blank? + @competition = current_competition + @stage = @competition.competition_stages.find_by!(id: params[:stage_id]) if params[:stage_id] + chart_rule = @competition.chart_rules.where(competition_stage_id: @stage&.id).first + if chart_rule + chart_rule.update_attributes!(content: params[:content]) + else + @competition.chart_rules.create!(competition_stage_id: @stage&.id, content: params[:content]) + end + normal_status("更新成功") + end + + def charts + @competition = current_competition + if params[:stage_id] + @stage = @competition.competition_stages.find_by(id: params[:stage_id]) + end + + if @competition.identifier == "gcc-annotation-2018" + @records = @competition.competition_teams.joins(:competition_scores) + .select("competition_teams.*, score, cost_time").order("score desc, cost_time desc") + else + @records = @competition.competition_teams.joins(:competition_scores).where(competition_scores: {competition_stage_id: @stage&.id.to_i}) + .select("competition_teams.*, score, cost_time").order("score desc, cost_time desc") + end + + current_team_ids = @competition.team_members.where(user_id: current_user.id).pluck(:competition_team_id).uniq + @user_ranks = @records.select{|com_team| current_team_ids.include?(com_team.id)} + @records = @records.where("score > 0") + if params[:format] == "xlsx" + @records = @records.includes(user: [user_extension: :school], team_members: :user) + respond_to do |format| + format.xlsx{ + set_export_cookies + chart_to_xlsx(@records, @competition) + exercise_export_name = "#{@competition.name}比赛成绩" + render xlsx: "#{exercise_export_name.strip}",template: "competitions/competitions/chart_list.xlsx.axlsx",locals: + {table_columns: @competition_head_cells, chart_lists: @competition_cells_column} + } + end + else + @records = @records.includes(:user, :team_members).limit(@competition.awards_count) + end end private @@ -47,4 +142,58 @@ class Competitions::CompetitionsController < Competitions::BaseController def allow_visit render_forbidden unless current_competition.published? || admin_or_business? end + + def chart_visible + chart_module = current_competition.competition_modules.find_by(name: "排行榜") + render_forbidden unless (chart_module.present? && !chart_module.hidden) || admin_or_business? + end + + # 竞赛成绩导出 + def chart_to_xlsx records, competition + @competition_head_cells = [] + @competition_cells_column = [] + + max_staff = competition.competition_staffs.sum(:maximum) + if max_staff < 2 # 个人赛 + @competition_head_cells = %w(序号 姓名 学校 学号) + else # 战队赛 + @competition_head_cells = %w(序号 战队名 指导老师 队员 学校) + end + + statistic_stages = competition.competition_stages.where("rate > 0") + statistic_stages.each do |stage| + @competition_head_cells += ["#{stage.name}得分", "#{stage.name}用时"] + end + @competition_head_cells += ["总得分", "总用时"] if statistic_stages.size > 1 + competition_scores = competition.competition_scores + + records.each_with_index do |record, index| + row_cells_column = [] + row_cells_column << index + 1 + record_user = record.user + if max_staff < 2 + row_cells_column << record_user.real_name + row_cells_column << record_user.school_name + row_cells_column << record_user.student_id + else + row_cells_column << record.name + row_cells_column << record.teachers_name + row_cells_column << record.members_name + row_cells_column << chart_school_str(record.team_members.select{|member| !member.is_teacher}.pluck(:user_id)) + end + + statistic_stages.each do |stage| + stage_score = competition_scores.select{|score| score.competition_stage_id == stage.id && score.competition_team_id == record.id}.first + row_cells_column << stage_score&.score.to_f.round(2) + row_cells_column << com_spend_time(stage_score&.cost_time.to_i) + end + + if statistic_stages.size > 1 + row_cells_column << record&.score.to_f.round(2) + row_cells_column << com_spend_time(record&.cost_time.to_i) + end + + @competition_cells_column.push(row_cells_column) + end + end end \ No newline at end of file diff --git a/app/helpers/competitions_helper.rb b/app/helpers/competitions_helper.rb new file mode 100644 index 000000000..66c7eec41 --- /dev/null +++ b/app/helpers/competitions_helper.rb @@ -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 \ No newline at end of file diff --git a/app/models/chart_rule.rb b/app/models/chart_rule.rb new file mode 100644 index 000000000..de8fceaf1 --- /dev/null +++ b/app/models/chart_rule.rb @@ -0,0 +1,6 @@ +class ChartRule < ApplicationRecord + belongs_to :competition + belongs_to :competition_stage, optional: true + + validates :content, length: { maximum: 1000 } +end diff --git a/app/models/competition.rb b/app/models/competition.rb index 9055adefc..23b1e3c81 100644 --- a/app/models/competition.rb +++ b/app/models/competition.rb @@ -9,12 +9,19 @@ class Competition < ApplicationRecord has_many :competition_teams, dependent: :destroy has_many :team_members, dependent: :destroy + has_many :chart_rules, dependent: :destroy + + has_many :competition_scores, dependent: :destroy has_many :competition_staffs, dependent: :destroy has_one :teacher_staff, -> { where(category: :teacher) }, class_name: 'CompetitionStaff' has_one :member_staff, -> { where.not(category: :teacher) }, class_name: 'CompetitionStaff' + has_one :competition_mode_setting, dependent: :destroy + + has_many :informs, as: :container, dependent: :destroy + has_many :attachments, as: :container, dependent: :destroy - has_many :attachments, as: :container + has_many :competition_awards, dependent: :destroy after_create :create_competition_modules @@ -81,6 +88,16 @@ class Competition < ApplicationRecord member_staff.mutiple_limited? end + def max_min_stage_time + CompetitionStageSection.find_by_sql("SELECT MAX(end_time) as max_end_time, MIN(start_time) as min_start_time, + competition_stage_id FROM competition_stage_sections WHERE competition_id = #{id} + GROUP BY competition_stage_id order by competition_stage_id") + end + + def awards_count + competition_awards.pluck(:num)&.sum > 0 ? competition_awards.pluck(:num)&.sum : 20 + end + private def create_competition_modules diff --git a/app/models/competition_award.rb b/app/models/competition_award.rb new file mode 100644 index 000000000..715d82ee2 --- /dev/null +++ b/app/models/competition_award.rb @@ -0,0 +1,3 @@ +class CompetitionAward < ApplicationRecord + belongs_to :competition +end diff --git a/app/models/competition_mode_setting.rb b/app/models/competition_mode_setting.rb index b6bafa7c3..e8d6c17d7 100644 --- a/app/models/competition_mode_setting.rb +++ b/app/models/competition_mode_setting.rb @@ -1,3 +1,4 @@ class CompetitionModeSetting < ApplicationRecord - belongs_to :course + belongs_to :course, optional: true + belongs_to :competition end diff --git a/app/models/competition_module.rb b/app/models/competition_module.rb index be73bf3c1..9579533da 100644 --- a/app/models/competition_module.rb +++ b/app/models/competition_module.rb @@ -4,4 +4,21 @@ class CompetitionModule < ApplicationRecord belongs_to :competition has_one :competition_module_md_content, dependent: :destroy + + def module_url + case name + when "首页", "赛制介绍" + "/competitions/#{competition.identifier}" + when "通知公告" + "/competitions/#{competition.identifier}/informs?status=1" + when "参赛手册" + "/competitions/#{competition.identifier}/informs?status=2" + when "排行榜" + "/competitions/#{competition.identifier}/charts" + when "报名" + "/competitions/#{competition.identifier}/competition_teams" + else + url || "/competitions/#{competition.identifier}/md_content?md_content_id=#{competition_module_md_content&.id}" + end + end end diff --git a/app/models/competition_score.rb b/app/models/competition_score.rb new file mode 100644 index 000000000..ce7bad427 --- /dev/null +++ b/app/models/competition_score.rb @@ -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 diff --git a/app/models/competition_stage.rb b/app/models/competition_stage.rb index 60d4b1644..dec777b39 100644 --- a/app/models/competition_stage.rb +++ b/app/models/competition_stage.rb @@ -3,5 +3,15 @@ class CompetitionStage < ApplicationRecord has_many :competition_stage_sections, dependent: :destroy has_many :competition_entries, dependent: :destroy + has_many :competition_scores, dependent: :destroy + has_one :chart_rule, dependent: :destroy + + def min_start_time + competition_stage_sections.where("start_time is not null").pluck(:start_time).min + end + + def max_end_time + competition_stage_sections.where("end_time is not null").pluck(:end_time).max + end end \ No newline at end of file diff --git a/app/models/competition_team.rb b/app/models/competition_team.rb index 625b29421..878f58882 100644 --- a/app/models/competition_team.rb +++ b/app/models/competition_team.rb @@ -7,6 +7,7 @@ class CompetitionTeam < ApplicationRecord has_many :team_members, dependent: :destroy has_many :users, through: :team_members, source: :user + has_many :competition_scores, dependent: :destroy has_many :members, -> { without_teachers }, class_name: 'TeamMember' has_many :teachers, -> { only_teachers }, class_name: 'TeamMember' @@ -30,4 +31,12 @@ class CompetitionTeam < ApplicationRecord self.code = code code end + + def teachers_name + teachers.map{|teacher| teacher.user.real_name}.join(",") + end + + def members_name + members.map{|member| member.user.real_name}.join(",") + end end \ No newline at end of file diff --git a/app/models/inform.rb b/app/models/inform.rb index 5caf80c5f..d486b6f11 100644 --- a/app/models/inform.rb +++ b/app/models/inform.rb @@ -3,4 +3,6 @@ class Inform < ApplicationRecord validates :name, length: { maximum: 60 } validates :description, length: { maximum: 5000 } + + has_many :attachments, as: :container, dependent: :destroy end diff --git a/app/models/shixun.rb b/app/models/shixun.rb index 0cdb2e82b..efa3098fc 100644 --- a/app/models/shixun.rb +++ b/app/models/shixun.rb @@ -27,6 +27,7 @@ class Shixun < ApplicationRecord has_one :first_shixun_tag_repertoire, class_name: 'ShixunTagRepertoire' has_one :first_tag_repertoire, through: :first_shixun_tag_repertoire, source: :tag_repertoire + has_many :homework_commons_shixuns, class_name: 'HomeworkCommonsShixun' #实训的关卡 has_many :exercise_shixun_challenges, :dependent => :destroy diff --git a/app/views/competitions/competition_teams/course_detail.json.jbuilder b/app/views/competitions/competition_teams/course_detail.json.jbuilder new file mode 100644 index 000000000..ce37d9568 --- /dev/null +++ b/app/views/competitions/competition_teams/course_detail.json.jbuilder @@ -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 \ No newline at end of file diff --git a/app/views/competitions/competition_teams/shixun_detail.json.jbuilder b/app/views/competitions/competition_teams/shixun_detail.json.jbuilder new file mode 100644 index 000000000..abeb8c85c --- /dev/null +++ b/app/views/competitions/competition_teams/shixun_detail.json.jbuilder @@ -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 \ No newline at end of file diff --git a/app/views/competitions/competitions/chart_list.xlsx.axlsx b/app/views/competitions/competitions/chart_list.xlsx.axlsx new file mode 100644 index 000000000..2186b6ffd --- /dev/null +++ b/app/views/competitions/competitions/chart_list.xlsx.axlsx @@ -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 \ No newline at end of file diff --git a/app/views/competitions/competitions/chart_rules.json.jbuilder b/app/views/competitions/competitions/chart_rules.json.jbuilder new file mode 100644 index 000000000..bf1d04689 --- /dev/null +++ b/app/views/competitions/competitions/chart_rules.json.jbuilder @@ -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 \ No newline at end of file diff --git a/app/views/competitions/competitions/charts.json.jbuilder b/app/views/competitions/competitions/charts.json.jbuilder new file mode 100644 index 000000000..f59f377f7 --- /dev/null +++ b/app/views/competitions/competitions/charts.json.jbuilder @@ -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 \ No newline at end of file diff --git a/app/views/competitions/competitions/common_header.json.jbuilder b/app/views/competitions/competitions/common_header.json.jbuilder index f172dae86..882d1d1c0 100644 --- a/app/views/competitions/competitions/common_header.json.jbuilder +++ b/app/views/competitions/competitions/common_header.json.jbuilder @@ -10,3 +10,15 @@ json.enroll_end_time @competition.enroll_end_time&.strftime("%Y-%m-%d %H:%M:%S") json.published @competition.published? json.nearly_published @competition.published_at.present? +json.competition_modules @competition_modules do |com_module| + json.(com_module, :name, :position) + json.module_url com_module.module_url +end + +json.stages + +if @competition.mode == 1 + json.course_id @competition.competition_mode_setting&.course_id + json.member_of_course @user.member_of_course?(@competition.competition_mode_setting&.course) +end + diff --git a/app/views/competitions/competitions/enroll.json.jbuilder b/app/views/competitions/competitions/enroll.json.jbuilder new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/competitions/competitions/informs.json.jbuilder b/app/views/competitions/competitions/informs.json.jbuilder new file mode 100644 index 000000000..1b40c93e7 --- /dev/null +++ b/app/views/competitions/competitions/informs.json.jbuilder @@ -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 \ No newline at end of file diff --git a/app/views/competitions/competitions/md_content.json.jbuilder b/app/views/competitions/competitions/md_content.json.jbuilder new file mode 100644 index 000000000..45325cecb --- /dev/null +++ b/app/views/competitions/competitions/md_content.json.jbuilder @@ -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 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index f9efaf794..75fcf9a04 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -787,12 +787,21 @@ Rails.application.routes.draw do resources :competition_teams, only: [:index, :show] do post :join, on: :collection post :leave, on: :member + get :course_detail, on: :member + get :shixun_detail, on: :member end resources :teachers, only: [:index] resources :students, only: [:index] member do get :common_header + get :informs + post :update_inform + get :md_content + post :update_md_content + get :charts + get :chart_rules + post :update_chart_rules end end end diff --git a/db/migrate/20191017063840_add_competition_id_to_mode_setting.rb b/db/migrate/20191017063840_add_competition_id_to_mode_setting.rb new file mode 100644 index 000000000..ef68d395b --- /dev/null +++ b/db/migrate/20191017063840_add_competition_id_to_mode_setting.rb @@ -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 diff --git a/db/migrate/20191017092449_migrate_competition_score.rb b/db/migrate/20191017092449_migrate_competition_score.rb new file mode 100644 index 000000000..095f403c5 --- /dev/null +++ b/db/migrate/20191017092449_migrate_competition_score.rb @@ -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 diff --git a/db/migrate/20191018022821_create_competition_awards.rb b/db/migrate/20191018022821_create_competition_awards.rb new file mode 100644 index 000000000..9da6068ce --- /dev/null +++ b/db/migrate/20191018022821_create_competition_awards.rb @@ -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 diff --git a/db/migrate/20191018030029_add_rate_to_competition_stage.rb b/db/migrate/20191018030029_add_rate_to_competition_stage.rb new file mode 100644 index 000000000..6e819cc17 --- /dev/null +++ b/db/migrate/20191018030029_add_rate_to_competition_stage.rb @@ -0,0 +1,5 @@ +class AddRateToCompetitionStage < ActiveRecord::Migration[5.2] + def change + add_column :competition_stages, :rate, :float, default: 1.0 + end +end diff --git a/spec/models/chart_rule_spec.rb b/spec/models/chart_rule_spec.rb new file mode 100644 index 000000000..b3059c94d --- /dev/null +++ b/spec/models/chart_rule_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe ChartRule, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/competition_award_spec.rb b/spec/models/competition_award_spec.rb new file mode 100644 index 000000000..dac3bddcf --- /dev/null +++ b/spec/models/competition_award_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CompetitionAward, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/competition_score_spec.rb b/spec/models/competition_score_spec.rb new file mode 100644 index 000000000..d0f1372b1 --- /dev/null +++ b/spec/models/competition_score_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CompetitionScore, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end