Class: Lich::DragonRealms::Flags

Inherits:
Object
  • Object
show all
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.

Examples:

Add a flag matching damage lines

Lich::DragonRealms::Flags.add("hit", "You strike the", /deals? \d+ damage/)
# ...
if Lich::DragonRealms::Flags["hit"]
  respond("Damage matched!")
end

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

Class Method Details

.[](key) ⇒ MatchData, ...

Retrieves the current value of a flag.

Examples:

Lich::DragonRealms::Flags["hit"] #=> #<MatchData "You strike the...">

Parameters:

  • key (String) —

    the flag name

Returns:

  • (MatchData, Boolean, nil) —

    the last MatchData if the flag matched game output, false if reset, or nil if the flag was not added



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.

Parameters:

  • key (String) —

    the flag name

  • value (Object) —

    the value to store

Returns:

  • (Object) —

    the 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.

Examples:

Lich::DragonRealms::Flags.add("spell_cast", "You feel", /spell.*ready/, "The magic feels ready")

Parameters:

  • key (String) —

    the flag name

  • matchers (Regexp, String) —

    one or more patterns to match against game output

Returns:

  • (Array<Regexp>) —

    the frozen array of compiled regex matchers

See Also:



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.

Parameters:

  • key (String) —

    the flag name

See Also:



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.

Returns:

  • (Hash) —

    a hash mapping flag names to MatchData, false, or nil



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.

Returns:

  • (Hash) —

    a frozen hash mapping flag names to frozen arrays of Regexp matchers



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.

Parameters:

  • key (String) —

    the flag

  • regexes (Array<Regexp>) —

    the matchers check_events matched against

  • matches (MatchData) —

    the match



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.

Parameters:

  • key (String) —

    the flag name

Returns:

  • (Boolean) —

    false

See Also:



79
80
81
# File 'documented/dragonrealms/drinfomon/events.rb', line 79

def self.reset(key)
  @@flags[key] = false
end