summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorDavid Gay <david@davidgay.org>2021-05-19 18:39:35 -0400
committerDavid Gay <david@davidgay.org>2021-05-19 18:39:35 -0400
commit9fec79398a34d26be1042e35cae429b88f8b96d0 (patch)
treec51814b60f5fb3ae753f9fe7549b30af6930adcc /app/models
parent9ab7dfd99e7015513bfe1a092f257c1c7a7afb1b (diff)
Revise and progress with hearth amenity construction
Diffstat (limited to 'app/models')
-rw-r--r--app/models/activity.rb12
-rw-r--r--app/models/character.rb28
-rw-r--r--app/models/hearth.rb9
-rw-r--r--app/models/hearth_amenity.rb11
4 files changed, 49 insertions, 11 deletions
diff --git a/app/models/activity.rb b/app/models/activity.rb
index fb99e1d..e70121e 100644
--- a/app/models/activity.rb
+++ b/app/models/activity.rb
@@ -1,6 +1,16 @@
class Activity < ApplicationRecord
include HasWhatnot
- belongs_to :location
+ belongs_to :location, optional: true
validates :gid, :name, :description, presence: true
+
+ def cost_string
+ requirements = []
+ data = self.whatnot[:cost]
+ return nil unless data
+ data[:items].each do |item_gid, quantity|
+ requirements.push "#{quantity} #{Item.find_by_gid(item_gid).name}"
+ end
+ requirements.join(", ")
+ end
end
diff --git a/app/models/character.rb b/app/models/character.rb
index 661521b..a55230e 100644
--- a/app/models/character.rb
+++ b/app/models/character.rb
@@ -11,6 +11,7 @@ class Character < ApplicationRecord
validates_format_of :name, with: /\A[a-z]+\z/i, message: "must consist of letters only"
after_create :create_skills
+ after_create { Hearth.create(character: self) }
def shift_item(gid, amount)
CharacterItem.transaction do
@@ -20,6 +21,11 @@ class Character < ApplicationRecord
end
end
+ def has_item?(item, quantity = 1)
+ ci = self.character_items.find_by(item: item)
+ ci && ci.quantity >= quantity
+ end
+
def add_skill_xp(skill, amount)
CharacterSkill.find_by(skill: skill).increment!(:xp, amount)
end
@@ -32,13 +38,31 @@ class Character < ApplicationRecord
return nil unless self.activity
duration_data = self.activity.whatnot[:duration]
duration = duration_data[:base]
- duration_data[:scaling].each do |skill, scaling_amount|
+ duration_data[:scaling]&.each do |skill, scaling_amount|
duration -= self.skill_level(skill) * scaling_amount
end
- duration = [duration, duration_data[:minimum]].max
+ duration = [duration, duration_data[:minimum] || 10].max
duration - (Time.now - self.activity_started_at)
end
+ def can_do_activity?(activity)
+ if activity.whatnot[:cost]
+ activity.whatnot[:cost][:items]&.each do |item_gid, quantity|
+ return false unless self.has_item?(item_gid, quantity)
+ end
+ end
+ activity.whatnot[:requirements]&.each do |requirement|
+ case requirement[:type]
+ when "hearth_amenity"
+ return false unless self.hearth.has_amenity?(requirement[:gid], requirement[:level])
+ end
+ end
+ end
+
+ def start_activity(activity)
+ self.update(activity: activity, activity_started_at: Time.now) if self.can_do_activity?(activity)
+ end
+
private
def create_skills
Skill.all.each { |skill| self.character_skills.create(skill: skill, xp: 0) }
diff --git a/app/models/hearth.rb b/app/models/hearth.rb
index 8885a08..5d244ec 100644
--- a/app/models/hearth.rb
+++ b/app/models/hearth.rb
@@ -1,3 +1,12 @@
class Hearth < ApplicationRecord
belongs_to :character
+ has_many :built_hearth_amenities
+ has_many :hearth_amenities, through: :built_hearth_amenities
+
+ 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
end
diff --git a/app/models/hearth_amenity.rb b/app/models/hearth_amenity.rb
index 16d8f97..f8ebad5 100644
--- a/app/models/hearth_amenity.rb
+++ b/app/models/hearth_amenity.rb
@@ -1,14 +1,9 @@
class HearthAmenity < ApplicationRecord
include HasWhatnot
- validates :gid, :name, :description, :whatnot, presence: true
+ validates :gid, :name, :description, presence: true
- def build_requirements_string(level)
- requirements = []
- data = self.whatnot[:constructions].find { |d| d[:level] == level }
- data[:cost][:items].each do |item_gid, quantity|
- requirements.push "#{quantity} #{Item.find_by_gid(item_gid).name}"
- end
- requirements.join(", ")
+ def construct_activity(level)
+ Activity.find_by_gid("construct_#{self.name.underscore}_level#{level}")
end
end