Module: Lich::Gemstone::CritRanks

Defined in:
lib/gemstone/critranks.rb,
lib/gemstone/critranks/generic_critical_table.rb

Class Method Summary collapse

Class Method Details

.clean_key(key) ⇒ String, Integer

Cleans the provided key by converting it to a standardized format.

Examples:

cleaned_key = CritRanks.clean_key("Some Key - Example")

Parameters:

  • key (Integer, Symbol, String)

    the key to clean.

Returns:

  • (String, Integer)

    the cleaned key.



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_indicesvoid

Note:

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.

Examples:

data = CritRanks.fetch(:type, :location, :rank)

Parameters:

  • type (String, Symbol)

    the type of critical hit.

  • location (String, Symbol)

    the location of the critical hit.

  • rank (String, Symbol)

    the rank of the critical hit.

Returns:

  • (Hash, nil)

    the critical hit data or nil if not found.

Raises:

  • (RuntimeError)

    if any of the keys are invalid.



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

.initvoid

Note:

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.

Examples:

CritRanks.init


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

.locationsArray<Symbol>

Returns an array of locations available in the critical table.

Examples:

locations = CritRanks.locations

Returns:

  • (Array<Symbol>)

    the locations of critical hits.



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.

Examples:

matches = CritRanks.parse("Some input line")

Parameters:

  • line (String)

    the line to parse.

Returns:

  • (Array)

    an array of matches found.



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

.ranksArray<Symbol>

Returns an array of ranks available in the critical table.

Examples:

ranks = CritRanks.ranks

Returns:

  • (Array<Symbol>)

    the ranks of critical hits.



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.

Examples:

CritRanks.reload!


57
58
59
60
# File 'lib/gemstone/critranks.rb', line 57

def self.reload!
  @critical_table = {}
  init
end

.tableHash

Returns the current critical table.

Examples:

table = CritRanks.table

Returns:

  • (Hash)

    the critical table containing critical hit data.



45
46
47
# File 'lib/gemstone/critranks.rb', line 45

def self.table
  @critical_table
end

.tablesArray<String>

Returns an array of types available in the critical table.

Examples:

types = CritRanks.tables

Returns:

  • (Array<String>)

    the types of critical hits.



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

.typesArray<Symbol>

Returns an array of types available in the critical table.

Examples:

types = CritRanks.types

Returns:

  • (Array<Symbol>)

    the types of critical hits.



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.

Examples:

valid_key = CritRanks.validate(:some_key, CritRanks.types)

Parameters:

  • key (String, Symbol)

    the key to validate.

  • valid (Array<String>)

    the list of valid keys.

Returns:

  • (String)

    the cleaned key if valid.

Raises:

  • (RuntimeError)

    if the key is invalid.



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