Class: Lich::Gemstone::Effects::Registry
- Inherits:
-
Object
- Object
- Lich::Gemstone::Effects::Registry
- 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
-
#active?(effect) ⇒ Boolean
Checks whether an effect is currently active (not expired).
-
#each {|name, expiry| ... } ⇒ Hash{String, Integer => Time}
Iterates over a snapshot of active effects in this registry.
-
#expiration(effect) ⇒ Time, Integer
Returns the expiration time of an effect by name, ID, or pattern.
-
#initialize(dialog) ⇒ Registry
constructor
private
Creates a Registry for effects in the named dialog window.
-
#time_left(effect) ⇒ Float, Integer
Returns the remaining duration of an effect in minutes.
-
#to_h ⇒ Hash{String, Integer => Time}
A snapshot of the dialog's effects.
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.
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).
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.
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.
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.
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.
27 28 29 |
# File 'documented/gemstone/effects.rb', line 27 def to_h live.dup end |