Module: Lich::Gemstone::Bank

Defined in:
documented/gemstone/bank.rb

Overview

Bank account management: deposit and withdraw silver, and query account balances.

Handles standard GemStone bank verbs (DEPOSIT, WITHDRAW, BANK ACCOUNT), Pinefar's distinct syntax, and free-to-play account caps with note conversion. Assumes the character is already standing at a bank.

Examples:

Deposit everything

Bank.deposit #=> 5000

Deposit a note

Bank.deposit_note #=> 50000

Withdraw with coins

Bank.withdraw(8000) #=> 8000

Withdraw as a note (f2p)

Bank.withdraw(25000, note: true) #=> 25000

Defined Under Namespace

Modules: Pattern

Constant Summary collapse

PINEFAR_TITLE =
'[Pinefar, Depository]'
NOTE_NOUN =

Matches the noun of a bank note item (note, scrip, or chit).

Used to identify notes in inventory and containers during deposit/withdrawal loops.

Examples:

"scrip"

"chit"

See Also:

/^(?:note|scrip|chit)$/.freeze
F2P_NOTE_BUFFER =

Silver kept on hand when the f2p deposit loop sizes a note, so the next pass can still deposit something. eloot's number.

10_000

Class Method Summary collapse

Class Method Details

.account ⇒ Hash?

BANK ACCOUNT, parsed. The listing covers every town's account, so balance is the one for the town the character is standing in, picked by matching the bank's name against the room's location.

Returns:

  • (Hash, nil) —

    { balance:, max:, banks:, total: } where banks maps town name to silver, max is the f2p cap (nil without one), or nil when this town's bank refuses access



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'documented/gemstone/bank.rb', line 211

def self.
  lines = Lich::Util.issue_command('bank account', Pattern::ACCOUNT_START, Pattern::ACCOUNT_END, silent: true, quiet: true)
  return nil if lines.any? { |l| l =~ Pattern::NO_ACCESS }
  banks = {}
  total = nil
  max = nil
  balance = nil
  lines.each do |line|
    if line =~ Pattern::ACCOUNT_LINE
      banks[Regexp.last_match[:bank]] = Regexp.last_match[:silver].delete(',').to_i
    elsif line =~ Pattern::ACCOUNT_TOTAL
      total = Regexp.last_match[:silver].delete(',').to_i
    elsif line =~ Pattern::ACCOUNT_BALANCE
      balance = Regexp.last_match[:silver].delete(',').to_i
    elsif line =~ Pattern::ACCOUNT_MAX
      max = Regexp.last_match[:silver].delete(',').to_i
    end
  end
  balance ||= banks[local_bank(banks.keys)] || 0
  { balance: balance, max: max, banks: banks, total: total || banks.values.sum }
end

.balance ⇒ Integer?

Returns balance at this town's bank, nil without access.

Returns:

  • (Integer, nil) —

    balance at this town's bank, nil without access



234
235
236
# File 'documented/gemstone/bank.rb', line 234

def self.balance
  &.fetch(:balance)
end

.deposit(amount = :all) ⇒ Integer?

Deposit silver.

Parameters:

  • amount (Integer, :all) (defaults to: :all)

Returns:

  • (Integer, nil) —

    silver deposited as the bank reported it, 0 when there was nothing to deposit, nil when the bank refused. The Pinefar banker confirms without echoing a figure, so a successful deposit there reports 0; do not sum these to track silver banked.



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'documented/gemstone/bank.rb', line 279

def self.deposit(amount = :all)
  return deposit_f2p(amount) if f2p? && !pinefar?

  waitrt?
  result = if pinefar?
             silver = amount == :all ? Currency.silver(refresh: true) : amount
             return 0 unless silver.to_i.positive?
             wait_for_banker
             dothistimeout("give banker #{silver} silver", 3, Pattern::DEPOSIT)
           else
             dothistimeout("deposit #{amount == :all ? 'all' : amount}", 3, Regexp.union(Pattern::DEPOSIT, Pattern::NO_ACCESS))
           end
  Currency.refresh
  deposited(result)
end

.deposit_f2p(amount = :all, stow: StowList.default) ⇒ Integer?

Free-to-play deposit: fill the account to its cap, convert the overflow to a note and stow it, repeat until everything carried is put away.

Parameters:

  • amount (Integer, :all) (defaults to: :all)
  • stow (GameObj, nil) (defaults to: StowList.default) —

    where notes go; the STOW default container

