Module: Lich::Gemstone::CritRanks
- Defined in:
- documented/gemstone/critranks.rb
Overview
Module CritRanks used to resolve critical hits into their mechanical results
This module queries against crit_tables files in lib/crit_tables/
Class Method Summary collapse
-
.clean_key(key) ⇒ String, Integer
Cleans the provided key by converting it to a standard format.
-
.create_indices ⇒ void
Creates indices for types, locations, and ranks from the critical table.
-
.fetch(type, location, rank) ⇒ Hash?
Fetches data from the critical table based on type, location, and rank.
-
.init ⇒ void
Initializes the critical table by loading critical_table.rb files.
-
.locations ⇒ Array
Returns an array of locations from the critical table.
-
.parse(line) ⇒ Hash
Parses a line against the regex patterns in the critical table.
-
.ranks ⇒ Array
Returns an array of ranks from the critical table.
-
.reload! ⇒ void
Reloads the critical table by clearing it and reinitializing.
-
.table ⇒ Hash
Returns the current critical table.
-
.tables ⇒ Array<String>
Returns an array of table names from the critical table.
-
.types ⇒ Array
Returns an array of types from 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 standard format.
97 98 99 100 101 102 |
# File 'documented/gemstone/critranks.rb', line 97 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 to set up the indices.
This method returns an undefined value.
Creates indices for types, locations, and ranks from the critical table.
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'documented/gemstone/critranks.rb', line 123 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 data from the critical table based on type, location, and rank.
156 157 158 159 160 161 162 163 164 |
# File 'documented/gemstone/critranks.rb', line 156 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 load files if the critical table is empty.
This method returns an undefined value.
Initializes the critical table by loading critical_table.rb files.
31 32 33 34 35 36 37 |
# File 'documented/gemstone/critranks.rb', line 31 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
Returns an array of locations from the critical table.
80 81 82 |
# File 'documented/gemstone/critranks.rb', line 80 def self.locations @locations end |
.parse(line) ⇒ Hash
Parses a line against the regex patterns in the critical table.
142 143 144 145 146 |
# File 'documented/gemstone/critranks.rb', line 142 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
Returns an array of ranks from the critical table.
88 89 90 |
# File 'documented/gemstone/critranks.rb', line 88 def self.ranks @ranks end |
.reload! ⇒ void
This method returns an undefined value.
Reloads the critical table by clearing it and reinitializing.
51 52 53 54 |
# File 'documented/gemstone/critranks.rb', line 51 def self.reload! @critical_table = {} init end |
.table ⇒ Hash
Returns the current critical table.
43 44 45 |
# File 'documented/gemstone/critranks.rb', line 43 def self.table @critical_table end |
.tables ⇒ Array<String>
Returns an array of table names from the critical table.
60 61 62 63 64 65 66 |
# File 'documented/gemstone/critranks.rb', line 60 def self.tables @tables = [] @types.each do |type| @tables.push(type.to_s.gsub(':', '')) end @tables end |
.types ⇒ Array
Returns an array of types from the critical table.
72 73 74 |
# File 'documented/gemstone/critranks.rb', line 72 def self.types @types end |
.validate(key, valid) ⇒ String
Validates the provided key against a list of valid keys.
111 112 113 114 115 116 |
# File 'documented/gemstone/critranks.rb', line 111 def self.validate(key, valid) clean = clean_key(key) raise "Invalid key '#{key}', expecting one of #{valid.join(',')}" unless valid.include?(clean) clean end |