Module: Lich::DragonRealms::DRExpMonitor

Defined in:
documented/dragonrealms/drinfomon/drexpmonitor.rb

Overview

Note:

Cannot run simultaneously with exp-monitor.lic script. Use ;kill exp-monitor first if needed.

Real-time experience gain tracking and reporting for DragonRealms.

Provides background thread-based monitoring of skill experience gains with optional inline display of cumulative gains in BRIEFEXP output. Thread-safe with mutex protection.

The reporter thread loop checks generation counter to gracefully retire when replaced or stopped; this prevents orphaned threads and avoids deadlock during reload via ;hmr drinfomon.

Constant Summary collapse

MAX_SQLITE_RETRIES =

Maximum SQLite retry attempts before giving up (prevents infinite loops)

10
BOOLEAN_TRUE_PATTERN =

Stricter boolean matching pattern - anchored to avoid partial matches

/\A(on|true|yes)\z/i.freeze
@@reporter_thread =

Reporter state. ;hmr drinfomon re-runs this file, so these keep their values across a reload: resetting them would orphan a running reporter, and the autostart that follows would add a second one.

nil
@@running =
false
@@generation =

Bumped whenever start launches a reporter, stop retires one, or reset! runs. A reporter thread only acts while the generation it was started with is still current, so a stopped thread cannot clear or outlive the state of its replacement.

0
@@mutex =

Thread safety for start/stop operations

Mutex.new
@@report_interval =

Hard-coded 1 second for real-time reporting

1
@@inline_display =

Lazy-loaded from DB, defaults to false

nil
@@exit_hook_registered =
true

Class Method Summary collapse

Class Method Details

.active? ⇒ Boolean

Check if reporter is running

Returns:

  • (Boolean)


103
104
105
# File 'documented/dragonrealms/drinfomon/drexpmonitor.rb', line 103

def self.active?
  @@running
end

.format_briefexp_off(line, skill, rate_word) ⇒ Object

Format BRIEFEXP OFF line to include cumulative gained experience Original format: " Augmentation: 565 39% learning " Modified format: " Augmentation: 565 39% learning 0.12"



212
213
214
215
216
217
218
# File 'documented/dragonrealms/drinfomon/drexpmonitor.rb', line 212

