ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/bank.ext
(Generate patch)

Comparing deliantra/maps/perl/bank.ext (file contents):
Revision 1.15 by pippijn, Tue May 23 13:07:10 2006 UTC vs.
Revision 1.22 by elmex, Mon Aug 14 07:11:10 2006 UTC

4# give the banker in-game: 4# give the banker in-game:
5# - help, will show the usage information 5# - help, will show the usage information
6# - deposit, can deposit a certain amount of a given currency unit 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 7# - withdraw, can give out a certain amount of a given currency unit
8# provided, the bank account has this amount of money 8# provided, the bank account has this amount of money
9# - exchange, will exchange between imperials and platinum coins 9# - exchange, will exchange between royalties and platinum coins
10# - balance, simply shows how much money one has on his/her account. 10# - balance, simply shows how much money one has on his/her account.
11# 11#
12# TODO: 12# TODO:
13# - Comment more steps 13# - Comment more steps
14# - Implement exchange() 14# - Implement exchange()
15 15
16# The units are being used in each sub so they are declared globally. 16# The units are being used in each sub so they are declared globally.
17my %unit = ( 17my %unit = (
18 silver => 1, 18 silver => 1,
19 gold => 10, 19 gold => 10,
20 platina => 50, 20 platina => 50,
21 imperial => 10000, 21 royalty => 5000,
22); 22);
23 23
24# Subroutine just to calculate exchange rates. 24# Subroutine just to calculate exchange rates.
25sub calc_exchange_rate { 25sub calc_exchange_rate {
26 my ($value, $from_cur, $to_cur) = @_; 26 my ($value, $from_cur, $to_cur) = @_;
27 27
28 $value * $unit{$from_cur} / $unit{$to_cur} 28 $value * $unit{$from_cur} / $unit{$to_cur}
29}
30
31sub amount_str {
32 my $amount = shift;
33
34 my $royalties = calc_exchange_rate $amount, "silver", "royalty";
35 my $platinum = calc_exchange_rate $royalties - (int $royalties), "royalty", "platina";
36 my $gold = calc_exchange_rate $platinum - (int $platinum), "platina", "gold";
37 my $silver = calc_exchange_rate $gold - (int $gold), "gold", "silver";
38
39 (int $royalties) . " royalties, "
40 . (int $platinum) . " platinum, "
41 . (int $gold) . " gold and about "
42 . (int $silver) ." silver coins.";
43
29} 44}
30 45
31# Calculates and returns how much money someone has starting with the 46# Calculates and returns how much money someone has starting with the
32# highest value currency going down. Think of it like having the date 47# highest value currency going down. Think of it like having the date
33# and time in the format of 2006-05-19 03:21:31 UTC instead of 1148001691 48# and time in the format of 2006-05-19 03:21:31 UTC instead of 1148001691
36 my ($who, $is_wiz) = @_; 51 my ($who, $is_wiz) = @_;
37 my $balance = $who->{bank_balance}; 52 my $balance = $who->{bank_balance};
38 53
39 unless ($is_wiz) { 54 unless ($is_wiz) {
40 return "Sorry, you have no balance." 55 return "Sorry, you have no balance."
41 unless $balance; 56 unless $balance >= 1;
42 } else { 57 } else {
43 return $who->name." has no balance." 58 return $who->name." has no balance."
44 unless $balance; 59 unless $balance >= 1;
45 } 60 }
46 61
47 my $imperials = calc_exchange_rate $balance, "silver", "imperial"; 62 my $account_info = amount_str ($balance);
48 my $platinum = calc_exchange_rate $imperials - (int $imperials), "imperial", "platina";
49 my $gold = calc_exchange_rate $platinum - (int $platinum), "platina", "gold";
50 my $silver = calc_exchange_rate $gold - (int $gold), "gold", "silver";
51
52 my $account_info =
53 (int $imperials) . " imperials, "
54 . (int $platinum) . " platinum, "
55 . (int $gold) . " gold and about "
56 . (int $silver) ." silver coins.";
57 63
58 return "You have $account_info" 64 return "You have $account_info"
59 unless $is_wiz; 65 unless $is_wiz;
60 66
61 return $who->name." has $account_info"; 67 return $who->name." has $account_info";
87 return "$amount $currency received."; 93 return "$amount $currency received.";
88 } else { 94 } else {
89 return "You don't have enough money with you."; 95 return "You don't have enough money with you.";
90 } 96 }
91 } elsif ($action eq "withdraw") { 97 } elsif ($action eq "withdraw") {
92 if ($who->{bank_balance} - $pay > 0) { 98 if ($pay <= $who->{bank_balance}) {
99 my $paid = $who->pay_player_arch (
100 $currency eq 'royalty' ? 'royalty' : $currency . "coin",
101 $amount
102 );
103 if ($paid) {
93 $who->{bank_balance} -= $pay; 104 $who->{bank_balance} -= $pay;
94 if ($currency eq "imperial") {
95 my $money = cf::object::new "imperial";
96 $money->set_property (1, 24, $amount);
97 $money->insert_in_ob ($who);
98 } else { 105 } else {
99 my $money = cf::object::new $currency . "coin"; 106 $amount = 0;
100 $money->set_property (1, 24, $amount);
101 $money->insert_in_ob ($who);
102 } 107 }
103 return "Withdrew $amount $currency"; 108 return "Withdrew $amount $currency";
109 } else {
110 return "You don't have that much money on your bank account.";
104 } 111 }
105 } 112 }
106} 113}
107 114
108sub help { 115sub help {
122 return "To get the money you stored in our system back, just say: 129 return "To get the money you stored in our system back, just say:
123 withdraw <amount> <currency>"; 130 withdraw <amount> <currency>";
124 } elsif ($command eq "balance") { 131 } elsif ($command eq "balance") {
125 return "I can tell you how much money you are saving in our bank, just ask me about your balance."; 132 return "I can tell you how much money you are saving in our bank, just ask me about your balance.";
126 } elsif ($command eq "exchange") { 133 } elsif ($command eq "exchange") {
127 return "We exchange between imperial notes and platina coins at a very low price. To change from platina to imperials, say: 134 return "We exchange between royalties and platina coins at a very low price. To change from platina to royalties, say:
128 exchange <amount> platina\nTo change from imperials to platina, say: 135 exchange <amount> platina royalty\nTo change from royalties to platina, say:
129 exchange <amount> imperial\nWe currently only exchange between these two currencies. If you want to know more about exchange rates, ask me: 136 exchange <amount> royalty platina\nWe currently only exchange between these two currencies. If you want to know more about exchange rates, ask me:
130 help exchange rates\nThanks for using our bank."; 137 help exchange rates\nThanks for using our bank.";
131 } elsif ($command eq "exchange rates") { 138 } elsif ($command eq "exchange rates") {
132 my @units = sort { $unit{$b} <=> $unit{$a} } keys %unit; 139 my @units = sort { $unit{$b} <=> $unit{$a} } keys %unit;
133 my $exchange_rates = "Here is a list of exchange rates:\n"; 140 my $exchange_rates = "Here is a list of exchange rates:\n";
134 141
139 } 146 }
140 return $exchange_rates."This may change a bit with the stock markets but don't expect it to be much."; 147 return $exchange_rates."This may change a bit with the stock markets but don't expect it to be much.";
141 } 148 }
142} 149}
143 150
144sub on_say { 151cf::register_script_function "bank::command" => sub {
145 my ($event, $ob, $who, $msg) = @_; 152 my ($who, $msg, $npc) = @_;
153
146 my ($cmd, $arguments) = split /\s+/, $msg, 2; 154 my ($cmd, $arguments) = split /\s+/, $msg, 2;
147 my ($amount, $currency) = split /\s+/, $arguments, 2; 155 my ($amount, $currency) = split /\s+/, $arguments, 2;
148 my $name = $ob->name;
149 my $service_charge = 5; 156 my $service_charge = 5;
150 my @units = sort keys %unit; 157 my @units = sort keys %unit;
151 my $units = join ", ", @units; 158 my $units = join ", ", @units;
152 my $fees = - ($service_charge / 100) + 1; 159 my $fees = - ($service_charge / 100) + 1;
160
161 # Have some aliases for currencies
153 if ($currency eq "platinum") { 162 if ($currency eq "platinum") {
154 $currency = "platina"; 163 $currency = "platina";
164 } elsif ($currency eq "royalties") {
165 $currency = "royalty";
155 } 166 }
156
157 167
158 if ($cmd eq "balance") { 168 if ($cmd eq "balance") {
159 if ($who->flag (cf::FLAG_WIZ)) { 169 if ($who->flag (cf::FLAG_WIZ)) {
160 if (my $player = cf::player::find $arguments) { 170 if (my $player = cf::player::find $arguments) {
161 $who->message ("$name says: ".balance ($player->ob, 1), cf::NDI_WHITE); 171 $who->reply ($npc, balance $player->ob, 1);
162 } 172 }
163 } else { 173 } else {
164 $who->message ("$name says: ".balance ($who, 0), cf::NDI_WHITE); 174 $who->reply ($npc, balance $who, 0);
165 } 175 }
166 176
167 } elsif ($cmd eq "balance" and !$arguments) { 177 } elsif ($cmd eq "balance" and !$arguments) {
168 $who->message ("$name says: Balance of whom?", cf::NDI_WHITE); 178 $who->reply ($npc, "Balance of whom?");
169 179
170 } elsif ($cmd eq "deposit" and $currency) { 180 } elsif ($cmd eq "deposit" and $currency) {
171 $who->message ("$name says: " . (transaction $who, "deposit", $amount, $currency), cf::NDI_WHITE); 181 $who->reply ($npc, transaction $who, "deposit", $amount, $currency);
172 $who->message ("$name says: " . (balance $who), cf::NDI_WHITE); 182 $who->reply ($npc, balance $who);
173 183
174 } elsif ($cmd eq "deposit" and $amount and $currency eq "") { 184 } elsif ($cmd eq "deposit" and $amount and $currency eq "") {
175 $who->message ("$name says: Deposit $amount of what: $units", cf::NDI_WHITE); 185 $who->reply ($npc, "deposit $amount of what: $units");
176 186
177 } elsif ($cmd eq "deposit" and $amount eq "" and $currency eq "") { 187 } elsif ($cmd eq "deposit" and $amount eq "" and $currency eq "") {
178 $who->message ("$name says: How much do you want to deposit?", cf::NDI_WHITE); 188 $who->reply ($npc, "How much do you want to deposit?");
179 189
180 } elsif ($cmd eq "withdraw" and $currency) { 190 } elsif ($cmd eq "withdraw" and $currency) {
181 $who->message ("$name says: " . (transaction $who, "withdraw", $amount, $currency), cf::NDI_WHITE); 191 $who->reply ($npc, transaction $who, "withdraw", $amount, $currency);
182 $who->message ("$name says: " . (balance $who), cf::NDI_WHITE); 192 $who->reply ($npc, balance $who);
183 193
184 } elsif ($cmd eq "withdraw" and $amount and $currency eq "") { 194 } elsif ($cmd eq "withdraw" and $amount and $currency eq "") {
185 $who->message ("$name says: Withdraw $amount of what: $units", cf::NDI_WHITE); 195 $who->reply ($npc, "Withdraw $amount of what: $units");
186 196
187 } elsif ($cmd eq "withdraw" and $amount eq "" and $currency eq "") { 197 } elsif ($cmd eq "withdraw" and $amount eq "" and $currency eq "") {
188 $who->message ("$name says: How much do you want to withdraw?", cf::NDI_WHITE); 198 $who->reply ($npc, "How much do you want to withdraw?");
189 199
190 } elsif ($cmd eq "exchange") { 200 } elsif ($cmd eq "exchange") {
191 $who->message ("$name says: This function is not yet implemented. Ask me again later.", cf::NDI_WHITE); 201 $who->reply ($npc, "This function is not yet implemented. Ask me again later.");
192 202
193 } elsif ($cmd eq "help" or $cmd eq "yes" and $arguments eq "") { 203 } elsif ($cmd eq "help" or $cmd eq "yes" and $arguments eq "") {
194 $who->message ("$name says: " . (help $arguments), cf::NDI_WHITE); 204 $who->reply ($npc, help $arguments);
195 205
196 } elsif ($cmd eq "help" or $cmd eq "yes" and $arguments ne "") { 206 } elsif ($cmd eq "help" or $cmd eq "yes" and $arguments ne "") {
197 $who->message ("$name says: " . (help), cf::NDI_WHITE); 207 $who->reply ($npc, help);
198 208
199 } else { 209 } else {
200 $who->message ("$name says: Do you need help?", cf::NDI_WHITE); 210 $who->reply ($npc, "Do you need help?");
201 } 211 }
202} 212}
203 213

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines