Class: Lich::Gemstone::Effects::Registry

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
documented/gemstone/effects.rb

Overview

Manages a collection of effects in the Lich system. This class includes methods to handle the registration and retrieval of effects.

Examples:

Creating a new effect registry

registry = Lich::Gemstone::Effects::Registry.new("Active Effects")

Instance Method Summary collapse

Constructor Details

#initialize(dialog) ⇒ Registry

Initializes a new Registry instance.

Parameters:

  • dialog (String)

    The name of the dialog associated with this registry.



14
15
16
# File 'documented/gemstone/effects.rb', line 14

def initialize(dialog)
  @dialog = dialog
end

Instance Method Details

#active?(effect) ⇒ Boolean

Checks if a given effect is currently active.

Parameters:

  • effect (String, Regexp)

    The effect to check.

Returns:

  • (Boolean)

    True if the effect is active, false otherwise.



45
46
47
# File 'documented/gemstone/effects.rb', line 45

def active?(effect)
  expiration(effect).to_f > Time.now.to_f
end

#each {|key, value| ... } ⇒ Enumerator

Iterates over each effect in the registry.

Yields:

  • (key, value)

    Yields each key-value pair in the registry.

Returns:

  • (Enumerator)

    An enumerator if no block is given.



27
28
29
# File 'documented/gemstone/effects.rb', line 27

def each()
  to_h.each { |k, v| yield(k, v) }
end

#expiration(effect) ⇒ Integer

Retrieves the expiration time of a given effect.

Parameters:

  • effect (String, Regexp)

    The effect to check for expiration.

Returns:

  • (Integer)

    The expiration time in seconds since epoch, or 0 if not found.



34
35
36
37
38
39
40
# File 'documented/gemstone/effects.rb', line 34

def expiration(effect)
  if effect.is_a?(Regexp)
    to_h.find { |k, _v| k.to_s =~ effect }[1] || 0
  else
    to_h.fetch(effect, 0)
  end
end

#time_left(effect) ⇒ Float

Calculates the time left for a given effect.

Parameters:

  • effect (String, Regexp)

    The effect to check.

Returns:

  • (Float)

    The time left in minutes, or the expiration time if not active.



52
53
54
55
56
57
58
# File 'documented/gemstone/effects.rb', line 52

def time_left(effect)
  if expiration(effect) != 0
    ((expiration(effect) - Time.now) / 60.to_f)
  else
    expiration(effect)
  end
end

#to_hHash

Converts the registry to a hash representation.

Returns:

  • (Hash)

    A hash of effects associated with the dialog.



20
21
22
# File 'documented/gemstone/effects.rb', line 20

def to_h
  XMLData.dialogs.fetch(@dialog, {})
end