Module: Lich::DragonRealms::DRCM
- Defined in:
- documented/dragonrealms/commons/common-money.rb,
documented/dragonrealms/commons/common-money-data.rb
Constant Summary collapse
- DENOMINATIONS =
Coin denominations ordered from highest to lowest value. Each entry is [copper_multiplier, name]. Coin denominations ordered from highest to lowest value. Each entry is [copper_multiplier, name].
[ [10_000, 'platinum'], [1000, 'gold'], [100, 'silver'], [10, 'bronze'], [1, 'copper'] ].freeze
- DENOMINATION_VALUES =
Maps denomination names to their copper multiplier. Maps denomination names to their copper multiplier.
{ 'platinum' => 10_000, 'gold' => 1000, 'silver' => 100, 'bronze' => 10, 'copper' => 1 }.freeze
- DENOMINATION_REGEX_MAP =
Regex abbreviations for coin denominations. Supports in-game abbreviations (e.g., "p" for platinum, "g" for gold). Regex abbreviations for coin denominations. Supports in-game abbreviations (e.g., "p" for platinum, "g" for gold).
{ 'platinum' => /\bp(l|la|lat|lati|latin|latinu|latinum)?\b/i, 'gold' => /\bg(o|ol|old)?\b/i, 'silver' => /\bs(i|il|ilv|ilve|ilver)?\b/i, 'bronze' => /\bb(r|ro|ron|ronz|ronze)?\b/i, 'copper' => /\bc(o|op|opp|oppe|opper)?\b/i }.freeze
- CURRENCY_REGEX_MAP =
Regex abbreviations for currency types. Supports in-game abbreviations (e.g., "k" for kronars, "l" for lirums). Regex abbreviations for currency types. Supports in-game abbreviations (e.g., "k" for kronars, "l" for lirums).
{ 'kronars' => /\bk(r|ro|ron|rona|ronar|ronars)?\b/i, 'lirums' => /\bl(i|ir|iru|irum|irums)?\b/i, 'dokoras' => /\bd(o|ok|oko|okor|okora|okoras)?\b/i }.freeze
- CURRENCIES =
Canonical currency names for abbreviation resolution. Canonical currency names for abbreviation resolution.
%w[kronars lirums dokoras].freeze
- EXCHANGE_RATES =
{ 'dokoras' => { 'dokoras' => 1, 'kronars' => 1.385808991, 'lirums' => 1.108646953 }.freeze, 'kronars' => { 'dokoras' => 0.7216, 'kronars' => 1, 'lirums' => 0.8 }.freeze, 'lirums' => { 'dokoras' => 0.902, 'kronars' => 1.25, 'lirums' => 1 }.freeze }.freeze
- WEALTH_COPPER_REGEX =
Regex for parsing copper values from wealth output. Named captures: :coppers, :currency Regex for parsing copper values from wealth output. Named captures: :coppers, :currency
/\((?<coppers>\d+) copper (?<currency>kronars|lirums|dokoras)\)/i.freeze
Class Method Summary collapse
-
.check_wealth(currency) ⇒ Integer
Queries the game and returns on-hand copper wealth for a specific currency.
-
.convert_currency(amount, from, to, fee) ⇒ Object
Converts an amount between DR currencies, accounting for exchange fees.
-
.convert_to_copper(amount, denomination) ⇒ Object
Convert an amount to copper given a denomination name (abbreviation permitted).
-
.debt(hometown) ⇒ Integer
Returns the copper value of bank debt for a given hometown.
-
.deposit_coins(keep_copper, settings, hometown = nil) ⇒ Array(Integer, String)
Deposits all coins at the bank, then re-withdraws a target amount to keep on hand.
-
.ensure_copper_on_hand(copper, settings, hometown = nil) ⇒ Boolean
Ensures the character has a target copper amount on hand, withdrawing from the bank if needed.
-
.get_canonical_currency(currency) ⇒ Object
Returns full canonical currency name from an abbreviation.
-
.get_money_from_bank(amount_as_string, settings, hometown = nil) ⇒ Boolean
Withdraws a coin amount from the bank via the teller, looping on retriable errors.
-
.get_total_wealth ⇒ Object
Captures total on-hand wealth across all three currencies.
-
.hometown_currency(hometown_name) ⇒ String
Returns the currency name for a given hometown.
-
.minimize_coins(copper) ⇒ Array<String>
Converts a copper value into an array of human-readable coin denominations.
-
.strip_xml(lines) ⇒ Array<String>
Strips XML tags and decodes common HTML entities from game output lines.
-
.town_currency(town) ⇒ Object
Alias for backward compatibility - common-crafting.rb uses this name.
-
.wealth(hometown) ⇒ Integer
Returns on-hand copper wealth for a given hometown.
-
.withdraw_exact_amount?(amount_as_string, settings, hometown = nil) ⇒ Boolean
Withdraws a coin amount from the bank, using a bankbot if available.
Class Method Details
.check_wealth(currency) ⇒ Integer
Queries the game and returns on-hand copper wealth for a specific currency.
Sends the "wealth" command filtered by currency and parses the response.
97 98 99 |
# File 'documented/dragonrealms/commons/common-money.rb', line 97 def check_wealth(currency) DRC.bput("wealth #{currency}", /\(\d+ copper #{currency}\)/i, /No #{currency}/i).scan(/\d+/).first.to_i end |
.convert_currency(amount, from, to, fee) ⇒ Object
Converts an amount between DR currencies, accounting for exchange fees. Use a negative fee to calculate how much is needed to receive a target amount. Use a positive fee to calculate how much will be received after the exchange.
60 61 62 63 64 65 66 |
# File 'documented/dragonrealms/commons/common-money.rb', line 60 def convert_currency(amount, from, to, fee) if fee < 0 ((amount / EXCHANGE_RATES[from][to]).ceil / (1 + fee)).ceil else ((amount * EXCHANGE_RATES[from][to]).ceil * (1 - fee)).floor end end |
.convert_to_copper(amount, denomination) ⇒ Object
Convert an amount to copper given a denomination name (abbreviation permitted). If denomination is nil/empty, assumes coppers and logs a warning. Supports fractional amounts (e.g., '1.5 plat' = 15000 copper).
40 41 42 43 44 45 46 47 48 49 |
# File 'documented/dragonrealms/commons/common-money.rb', line 40 def convert_to_copper(amount, denomination) denomination = denomination.to_s.strip unless denomination.empty? DENOMINATION_VALUES.each do |name, multiplier| return (amount.to_f * multiplier).to_i if name.start_with?(denomination.downcase) end end Lich::Messaging.msg('bold', "DRCM: Unknown denomination, assuming coppers: #{denomination}") amount.to_i end |
.debt(hometown) ⇒ Integer
Returns the copper value of bank debt for a given hometown.
Sends the "wealth" command and parses the debt amount from the response.
262 263 264 265 |
# File 'documented/dragonrealms/commons/common-money.rb', line 262 def debt(hometown) currency = hometown_currency(hometown) DRC.bput('wealth', /\(\d+ copper #{currency}\)/i, /Wealth:/i).scan(/\d+/).first.to_i end |
.deposit_coins(keep_copper, settings, hometown = nil) ⇒ Array(Integer, String)
Handles multiple "check balance" response formats, including pre-formatted coin breakdowns.
Prints a bold message if no teller is found at the deposit location.
Deposits all coins at the bank, then re-withdraws a target amount to keep on hand.
Walks to the deposit location, sends "deposit all", then if keeping coins for the current hometown, withdraws denominations to reach the keep_copper target. Finally, queries "check balance" to parse and return the updated bank balance and currency. Returns early if settings.skip_bank is true. Uses the hometown from settings if not explicitly provided.
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'documented/dragonrealms/commons/common-money.rb', line 285 def deposit_coins(keep_copper, settings, hometown = nil) return if settings.skip_bank hometown = settings.hometown if hometown.nil? DRCT.walk_to(get_data('town')[hometown]['deposit']['id']) DRC.release_invisibility DRC.bput('wealth', 'Wealth:') case DRC.bput('deposit all', 'you drop all your', 'You hand the clerk some coins', "You don't have any", 'There is no teller here', 'reached the maximum balance I can permit', 'You find your jar with little effort', 'Searching methodically through the shelves') when 'There is no teller here' Lich::Messaging.msg('bold', "DRCM: No teller found at this location. Cannot deposit coins.") return end minimize_coins(keep_copper).each { |amount| withdraw_exact_amount?(amount, settings) } if settings.hometown == hometown balance_result = DRC.bput('check balance', /current balance is .*? (?:Kronars?|Dokoras?|Lirums?)\."$/, /If you would like to open one, you need only deposit a few (?:Kronars?|Dokoras?|Lirums?)\."$/, /As expected, there are .*? (?:Kronars?|Dokoras?|Lirums?)\.$/, 'Perhaps you should find a new deposit jar for your financial needs. Be sure to mark it with your name') case balance_result when /current balance is (?<bal>.*?) (?<cur>Kronars?|Dokoras?|Lirums?)\."$/, /As expected, there are (?<bal>.*?) (?<cur>Kronars?|Dokoras?|Lirums?)\.$/ match = balance_result.match(/(?:current balance is|As expected, there are) (?<bal>.*?) (?<cur>Kronars?|Dokoras?|Lirums?)/) currency = match[:cur] balance = 0 match[:bal].gsub(/and /, '').split(', ').each do |amount_as_string| amount, denomination = amount_as_string.split balance += convert_to_copper(amount, denomination) end when /If you would like to open one, you need only deposit a few (?<cur>Kronars?|Dokoras?|Lirums?)\."$/ match = balance_result.match(/deposit a few (?<cur>Kronars?|Dokoras?|Lirums?)/) balance = 0 currency = match[:cur] when /Perhaps you should find a new deposit jar/ balance = 0 currency = 'Dokoras' end [balance, currency] end |
.ensure_copper_on_hand(copper, settings, hometown = nil) ⇒ Boolean
Ensures the character has a target copper amount on hand, withdrawing from the bank if needed.
Checks current wealth; if insufficient, breaks down the deficit into coin denominations and withdraws each amount from the bank using #withdraw_exact_amount?. Uses the hometown from settings if not explicitly provided.
162 163 164 165 166 167 168 169 170 171 |
# File 'documented/dragonrealms/commons/common-money.rb', line 162 def ensure_copper_on_hand(copper, settings, hometown = nil) hometown = settings.hometown if hometown.nil? on_hand = wealth(hometown) return true if on_hand >= copper withdrawals = minimize_coins(copper - on_hand) withdrawals.all? { |amount| withdraw_exact_amount?(amount, settings, hometown) } end |
.get_canonical_currency(currency) ⇒ Object
Returns full canonical currency name from an abbreviation. e.g., "k" -> "kronars", "li" -> "lirums"
53 54 55 |
# File 'documented/dragonrealms/commons/common-money.rb', line 53 def get_canonical_currency(currency) CURRENCIES.find { |c| c.start_with?(currency) } end |
.get_money_from_bank(amount_as_string, settings, hometown = nil) ⇒ Boolean
Pauses for 15 seconds on rate-limit responses before retrying.
Withdraws a coin amount from the bank via the teller, looping on retriable errors.
Walks to the deposit location for the hometown, releases invisibility, and sends "withdraw" commands. Handles various clerk responses: immediate success on "The clerk counts" or "You count out", temporary failures (glare, rate-limiting) trigger a 15-second pause and retry, permanent failures (no account, insufficient funds, maximum withdrawal) return false.
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'documented/dragonrealms/commons/common-money.rb', line 226 def get_money_from_bank(amount_as_string, settings, hometown = nil) hometown = settings.hometown if hometown.nil? DRCT.walk_to(get_data('town')[hometown]['deposit']['id']) DRC.release_invisibility loop do case DRC.bput("withdraw #{amount_as_string}", 'The clerk counts', 'The clerk tells', 'The clerk glares at you.', 'You count out', 'find a new deposit jar', 'If you value your hands', 'Hey! Slow down!', "You must be at a bank teller's window to withdraw money", "You don't have that much money", 'have an account', /The clerk says, "I'm afraid you can't withdraw that much at once/, /^How much do you wish to withdraw/i) when 'The clerk counts', 'You count out' break true when 'The clerk glares at you.', 'Hey! Slow down!', "I don't know what you think you're doing" pause 15 when 'The clerk tells', 'If you value your hands', 'find a new deposit jar', "You must be at a bank teller's window to withdraw money", "You don't have that much money", 'have an account', /The clerk says, "I'm afraid you can't withdraw that much at once/, /^How much do you wish to withdraw/i break false else break false end end end |
.get_total_wealth ⇒ Object
Captures total on-hand wealth across all three currencies. Returns a hash of currency name => copper value. Uses DRC.capture_to_prompt for reliable bounded capture.
When the reply does not arrive in time, a bold message is printed and all zeros are returned. The WEALTH reply ends on the prompt, which is how a complete reply is told from one cut off by the timeout. A cut-off reply prints a bold message and returns what was parsed, with 0 for any currency whose line never arrived.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'documented/dragonrealms/commons/common-money.rb', line 124 def get_total_wealth wealth_lines, complete = DRC.capture_to_prompt('wealth', /^Wealth:/) result = { 'kronars' => 0, 'lirums' => 0, 'dokoras' => 0 } if wealth_lines.empty? Lich::Messaging.msg("bold", "DRCM: Failed to capture WEALTH output (timeout).") return result end unless complete Lich::Messaging.msg("bold", "DRCM: WEALTH output was cut off (timeout); the result is incomplete.") end strip_xml(wealth_lines).each do |line| match = line.match(WEALTH_COPPER_REGEX) next unless match result[match[:currency].downcase] = match[:coppers].to_i end result end |
.hometown_currency(hometown_name) ⇒ String
Returns the currency name for a given hometown.
Looks up the currency from game data (e.g., "kronars" for Wehnimer's Landing).
78 79 80 |
# File 'documented/dragonrealms/commons/common-money.rb', line 78 def hometown_currency(hometown_name) get_data('town')[hometown_name]['currency'] end |
.minimize_coins(copper) ⇒ Array<String>
Converts a copper value into an array of human-readable coin denominations.
Breaks down the copper amount into plats, crowns, sovereigns, and remaining coppers, returning only non-zero denominations.
26 27 28 29 30 31 32 33 34 35 |
# File 'documented/dragonrealms/commons/common-money.rb', line 26 def minimize_coins(copper) DENOMINATIONS.inject([copper, []]) do |result, denomination| remaining = result.first display = result.last if remaining / denomination.first > 0 display << "#{remaining / denomination.first} #{denomination.last}" end [remaining % denomination.first, display] end.last end |
.strip_xml(lines) ⇒ Array<String>
Strips XML tags and decodes common HTML entities from game output lines.
12 13 14 |
# File 'documented/dragonrealms/commons/common-money.rb', line 12 def strip_xml(lines) DRC.strip_xml(lines) end |
.town_currency(town) ⇒ Object
Alias for backward compatibility - common-crafting.rb uses this name.
83 84 85 |
# File 'documented/dragonrealms/commons/common-money.rb', line 83 def town_currency(town) hometown_currency(town) end |
.wealth(hometown) ⇒ Integer
Returns on-hand copper wealth for a given hometown.
Derives the currency from the hometown, then checks wealth in that currency.
111 112 113 |
# File 'documented/dragonrealms/commons/common-money.rb', line 111 def wealth(hometown) check_wealth(hometown_currency(hometown)) end |
.withdraw_exact_amount?(amount_as_string, settings, hometown = nil) ⇒ Boolean
Withdraws a coin amount from the bank, using a bankbot if available.
Parses the amount string (e.g., "5 crowns"), converts to copper, and either whispers to a bankbot or calls #get_money_from_bank. Uses the hometown from settings if not explicitly provided.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'documented/dragonrealms/commons/common-money.rb', line 188 def withdraw_exact_amount?(amount_as_string, settings, hometown = nil) hometown = settings.hometown if hometown.nil? if settings.bankbot_enabled DRCT.walk_to(settings.bankbot_room_id) DRC.release_invisibility if DRRoom.pcs.include?(settings.bankbot_name) amount_convert, type = amount_as_string.split amount = convert_to_copper(amount_convert, type) currency = hometown_currency(settings.hometown) case DRC.bput("whisper #{settings.bankbot_name} withdraw #{amount} #{currency}", 'offers you', 'Whisper what to who?') when 'offers you' DRC.bput('accept tip', 'Your current balance is') end else get_money_from_bank(amount_as_string, settings, hometown) end else get_money_from_bank(amount_as_string, settings, hometown) end end |