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/

Examples:

Usage

Lich::Gemstone::CritRanks.init

Class Method Summary collapse

Class Method Details

.clean_key(key) ⇒ String, Integer

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

Examples:

Cleaning a key

cleaned_key = Lich::Gemstone::CritRanks.clean_key(:SomeKey)

Parameters:

  • key (String, Symbol, Integer)

    The key to clean.

Returns:

  • (String, Integer)

    The cleaned key.



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_indicesvoid

Note:

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.

Examples:

Creating indices

Lich::Gemstone::CritRanks.create_indices


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.

Examples:

Fetching data

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

Parameters:

  • type (String)

    The type of critical hit.

  • location (String)

    The location of the critical hit.

  • rank (String)

    The rank of the critical hit.

Returns:

  • (Hash, nil)

    The data for the specified type, location, and rank, or nil if not found.

Raises:

  • (StandardError)

    If an error occurs during fetching.



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

.initvoid

Note:

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.

Examples:

Initializing the critical table

Lich::Gemstone::CritRanks.init


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

.locationsArray

Returns an array of locations from the critical table.

Examples:

Getting locations

locations = Lich::Gemstone::CritRanks.locations

Returns:

  • (Array)

    An array of locations.



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.

Examples:

Parsing a line

matches = Lich::Gemstone::CritRanks.parse("Some input line")

Parameters:

  • line (String)

    The line to parse.

Returns:

  • (Hash)

    A hash of matched regex patterns and their data.



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

.ranksArray

Returns an array of ranks from the critical table.

Examples:

Getting ranks

ranks = Lich::Gemstone::CritRanks.ranks

Returns:

  • (Array)

    An array of ranks.



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.

Examples:

Reloading the critical table

Lich::Gemstone::CritRanks.reload!


51
52
53
54
# File 'documented/gemstone/critranks.rb', line 51

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

.tableHash

Returns the current critical table.

Examples:

Accessing the critical table

critical_data = Lich::Gemstone::CritRanks.table

Returns:

  • (Hash)

    The critical table containing critical hit data.



43
44
45
# File 'documented/gemstone/critranks.rb', line 43

def self.table
  @critical_table
end

.tablesArray<String>

Returns an array of table names from the critical table.

Examples:

Getting table names

table_names = Lich::Gemstone::CritRanks.tables

Returns:

  • (Array<String>)

    An array of table names.



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

.typesArray

Returns an array of types from the critical table.

Examples:

Getting types

types = Lich::Gemstone::CritRanks.types

Returns:

  • (Array)

    An array of types.



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.

Examples:

Validating a key

valid_key = Lich::Gemstone::CritRanks.validate(:SomeKey, Lich::Gemstone::CritRanks.types)

Parameters:

  • key (String, Symbol, Integer)

    The key to validate.

  • valid (Array)

    An array of valid keys.

Returns:

  • (String)

    The cleaned key if valid.

Raises:

  • (RuntimeError)

    If the key is invalid.



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