Module: Lich::DragonRealms::DRBanking

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

Overview

DRBanking provides bank account tracking and vault information storage.

Bank balances are tracked passively by parsing game output when players deposit, withdraw, or check their balance at banks across Elanthia.

Storage: each character's balances live in that character's own row of the shared settings table (script STORE_SCRIPT, scope ":", value { "Town" => copper }). Normally only that character's session writes its row; the one-time migration and ;banks reset all write all of this game's rows, each in one transaction. Every read comes straight from the database, and every write is an atomic read-modify-write. So several sessions on one install can't overwrite each other's balances, and ;banks all always shows every character's latest recorded balances. A row that can't be loaded is logged and treated as empty.

Older versions kept every character in one game-wide row (script 'core', scope "", key 'banking'), and each session wrote back its own stale copy of it. The first read or write that finds that row moves it into per-character rows (see migrate_legacy!).

Usage:

DRBanking.my_accounts                    # Current character's bank balances
DRBanking.all_accounts                   # All characters' bank balances
DRBanking.update_balance("Crossings", 15000)  # Update a balance
DRBanking.total_wealth                   # Sum of current character's balances
DRBanking.total_wealth_all               # Sum across all characters

Defined Under Namespace

Modules: Pattern

Constant Summary collapse

DENOMINATION_VALUES =

Denomination multipliers for converting to copper

{
  'platinum' => 10_000,
  'gold'     => 1_000,
  'silver'   => 100,
  'bronze'   => 10,
  'copper'   => 1
}.freeze
CURRENCY_BANKS =

Currency to bank list mapping

{
  'Kronars' => KRONAR_BANKS,
  'Lirums'  => LIRUM_BANKS,
  'Dokoras' => DOKORA_BANKS
}.freeze
SETTINGS_KEY =

Key that held every character's balances in the legacy game-wide row

'banking'
STORE_SCRIPT =

Script name of the per-character balance rows in script_auto_settings

'core.banking'
LEGACY_SCRIPT =

Script name of the legacy game-wide row (InstanceSettings.game)

'core'
BALANCE_AMOUNT_PATTERN =

Pattern for parsing balance amounts from strings

/(\d+)\s+(platinum|gold|silver|bronze|copper)/i.freeze
DEFERRED_LOG_KEY =

Thread-local key of the log lines held back while a transaction runs (see in_transaction)

:lich_drbanking_deferred_log
SYNC_HINT =

Added to a failed balance write's error: nothing is cached, so the change is gone and the recorded balance stays off until a BALANCE.

'check BALANCE to sync'

Class Method Summary collapse

Class Method Details

.all_accounts ⇒ Hash

Returns the bank accounts of every character in this game, read fresh from the database on every call (nothing is cached). Characters with no recorded balance are left out. A database error is reported and gives {}.

Returns:

  • (Hash) —

    { "CharName" => { "Town" => copper_amount } }



116
117
118
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 116

def all_accounts
  read_all_accounts || {}
end

.clear_balance(town) ⇒ Object

Clears the balance for a town (no account)

Parameters:

  • town (String) —

    The town/bank name



141
142
143
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 141

def clear_balance(town)
  update_balance(town, 0)
end

.current_bank_town ⇒ String?

Finds the current bank town based on room title

Returns:

  • (String, nil) —

    The town name or nil if not in a bank



199
200
201
202
203
204
205
206
207
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 199

def current_bank_town
  room_title = XMLData.room_title
  return nil if room_title.nil? || room_title.empty?

  BANK_TITLES.each do |town, titles|
    return town if titles.any? { |title| room_title.include?(title.gsub('[[', '').gsub(']]', '')) }
  end
  nil
end

.display_banks ⇒ Object

Displays bank balances for current character



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 236

