Support remaining pieces in completions and scoreboards
Some checks failed
CI / scan_ruby (push) Failing after 16s
CI / scan_js (push) Successful in 13s
CI / lint (push) Successful in 13s
CI / test (push) Successful in 36s

This commit is contained in:
sto
2025-10-28 15:13:29 +01:00
parent 1fa7bf10ec
commit bbd2cef168
11 changed files with 66 additions and 19 deletions

View File

@@ -5,6 +5,7 @@
# id :integer not null, primary key
# display_relative_time :string
# display_time_from_start :string
# remaining_pieces :integer
# time_seconds :integer
# created_at :datetime not null
# updated_at :datetime not null
@@ -34,10 +35,25 @@ class Completion < ApplicationRecord
belongs_to :message, optional: true
before_save :add_time_seconds
before_save :nullify_display_time
validates :display_time_from_start, presence: true, format: { with: /\A(((\d\d|\d):\d\d|\d\d|\d):\d\d|\d\d|\d)\z/ }
validates :display_time_from_start, presence: true, format: { with: /\A(((\d\d|\d):\d\d|\d\d|\d):\d\d|\d\d|\d)\z/ }, if: -> { remaining_pieces == nil }
validates :contestant_id, uniqueness: { scope: :puzzle }, if: -> { contest.puzzles.size == 1 }
validates :puzzle_id, uniqueness: { scope: :contestant }, if: -> { contest.puzzles.size > 1 }
validate :remaining_pieces_is_correct
def remaining_pieces_is_correct
if self.remaining_pieces && self.remaining_pieces > self.puzzle.pieces
errors.add(:remaining_pieces, "Cannot be greater than the number of pieces for this puzzle")
end
end
def nullify_display_time
if self.remaining_pieces
self.display_time_from_start = nil
self.display_relative_time = nil
end
end
def add_time_seconds
arr = display_time_from_start.split(":")