Class: Lich::DragonRealms::Flags
- Inherits:
-
Object
- Object
- Lich::DragonRealms::Flags
- Defined in:
- documented/dragonrealms/drinfomon/events.rb
Overview
Manages named event flags and matchers for game line pattern matching.
The Flags class provides thread-safe storage for event flags that track whether game output has matched regex patterns added by scripts. Matchers are stored in a frozen hash that is atomically swapped during add/delete operations, allowing the game thread to safely iterate over patterns while scripts modify the set.
Constant Summary collapse
- @@flags =
{}
- @@matchers =
DRParser.check_events iterates @@matchers on the game thread while scripts add and delete flags from their own threads. Inserting a key into a hash another thread is iterating raises "can't add a new key into hash during iteration", so writers never mutate @@matchers in place: they build a new hash and swap the reference, and an iteration in progress keeps walking the hash it started with. The mutex stops two writers from both copying the same hash and losing an update, and makes record_match's check-and-write atomic against add and delete.
{}.freeze
- @@matchers_lock =
Mutex.new
Class Method Summary collapse
-
.[](key) ⇒ MatchData, ...
Retrieves the current value of a flag.
-
.[]=(key, value) ⇒ Object
private
Sets a flag to an arbitrary value.
-
.add(key, *matchers) ⇒ Array<Regexp>
Registers a new flag with regex matchers for game output.
-
.delete(key) ⇒ void
Unregisters a flag and removes it from storage.
-
.flags ⇒ Hash
private
Returns the internal flags hash.
-
.matchers ⇒ Hash
private
Returns the internal matchers hash.
-
.record_match(key, regexes, matches) ⇒ void
Sets a flag to a match DRParser.check_events found, unless the flag was deleted or added again since check_events took its snapshot of the matchers.
-
.reset(key) ⇒ Boolean
Sets a flag back to false.
Class Method Details
.[](key) ⇒ MatchData, ...
Retrieves the current value of a flag.
39 40 41 |
# File 'documented/dragonrealms/drinfomon/events.rb', line 39 def self.[](key) @@flags[key] end |
.[]=(key, value) ⇒ Object
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.
Sets a flag to an arbitrary value.
49 50 51 |
# File 'documented/dragonrealms/drinfomon/events.rb', line 49 def self.[]=(key, value) @@flags[key] = value end |
.add(key, *matchers) ⇒ Array<Regexp>
Registers a new flag with regex matchers for game output.
Patterns can be Regexp objects or strings; strings are converted to case-insensitive regexes. The flag is initialized to false and will be set to the MatchData of the first matching pattern when the game outputs a matching line.
65 66 67 68 69 70 71 72 |
# File 'documented/dragonrealms/drinfomon/events.rb', line 65 def self.add(key, *matchers) regexes = matchers.map { |item| item.is_a?(Regexp) ? item : /#{item}/i }.freeze @@matchers_lock.synchronize do @@flags[key] = false @@matchers = @@matchers.merge(key => regexes).freeze end regexes end |
.delete(key) ⇒ void
This method returns an undefined value.
Unregisters a flag and removes it from storage.
The matchers for the key are removed atomically with the flag deletion to prevent stale pattern matches from re-adding the flag.
91 92 93 94 95 96 |
# File 'documented/dragonrealms/drinfomon/events.rb', line 91 def self.delete(key) @@matchers_lock.synchronize do @@matchers = @@matchers.reject { |k, _| k == key }.freeze @@flags.delete key end end |
.flags ⇒ Hash
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.
Returns the internal flags hash.
117 118 119 |
# File 'documented/dragonrealms/drinfomon/events.rb', line 117 def self.flags @@flags end |
.matchers ⇒ Hash
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.
Returns the internal matchers hash.
125 126 127 |
# File 'documented/dragonrealms/drinfomon/events.rb', line 125 def self.matchers @@matchers end |
.record_match(key, regexes, matches) ⇒ void
This method returns an undefined value.
Sets a flag to a match DRParser.check_events found, unless the flag was deleted or added again since check_events took its snapshot of the matchers. Without the check, a line matching a stale snapshot puts a deleted flag back into flags, or sets a re-added flag from its old patterns.
107 108 109 110 111 |
# File 'documented/dragonrealms/drinfomon/events.rb', line 107 def self.record_match(key, regexes, matches) @@matchers_lock.synchronize do @@flags[key] = matches if @@matchers[key].equal?(regexes) end end |
.reset(key) ⇒ Boolean
Sets a flag back to false.
79 80 81 |
# File 'documented/dragonrealms/drinfomon/events.rb', line 79 def self.reset(key) @@flags[key] = false end |