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.
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.
Defined Under Namespace
Modules: Pattern
Constant Summary collapse
- DENOMINATION_VALUES =
Denomination multipliers for converting to copper Denomination multipliers for converting to copper.
This hash maps currency denominations to their values in copper.
{ 'platinum' => 10_000, 'gold' => 1_000, 'silver' => 100, 'bronze' => 10, 'copper' => 1 }.freeze
- CURRENCY_BANKS =
Currency to bank list mapping Currency to bank list mapping.
This hash maps currency types to their respective bank lists.
{ 'Kronars' => KRONAR_BANKS, 'Lirums' => LIRUM_BANKS, 'Dokoras' => DOKORA_BANKS }.freeze
- SETTINGS_KEY =
Settings key for banking data Settings key for banking data.
This constant holds the key used to store and retrieve banking data.
'banking'- BALANCE_AMOUNT_PATTERN =
Pattern for parsing balance amounts from strings Pattern for parsing balance amounts from strings.
Matches strings that represent balance amounts in various denominations.
/(\d+)\s+(platinum|gold|silver|bronze|copper)/i.freeze
- @@accounts_cache =
In-memory cache of accounts data
nil
Class Method Summary collapse
-
.all_accounts ⇒ Hash
Retrieves all bank accounts for the current character.
-
.clear_balance(town) ⇒ void
Clears the balance for a specific town by setting it to zero.
-
.current_bank_town ⇒ String?
Determines the current bank town based on the room title.
-
.display_banks ⇒ void
Displays the bank balances for the current character.
-
.display_banks_all ⇒ void
Displays the bank balances for all characters.
-
.format_currency(copper) ⇒ String
Formats a copper amount into a human-readable currency string.
-
.my_accounts ⇒ Hash
Retrieves the bank accounts for the current character.
-
.parse(line) ⇒ void
Parses a line of game output to handle banking actions.
-
.parse_balance_string(balance_string) ⇒ Integer
Parses a balance string and converts it to copper.
-
.reload! ⇒ void
Reloads the bank accounts from storage.
-
.reset_all! ⇒ void
Resets the bank data for all characters.
-
.reset_character! ⇒ void
Resets the bank data for the current character.
-
.to_copper(amount, denomination) ⇒ Integer
Converts an amount of currency to copper based on its denomination.
-
.total_wealth ⇒ Integer
Calculates the total wealth across all accounts for the current character.
-
.total_wealth_all ⇒ Integer
Calculates the total wealth across all characters' accounts.
-
.update_balance(town, copper) ⇒ void
Updates the balance for a specific town.
Class Method Details
.all_accounts ⇒ Hash
Retrieves all bank accounts for the current character.
If the accounts are not already loaded, they will be loaded from storage.
142 143 144 145 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 142 def all_accounts load_accounts unless @@accounts_cache @@accounts_cache end |
.clear_balance(town) ⇒ void
This method returns an undefined value.
Clears the balance for a specific town by setting it to zero.
171 172 173 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 171 def clear_balance(town) update_balance(town, 0) end |
.current_bank_town ⇒ String?
Determines the current bank town based on the room title.
235 236 237 238 239 240 241 242 243 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 235 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 ⇒ void
This method returns an undefined value.
Displays the bank balances for the current character.
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 274 def display_banks accounts = my_accounts 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(total_wealth)}") end |
.display_banks_all ⇒ void
This method returns an undefined value.
Displays the bank balances for all characters.
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 304 def display_banks_all accounts = all_accounts 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 a copper amount into a human-readable currency string.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 217 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
Retrieves the bank accounts for the current character.
If no accounts exist, an empty hash is returned.
151 152 153 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 151 def my_accounts all_accounts[character_name] ||= {} end |
.parse(line) ⇒ void
This method returns an undefined value.
Parses a line of game output to handle banking actions.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 249 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) handle_no_account(town) end end |
.parse_balance_string(balance_string) ⇒ Integer
Parses a balance string and converts it to copper.
203 204 205 206 207 208 209 210 211 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 203 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! ⇒ void
This method returns an undefined value.
Reloads the bank accounts from storage.
336 337 338 339 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 336 def reload! @@accounts_cache = nil load_accounts end |
.reset_all! ⇒ void
This method returns an undefined value.
Resets the bank data for all characters.
353 354 355 356 357 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 353 def reset_all! @@accounts_cache = {} save_accounts Lich::Messaging.msg('info', 'DRBanking: Cleared all bank data for all characters.') end |
.reset_character! ⇒ void
This method returns an undefined value.
Resets the bank data for the current character.
344 345 346 347 348 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 344 def reset_character! all_accounts.delete(character_name) save_accounts Lich::Messaging.msg('info', "DRBanking: Cleared bank data for #{character_name}.") end |
.to_copper(amount, denomination) ⇒ Integer
Converts an amount of currency to copper based on its denomination.
194 195 196 197 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 194 def to_copper(amount, denomination) multiplier = DENOMINATION_VALUES[denomination.downcase] || 1 amount.to_i * multiplier end |
.total_wealth ⇒ Integer
Calculates the total wealth across all accounts for the current character.
178 179 180 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 178 def total_wealth my_accounts.values.sum end |
.total_wealth_all ⇒ Integer
Calculates the total wealth across all characters' accounts.
185 186 187 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 185 def total_wealth_all all_accounts.values.map { |banks| banks.values.sum }.sum end |
.update_balance(town, copper) ⇒ void
This method returns an undefined value.
Updates the balance for a specific town.
160 161 162 163 164 165 |
# File 'documented/dragonrealms/drinfomon/drbanking.rb', line 160 def update_balance(town, copper) all_accounts[character_name] ||= {} all_accounts[character_name][town] = copper.to_i save_accounts Lich::Messaging.msg('info', "DRBanking: Updated #{town} balance to #{format_currency(copper)}") end |