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 |
|
|
sub transaction { |
47 |
|
|
my ($who, $action, $amount, $currency) = @_; |
48 |
|
|
|
49 |
root |
1.5 |
my $coin = cf::coin_from_name $currency |
50 |
|
|
or return "We don't trade in that currency.\n"; |
51 |
root |
1.1 |
|
52 |
root |
1.5 |
return "You can not $action fractions or weirder numbers." |
53 |
root |
1.1 |
unless $amount =~ /^\d+$/; |
54 |
|
|
|
55 |
root |
1.5 |
$currency = $amount > 1 ? $coin->name_pl : $coin->name; |
56 |
|
|
|
57 |
|
|
my $pay = $amount * $coin->value; |
58 |
|
|
|
59 |
root |
1.1 |
# First check for possible overflow and user stupidity |
60 |
elmex |
1.8 |
if ($pay > 2**30) { |
61 |
root |
1.5 |
return "Sorry, we do not handle that kind of money for one $action."; |
62 |
elmex |
1.8 |
} elsif ($pay == 0) { |
63 |
root |
1.5 |
return "You can not $action nothing."; |
64 |
elmex |
1.8 |
} elsif ($pay < 0) { |
65 |
root |
1.5 |
return "You can not $action negative values."; |
66 |
root |
1.1 |
} |
67 |
|
|
|
68 |
|
|
# Here we handle the transactions |
69 |
|
|
if ($action eq "deposit") { |
70 |
|
|
if ($who->pay_amount ($pay)) { |
71 |
|
|
$who->{bank_balance} += $pay; |
72 |
|
|
return "$amount $currency received."; |
73 |
|
|
} else { |
74 |
|
|
return "You don't have enough money with you."; |
75 |
|
|
} |
76 |
|
|
} elsif ($action eq "withdraw") { |
77 |
|
|
if ($pay <= $who->{bank_balance}) { |
78 |
root |
1.5 |
if ($who->pay_player_arch ($coin->archname, $amount)) { |
79 |
root |
1.1 |
$who->{bank_balance} -= $pay; |
80 |
root |
1.5 |
return "Withdrew $amount $currency"; |
81 |
root |
1.1 |
} else { |
82 |
root |
1.5 |
return "You can't withdraw this many $currency in one go!"; |
83 |
root |
1.1 |
} |
84 |
|
|
} else { |
85 |
|
|
return "You don't have that much money on your bank account."; |
86 |
|
|
} |
87 |
|
|
} |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
sub help { |
91 |
|
|
my ($command) = @_; |
92 |
|
|
|
93 |
|
|
if ($command eq "") { |
94 |
|
|
return "You can: |
95 |
root |
1.7 |
|
96 |
root |
1.1 |
deposit <amount> <currency> |
97 |
|
|
withdraw <amount> <currency> |
98 |
|
|
balance |
99 |
root |
1.5 |
help rates |
100 |
|
|
help <deposit|withdraw|balance>"; |
101 |
root |
1.1 |
} elsif ($command eq "deposit") { |
102 |
|
|
return "You can deposit some of the money you don't currently use in our bank. To do this, ask me to: |
103 |
root |
1.5 |
deposit <amount> <currency>\nSee help rates for our currencies."; |
104 |
root |
1.1 |
} elsif ($command eq "withdraw") { |
105 |
|
|
return "To get the money you stored in our system back, just say: |
106 |
|
|
withdraw <amount> <currency>"; |
107 |
|
|
} elsif ($command eq "balance") { |
108 |
|
|
return "I can tell you how much money you are saving in our bank, just ask me about your balance."; |
109 |
root |
1.5 |
} elsif ($command eq "rates") { |
110 |
|
|
my @coins = cf::coin_archetypes; |
111 |
root |
1.7 |
my $exchange_rates = "Here is a list of exchange rates:\n\n"; |
112 |
root |
1.5 |
|
113 |
|
|
for my $f (0 .. $#coins) { |
114 |
|
|
for my $t ($f + 1 .. $#coins) { |
115 |
|
|
$exchange_rates .= |
116 |
root |
1.7 |
sprintf " One %s is %d %s.\n", |
117 |
root |
1.5 |
$coins[$f]->name, |
118 |
|
|
(calc_exchange_rate 1, $coins[$f], $coins[$t]), |
119 |
|
|
$coins[$t]->name_pl; |
120 |
root |
1.1 |
} |
121 |
|
|
} |
122 |
root |
1.5 |
|
123 |
root |
1.7 |
return "$exchange_rates\nThis may change a bit with the stock markets but don't expect it to be much."; |
124 |
root |
1.1 |
} |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
cf::register_script_function "bank::command" => sub { |
128 |
|
|
my ($who, $msg, $npc) = @_; |
129 |
|
|
|
130 |
|
|
my ($cmd, $arguments) = split /\s+/, $msg, 2; |
131 |
|
|
my ($amount, $currency) = split /\s+/, $arguments, 2; |
132 |
|
|
my $service_charge = 5; |
133 |
|
|
my $fees = - ($service_charge / 100) + 1; |
134 |
|
|
|
135 |
|
|
if ($cmd eq "balance") { |
136 |
elmex |
1.3 |
if ($who->flag (cf::FLAG_WIZ) && $arguments =~ /\S/) { |
137 |
|
|
cf::async { |
138 |
|
|
if (my $player = cf::player::find $arguments) { |
139 |
root |
1.6 |
$who->reply ($npc, |
140 |
|
|
"Nancy says: " . balance ($player->ob, 1), |
141 |
elmex |
1.3 |
cf::NDI_BROWN | cf::NDI_REPLY |
142 |
root |
1.6 |
); |
143 |
elmex |
1.3 |
} |
144 |
|
|
}; |
145 |
root |
1.1 |
} else { |
146 |
elmex |
1.3 |
$who->reply ($npc, balance ($who, 0)); |
147 |
root |
1.1 |
} |
148 |
|
|
|
149 |
|
|
} elsif ($cmd eq "balance" and !$arguments) { |
150 |
|
|
$who->reply ($npc, "Balance of whom?"); |
151 |
|
|
|
152 |
|
|
} elsif ($cmd eq "deposit" and $currency) { |
153 |
|
|
$who->reply ($npc, transaction $who, "deposit", $amount, $currency); |
154 |
|
|
|
155 |
|
|
} elsif ($cmd eq "deposit" and $amount and $currency eq "") { |
156 |
root |
1.5 |
$who->reply ($npc, "deposit $amount of what: " . join ", ", cf::coin_names); |
157 |
root |
1.1 |
|
158 |
|
|
} elsif ($cmd eq "deposit" and $amount eq "" and $currency eq "") { |
159 |
|
|
$who->reply ($npc, "How much do you want to deposit?"); |
160 |
|
|
|
161 |
|
|
} elsif ($cmd eq "withdraw" and $currency) { |
162 |
|
|
$who->reply ($npc, transaction $who, "withdraw", $amount, $currency); |
163 |
|
|
|
164 |
|
|
} elsif ($cmd eq "withdraw" and $amount and $currency eq "") { |
165 |
root |
1.5 |
$who->reply ($npc, "Withdraw $amount of what: " . join ", ", cf::coin_names); |
166 |
root |
1.1 |
|
167 |
|
|
} elsif ($cmd eq "withdraw" and $amount eq "" and $currency eq "") { |
168 |
|
|
$who->reply ($npc, "How much do you want to withdraw?"); |
169 |
|
|
|
170 |
|
|
} elsif ($cmd eq "help" or $cmd eq "yes" and $arguments eq "") { |
171 |
|
|
$who->reply ($npc, help $arguments); |
172 |
|
|
|
173 |
|
|
} elsif ($cmd eq "help" or $cmd eq "yes" and $arguments ne "") { |
174 |
|
|
$who->reply ($npc, help); |
175 |
|
|
|
176 |
|
|
} else { |
177 |
|
|
$who->reply ($npc, "Do you need help?"); |
178 |
|
|
} |
179 |
|
|
} |
180 |
|
|
|