Module: Lich::Gemstone::Combat::Messages

Defined in:
documented/gemstone/combat/messages.rb

Overview

Combat message event dispatcher - matches game output to subscription events.

Messages are non-combat events delivered as combat. on the Events board, matched only while subscribed. A single worker thread processes game output lines through a message family matcher; subscribers run on that worker and must be cheap, non-blocking, and issue no game commands.

Installation is automatic: a hook is installed when the first subscription to a message event appears and removed with the last, gated by the matcher checking whether any subscriber exists for each event. With no subscribers, no hook runs.

Examples:

Subscribe to a disarm message event

Combat::Tracker.on(:disarm_seen) { |_type, data| queue << data }

Subscribe to multiple events

Combat::Tracker.on(:ambusher, :bolted, name: 'myscript') { |type, data| ... }

See Also:

Constant Summary collapse

HOOK_ID =
'Combat::Messages::downstream'

Class Method Summary collapse

Class Method Details

.active_families ⇒ Object

The families with a subscriber for at least one of their events. Held as NAMES and resolved against the current table, so a reload never leaves the scanner on a stale Family object.



84
# File 'documented/gemstone/combat/messages.rb', line 84

def active_families = table.by_name.values_at(*@active).compact

.enqueue(line) ⇒ Object

The hook's entry: enqueue for the worker, never block the stream.



129
130
131
132
133
134
135
# File 'documented/gemstone/combat/messages.rb', line 129

def enqueue(line)
  return if @active.empty?

  @queue ||= Queue.new
  @queue.push(line)
  ensure_worker
end

.event?(type) ⇒ Boolean

A message event, as opposed to a combat fact.

Returns:

  • (Boolean)


79
# File 'documented/gemstone/combat/messages.rb', line 79

def event?(type) = table.family_of.key?(type.to_sym)

.events ⇒ Array<Symbol>

All message event names defined in the current definition table.

Returns:

  • (Array<Symbol>) —

    the event names



76
# File 'documented/gemstone/combat/messages.rb', line 76

def events = table.events

.families ⇒ Array

All message families defined in the current definition table.

Returns:

  • (Array) —

    the message families



72
73
74
75
# File 'documented/gemstone/combat/messages.rb', line 72

def families = table.families
# All message event names defined in the current definition table.
#
# @return [Array<Symbol>] the event names

.installed? ⇒ Boolean

Whether the downstream hook is currently installed.

Returns:

  • (Boolean) —

    true if the hook is installed and active



140
# File 'documented/gemstone/combat/messages.rb', line 140

def installed? = @hook

.process(line) ⇒ Object

Scan and emit, synchronously. Used by the worker; tools may call it directly to replay a log.



120
121
122
123
124
125
126
# File 'documented/gemstone/combat/messages.rb', line 120

def process(line)
  found = scan(line)
  @scanned += 1
  @matched += found.size
  found.each { |event, data| Lich::Common::Events.emit("combat.#{event}", data) }
  found
end

.refresh! ⇒ Object

Recompute the active families from the subscriptions and put the hook up or take it down to match. Events calls this on every combat.* subscription change, and Supplements.reload_defs! after a reload (a script may subscribe to an event before the player's file defines it); harmless to call again.

The whole read-decide-act sequence is held under @mutex: two scripts subscribing at once would otherwise interleave so that the last @active write is non-empty while the last hook call is uninstall!, silently leaving a live subscriber with no hook.

DownstreamHook.add/remove raises (Events swallows and logs it), the previous consistent @active/@hook pair stays in place rather than reporting a family active with no hook up, or the reverse.



101
102
103
104
105
106
107
108
# File 'documented/gemstone/combat/messages.rb', line 101

def refresh!
  @mutex.synchronize do
    active = families.select { |f| f.events.any? { |e| Lich::Common::Events.any_for?("combat.#{e}") } }.map(&:name).freeze
    active.empty? ? uninstall! : install!
    @active = active
    active_families
  end
end

.scan(line, families: active_families) ⇒ Array<Array(Symbol, Hash)>

What one line yields, synchronously, over the given families (the active ones by default; pass families to scan them all).

Returns:

  • (Array<Array(Symbol, Hash)>)


114
115
116
# File 'documented/gemstone/combat/messages.rb', line 114

def scan(line, families: active_families)
  Definitions::Messages.scan(line, families)
end

.shutdown ⇒ Object

Drain and stop the worker (tests, shutdown).



154
155
156
157
158
159
160
# File 'documented/gemstone/combat/messages.rb', line 154

def shutdown
  return unless @worker&.alive?

  @queue&.push(:shutdown)
  @worker.join(2)
  @worker = nil
end

.stats ⇒ Hash

Current statistics on the message scanner and worker.

Returns:

  • (Hash) —

    a hash with keys :installed (Boolean), :families (Array), :scanned (Integer count of lines processed), :matched (Integer count of events emitted), :queued (Integer lines waiting in the worker queue), :worker_alive (Boolean)



148
149
150
151
# File 'documented/gemstone/combat/messages.rb', line 148

def stats
  { installed: @hook, families: @active, scanned: @scanned, matched: @matched,
    queued: @queue ? @queue.size : 0, worker_alive: !@worker.nil? && @worker.alive? }
end

.table ⇒ Object

The current definition table, read once per call: after a hot reload (Tracker.reload_defs!, ;hmr combat/defs/) the def module binds a new one, and nothing here may hold the old.



68
69
70
71
# File 'documented/gemstone/combat/messages.rb', line 68

def table = Definitions::Messages.table
# All message families defined in the current definition table.
#
# @return [Array] the message families