diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/character.rb | 25 | ||||
-rw-r--r-- | app/models/equipment.rb | 11 | ||||
-rw-r--r-- | app/models/item.rb | 9 |
3 files changed, 45 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 diff --git a/app/models/equipment.rb b/app/models/equipment.rb new file mode 100644 index 0000000..11030fd --- /dev/null +++ b/app/models/equipment.rb @@ -0,0 +1,11 @@ +class Equipment < ApplicationRecord + belongs_to :character + belongs_to :item + enum slot: [:mainhand, :offhand, :head, :neck, :back, :torso, :grip, + :left_ring, :right_ring, :waist, :legs, :feet, :curio] + validates :slot, presence: true, uniqueness: { scope: :character } + + def slot + self[:slot].to_sym + end +end diff --git a/app/models/item.rb b/app/models/item.rb index d42460b..5e60f04 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -5,4 +5,13 @@ class Item < ApplicationRecord :left_ring, :right_ring, :waist, :legs, :feet, :curio] validates :gid, :name, :description, presence: true validates :usable, inclusion: { in: [true, false] } + + def equipment? + self.whatnot && self.whatnot[:equip_slots]&.any? + end + + def equip_slots + return [] unless self.equipment? + self.whatnot[:equip_slots].map { |data| data.to_sym } + end end |