Implement projected time
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
# display_relative_time :string
|
||||
# display_time_from_start :string
|
||||
# missing_pieces :integer
|
||||
# projected_time :integer
|
||||
# remaining_pieces :integer
|
||||
# time_seconds :integer
|
||||
# created_at :datetime not null
|
||||
@@ -36,11 +37,12 @@ class Completion < ApplicationRecord
|
||||
belongs_to :puzzle
|
||||
belongs_to :message, optional: true
|
||||
|
||||
before_save :add_time_seconds, if: -> { display_time_from_start.present? }
|
||||
before_save :nullify_display_time
|
||||
before_save :clean_pieces
|
||||
has_one :offline, dependent: :destroy
|
||||
|
||||
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 }
|
||||
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 :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 }
|
||||
@@ -53,21 +55,18 @@ class Completion < ApplicationRecord
|
||||
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(":")
|
||||
if arr.size == 3
|
||||
self.time_seconds = arr[0].to_i * 3600 + arr[1].to_i * 60 + arr[2].to_i
|
||||
elsif arr.size == 2
|
||||
self.time_seconds = arr[0].to_i * 60 + arr[1].to_i
|
||||
elsif arr.size == 1
|
||||
self.time_seconds = arr[0].to_i
|
||||
if display_time_from_start.present?
|
||||
arr = display_time_from_start.split(":")
|
||||
if arr.size == 3
|
||||
self.time_seconds = arr[0].to_i * 3600 + arr[1].to_i * 60 + arr[2].to_i
|
||||
elsif arr.size == 2
|
||||
self.time_seconds = arr[0].to_i * 60 + arr[1].to_i
|
||||
elsif arr.size == 1
|
||||
self.time_seconds = arr[0].to_i
|
||||
end
|
||||
else
|
||||
self.time_seconds = 1
|
||||
end
|
||||
end
|
||||
|
||||
@@ -78,4 +77,16 @@ class Completion < ApplicationRecord
|
||||
self.missing_pieces = nil
|
||||
end
|
||||
end
|
||||
|
||||
def compute_projected_time
|
||||
add_time_seconds
|
||||
if self.completed
|
||||
self.projected_time = self.time_seconds
|
||||
elsif self.offline.present?
|
||||
assembled_time = self.time_seconds
|
||||
assembled_pieces = self.puzzle.pieces - self.remaining_pieces
|
||||
pieces_per_second = assembled_pieces.to_f / assembled_time.to_f
|
||||
self.projected_time = assembled_time + Integer(self.remaining_pieces.to_f / pieces_per_second)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
#
|
||||
# Table name: contestants
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# display_time :string
|
||||
# email :string
|
||||
# name :string
|
||||
# time_seconds :integer
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# contest_id :integer not null
|
||||
# id :integer not null, primary key
|
||||
# display_time :string
|
||||
# email :string
|
||||
# name :string
|
||||
# projected_time :string
|
||||
# time_seconds :integer
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# contest_id :integer not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
@@ -22,7 +23,7 @@
|
||||
class Contestant < ApplicationRecord
|
||||
belongs_to :contest
|
||||
has_many :completions, dependent: :destroy
|
||||
has_one :offline
|
||||
has_one :offline, dependent: :destroy
|
||||
has_and_belongs_to_many :categories
|
||||
|
||||
before_validation :initialize_time_seconds_if_empty
|
||||
|
||||
@@ -12,22 +12,26 @@
|
||||
# submitted :boolean
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# completion_id :integer
|
||||
# contest_id :integer not null
|
||||
# contestant_id :integer
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_offlines_on_completion_id (completion_id)
|
||||
# index_offlines_on_contest_id (contest_id)
|
||||
# index_offlines_on_contestant_id (contestant_id)
|
||||
#
|
||||
# Foreign Keys
|
||||
#
|
||||
# completion_id (completion_id => completions.id)
|
||||
# contest_id (contest_id => contests.id)
|
||||
# contestant_id (contestant_id => contestants.id)
|
||||
#
|
||||
class Offline < ApplicationRecord
|
||||
belongs_to :contest
|
||||
belongs_to :contestant, optional: true
|
||||
belongs_to :completion, optional: true
|
||||
|
||||
has_many_attached :images
|
||||
|
||||
|
||||
Reference in New Issue
Block a user