From 5ae89950fb330fd623634b98a77de259ab174b53 Mon Sep 17 00:00:00 2001 From: David Gay Date: Fri, 28 May 2021 11:26:44 -0400 Subject: Lockbox unlocking, and with it items that start activities when used --- app/models/activity.rb | 28 +------------------ app/models/character.rb | 15 +++++++++-- app/models/concerns/has_costs_and_requirements.rb | 33 +++++++++++++++++++++++ 3 files changed, 47 insertions(+), 29 deletions(-) create mode 100644 app/models/concerns/has_costs_and_requirements.rb (limited to 'app/models') diff --git a/app/models/activity.rb b/app/models/activity.rb index c557752..4a169b4 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -1,34 +1,8 @@ class Activity < ApplicationRecord - include HasWhatnot + include HasWhatnot, HasCostsAndRequirements belongs_to :location, optional: true validates :gid, :name, :description, presence: true attribute :innate, :boolean, default: false - - def costs - costs = [] - self.whatnot[:cost]&.each do |cost| - case cost[:type] - when "item" - costs.push "#{cost[:quantity]} #{Item.find_by_gid(cost[:gid]).name}" - end - end - costs - end - - def requirements - requirements = [] - self.whatnot[:requirements]&.each do |req| - case req[:type] - when "skill" - requirements.push "level #{req[:level]} #{Skill.find_by_gid(req[:gid]).name}" - when "hearth_amenity" - requirements.push "level #{req[:level]} #{HearthAmenity.find_by_gid(req[:gid]).name}" - else - raise "Invalid requirement type string (#{req[:type]})" - end - end - requirements - end end diff --git a/app/models/character.rb b/app/models/character.rb index 42f5f10..fda6868 100644 --- a/app/models/character.rb +++ b/app/models/character.rb @@ -60,7 +60,7 @@ class Character < ApplicationRecord activity.whatnot[:cost]&.each do |cost| case cost[:type] when "item" - self.shift_item(cost[:gid], -cost[:quantity]) + self.shift_item(cost[:gid], -(cost[:quantity] || 1)) end end end @@ -72,6 +72,11 @@ class Character < ApplicationRecord ci && ci.quantity >= quantity end + def equipment_with_gid(item) + item = Item.find_by_gid(item) if item.is_a? String + self.equipment.find_by(item: item) + end + def open_slots_for(item) full_slots = self.equipment.map { |e| e.slot } item.equip_slots.reject { |slot| full_slots.include?(slot) } @@ -134,17 +139,23 @@ class Character < ApplicationRecord activity.whatnot[:cost]&.each do |cost| case cost[:type] when "item" - return false unless self.has_item?(cost[:gid], cost[:quantity]) + return false unless self.has_item?(cost[:gid], cost[:quantity] || 1) + else + raise "Invalid cost type string (#{cost[:type]})" end end end unless ignore_requirements activity.whatnot[:requirements]&.each do |requirement| case requirement[:type] + when "equipment" + return false unless self.equipment_with_gid(requirement[:gid]) when "skill" return false unless self.skill_level(requirement[:gid]) >= requirement[:level] when "hearth_amenity" return false unless self.hearth.has_amenity?(requirement[:gid], requirement[:level]) + else + raise "Invalid requirement type string (#{requirement[:type]})" end end end diff --git a/app/models/concerns/has_costs_and_requirements.rb b/app/models/concerns/has_costs_and_requirements.rb new file mode 100644 index 0000000..34ff0f3 --- /dev/null +++ b/app/models/concerns/has_costs_and_requirements.rb @@ -0,0 +1,33 @@ +module HasCostsAndRequirements + extend ActiveSupport::Concern + + included do + def costs + costs = [] + self.whatnot[:cost]&.each do |cost| + case cost[:type] + when "item" + costs.push "#{cost[:quantity]} #{Item.find_by_gid(cost[:gid]).name}" + end + end + costs + end + + def requirements + requirements = [] + self.whatnot[:requirements]&.each do |req| + case req[:type] + when "skill" + requirements.push "level #{req[:level]} #{Skill.find_by_gid(req[:gid]).name}" + when "equipment" + requirements.push "equipped #{Item.find_by_gid(req[:gid]).name}" + when "hearth_amenity" + requirements.push "level #{req[:level]} #{HearthAmenity.find_by_gid(req[:gid]).name}" + else + raise "Invalid requirement type string (#{req[:type]})" + end + end + requirements + end + end +end -- cgit v1.2.3