ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/bank.ext
Revision: 1.6
Committed: Fri May 2 16:16:02 2008 UTC (16 years ago) by root
Branch: MAIN
CVS Tags: rel-2_6, rel-2_7, rel-2_54, rel-2_55, rel-2_56, rel-2_53, rel-2_61
Changes since 1.5: +3 -3 lines
Log Message:
fix nancy and irc's SAY_CHANNEL

File Contents

# User Rev Content
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 root 1.5 if ($amount > 2**30) {
61     return "Sorry, we do not handle that kind of money for one $action.";
62 root 1.1 } elsif ($amount == 0) {
63 root 1.5 return "You can not $action nothing.";
64 root 1.1 } elsif ($amount < 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     deposit <amount> <currency>
96     withdraw <amount> <currency>
97     balance
98 root 1.5 help rates
99     help <deposit|withdraw|balance>";
100 root 1.1 } elsif ($command eq "deposit") {
101     return "You can deposit some of the money you don't currently use in our bank. To do this, ask me to:
102 root 1.5 deposit <amount> <currency>\nSee help rates for our currencies.";
103 root 1.1 } elsif ($command eq "withdraw") {
104     return "To get the money you stored in our system back, just say:
105     withdraw <amount> <currency>";
106     } elsif ($command eq "balance") {
107     return "I can tell you how much money you are saving in our bank, just ask me about your balance.";
108 root 1.5 } elsif ($command eq "rates") {
109     my @coins = cf::coin_archetypes;
110     my $exchange_rates = "Here is a list of exchange rates:\n\n\n";
111    
112     for my $f (0 .. $#coins) {
113     for my $t ($f + 1 .. $#coins) {
114     $exchange_rates .=
115     sprintf "One %s is %d %s.\n\n",
116     $coins[$f]->name,
117     (calc_exchange_rate 1, $coins[$f], $coins[$t]),
118     $coins[$t]->name_pl;
119 root 1.1 }
120     }
121 root 1.5
122     return "$exchange_rates\n\nThis may change a bit with the stock markets but don't expect it to be much.";
123 root 1.1 }
124     }
125    
126     cf::register_script_function "bank::command" => sub {
127     my ($who, $msg, $npc) = @_;
128    
129     my ($cmd, $arguments) = split /\s+/, $msg, 2;
130     my ($amount, $currency) = split /\s+/, $arguments, 2;
131     my $service_charge = 5;
132     my $fees = - ($service_charge / 100) + 1;
133    
134     if ($cmd eq "balance") {
135 elmex 1.3 if ($who->flag (cf::FLAG_WIZ) && $arguments =~ /\S/) {
136     cf::async {
137     if (my $player = cf::player::find $arguments) {
138 root 1.6 $who->reply ($npc,
139     "Nancy says: " . balance ($player->ob, 1),
140 elmex 1.3 cf::NDI_BROWN | cf::NDI_REPLY
141 root 1.6 );
142 elmex 1.3 }
143     };
144 root 1.1 } else {
145 elmex 1.3 $who->reply ($npc, balance ($who, 0));
146 root 1.1 }
147    
148     } elsif ($cmd eq "balance" and !$arguments) {
149     $who->reply ($npc, "Balance of whom?");
150    
151     } elsif ($cmd eq "deposit" and $currency) {
152     $who->reply ($npc, transaction $who, "deposit", $amount, $currency);
153    
154     } elsif ($cmd eq "deposit" and $amount and $currency eq "") {
155 root 1.5 $who->reply ($npc, "deposit $amount of what: " . join ", ", cf::coin_names);
156 root 1.1
157     } elsif ($cmd eq "deposit" and $amount eq "" and $currency eq "") {
158     $who->reply ($npc, "How much do you want to deposit?");
159    
160     } elsif ($cmd eq "withdraw" and $currency) {
161     $who->reply ($npc, transaction $who, "withdraw", $amount, $currency);
162    
163     } elsif ($cmd eq "withdraw" and $amount and $currency eq "") {
164 root 1.5 $who->reply ($npc, "Withdraw $amount of what: " . join ", ", cf::coin_names);
165 root 1.1
166     } elsif ($cmd eq "withdraw" and $amount eq "" and $currency eq "") {
167     $who->reply ($npc, "How much do you want to withdraw?");
168    
169     } elsif ($cmd eq "help" or $cmd eq "yes" and $arguments eq "") {
170     $who->reply ($npc, help $arguments);
171    
172     } elsif ($cmd eq "help" or $cmd eq "yes" and $arguments ne "") {
173     $who->reply ($npc, help);
174    
175     } else {
176     $who->reply ($npc, "Do you need help?");
177     }
178     }
179