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

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

Overview

Enumerable registry of active effects. Wraps the game server's XML dialog data (effects by name or numeric ID and their expiration time) and provides thread-safe iteration and lookup.

Instance Method Summary collapse

Constructor Details

#initialize(dialog) ⇒ Registry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a Registry for effects in the named dialog window.

Parameters:

  • dialog (String) —

    the XML dialog window name, e.g. "Active Spells"



17
18
19
# File 'documented/gemstone/effects.rb', line 17

def initialize(dialog)
  @dialog = dialog
end

Instance Method Details

#active?(effect) ⇒ Boolean

Checks whether an effect is currently active (not expired).

Examples:

Effects::Spells.active?("Magic Missile") #=> true

Parameters:

  • effect (String, Integer, Regexp) —

    the effect name, numeric ID, or regex pattern to match

Returns:

  • (Boolean) —

    true if the effect's expiration time is in the future, false otherwise



68
69
70
# File 'documented/gemstone/effects.rb', line 68

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

#each {|name, expiry| ... } ⇒ Hash{String, Integer => Time}

Iterates over a snapshot of active effects in this registry.

Examples:

Effects::Spells.each { |name, expiry| puts "#{name} expires at #{expiry}" }

Yields:

  • (name, expiry) —

    yields each effect

Yield Parameters:

  • name (String, Integer) —

    the effect name or numeric ID

  • expiry (Time) —

    the time the effect expires, or 0 if inactive

Returns:

  • (Hash{String, Integer => Time}) —

    the effects hash



39
40
41
# File 'documented/gemstone/effects.rb', line 39

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

#expiration(effect) ⇒ Time, Integer

Returns the expiration time of an effect by name, ID, or pattern.

Examples:

Look up effect by name

Effects::Spells.expiration("Magic Missile") #=> 2024-01-15 14:32:10 +0000

Look up effect by ID

Effects::Spells.expiration(510) #=> 2024-01-15 14:32:10 +0000

Match effect name by pattern

Effects::Buffs.expiration(/Protect/) #=> 2024-01-15 14:35:22 +0000

Parameters:

  • effect (String, Integer, Regexp) —

    the effect name, numeric ID, or regex pattern to match

Returns:

  • (Time, Integer) —

    the expiration time as a Time object, or 0 if the effect is not active



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

def expiration(effect)
  if effect.is_a?(Regexp)
    (to_h.find { |k, _v| k.to_s =~ effect } || [nil, 0])[1]
  else
    # a single lookup does not iterate, so it can read the live Hash
    live.fetch(effect, 0)
  end
end

#time_left(effect) ⇒ Float, Integer

Returns the remaining duration of an effect in minutes.

Examples:

Effects::Spells.time_left("Magic Missile") #=> 4.2

Parameters:

  • effect (String, Integer, Regexp) —

    the effect name, numeric ID, or regex pattern to match

Returns:

  • (Float, Integer) —

    the minutes remaining (as a decimal), or 0 if the effect is not active



78
79
80
81
82
83
84
# File 'documented/gemstone/effects.rb', line 78

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

#to_h ⇒ Hash{String, Integer => Time}

A snapshot of the dialog's effects. The XML parser thread writes the live Hash, and adding a key to a Hash while another thread iterates it raises in the thread doing the adding: the parser, not the script. Iterating a copy keeps a script's scan from breaking the parser.

Returns:

  • (Hash{String, Integer => Time}) —

    effect name or id => expiry



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

def to_h
  live.dup
end