def display_banks
  # One read: the per-bank lines and the totals come from the same copy.
  accounts = read_my_accounts
  return unless accounts # the failed read was already reported

  if accounts.empty?
    Lich::Messaging.msg('info', 'DRBanking: No bank account info recorded.')
    return
  end

  Lich::Messaging.msg('info', 'DRBanking: Your bank balances:')
  Lich::Messaging.msg('info', '-' * 50)

  # Group by currency
  { 'Kronars' => KRONAR_BANKS, 'Lirums' => LIRUM_BANKS, 'Dokoras' => DOKORA_BANKS }.each do |currency, banks|
    currency_total = 0
    banks.each do |bank_town|
      next unless accounts[bank_town]

      amount = accounts[bank_town]
      currency_total += amount
      Lich::Messaging.msg('info', "  #{bank_town.rjust(25)}: #{format_currency(amount)}")
    end
    Lich::Messaging.msg('info', "  #{currency} Total:".rjust(27) + " #{format_currency(currency_total)}") if currency_total > 0
  end

  Lich::Messaging.msg('info', '-' * 50)
  Lich::Messaging.msg('info', "  #{'Grand Total:'.rjust(25)} #{format_currency(accounts.values.sum)}")
end

.display_banks_all ⇒ Object

Displays bank balances for all characters



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 267

def display_banks_all
  accounts = read_all_accounts
  return unless accounts # the failed read was already reported

  if accounts.empty?
    Lich::Messaging.msg('info', 'DRBanking: No bank account info recorded for any character.')
    return
  end

  Lich::Messaging.msg('info', 'DRBanking: Bank balances for all characters:')
  Lich::Messaging.msg('info', '=' * 60)

  grand_total = 0
  accounts.each do |char_name, char_accounts|
    next if char_accounts.empty?

    char_total = char_accounts.values.sum
    grand_total += char_total

    Lich::Messaging.msg('info', "#{char_name}:")
    char_accounts.each do |town, amount|
      Lich::Messaging.msg('info', "    #{town.rjust(23)}: #{format_currency(amount)}")
    end
    Lich::Messaging.msg('info', "    #{'Character Total:'.rjust(23)} #{format_currency(char_total)}")
    Lich::Messaging.msg('info', '')
  end

  Lich::Messaging.msg('info', '=' * 60)
  Lich::Messaging.msg('info', "Grand Total (all characters): #{format_currency(grand_total)}")
end

.format_currency(copper) ⇒ String

Formats copper amount as a readable currency string

Parameters:

  • copper (Integer, String) —

    Amount in copper

Returns:

  • (String) —

    Formatted string like "5 platinum, 3 gold, 2 silver"



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

def format_currency(copper)
  copper = copper.to_i
  return 'none' if copper <= 0

  parts = []
  DENOMINATION_VALUES.each do |name, value|
    count = copper / value
    if count > 0
      parts << "#{count} #{name}"
      copper %= value
    end
  end
  parts.empty? ? 'none' : parts.join(', ')
end

.my_accounts ⇒ Hash

Returns bank accounts for the current character (a fresh copy)

Returns:

  • (Hash) —

    { "Town" => copper_amount }



122
123
124
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 122

def my_accounts
  read_my_accounts || {}
end

.parse(line) ⇒ Object

Note:

Uses explicit match variables instead of Regexp.last_match for reliability

Parses a line of game output for bank transactions Called by DRParser for each line of server output

Parameters:

  • line (String) —

    A line of game output



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 213

def parse(line)
  return unless line.is_a?(String)

  town = current_bank_town
  return unless town

  # Use explicit match variables instead of Regexp.last_match (more reliable)
  if (match = line.match(Pattern::DEPOSIT_PORTION))
    handle_deposit_portion(town, match)
  elsif line.match?(Pattern::DEPOSIT_ALL_TELLER) || line.match?(Pattern::DEPOSIT_ALL_JAR)
    handle_deposit_all(town)
  elsif (match = line.match(Pattern::WITHDRAW_PORTION))
    handle_withdraw_portion(town, match)
  elsif line.match?(Pattern::WITHDRAW_ALL)
    handle_withdraw_all(town)
  elsif (match = line.match(Pattern::BALANCE_CHECK))
    handle_balance_check(town, match)
  elsif line.match?(Pattern::NO_ACCOUNT)
    (town)
  end
