1 |
root |
1.1 |
#! perl |
2 |
|
|
|
3 |
|
|
# The international bank script. There are five commands one can |
4 |
|
|
# give the banker in-game: |
5 |
|
|
# - help, will show the usage information |
6 |
|
|
# - deposit, can deposit a certain amount of a given currency unit |
7 |
|
|
# - withdraw, can give out a certain amount of a given currency unit |
8 |
|
|
# provided, the bank account has this amount of money |
9 |
|
|
# - exchange, will exchange between royalties and platinum coins |
10 |
|
|
# - balance, simply shows how much money one has on his/her account. |
11 |
|
|
# |
12 |
|
|
# TODO: |
13 |
|
|
# - Comment more steps |
14 |
|
|
# - Implement exchange() |
15 |
|
|
|
16 |
|
|
# Subroutine just to calculate exchange rates. |
17 |
|
|
sub calc_exchange_rate { |
18 |
|
|
my ($value, $from_cur, $to_cur) = @_; |
19 |
|
|
|
20 |
root |
1.5 |
$value * $from_cur->value / $to_cur->value |
21 |
root |
1.1 |
} |
22 |
|
|
|
23 |
|
|
# Calculates and returns how much money someone has starting with the |
24 |
|
|
# highest value currency going down. Think of it like having the date |
25 |
|
|
# and time in the format of 2006-05-19 03:21:31 UTC instead of 1148001691 |
26 |
|
|
# seconds since 1970-01-01 00:00:00 UTC. |
27 |
|
|
sub balance { |
28 |
|
|
my ($who, $is_wiz) = @_; |
29 |
|
|
my $balance = $who->{bank_balance}; |
30 |
|
|
|
31 |
|
|
unless ($is_wiz) { |
32 |
|
|
return "Sorry, you have no balance." |
33 |
|
|
unless $balance >= 1; |
34 |
|
|
} else { |
35 |
|
|
return $who->name." has no balance." |
36 |
|
|
unless $balance >= 1; |
37 |
|
|
} |
38 |
|
|
|
39 |
root |
1.5 |
my $account_info = cf::cost_string_from_value $balance; |
40 |
root |
1.1 |
|
41 |
root |
1.5 |
$is_wiz |
42 |
|
|
? $who->name . " has $account_info." |
43 |
|
|
: "You have $account_info." |
44 |
root |
1.1 |
} |
45 |
|
|
|
46 |
elmex |
1.10 |
sub transaction_all { |
47 |
|
|
my ($who, $action) = @_; |
48 |
|
|
|
49 |
|
|
my $money = $who->query_money; |
50 |
|
|
|
51 |
|
|
if ($action eq 'deposit') { |
52 |
|
|
return "Sorry, you don't seem to have any money to deposit." |
53 |
|
|
unless $money > 0; |
54 |
|
|
|
55 |
|
|
if ($who->pay_amount ($money)) { |
56 |
|
|
$who->{bank_balance} += $money; |
57 |
|
|
my $account_info = cf::cost_string_from_value $money; |
58 |
|
|
return "$account_info received."; |
59 |
|
|
} else { |
60 |
|
|
return "Sorry, but we can't reach all your money. Something is wrong."; |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
} elsif ($action eq 'withdraw') { |
64 |
|
|
$who->pay_player ($who->{bank_balance}); |
65 |
|
|
my $account_info = cf::cost_string_from_value $who->{bank_balance}; |
66 |
|
|
$who->{bank_balance} = 0; |
67 |
|
|
return "You withdrew $account_info."; |
68 |
|
|
} |
69 |
|
|
return "Sorry, we don't know what you mean by '$action'." |
70 |
|
|
} |
71 |
|
|
|
72 |
root |
1.1 |
sub transaction { |
73 |
|
|
my ($who, $action, $amount, $currency) = @_; |
74 |
|
|
|
75 |
root |
1.5 |
my $coin = cf::coin_from_name $currency |
76 |
|
|
or return "We don't trade in that currency.\n"; |
77 |
root |
1.1 |
|
78 |
root |
1.5 |
return "You can not $action fractions or weirder numbers." |
79 |
root |
1.1 |
unless $amount =~ /^\d+$/; |
80 |
|
|
|
81 |
root |
1.5 |
$currency = $amount > 1 ? $coin->name_pl : $coin->name; |
82 |
|
|
|
83 |
|
|
my $pay = $amount * $coin->value; |
84 |
|
|
|
85 |
root |
1.1 |
# First check for possible overflow and user stupidity |
86 |
elmex |
1.8 |
if ($pay > 2**30) { |
87 |
root |
1.9 |
return "Sorry, we do not handle that amount of money for one $action."; |
88 |
elmex |
1.8 |
} elsif ($pay == 0) { |
89 |
root |
1.5 |
return "You can not $action nothing."; |
90 |
elmex |
1.8 |
} elsif ($pay < 0) { |
91 |
root |
1.5 |
return "You can not $action negative values."; |
92 |
root |
1.1 |
} |
93 |
|
|
|
94 |
root |
1.11 |
# Here we handle the transactions |
95 |
root |
1.1 |
if ($action eq "deposit") { |
96 |
|
|
if ($who->pay_amount ($pay)) { |
97 |
|
|
$who->{bank_balance} += $pay; |
98 |
|
|
return "$amount $currency received."; |
99 |
|
|
} else { |
100 |
|
|
return "You don't have enough money with you."; |
101 |
|
|
} |
102 |
|
|
} elsif ($action eq "withdraw") { |
103 |
|
|
if ($pay <= $who->{bank_balance}) { |
104 |
root |
1.5 |
if ($who->pay_player_arch ($coin->archname, $amount)) { |
105 |
root |
1.1 |
$who->{bank_balance} -= $pay; |
106 |
root |
1.5 |
return "Withdrew $amount $currency"; |
107 |
root |
1.1 |
} else { |
108 |
root |
1.5 |
return "You can't withdraw this many $currency in one go!"; |
109 |
root |
1.1 |
} |
110 |
|
|
} else { |
111 |
|
|
return "You don't have that much money on your bank account."; |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
sub help { |
117 |
|
|
my ($command) = @_; |
118 |
|
|
|
119 |
|
|
if ($command eq "") { |
120 |
|
|
return "You can: |
121 |
root |
1.7 |
|
122 |
root |
1.1 |
deposit <amount> <currency> |
123 |
|
|
withdraw <amount> <currency> |
124 |
|
|
balance |
125 |
root |
1.5 |
help rates |
126 |
|
|
help <deposit|withdraw|balance>"; |
127 |
root |
1.1 |
} elsif ($command eq "deposit") { |
128 |
|
|
return "You can deposit some of the money you don't currently use in our bank. To do this, ask me to: |
129 |
elmex |
1.10 |
deposit <amount> <currency>\nOr, to deposit all your money: deposit all\nSee help rates for our currencies."; |
130 |
root |
1.1 |
} elsif ($command eq "withdraw") { |
131 |
|
|
return "To get the money you stored in our system back, just say: |
132 |
elmex |
1.10 |
withdraw <amount> <currency>\nOr withdraw all to withdraw all your money."; |
133 |
root |
1.1 |
} elsif ($command eq "balance") { |
134 |
|
|
return "I can tell you how much money you are saving in our bank, just ask me about your balance."; |
135 |
root |
1.5 |
} elsif ($command eq "rates") { |
136 |
|
|
my @coins = cf::coin_archetypes; |
137 |
root |
1.7 |
my $exchange_rates = "Here is a list of exchange rates:\n\n"; |
138 |
root |
1.5 |
|
139 |
|
|
for my $f (0 .. $#coins) { |
140 |
|
|
for my $t ($f + 1 .. $#coins) { |
141 |
|
|
$exchange_rates .= |
142 |
root |
1.7 |
sprintf " One %s is %d %s.\n", |
143 |
root |
1.5 |
$coins[$f]->name, |
144 |
|
|
(calc_exchange_rate 1, $coins[$f], $coins[$t]), |
145 |
|
|
$coins[$t]->name_pl; |
146 |
root |
1.1 |
} |
147 |
|
|
} |
148 |
root |
1.5 |
|
149 |
root |
1.7 |
return "$exchange_rates\nThis may change a bit with the stock markets but don't expect it to be much."; |
150 |
root |
1.1 |
} |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
cf::register_script_function "bank::command" => sub { |
154 |
|
|
my ($who, $msg, $npc) = @_; |
155 |
|
|
|
156 |
|
|
my ($cmd, $arguments) = split /\s+/, $msg, 2; |
157 |
|
|
my ($amount, $currency) = split /\s+/, $arguments, 2; |
158 |
|
|
my $service_charge = 5; |
159 |
|
|
my $fees = - ($service_charge / 100) + 1; |
160 |
|
|
|
161 |
|
|
if ($cmd eq "balance") { |
162 |
elmex |
1.3 |
if ($who->flag (cf::FLAG_WIZ) && $arguments =~ /\S/) { |
163 |
|
|
cf::async { |
164 |
|
|
if (my $player = cf::player::find $arguments) { |
165 |
root |
1.6 |
$who->reply ($npc, |
166 |
|
|
"Nancy says: " . balance ($player->ob, 1), |
167 |
elmex |
1.3 |
cf::NDI_BROWN | cf::NDI_REPLY |
168 |
root |
1.6 |
); |
169 |
elmex |
1.3 |
} |
170 |
|
|
}; |
171 |
root |
1.1 |
} else { |
172 |
elmex |
1.3 |
$who->reply ($npc, balance ($who, 0)); |
173 |
root |
1.1 |
} |
174 |
|
|
|
175 |
|
|
} elsif ($cmd eq "balance" and !$arguments) { |
176 |
|
|
$who->reply ($npc, "Balance of whom?"); |
177 |
|
|
|
178 |
|
|
} elsif ($cmd eq "deposit" and $currency) { |
179 |
|
|
$who->reply ($npc, transaction $who, "deposit", $amount, $currency); |
180 |
elmex |
1.10 |
|
181 |
|
|
} elsif ($cmd eq 'deposit' and $amount eq 'all') { |
182 |
|
|
$who->reply ($npc, transaction_all $who, "deposit"); |
183 |
|
|
|
184 |
root |
1.1 |
} elsif ($cmd eq "deposit" and $amount and $currency eq "") { |
185 |
root |
1.5 |
$who->reply ($npc, "deposit $amount of what: " . join ", ", cf::coin_names); |
186 |
root |
1.1 |
|
187 |
|
|
} elsif ($cmd eq "deposit" and $amount eq "" and $currency eq "") { |
188 |
|
|
$who->reply ($npc, "How much do you want to deposit?"); |
189 |
|
|
|
190 |
elmex |
1.10 |
} elsif ($cmd eq 'withdraw' and $amount eq 'all') { |
191 |
|
|
$who->reply ($npc, transaction_all $who, "withdraw"); |
192 |
|
|
|
193 |
root |
1.1 |
} elsif ($cmd eq "withdraw" and $currency) { |
194 |
|
|
$who->reply ($npc, transaction $who, "withdraw", $amount, $currency); |
195 |
|
|
|
196 |
|
|
} elsif ($cmd eq "withdraw" and $amount and $currency eq "") { |
197 |
root |
1.5 |
$who->reply ($npc, "Withdraw $amount of what: " . join ", ", cf::coin_names); |
198 |
root |
1.1 |
|
199 |
|
|
} elsif ($cmd eq "withdraw" and $amount eq "" and $currency eq "") { |
200 |
|
|
$who->reply ($npc, "How much do you want to withdraw?"); |
201 |
|
|
|
202 |
|
|
} elsif ($cmd eq "help" or $cmd eq "yes" and $arguments eq "") { |
203 |
|
|
$who->reply ($npc, help $arguments); |
204 |
|
|
|
205 |
|
|
} elsif ($cmd eq "help" or $cmd eq "yes" and $arguments ne "") { |
206 |
|
|
$who->reply ($npc, help); |
207 |
|
|
|
208 |
|
|
} else { |
209 |
|
|
$who->reply ($npc, "Do you need help?"); |
210 |
|
|
} |
211 |
|
|
} |
212 |
|
|
|