def self.format_briefexp_off(line, skill, rate_word)
  return line unless @@inline_display

  gained = DRSkill.gained_exp(skill) || 0.00
  padded_rate = rate_word.ljust(DR_LONGEST_LEARNING_RATE_LENGTH)
  line.sub(/(%\s+)(#{Regexp.escape(rate_word)})/, "\\1#{padded_rate} #{format('%0.2f', gained)}")
end

.format_briefexp_on(line, skill) ⇒ Object

Format BRIEFEXP ON line to include cumulative gained experience Original format: " Aug: 565 39% [ 2/34]" Modified format: " Aug: 565 39% [ 2/34] 0.12"



202
203
204
205
206
207
# File 'documented/dragonrealms/drinfomon/drexpmonitor.rb', line 202

def self.format_briefexp_on(line, skill)
  return line unless @@inline_display

  gained = DRSkill.gained_exp(skill) || 0.00
  line.sub(%r{(/34\])}, "\\1 #{format('%0.2f', gained)}")
end

.format_gains(gains_array) ⇒ Array<String>

Aggregates and formats skill gains for display. Combines multiple pulses of the same skill into single entries.

Parameters:

  • gains_array (Array<Hash>) —

    Array of { skill:, change: } hashes

Returns:

  • (Array<String>) —

    Array of "Skill(+N)" formatted strings



224
225
226
227
228
229
230
231
232
233
# File 'documented/dragonrealms/drinfomon/drexpmonitor.rb', line 224

def self.format_gains(gains_array)
  # Aggregate multiple pulses of same skill
  aggregated = gains_array.reduce(Hash.new(0)) do |result, gain|
    result[gain[:skill]] += gain[:change]
    result
  end

  # Format as "Skill(+N)" strings
  aggregated.keys.sort.map { |skill| "#{skill}(+#{aggregated[skill]})" }
end

.inline_display=(value) ⇒ Object

Enable/disable inline display (persisted to DB) BUG FIX: Added max retry count to prevent infinite loop on persistent SQLite lock



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'documented/dragonrealms/drinfomon/drexpmonitor.rb', line 183

def self.inline_display=(value)
  @@inline_display = BOOLEAN_TRUE_PATTERN.match?(value.to_s)
  retries = 0
  begin
    Lich.db.execute("INSERT OR REPLACE INTO lich_settings(name,value) values('display_inline_exp',?);", [@@inline_display.to_s.encode('UTF-8')])
  rescue SQLite3::BusyException
    retries += 1
    if retries >= MAX_SQLITE_RETRIES
      Lich::Messaging.msg('error', "DRExpMonitor: SQLite busy after #{MAX_SQLITE_RETRIES} retries, inline_display setting may not be persisted")
      return
    end
    sleep 0.1
    retry
  end
end

.inline_display? ⇒ Boolean

Check if inline display is enabled (lazy-loaded from DB, defaults to false) BUG FIX: Added max retry count to prevent infinite loop on persistent SQLite lock

Returns:

  • (Boolean)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'documented/dragonrealms/drinfomon/drexpmonitor.rb', line 158

def self.inline_display?
  if @@inline_display.nil?
    retries = 0
    begin
      val = Lich.db.get_first_value("SELECT value FROM lich_settings WHERE name='display_inline_exp';")
    rescue SQLite3::BusyException
      retries += 1
      if retries >= MAX_SQLITE_RETRIES
        Lich::Messaging.msg('error', "DRExpMonitor: SQLite busy after #{MAX_SQLITE_RETRIES} retries, defaulting inline_display to false")
        @@inline_display = false
        return @@inline_display
      end
      sleep 0.1
      retry
    end
    # Default to false - inline display must be explicitly enabled
    # Once enabled, the persisted value takes precedence
    # BUG FIX: Use anchored pattern to avoid matching partial strings like "money" or "trust"
    @@inline_display = val.nil? ? false : BOOLEAN_TRUE_PATTERN.match?(val.to_s)
  end
  @@inline_display
end

.report_skill_gains ⇒ void

This method returns an undefined value.

Reports aggregated skill gains to the user. Drains the DRSkill.gained_skills array and displays formatted gains.



238
239
240
241
242
243
244
245
246
# File 'documented/dragonrealms/drinfomon/drexpmonitor.rb', line 238

def self.report_skill_gains
  # Drain the gained_skills array
  new_skills = DRSkill.gained_skills.shift(DRSkill.gained_skills.size)
  return if new_skills.empty?

  # Format and display
  formatted_gains = format_gains(new_skills)
  Lich::Messaging.msg('info', "DRExpMonitor: #{formatted_gains.join(', ')}")
end

.reset! ⇒ Object

Reset state (for testing) Thread-safe: uses mutex to prevent race conditions



109
110
111
112
113
114
# File 'documented/dragonrealms/drinfomon/drexpmonitor.rb', line 109

def self.reset!
  @@mutex.synchronize do
    @@inline_display = nil
    retire_reporter
  end
end

.start ⇒ Object

Start the background reporter thread Thread-safe: uses mutex to prevent race conditions



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'documented/dragonrealms/drinfomon/drexpmonitor.rb', line 65

def self.start
  @@mutex.synchronize do
    if @@running
      Lich::Messaging.msg('info', 'DRExpMonitor: Experience gain reporting is already active')
      return
    end

    # Check for exp-monitor.lic conflict
    if Script.running?('exp-monitor')
      Lich::Messaging.msg('error', 'DRExpMonitor: Cannot start: exp-monitor.lic script is running')
      Lich::Messaging.msg('error', "DRExpMonitor: Stop it first with: #{$clean_lich_char}kill exp-monitor")
      return
    end

    generation = (@@generation += 1)
    @@running = true
    @@reporter_thread = Thread.new(generation) { |owned| run_reporter(owned) }
  end
end

.stop ⇒ Object

Stop the background reporter thread Thread-safe: uses mutex to prevent race conditions

Does not wait for the thread to finish. The thread's ensure takes @@mutex, so joining here would deadlock; the generation bump already keeps the dying thread from touching a later start's state.



91
92
93
94
95
96
97
98
99
100
# File 'documented/dragonrealms/drinfomon/drexpmonitor.rb', line 91

def self.stop
  @@mutex.synchronize do
    unless @@running
      Lich::Messaging.msg('info', 'DRExpMonitor: Experience gain reporting is already inactive')
      return
    end

    retire_reporter
  end
end