summaryrefslogtreecommitdiff
path: root/app/models/hearth.rb
blob: cbd98dca2e56583f578f202a0af3ec4090577069 (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
class Hearth < ApplicationRecord
  belongs_to :character
  belongs_to :location
  has_many :built_hearth_amenities
  has_many :hearth_amenities, through: :built_hearth_amenities
  has_many :hearth_plantings

  def has_amenity?(hearth_amenity, level = 1)
    hearth_amenity = HearthAmenity.find_by_gid(hearth_amenity) if hearth_amenity.is_a? String
    bhi = self.built_hearth_amenities.find_by(hearth_amenity: hearth_amenity)
    return false unless bhi
    bhi.level >= level
  end

  def amenity_level(hearth_amenity)
    hearth_amenity = HearthAmenity.find_by_gid(hearth_amenity) if hearth_amenity.is_a? String
    bhi = self.built_hearth_amenities.find_by(hearth_amenity: hearth_amenity)
    bhi ? bhi.level : 0
  end

  def available_planting_spots
    character.planting_spots - hearth_plantings.count
  end

  def ripe_hearth_plantings_of(item)
    item = Item.find_by_gid(item) if item.is_a? String
    # TODO: Proper querey instead of loading all into memory.
    hearth_plantings.all.select { |hp| hp.item == item && hp.ripens_at < Time.now }
  end
end