diff --git a/app/channels/api_channel.rb b/app/channels/api_channel.rb index c061ddff..2e3746e9 100644 --- a/app/channels/api_channel.rb +++ b/app/channels/api_channel.rb @@ -3,10 +3,12 @@ class ApiChannel < ApplicationCable::Channel def subscribed ensure_confirmation_sent + task = Task.find task_id dispatch_self 'app/start' dispatch_self 'users/load', task_users.order(:judge_secret).pluck(:judge_secret) dispatch_self 'criteria/load', task_criterions.order(:position) - dispatch_self 'judges/load', Task.find(task_id).judges + dispatch_self 'judges/load', task.judges + dispatch_self 'resultMultiplier/load', task.result_multiplier dispatch_self 'results/load', CriterionUserResult.includes(:user).where(criterion: task_criterions) dispatch_self 'comments/load', Comment.includes(:user).where(task_id:) dispatch_self 'locks/load', RedisLockManager.all @@ -62,6 +64,15 @@ def delete_judge data end end + def write_result_multiplier data + return unless scoring_open data + + task = Task.find task_id + task.result_multiplier = data['value'] + task.save! + dispatch_all 'resultMultiplier/load', task.result_multiplier + end + def drag_drop data return unless scoring_open data @@ -144,11 +155,13 @@ def finish data users_without_result = [] begin + result_multiplier = Rational task.result_multiplier + ActiveRecord::Base.transaction do task.contest.users.find_each do |user| user_result = CriterionUserResult.where user:, criterion: task.criterions users_without_result << user.judge_secret unless user_result.count == task.criterions_count - score = user_result.sum :value + score = user_result.sum(:value) * result_multiplier Result.create_or_find_by!(user:, task:).update!(score:) end diff --git a/db/migrate/20230930203405_add_result_multiplier_to_tasks.rb b/db/migrate/20230930203405_add_result_multiplier_to_tasks.rb new file mode 100644 index 00000000..3be23ef9 --- /dev/null +++ b/db/migrate/20230930203405_add_result_multiplier_to_tasks.rb @@ -0,0 +1,5 @@ +class AddResultMultiplierToTasks < ActiveRecord::Migration[7.0] + def change + add_column :tasks, :result_multiplier, :string, default: '1', null: false + end +end diff --git a/db/structure.sql b/db/structure.sql index 531ba4ce..612b5e7f 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -373,7 +373,8 @@ CREATE TABLE public.tasks ( updated_at timestamp(6) without time zone NOT NULL, criterions_count integer DEFAULT 0 NOT NULL, scoring_open boolean DEFAULT true NOT NULL, - judges character varying[] DEFAULT '{}'::character varying[] NOT NULL + judges character varying[] DEFAULT '{}'::character varying[] NOT NULL, + result_multiplier character varying DEFAULT '1'::character varying NOT NULL ); @@ -919,6 +920,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20230630172057'), ('20230902145307'), ('20230902145308'), -('20230902145309'); +('20230902145309'), +('20230930203405');