-
Notifications
You must be signed in to change notification settings - Fork 177
/
bankbot.lic
476 lines (397 loc) · 16 KB
/
bankbot.lic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
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
388
389
390
391
392
393
394
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# want_script_output
=begin
Documentation: https://elanthipedia.play.net/Lich_script_repository#bankbot
=end
custom_require.call(%w[common common-validation events])
class Bankbot
def initialize
Flags.add('tip-accepted', '.* accepts your tip and slips it away with a smile')
Flags.add('tip-declined', '.* declines your tip offer')
Flags.add('tip-expired', 'Your tip offer to .* has expired', 'Your tip offer has expired')
# This provides a default balance of 0 for all currencies
@ledger = Hash.new { |hash, key| hash[key] = Hash.new { |h, k| h[k] = 0 } }
# Convert symbols to strings
load_bankbot_ledger.each { |k, v| @ledger[k.to_s] = v }
echo("Initialized ledger: #{@ledger}")
fput('avoid !all')
fput('avoid whispering')
end
def help(character)
help_content.each { |line| fput "whisper #{character} #{line}" }
end
# TODO: Do not prefix reader method names with get_.
def help_content
[
'I accept coins via TIPs to hold for you.',
'Supported commands are:',
' (help) - this list',
' (bal|balance) - view your current balance',
' (with|withdraw) [amount] [currency] - withdraw [amount] [currency] from your bankbot balance.',
' (with|withdraw) all [currency] - withdraw all [currency] from your bankbot balance.',
' (transfer) [amount] [currency] [destination] - transfer [amount] [currency] from your bankbot balance to the [destination] account (case-sensitive). Destination character must already have a bankbot account.',
' (transfer) all [currency] [destination] - transfer all [currency] from your bankbot balance to the [destination] account.',
# " (proxy) set [character name] - all deposits you make will now be directed to the account of [character name]. This overwrites any previous proxy. [Character name] must already have an account with bankbot.",
# " (proxy) (list|clear) - list or clear your deposit proxy.",
' (where|room|location) - view the current location of bankbot',
"Help, balance, transfer, and location can be done via LNet private message: ';chat to #{checkname} [command]'."
]
end
def report_balance(character)
pretty_print = get_balance(character)
fput "whisper #{character} Your current balance is: #{pretty_print}."
end
def get_balance(character)
$CURRENCIES.map { |currency| "#{@ledger[character][currency]} #{currency}" }.join(', ')
end
def deposit(character, amount, currency)
unless currency
log(character, 'deposit', amount, currency, 'currency not recognized')
fput "whisper #{character} I'm sorry, I don't recognize the currency '#{currency}'."
return
end
case DRC.bput('accept tip', 'But you have no tip offers outstanding', "You accept #{character}'s tip and slip it away", "But #{character} isn't here")
when 'But you have no tip offers outstanding', /But .* isn't here/i
log(character, 'deposit', amount, currency, 'character not found')
return
when /You accept .*'s tip and slip it away/i
@ledger[character][currency] += amount
log(character, 'deposit', amount, currency, 'succeeded')
end
end
def withdraw_all(character, currency)
amount = @ledger[character][currency]
if amount < 1
log(character, 'withdrawal all', amount, currency, 'no balance to withdrawal all')
fput "whisper #{character} You do not have any #{currency} to withdraw."
return
end
log(character, 'withdrawal all', 'all', currency, 'withdrawal all attempted')
withdraw(character, amount, currency)
end
def withdraw(character, amount, currency)
unless currency
log(character, 'withdrawal', amount, currency, 'currency not recognized')
fput "whisper #{character} I'm sorry, I don't recognize the currency '#{currency}'."
return
end
if amount < 1
log(character, 'withdrawal', amount, currency, 'invalid withdrawal amount')
fput "whisper #{character} That is an invalid amount to withdraw."
return
end
if @ledger[character][currency] < amount
log(character, 'withdrawal', amount, currency, 'insufficient funds')
fput "whisper #{character} You don't have that much money."
return
end
Flags.reset('tip-accepted')
Flags.reset('tip-expired')
Flags.reset('tip-declined')
case DRC.bput("tip #{character} #{amount} #{currency}", 'You offer', "I don't know who", 'you really should keep every bronze you can get your hands on', 'You already have a tip offer outstanding')
when "I don't know who"
log(character, 'withdrawal', amount, currency, 'character not found')
return
when 'You already have a tip offer outstanding'
fput "whisper #{character} Sorry, someone else is in the middle of a withdrawal. Please try again in a moment."
log(character, 'withdrawal', amount, currency, 'withdrawal already pending')
return
when 'you really should keep every bronze you can get your hands on'
echo '***ERROR*** UNABLE TO TIP DUE TO LOW CIRCLE, EXITING'
# TODO: Report error to person attempting a withdrawal
exit
end
pause 0.5 until Flags['tip-accepted'] || Flags['tip-expired'] || Flags['tip-declined']
if Flags['tip-expired']
log(character, 'withdrawal', amount, currency, 'expired')
end
if Flags['tip-declined']
log(character, 'withdrawal', amount, currency, 'declined')
end
return unless Flags['tip-accepted']
@ledger[character][currency] -= amount
log(character, 'withdrawal', amount, currency, 'succeeded')
end
def transfer(character, amount, currency, destination)
# TODO: Send success/failure message over LNet if necessary
unless currency
log(character, 'transfer', amount, currency, 'currency not recognized')
fput "whisper #{character} I'm sorry, I don't recognize the currency '#{currency}'."
return
end
if amount < 1
log(character, 'transfer', amount, currency, 'invalid transfer amount')
fput "whisper #{character} That is an invalid amount to transfer."
return
end
if @ledger[character][currency] < amount
log(character, 'transfer', amount, currency, 'insufficient funds')
fput "whisper #{character} You don't have that much money."
return
end
# TODO: Make case-insensitive
# TODO: Validate against lnet and retry
unless @ledger.key?(destination)
log(character, 'transfer', amount, currency, "destination does not exist: #{destination}")
fput "whisper #{character} The destination character does not have a bankbot account."
return
end
@ledger[character][currency] -= amount
@ledger[destination][currency] += amount
log(character, 'transfer', amount, currency, "succeeded to: #{destination}")
fput "whisper #{character} Transfer succeeded!"
# TODO: Send balance to recipient
end
def transfer_all(character, currency, destination)
amount = @ledger[character][currency]
if amount < 1
log(character, 'transfer all', amount, currency, 'no balance to transfer all')
fput "whisper #{character} You do not have any #{currency} to transfer."
return
end
log(character, 'transfer all', 'all', currency, 'transfer all attempted')
transfer(character, amount, currency, destination)
end
private
def log(character, action, amount, currency, message)
save_bankbot_transaction("#{character}, #{action}, #{amount}, #{currency}, #{message}", @ledger)
echo("Logging: #{character}, #{action}, #{amount}, #{currency}, #{message}")
echo("Ledger: #{@ledger}")
end
def determine_currency(input)
$CURRENCIES.find { |s| s.downcase.include?(input.downcase) }
end
end
class BankbotInputSanitizer
attr_reader :bankbot
def initialize
@bankbot = Bankbot.new
end
def help(character)
@bankbot.help(character)
end
def deposit(character, amount, currency)
sanitized_amount = amount.to_i
sanitized_currency = determine_currency(currency)
@bankbot.deposit(character, sanitized_amount, sanitized_currency)
end
def withdraw(character, amount, currency)
sanitized_amount = amount.to_i
sanitized_currency = determine_currency(currency)
@bankbot.withdraw(character, sanitized_amount, sanitized_currency)
end
def withdraw_all(character, currency)
sanitized_currency = determine_currency(currency)
@bankbot.withdraw_all(character, sanitized_currency)
end
def report_balance(character)
@bankbot.report_balance(character)
end
def transfer(character, amount, currency, destination)
sanitized_amount = amount.to_i
sanitized_currency = determine_currency(currency)
@bankbot.transfer(character, sanitized_amount, sanitized_currency, destination)
end
def transfer_all(character, currency, destination)
sanitized_currency = determine_currency(currency)
@bankbot.transfer_all(character, sanitized_currency, destination)
end
def get_balance(character)
@bankbot.get_balance(character)
end
def help_content
@bankbot.help_content
end
private
def determine_currency(input)
$CURRENCIES.find { |s| s.downcase.include?(input.downcase) }
end
end
class BankbotController
attr_reader :validator, :sanitizer
def initialize(validator)
@validator = validator
@sanitizer = BankbotInputSanitizer.new
end
def whisper_help(character)
@sanitizer.help(character)
end
def accept_tip(character, amount, currency)
if @validator.valid?(character)
@sanitizer.deposit(character, amount, currency)
whisper_balance(character)
else
fput('decline tip')
end
end
def withdraw(character, amount, currency)
@sanitizer.withdraw(character, amount, currency)
whisper_balance(character)
end
def withdraw_all(character, currency)
@sanitizer.withdraw_all(character, currency)
whisper_balance(character)
end
def whisper_balance(character)
@sanitizer.report_balance(character)
end
def whisper_transfer(character, amount, currency, destination)
@sanitizer.transfer(character, amount, currency, destination)
whisper_balance(character)
end
def whisper_transfer_all(character, currency, destination)
@sanitizer.transfer_all(character, currency, destination)
whisper_balance(character)
end
def whisper_unknown_command(character)
fput "whisper #{character} I'm sorry, I did not understand that command"
@sanitizer.help(character)
end
def send_slack_token(character)
@validator.send_slack_token(character)
end
def lnet_balance(character)
@validator.send_bankbot_balance(character, @sanitizer.get_balance(character))
end
def lnet_help(character)
@validator.send_bankbot_help(character, @sanitizer.help_content)
end
def lnet_transfer(character, amount, currency, destination)
@sanitizer.transfer(character, amount, currency, destination)
lnet_balance(character)
end
def lnet_transfer_all(character, currency, destination)
@sanitizer.transfer_all(character, currency, destination)
lnet_balance(character)
end
def lnet_send_location(character)
@validator.send_bankbot_location(character)
end
end
class BankbotParser
attr_reader :validator, :controller
def initialize(announce, greet)
@validator = CharacterValidator.new(announce, false, greet, 'Bankbot')
@controller = BankbotController.new(@validator)
end
def handle_new_character_seen(character)
@validator.validate(character)
end
def handle_whisper(character, message)
return unless @validator.valid?(character)
case message
when /^(help)$/i
@controller.whisper_help(character)
when /^(with|withdraw) (\d+) (K.*|L.*|D.*)$/i
amount = Regexp.last_match(2)
currency = Regexp.last_match(3)
@controller.withdraw(character, amount, currency)
when /^(with|withdraw) all (K.*|L.*|D.*)$/i
currency = Regexp.last_match(2)
@controller.withdraw_all(character, currency)
when /^(bal|balance)$/i
@controller.whisper_balance(character)
when /^(transfer) (\d+) (K.*|L.*|D.*) (.*)$/i
amount = Regexp.last_match(2)
currency = Regexp.last_match(3)
destination = Regexp.last_match(4)
@controller.whisper_transfer(character, amount, currency, destination)
when /(transfer) all (K.*|L.*|D.*) (.*)$/i
currency = Regexp.last_match(2)
destination = Regexp.last_match(3)
@controller.whisper_transfer_all(character, currency, destination)
else
@controller.whisper_unknown_command(character)
end
end
def handle_lnet_player_message(character, message)
@validator.confirm(character)
# TODO: Handle the 'else' case
case message
when /^RequestSlackToken/
@controller.send_slack_token(character)
when /^(bal|balance)$/i
@controller.lnet_balance(character)
when /^(help)$/i
@controller.lnet_help(character)
when /^(transfer) (\d+) (K.*|L.*|D.*) (.*)$/i
amount = Regexp.last_match(2)
currency = Regexp.last_match(3)
destination = Regexp.last_match(4)
@controller.lnet_transfer(character, amount, currency, destination)
when /^(transfer) all (K.*|L.*|D.*) (.*)$/i
currency = Regexp.last_match(2)
destination = Regexp.last_match(3)
@controller.lnet_transfer_all(character, currency, destination)
when /^(where|room|location)$/i
@controller.lnet_send_location(character)
end
end
def handle_lnet_server_message(message)
case message
when /^(.*) is tuned to the following channels.*$/
character = Regexp.last_match(1)
@validator.confirm(character)
when /^(.*) is connected but not tuned to any channels.*$/
character = Regexp.last_match(1)
@validator.confirm(character)
end
end
def handle_tip(character, message)
case message
when /^(\d+) (\w+)$/
amount = Regexp.last_match(1)
currency = Regexp.last_match(2)
@controller.accept_tip(character, amount, currency)
end
end
end
def warn_before_run
# Pause script and warn that it may cause account/plat loss
DRC.message("WARNING: bankbot is no longer maintained, or supported.")
DRC.message("WARNING: bankbot usage may result in your account being banned, and/or any moneys deposited being forfeit to Simutronics")
DRC.message("WARNING: use at your own risk")
DRC.message(" ")
DRC.message("WARNING: Unpause the script to continue or #{$clean_lich_char}kill the script to stop.")
pause_script
end
arg_definitions = [
[
{ name: 'start', regex: /start/i, description: 'Required: prevents accidentally starting up bankbot' },
{ name: 'announce', regex: /announce/i, optional: true, description: 'If arg is present, bankbot will announce its presence in LNet' },
{ name: 'greet', regex: /greet/i, optional: true, description: 'If arg is present, bankbot will greet characters after validating (but only once)' }
]
]
args = parse_args(arg_definitions)
# Warn the script user that this is designed to perform actions that may get them banned, or lose plats.
warn_before_run
parser = BankbotParser.new(args.announce, args.greet)
@last_room_list = []
loop do
line = script.gets?
pause 0.05 unless line
if DRRoom.pcs != @last_room_list
(DRRoom.pcs - @last_room_list).each { |character| parser.handle_new_character_seen(character) }
@last_room_list = DRRoom.pcs
end
case line
when /^You catch (.*) making a grab for your pockets!$/
thief = Regexp.last_match(1)
echo "***EXITING, CAUGHT #{thief} TRYING TO STEAL***"
fput('exit')
when /^(.*) whispers, "(.*)"$/i
character = Regexp.last_match(1)
message = Regexp.last_match(2)
parser.handle_whisper(character, message)
when /^(.*) offers you a tip of (.*)\. Type ACCEPT TIP, to accept it or DECLINE TIP to refuse it\.$/
character = Regexp.last_match(1)
message = Regexp.last_match(2)
parser.handle_tip(character, message)
when /^\[server\]: "DR:(.*)"\s*(?:\(\d{1,2}:\d{1,2}:\d{1,2}\))?$/
message = Regexp.last_match(1)
parser.handle_lnet_server_message(message)
when /^\[Private\]-.*:(.*): "(.*)"/
character = Regexp.last_match(1)
message = Regexp.last_match(2)
parser.handle_lnet_player_message(character, message)
end
end