Returns:

  • (Integer, nil) —

    silver put away in total, nil when the bank refused before anything was deposited



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'documented/gemstone/bank.rb', line 345

def self.deposit_f2p(amount = :all, stow: StowList.default)
  total = 0
  refused = false
  remaining = amount == :all ? nil : amount
  loop do
    info = 
    if info.nil?
      refused = true
      break
    end
    carried = Currency.silver(refresh: true).to_i
    carried = [carried, remaining].min if remaining
    break unless carried.positive?

    max = info[:max]
    if max.nil? || info[:balance] + carried < max
      got = deposited(dothistimeout("deposit #{carried}", 3, Pattern::DEPOSIT))
      refused = got.nil?
      total += got.to_i
      break
    end

    room = max - info[:balance]
    if room.positive?
      got = deposited(dothistimeout("deposit #{room}", 3, Pattern::DEPOSIT))
      if got.nil?
        refused = true
        break
      end
      total += got
      remaining -= got if remaining
    end
    carried = Currency.silver(refresh: true).to_i
    break if (remaining && remaining <= 0) || !carried.positive?
    note_size = carried >= F2P_NOTE_BUFFER ? max : max - (F2P_NOTE_BUFFER - carried)
    break if withdraw(note_size, note: true).nil?
    note = note_in_hand
    break if note.nil? || stow.nil?
    Lich::Stash.add_to_bag(stow, note)
  end
  Currency.refresh
  refused && total.zero? ? nil : total
end

.deposit_note(note = note_in_hand) ⇒ Integer?

Deposit the note in hand. Pinefar cannot take notes.

Parameters:

  • note (GameObj, nil) (defaults to: note_in_hand)

Returns:

  • (Integer, nil) —

    the note's value as the bank reported it, nil when there is no note or the bank cannot take it



300
301
302
303
304
305
306
# File 'documented/gemstone/bank.rb', line 300

def self.deposit_note(note = note_in_hand)
  return nil if note.nil? || pinefar?
  waitrt?
  result = dothistimeout("deposit ##{note.id}", 3, Pattern::DEPOSIT)
  20.times { break if note_in_hand.nil?; sleep 0.1 }
  deposited(result)
end

.f2p? ⇒ Boolean

Returns free-to-play account: capped balance, notes for the overflow.

Returns:

  • (Boolean) —

    free-to-play account: capped balance, notes for the overflow



200
201
202
# File 'documented/gemstone/bank.rb', line 200

def self.f2p?
  Lich::Common::Account.type.to_s == 'FREE'
end

.here? ⇒ Boolean

Returns standing at a bank (a tagged room or the Pinefar depository).

Returns:

  • (Boolean) —

    standing at a bank (a tagged room or the Pinefar depository)



190
191
192
# File 'documented/gemstone/bank.rb', line 190

def self.here?
  pinefar? || Room.current&.tags.to_a.include?('bank') ? true : false
end

.local_bank(names) ⇒ String?

The bank named for the town the character is in ("Four Winds" for a room located on Four Winds Isle).

Parameters:

  • names (Array<String>) —

    bank names from BANK ACCOUNT

Returns:



243
244
245
246
247
248
249
250
# File 'documented/gemstone/bank.rb', line 243

def self.local_bank(names)
  # Map#location can be false for a room that has not been surveyed, and
  # false.to_s is "false" -- a non-empty string that matches no bank.
  location = Room.current&.location
  return nil unless location.is_a?(String)
  return nil if location.empty?
  names.find { |n| location.start_with?(n) } || names.find { |n| location.include?(n) || n.include?(location) }
end

.note_in_hand ⇒ GameObj?

Returns a bank note in either hand.

Returns:

  • (GameObj, nil) —

    a bank note in either hand



253
254
255
# File 'documented/gemstone/bank.rb', line 253

def self.note_in_hand
  [GameObj.right_hand, GameObj.left_hand].compact.find { |i| i.noun =~ NOTE_NOUN }
end

.note_value(note = note_in_hand) ⇒ Integer

Returns its value, 0 when unreadable.

Parameters:

  • note (GameObj) (defaults to: note_in_hand) —

    a note in hand

Returns:

  • (Integer) —

    its value, 0 when unreadable



265
266
267
268
269
270
# File 'documented/gemstone/bank.rb', line 265

