summaryrefslogtreecommitdiff
path: root/app/models/built_hearth_amenity.rb
blob: a1e35e82d852809180fdcec72b6ae2a9a102f9d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class BuiltHearthAmenity < ApplicationRecord
  belongs_to :hearth
  belongs_to :hearth_amenity

  validates :hearth_id, uniqueness: { scope: :hearth_amenity_id }

  def cooldown
    # TODO: HACK: Update this so multiple use effects will work
    self.hearth_amenity.whatnot[:use_effects].first[:cooldown]
  end

  def usable?
    self.hearth_amenity.whatnot[:use_effects].present?
  end

  def on_cooldown?
    if usable? && cooldown.present?
      # If it's never been used, it's off cooldown
      return false unless self.last_used_at
      seconds_until_cooled_down > 0
    else
      false
    end
  end

  def seconds_until_cooled_down
    return nil unless usable?
    seconds_until_cooled_down = cooldown - (Time.now - self.last_used_at)
    [0, seconds_until_cooled_down].max
  end

  def effects
    self.hearth_amenity.whatnot[:effects]&.reject { |e| e[:level] > self.level }
  end
end