Module: Lich::Gemstone::CritRanks
- Defined in:
- lib/gemstone/critranks.rb,
lib/gemstone/critranks/generic_critical_table.rb
Class Method Summary collapse
-
.clean_key(key) ⇒ String, Integer
Cleans the provided key by converting it to a standardized format.
-
.create_indices ⇒ void
Creates indices for types, locations, and ranks from the critical table.
-
.fetch(type, location, rank) ⇒ Hash?
Fetches the critical hit data for a given type, location, and rank.
-
.init ⇒ void
Initializes the critical table by loading all critical table files.
-
.locations ⇒ Array<Symbol>
Returns an array of locations available in the critical table.
-
.parse(line) ⇒ Array
Parses a line to find matches against the critical table’s regex patterns.
-
.ranks ⇒ Array<Symbol>
Returns an array of ranks available in the critical table.
-
.reload! ⇒ void
Reloads the critical table by clearing the current data and reinitializing it.
-
.table ⇒ Hash
Returns the current critical table.
-
.tables ⇒ Array<String>
Returns an array of types available in the critical table.
-
.types ⇒ Array<Symbol>
Returns an array of types available in the critical table.
-
.validate(key, valid) ⇒ String
Validates the provided key against a list of valid keys.
Class Method Details
.clean_key(key) ⇒ String, Integer
Cleans the provided key by converting it to a standardized format.
123 124 125 126 127 128 |
# File 'lib/gemstone/critranks.rb', line 123 def self.clean_key(key) return key.to_i if key.is_a?(Integer) || key =~ (/^\d+$/) return key.downcase if key.is_a?(Symbol) key.strip.downcase.gsub(/[ -]/, '_') end |
.create_indices ⇒ void
This method is called internally during initialization.
This method returns an undefined value.
Creates indices for types, locations, and ranks from the critical table.
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/gemstone/critranks.rb', line 155 def self.create_indices @index_rx ||= {} @critical_table.each do |type, typedata| @types.append(type) typedata.each do |loc, locdata| @locations.append(loc) unless @locations.include?(loc) locdata.each do |rank, record| @ranks.append(rank) unless @ranks.include?(rank) @index_rx[record[:regex]] = record end end end end |
.fetch(type, location, rank) ⇒ Hash?
Fetches the critical hit data for a given type, location, and rank.
196 197 198 199 200 201 202 203 204 |
# File 'lib/gemstone/critranks.rb', line 196 def self.fetch(type, location, rank) table.dig( validate(type, types), validate(location, locations), validate(rank, ranks) ) rescue StandardError => e Lich::Messaging.msg('error', "Error! #{e}") end |
.init ⇒ void
This method will only run if the critical table is empty.
This method returns an undefined value.
Initializes the critical table by loading all critical table files.
29 30 31 32 33 34 35 |
# File 'lib/gemstone/critranks.rb', line 29 def self.init return unless @critical_table.empty? Dir.glob("#{File.join(LIB_DIR, "gemstone", "critranks", "*critical_table.rb")}").each do |file| require file end create_indices end |
.locations ⇒ Array<Symbol>
Returns an array of locations available in the critical table.
98 99 100 |
# File 'lib/gemstone/critranks.rb', line 98 def self.locations @locations end |
.parse(line) ⇒ Array
Parses a line to find matches against the critical table’s regex patterns.
178 179 180 181 182 |
# File 'lib/gemstone/critranks.rb', line 178 def self.parse(line) @index_rx.filter do |rx, _data| rx =~ line.strip # need to strip spaces to support anchored regex in tables end end |
.ranks ⇒ Array<Symbol>
Returns an array of ranks available in the critical table.
110 111 112 |
# File 'lib/gemstone/critranks.rb', line 110 def self.ranks @ranks end |
.reload! ⇒ void
This method returns an undefined value.
Reloads the critical table by clearing the current data and reinitializing it.
57 58 59 60 |
# File 'lib/gemstone/critranks.rb', line 57 def self.reload! @critical_table = {} init end |
.table ⇒ Hash
Returns the current critical table.
45 46 47 |
# File 'lib/gemstone/critranks.rb', line 45 def self.table @critical_table end |
.tables ⇒ Array<String>
Returns an array of types available in the critical table.
70 71 72 73 74 75 76 |
# File 'lib/gemstone/critranks.rb', line 70 def self.tables @tables = [] @types.each do |type| @tables.push(type.to_s.gsub(':', '')) end @tables end |
.types ⇒ Array<Symbol>
Returns an array of types available in the critical table.
86 87 88 |
# File 'lib/gemstone/critranks.rb', line 86 def self.types @types end |
.validate(key, valid) ⇒ String
Validates the provided key against a list of valid keys.
141 142 143 144 145 146 |
# File 'lib/gemstone/critranks.rb', line 141 def self.validate(key, valid) clean = clean_key(key) raise "Invalid key '#{key}', expecting one of #{valid.join(',')}" unless valid.include?(clean) clean end |