summaryrefslogtreecommitdiff
path: root/app/models/item.rb
blob: 8b8078888a7d5ba88e766805db7ad85f67327f05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Item < ApplicationRecord
  include HasWhatnot

  enum equip_slot: [:mainhand, :offhand, :head, :neck, :back, :torso, :grip,
                    :left_ring, :right_ring, :waist, :legs, :feet, :curio]
  validates :gid, :name, :description, presence: true

  def equipment?
    self.whatnot && self.whatnot[:equip_slots]&.any?
  end

  def usable?
    self.whatnot && self.whatnot[:use_effects]&.any?
  end

  def equip_slots
    return [] unless self.equipment?
    self.whatnot[:equip_slots].map { |data| data.to_sym }
  end

  def equip_requirements
    requirements = []
    self.whatnot[:equip_requirements]&.each do |req|
      case req[:type]
      when "skill"
        requirements.push "level #{req[:level]} #{Skill.find_by_gid(req[:gid]).name}"
      else
        raise "Invalid equip requirement type string (#{req[:type]})"
      end
    end
    requirements
  end
end