def self.note_value(note = note_in_hand)
  return 0 if note.nil?
  waitrt?
  line = dothistimeout("read ##{note.id}", 3, Pattern::NOTE_READ)
  line.to_s =~ Pattern::NOTE_VALUE ? Regexp.last_match[:silver].delete(',').to_i : 0
end

.notes_in(container = StowList.default) ⇒ Array<GameObj>

Returns bank notes Lich knows to be in that container.

Parameters:

  • container (GameObj, nil) (defaults to: StowList.default) —

    defaults to the STOW default container

Returns:

  • (Array<GameObj>) —

    bank notes Lich knows to be in that container



259
260
261
# File 'documented/gemstone/bank.rb', line 259

def self.notes_in(container = StowList.default)
  container&.contents.to_a.select { |obj| obj.noun =~ NOTE_NOUN }
end

.pinefar? ⇒ Boolean

Returns the Pinefar depository, which has a banker NPC and its own verbs.

Returns:

  • (Boolean) —

    the Pinefar depository, which has a banker NPC and its own verbs



195
196
197
# File 'documented/gemstone/bank.rb', line 195

def self.pinefar?
  XMLData.room_title == PINEFAR_TITLE
end

.wait_for_banker(timeout: 30) ⇒ Boolean

The Pinefar banker wanders; give him a moment to be at the counter.

Parameters:

  • timeout (Numeric) (defaults to: 30)

Returns:

  • (Boolean)


450
451
452
453
454
455
456
457
# File 'documented/gemstone/bank.rb', line 450

def self.wait_for_banker(timeout: 30)
  deadline = Time.now + timeout
  until GameObj.npcs.to_a.any? { |npc| npc.noun == 'banker' }
    return false if Time.now > deadline
    sleep 1
  end
  true
end

.withdraw(amount, note: false) ⇒ Integer?

Withdraw silver, or a note for that amount.

Parameters:

  • amount (Integer)
  • note (Boolean) (defaults to: false) —

    take a note rather than coins

Returns:

  • (Integer, nil) —

    silver withdrawn (the note's face value for a note), nil when the bank refused



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'documented/gemstone/bank.rb', line 314

def self.withdraw(amount, note: false)
  return withdraw_f2p(amount) if f2p? && !note && !pinefar?

  waitrt?
  if note
    result = withdraw_reply("withdraw #{amount} note", 5)
    return nil if result.nil? || result =~ Pattern::WITHDRAW_REFUSED
    return result =~ Pattern::NOTE_HANDED ? amount : nil
  end

  if pinefar?
    wait_for_banker
    result = withdraw_reply("ask banker for #{amount} silvers", 3)
    Currency.refresh
    return nil if result.nil? || result =~ Pattern::WITHDRAW_REFUSED
    # The banker confirms without echoing a figure.
    return result =~ Pattern::PINEFAR_HANDED ? amount : nil
  end

  got = withdraw_silver(amount)
  Currency.refresh
  got
end

.withdraw_f2p(amount, stow: StowList.default) ⇒ Integer?

Free-to-play withdrawal: the balance may be short of amount while notes in the stow container hold the rest, so deposit notes until it covers.

Parameters:

  • amount (Integer)
  • stow (GameObj, nil) (defaults to: StowList.default)

Returns:

  • (Integer, nil) —

    silver withdrawn, nil when the bank refused



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'documented/gemstone/bank.rb', line 395

def self.withdraw_f2p(amount, stow: StowList.default)
  info = 
  return nil if info.nil?
  bal = info[:balance]
  if bal >= amount
    got = withdraw_silver(amount)
    Currency.refresh
    return got
  end

  taken = 0
  want = amount
  refused = false

  # Drain what the account already holds before touching any notes: that
  # has to happen even when the stow container is empty.
  if bal.positive?
    got = withdraw_silver(bal)
    if got.nil?
      refused = true
    else
      taken += got
      want -= got
    end
  end

  until refused || want <= 0
    note = notes_in(stow).first
    if note.nil?
      refused = true
      break
    end
    fput "get ##{note.id}"
    value = deposit_note(note)
    if value.nil?
      refused = true
      break
    end
    got = withdraw_silver([value, want].min)
    if got.nil?
      refused = true
      break
    end
    taken += got
    want -= got
  end

  Currency.refresh
  refused && taken.zero? ? nil : taken
end