Add contest duration & complete ranking mode implementation
This commit is contained in:
@@ -32,6 +32,8 @@
|
||||
# puzzle_id (puzzle_id => puzzles.id)
|
||||
#
|
||||
class Completion < ApplicationRecord
|
||||
include ContestsHelper
|
||||
|
||||
belongs_to :contest
|
||||
belongs_to :contestant
|
||||
belongs_to :puzzle
|
||||
@@ -42,7 +44,7 @@ class Completion < ApplicationRecord
|
||||
before_save :clean_pieces
|
||||
before_save :compute_projected_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: -> { completed || offline.present? }
|
||||
validates :remaining_pieces, presence: true, if: -> { !completed }
|
||||
validates :contestant_id, uniqueness: { scope: :puzzle }, if: -> { contest.puzzles.size == 1 }
|
||||
validates :puzzle_id, uniqueness: { scope: :contestant }, if: -> { contest.puzzles.size > 1 }
|
||||
@@ -66,7 +68,8 @@ class Completion < ApplicationRecord
|
||||
self.time_seconds = arr[0].to_i
|
||||
end
|
||||
else
|
||||
self.time_seconds = 1
|
||||
self.time_seconds = self.contest.duration_seconds
|
||||
self.display_time_from_start = display_time(self.time_seconds)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -75,6 +78,9 @@ class Completion < ApplicationRecord
|
||||
self.remaining_pieces = nil
|
||||
else
|
||||
self.missing_pieces = nil
|
||||
if !self.offline.present?
|
||||
self.display_time_from_start = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -82,7 +88,7 @@ class Completion < ApplicationRecord
|
||||
add_time_seconds
|
||||
if self.completed
|
||||
self.projected_time = self.time_seconds
|
||||
elsif self.offline.present?
|
||||
else
|
||||
assembled_time = self.time_seconds
|
||||
assembled_pieces = self.puzzle.pieces - self.remaining_pieces
|
||||
pieces_per_second = assembled_pieces.to_f / assembled_time.to_f
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# allow_registration :boolean default(FALSE)
|
||||
# duration :string
|
||||
# duration_seconds :integer
|
||||
# lang :string default("en")
|
||||
# name :string
|
||||
# offline_form :boolean default(FALSE)
|
||||
@@ -37,9 +39,19 @@ class Contest < ApplicationRecord
|
||||
|
||||
friendly_id :name, use: :slugged
|
||||
|
||||
before_save :add_duration_seconds
|
||||
|
||||
validates :name, presence: true
|
||||
validates :lang, inclusion: { in: Languages::AVAILABLE_LANGUAGES.map { |lang| lang[:id] } }
|
||||
validates :ranking_mode, inclusion: { in: Ranking::AVAILABLE_RANKING_MODES.map { |lang| lang[:id] } }
|
||||
validates :duration, format: { with: /\A(\d\d:\d\d|\d:\d\d)\z/ }
|
||||
|
||||
generates_token_for :token
|
||||
|
||||
def add_duration_seconds
|
||||
arr = self.duration.split(":")
|
||||
if arr.size == 2
|
||||
self.duration_seconds = arr[0].to_i * 3600 + arr[1].to_i * 60
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user