end

.parse_balance_string(balance_string) ⇒ Integer

Parses a balance string like "5 platinum, 3 gold, 2 silver" into copper

Parameters:

  • balance_string (String) —

    The balance text from game output

Returns:

  • (Integer) —

    The total value in copper



169
170
171
172
173
174
175
176
177
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 169

def parse_balance_string(balance_string)
  return 0 if balance_string.nil? || balance_string.empty?

  copper = 0
  balance_string.scan(BALANCE_AMOUNT_PATTERN) do |amount, denom|
    copper += to_copper(amount, denom)
  end
  copper
end

.reload! ⇒ Object

Balances are no longer cached, so there is nothing to reload. Kept for compatibility; returns a fresh read, like all_accounts.



300
301
302
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 300

def reload!
  all_accounts
end

.reset_all! ⇒ Object

Reset all bank data for all characters in this game. Empties every character's row and removes the old game-wide row's 'banking' key, in one transaction. Character rows are overwritten without being read, so a character row that can't be loaded is cleared too. An old game-wide row that can't be loaded is left as it is, since it isn't DRBanking's alone.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 321

def reset_all!
  game = game_code
  unless game
    Lich::Messaging.msg('info', "DRBanking: Can't clear bank data before the game is known.")
    return
  end

  db = adapter
  in_transaction(db) do
    migrate_legacy!(db)
    db.scopes_by_prefix(STORE_SCRIPT, "#{game}:").each do |scope|
      db.save_settings!(STORE_SCRIPT, {}, scope)
    end
  end
  Lich::Messaging.msg('info', 'DRBanking: Cleared all bank data for all characters.')
rescue StandardError => e
  report_storage_error('clear all bank data', e)
end

.reset_character! ⇒ Object

Reset current character's bank data. The row is overwritten without being read, so a row that can't be loaded is cleared too.



306
307
308
309
310
311
312
313
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 306

def reset_character!
  cleared = in_my_row_transaction('clear your bank data') do |db, scope|
    db.save_settings!(STORE_SCRIPT, {}, scope)
  end
  return unless cleared

  Lich::Messaging.msg('info', "DRBanking: Cleared bank data for #{character_name}.")
end

.to_copper(amount, denomination) ⇒ Integer

Converts an amount and denomination to copper

Parameters:

  • amount (Integer, String) —

    The amount

  • denomination (String) —

    The denomination (platinum, gold, silver, bronze, copper)

Returns:

  • (Integer) —

    The value in copper



161
162
163
164
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 161

def to_copper(amount, denomination)
  multiplier = DENOMINATION_VALUES[denomination.downcase] || 1
  amount.to_i * multiplier
end

.total_wealth ⇒ Integer

Calculates total wealth for current character

Returns:

  • (Integer) —

    Total copper across all banks



147
148
149
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 147

def total_wealth
  my_accounts.values.sum
end

.total_wealth_all ⇒ Integer

Calculates total wealth across all characters

Returns:

  • (Integer) —

    Total copper across all characters and banks



153
154
155
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 153

def total_wealth_all
  all_accounts.values.map { |banks| banks.values.sum }.sum
end

.update_balance(town, copper) ⇒ Object

Updates a bank balance for the current character

Parameters:

  • town (String) —

    The town/bank name

  • copper (Integer) —

    The balance in copper



129
130
131
132
133
134
135
136
137
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 129

def update_balance(town, copper)
  stored = modify_my_accounts("save the #{town} balance", hint: SYNC_HINT) do |accounts|
    accounts[town] = copper.to_i
    accounts
  end
  return unless stored

  Lich::Messaging.msg('info', "DRBanking: Updated #{town} balance to #{format_currency(copper)}")
end