forked from elanthia-online/dr-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bankbot.lic
201 lines (166 loc) · 7.11 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
# want_script_output
=begin
Documentation: https://elanthipedia.play.net/Lich_script_repository#bankbot
=end
custom_require.call(%w(common common-validation drinfomon events))
class Bankbot
include DRC
$CURRENCIES = %w(Kronars Lirums Dokoras)
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')
# 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)
fput "whisper #{character} Supported commands are:"
fput "whisper #{character} (help) - this list"
fput "whisper #{character} (bal|balance) - view your current balance"
fput "whisper #{character} (with|withdraw) [amount] [currency] - withdraw [amount] [currency] from your bot balance."
fput "whisper #{character} I accept coins via TIPs to hold for you."
end
def report_balance(character)
pretty_print = get_balance(character)
fput "whisper #{character} Your balance is: #{pretty_print}."
end
def get_balance(character)
$CURRENCIES.map { |currency| "#{@ledger[character][currency]} #{currency}" }.join(', ')
end
def deposit(character, amount, input_currency)
amount = amount.to_i
currency = determine_currency(input_currency)
unless currency
log(character, 'deposit', amount, input_currency, 'currency not recognized')
fput "whisper #{character} I'm sorry, I don't recognize the currency '#{input_currency}'."
return
end
case 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.to_i
log(character, 'deposit', amount, currency, 'succeeded')
report_balance(character)
end
end
def withdraw(character, amount, input_currency)
amount = amount.to_i
currency = determine_currency(input_currency)
unless currency
log(character, 'withdrawal', amount, input_currency, 'currency not recognized')
fput "whisper #{character} I'm sorry, I don't recognize the currency '#{input_currency}'."
return
end
if @ledger[character][currency] < amount
log(character, 'withdrawal', amount, currency, 'insufficient funds')
fput "whisper #{character} You don't have that much money."
report_balance(character)
return
end
Flags.reset('tip-accepted')
Flags.reset('tip-expired')
Flags.reset('tip-declined')
case 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'
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')
report_balance(character)
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
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)
bankbot = Bankbot.new
validator = CharacterValidator.new(args.announce, false, args.greet, 'Bankbot')
@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 { |name| validator.validate(name) }
@last_room_list = DRRoom.pcs
end
case line
when /^(.*) whispers, "(help)"$/i
character = Regexp.last_match(1)
next unless validator.valid?(character)
bankbot.help(character)
when /^(.*) offers you a tip of (\d+) (\w+)\. Type ACCEPT TIP, to accept it or DECLINE TIP to refuse it\.$/
character = Regexp.last_match(1)
amount = Regexp.last_match(2)
currency = Regexp.last_match(3)
if validator.valid?(character)
bankbot.deposit(character, amount, currency)
else
fput('decline tip')
end
when /^(.*) whispers, "(with|withdraw) (\d+) (K.*|L.*|D.*)"$/i
character = Regexp.last_match(1)
amount = Regexp.last_match(3)
currency = Regexp.last_match(4)
next unless validator.valid?(character)
bankbot.withdraw(character, amount, currency)
when /^(.*) whispers, "(bal|balance)"$/i
character = Regexp.last_match(1)
next unless validator.valid?(character)
bankbot.report_balance(character)
when /^(.*) whispers, ".*"$/i
character = Regexp.last_match(1)
next unless validator.valid?(character)
fput "whisper #{character} I'm sorry, I did not understand that command"
bankbot.help(character)
when /^\[server\]: "DR:(.*) is tuned to the following channels.*"$/
character = Regexp.last_match(1)
validator.confirm(character)
when /^\[server\]: "DR:(.*) is connected but not tuned to any channels.*"$/
character = Regexp.last_match(1)
validator.confirm(character)
when /^\[Private\]-.*:(.*): "RequestSlackToken"/
character = Regexp.last_match(1)
validator.confirm(character)
validator.send_slack_token(character)
when /^\[Private\]-.*:(.*): "(bal|balance)"$/i
character = Regexp.last_match(1)
validator.confirm(character)
validator.send_bankbot_balance(character, bankbot.get_balance(character))
end
end