From 4c96893a114059dc4e748307c6d046ec1faa778f Mon Sep 17 00:00:00 2001 From: David Gay Date: Wed, 2 Jun 2021 22:15:02 -0400 Subject: Magiculture --- app/models/character.rb | 10 ++++++++++ app/models/concerns/has_whatnot.rb | 4 ++++ app/models/hearth.rb | 11 +++++++++++ app/models/hearth_planting.rb | 9 +++++++++ 4 files changed, 34 insertions(+) create mode 100644 app/models/hearth_planting.rb (limited to 'app/models') diff --git a/app/models/character.rb b/app/models/character.rb index b7b323d..84583a8 100644 --- a/app/models/character.rb +++ b/app/models/character.rb @@ -106,6 +106,10 @@ class Character < ApplicationRecord case cost[:type] when "item" self.shift_item(cost[:gid], -(cost[:quantity] || 1)) + when "hearth_planting" + hp = hearth.ripe_hearth_plantings_of(cost[:gid]).first + raise HearthPlantingError unless hp + hp.destroy end end end @@ -217,6 +221,8 @@ class Character < ApplicationRecord case cost[:type] when "item" return false unless self.has_item?(cost[:gid], cost[:quantity] || 1) + when "hearth_planting" + return false unless hearth.ripe_hearth_plantings_of(cost[:gid]).first else raise "Invalid cost type string (#{cost[:type]})" end @@ -288,6 +294,10 @@ class Character < ApplicationRecord effects.filter_map { |e| e[:modifier] if e[:type] == "stat_change" && e[:gid] == gid }.sum end + def planting_spots + [total_stat_change("planting_spots"), 0].max + end + def can_fight? self.wounds < max_wounds end diff --git a/app/models/concerns/has_whatnot.rb b/app/models/concerns/has_whatnot.rb index a4dcfa0..289fe40 100644 --- a/app/models/concerns/has_whatnot.rb +++ b/app/models/concerns/has_whatnot.rb @@ -5,5 +5,9 @@ module HasWhatnot def whatnot @whatnot ||= super.is_a?(Hash) ? super.deep_symbolize_keys! : nil end + + def tags + whatnot[:tags] if whatnot + end end end diff --git a/app/models/hearth.rb b/app/models/hearth.rb index 1bd54a5..ee00c0c 100644 --- a/app/models/hearth.rb +++ b/app/models/hearth.rb @@ -2,6 +2,7 @@ class Hearth < ApplicationRecord belongs_to :character 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 @@ -15,4 +16,14 @@ class Hearth < ApplicationRecord 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 diff --git a/app/models/hearth_planting.rb b/app/models/hearth_planting.rb new file mode 100644 index 0000000..d6ae714 --- /dev/null +++ b/app/models/hearth_planting.rb @@ -0,0 +1,9 @@ +class HearthPlanting < ApplicationRecord + belongs_to :hearth + belongs_to :item + + def ripens_at + created_at + item.whatnot[:ripen_duration][:base].seconds + end +end + -- cgit v1.2.3