Module: Lich::Gemstone::CritRanks

Defined in:
documented/gemstone/critranks.rb

Overview

Handles critical ranks for the Gemstone game.

This module manages critical hit tables and related data.

Class Method Summary collapse

Class Method Details

.clean_key(key) ⇒ String, Integer

Cleans and normalizes the provided key for consistency.

Parameters:

  • key (String, Symbol, Integer)

    the key to clean

Returns:

  • (String, Integer)

    the cleaned key



72
73
74
75
76
77
# File 'documented/gemstone/critranks.rb', line 72

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

This method returns an undefined value.

Creates indices for types, locations, and ranks from the critical table.



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'documented/gemstone/critranks.rb', line 93

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 rank data for the specified type, location, and rank.

Parameters:

  • type (String)

    the type of critical rank

  • location (String)

    the location of the critical rank

  • rank (String)

    the rank to fetch

Returns:

  • (Hash, nil)

    the critical rank data or nil if not found

Raises:

  • (RuntimeError)

    if any of the parameters are invalid



122
123
124
125
126
127
128
129
130
# File 'documented/gemstone/critranks.rb', line 122

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

This method returns an undefined value.

Initializes the critical ranks module by loading critical tables.



20
21
22
23
24
25
26
# File 'documented/gemstone/critranks.rb', line 20

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<String>

Returns the locations associated with critical ranks.

Returns:

  • (Array<String>)

    the locations



59
60
61
# File 'documented/gemstone/critranks.rb', line 59

def self.locations
  @locations
end

.parse(line) ⇒ Hash

Parses a line of text and matches it against the critical rank indices.

Parameters:

  • line (String)

    the line to parse

Returns:

  • (Hash)

    matched indices



110
111
112
113
114
# File 'documented/gemstone/critranks.rb', line 110

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<String>

Returns the ranks associated with critical hits.

Returns:

  • (Array<String>)

    the ranks



65
66
67
# File 'documented/gemstone/critranks.rb', line 65

def self.ranks
  @ranks
end

.reload!void

This method returns an undefined value.

Reloads the critical table data.



36
37
38
39
# File 'documented/gemstone/critranks.rb', line 36

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

.tableHash

Returns the critical table data.

Returns:

  • (Hash)

    the critical table



30
31
32
# File 'documented/gemstone/critranks.rb', line 30

def self.table
  @critical_table
end

.tablesArray<String>

Returns an array of table names derived from types.

Returns:

  • (Array<String>)

    the list of table names



43
44
45
46
47
48
49
# File 'documented/gemstone/critranks.rb', line 43

def self.tables
  @tables = []
  @types.each do |type|
    @tables.push(type.to_s.gsub(':', ''))
  end
  @tables
end

.typesArray<String>

Returns the types of critical ranks available.

Returns:

  • (Array<String>)

    the types of critical ranks



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

def self.types
  @types
end

.validate(key, valid) ⇒ String

Validates the provided key against a list of valid options.

Parameters:

  • key (String, Symbol, Integer)

    the key to validate

  • valid (Array<String>)

    the list of valid keys

Returns:

  • (String)

    the cleaned key

Raises:

  • (RuntimeError)

    if the key is invalid



84
85
86
87
88
89
# File 'documented/gemstone/critranks.rb', line 84

def self.validate(key, valid)
  clean = clean_key(key)
  raise "Invalid key '#{key}', expecting one of #{valid.join(',')}" unless valid.include?(clean)

  clean
end