forked from rpherbig/dr-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common-money.lic
155 lines (139 loc) · 5.61 KB
/
common-money.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
# quiet
=begin
Documentation: https://elanthipedia.play.net/Lich_script_development#common-money
=end
custom_require.call(%w[common common-travel drinfomon])
module DRCM
module_function
def minimize_coins(copper)
denominations = [[10_000, 'platinum'], [1000, 'gold'], [100, 'silver'], [10, 'bronze'], [1, '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
def convert_to_copper(amount, denomination)
return (amount.to_i * 10_000) if 'platinum' =~ /^#{denomination}/
return (amount.to_i * 1000) if 'gold' =~ /^#{denomination}/
return (amount.to_i * 100) if 'silver' =~ /^#{denomination}/
return (amount.to_i * 10) if 'bronze' =~ /^#{denomination}/
amount
end
def convert_currency(amount, from, to, fee)
# When determining how much coin is needed to receive X amount in another currency
# Use a negative fee percentage
# When determining how much coin will be received after the exchange
# Use a positive fee percentage
exchange_rates = {
'dokoras' => {
'dokoras' => 1,
'kronars' => 1.38580991,
'lirums' => 1.108646953
},
'kronars' => {
'dokoras' => 0.7216,
'kronars' => 1,
'lirums' => 0.8
},
'lirums' => {
'dokoras' => 0.902,
'kronars' => 1.25,
'lirums' => 1
}
}
if fee < 0
((exchange_rates[from][to] * amount).ceil * (1 - fee)).ceil
else
((exchange_rates[from][to] * amount).floor * (1 - fee)).floor
end
end
def hometown_currency(hometown_name)
get_data('town')[hometown_name]['currency']
end
def check_wealth(currency)
DRC.bput("wealth #{currency}", /\(\d+ copper #{currency}\)/i, /No #{currency}/i).scan(/\d+/).first.to_i
end
def wealth(hometown)
check_wealth(hometown_currency(hometown))
end
def ensure_copper_on_hand(copper, settings)
on_hand = wealth(settings.hometown)
return true if on_hand >= copper
withdrawals = minimize_coins(copper - on_hand)
withdrawals.all? { |amount| withdraw_exact_amount?(amount, settings) }
end
def withdraw_exact_amount?(amount_as_string, settings)
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)
end
else
get_money_from_bank(amount_as_string, settings)
end
end
def get_money_from_bank(amount_as_string, settings)
DRCT.walk_to(get_data('town')[settings.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')
when 'The clerk counts', 'You count out'
break true
when 'The clerk glares at you.', 'Hey! Slow down!'
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'
break false
else
break false
end
end
end
def debt(hometown)
currency = hometown_currency(hometown)
DRC.bput('wealth', /\(\d+ copper #{currency}\)/i, /Wealth:/i).scan(/\d+/).first.to_i
end
def deposit_coins(keep_copper, settings)
return if settings.skip_bank
DRCT.walk_to(get_data('town')[settings.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')
when 'There is no teller here'
return
end
minimize_coins(keep_copper).each { |amount| withdraw_exact_amount?(amount, settings) }
DRC.bput('check balance', 'your current balance is')
end
def town_currency(town)
get_data('town')[town]['currency']
end
def get_money_from_specific_bank?(amount_as_string, town)
DRCT.walk_to(get_data('town')[town]['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')
when 'The clerk counts', 'You count out'
break true
when 'The clerk glares at you.', 'Hey! Slow down!'
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'
break false
else
break false
end
end
end
end