ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/bank.ext
Revision: 1.11
Committed: Sun Jan 29 02:47:04 2017 UTC (7 years, 3 months ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +1 -1 lines
Log Message:
remove eol whitespace

File Contents

# Content
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 $value * $from_cur->value / $to_cur->value
21 }
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 my $account_info = cf::cost_string_from_value $balance;
40
41 $is_wiz
42 ? $who->name . " has $account_info."
43 : "You have $account_info."
44 }
45
46 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 sub transaction {
73 my ($who, $action, $amount, $currency) = @_;
74
75 my $coin = cf::coin_from_name $currency
76 or return "We don't trade in that currency.\n";
77
78 return "You can not $action fractions or weirder numbers."
79 unless $amount =~ /^\d+$/;
80
81 $currency = $amount > 1 ? $coin->name_pl : $coin->name;
82
83 my $pay = $amount * $coin->value;
84
85 # First check for possible overflow and user stupidity
86 if ($pay > 2**30) {
87 return "Sorry, we do not handle that amount of money for one $action.";
88 } elsif ($pay == 0) {
89 return "You can not $action nothing.";
90 } elsif ($pay < 0) {
91 return "You can not $action negative values.";
92 }
93
94 # Here we handle the transactions
95 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 if ($who->pay_player_arch ($coin->archname, $amount)) {
105 $who->{bank_balance} -= $pay;
106 return "Withdrew $amount $currency";
107 } else {
108 return "You can't withdraw this many $currency in one go!";
109 }
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
122 deposit <amount> <currency>
123 withdraw <amount> <currency>
124 balance
125 help rates
126 help <deposit|withdraw|balance>";
127 } 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 deposit <amount> <currency>\nOr, to deposit all your money: deposit all\nSee help rates for our currencies.";
130 } elsif ($command eq "withdraw") {
131 return "To get the money you stored in our system back, just say:
132 withdraw <amount> <currency>\nOr withdraw all to withdraw all your money.";
133 } 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 } elsif ($command eq "rates") {
136 my @coins = cf::coin_archetypes;
137 my $exchange_rates = "Here is a list of exchange rates:\n\n";
138
139 for my $f (0 .. $#coins) {
140 for my $t ($f + 1 .. $#coins) {
141 $exchange_rates .=
142 sprintf " One %s is %d %s.\n",
143 $coins[$f]->name,
144 (calc_exchange_rate 1, $coins[$f], $coins[$t]),
145 $coins[$t]->name_pl;
146 }
147 }
148
149 return "$exchange_rates\nThis may change a bit with the stock markets but don't expect it to be much.";
150 }
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 if ($who->flag (cf::FLAG_WIZ) && $arguments =~ /\S/) {
163 cf::async {
164 if (my $player = cf::player::find $arguments) {
165 $who->reply ($npc,
166 "Nancy says: " . balance ($player->ob, 1),
167 cf::NDI_BROWN | cf::NDI_REPLY
168 );
169 }
170 };
171 } else {
172 $who->reply ($npc, balance ($who, 0));
173 }
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
181 } elsif ($cmd eq 'deposit' and $amount eq 'all') {
182 $who->reply ($npc, transaction_all $who, "deposit");
183
184 } elsif ($cmd eq "deposit" and $amount and $currency eq "") {
185 $who->reply ($npc, "deposit $amount of what: " . join ", ", cf::coin_names);
186
187 } elsif ($cmd eq "deposit" and $amount eq "" and $currency eq "") {
188 $who->reply ($npc, "How much do you want to deposit?");
189
190 } elsif ($cmd eq 'withdraw' and $amount eq 'all') {
191 $who->reply ($npc, transaction_all $who, "withdraw");
192
193 } 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 $who->reply ($npc, "Withdraw $amount of what: " . join ", ", cf::coin_names);
198
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