diff options
Diffstat (limited to 'app/models/character.rb')
-rw-r--r-- | app/models/character.rb | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/app/models/character.rb b/app/models/character.rb index 73d2c4b..59ed5ae 100644 --- a/app/models/character.rb +++ b/app/models/character.rb @@ -5,6 +5,7 @@ class Character < ApplicationRecord has_many :titles, through: :title_awards belongs_to :active_title, class_name: "Title", optional: true has_one :hearth + has_many :equipment has_many :character_items has_many :learned_activities has_many :items, through: :character_items @@ -44,6 +45,30 @@ class Character < ApplicationRecord ci && ci.quantity >= quantity end + def open_slots_for(item) + full_slots = self.equipment.map { |e| e.slot } + item.equip_slots.reject { |slot| full_slots.include?(slot) } + end + + def equip(item) + Character.transaction do + open_slots = self.open_slots_for(item) + raise EquipmentError unless open_slots.any? + self.shift_item(item, -1) + self.equipment.create(item: item, slot: open_slots.first) + end + end + + def unequip(slot) + Character.transaction do + equipment = self.equipment.find_by(slot: slot) + raise EquipmentError unless equipment + item = equipment.item + equipment.destroy + self.shift_item(item, 1) + end + end + def add_skill_xp(skill, amount) CharacterSkill.find_by(skill: skill).increment!(:xp, amount) end |