diff options
Diffstat (limited to 'app/models/character.rb')
-rw-r--r-- | app/models/character.rb | 28 |
1 files changed, 26 insertions, 2 deletions
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) } |