… | |
… | |
41 | $is_wiz |
41 | $is_wiz |
42 | ? $who->name . " has $account_info." |
42 | ? $who->name . " has $account_info." |
43 | : "You have $account_info." |
43 | : "You have $account_info." |
44 | } |
44 | } |
45 | |
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 | |
46 | sub transaction { |
72 | sub transaction { |
47 | my ($who, $action, $amount, $currency) = @_; |
73 | my ($who, $action, $amount, $currency) = @_; |
48 | |
74 | |
49 | my $coin = cf::coin_from_name $currency |
75 | my $coin = cf::coin_from_name $currency |
50 | or return "We don't trade in that currency.\n"; |
76 | or return "We don't trade in that currency.\n"; |
… | |
… | |
55 | $currency = $amount > 1 ? $coin->name_pl : $coin->name; |
81 | $currency = $amount > 1 ? $coin->name_pl : $coin->name; |
56 | |
82 | |
57 | my $pay = $amount * $coin->value; |
83 | my $pay = $amount * $coin->value; |
58 | |
84 | |
59 | # First check for possible overflow and user stupidity |
85 | # First check for possible overflow and user stupidity |
60 | if ($amount > 2**30) { |
86 | if ($pay > 2**30) { |
61 | return "Sorry, we do not handle that kind of money for one $action."; |
87 | return "Sorry, we do not handle that amount of money for one $action."; |
62 | } elsif ($amount == 0) { |
88 | } elsif ($pay == 0) { |
63 | return "You can not $action nothing."; |
89 | return "You can not $action nothing."; |
64 | } elsif ($amount < 0) { |
90 | } elsif ($pay < 0) { |
65 | return "You can not $action negative values."; |
91 | return "You can not $action negative values."; |
66 | } |
92 | } |
67 | |
93 | |
68 | # Here we handle the transactions |
94 | # Here we handle the transactions |
69 | if ($action eq "deposit") { |
95 | if ($action eq "deposit") { |
… | |
… | |
90 | sub help { |
116 | sub help { |
91 | my ($command) = @_; |
117 | my ($command) = @_; |
92 | |
118 | |
93 | if ($command eq "") { |
119 | if ($command eq "") { |
94 | return "You can: |
120 | return "You can: |
|
|
121 | |
95 | deposit <amount> <currency> |
122 | deposit <amount> <currency> |
96 | withdraw <amount> <currency> |
123 | withdraw <amount> <currency> |
97 | balance |
124 | balance |
98 | help rates |
125 | help rates |
99 | help <deposit|withdraw|balance>"; |
126 | help <deposit|withdraw|balance>"; |
100 | } elsif ($command eq "deposit") { |
127 | } 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: |
128 | return "You can deposit some of the money you don't currently use in our bank. To do this, ask me to: |
102 | deposit <amount> <currency>\nSee help rates for our currencies."; |
129 | deposit <amount> <currency>\nOr, to deposit all your money: deposit all\nSee help rates for our currencies."; |
103 | } elsif ($command eq "withdraw") { |
130 | } elsif ($command eq "withdraw") { |
104 | return "To get the money you stored in our system back, just say: |
131 | return "To get the money you stored in our system back, just say: |
105 | withdraw <amount> <currency>"; |
132 | withdraw <amount> <currency>\nOr withdraw all to withdraw all your money."; |
106 | } elsif ($command eq "balance") { |
133 | } 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."; |
134 | return "I can tell you how much money you are saving in our bank, just ask me about your balance."; |
108 | } elsif ($command eq "rates") { |
135 | } elsif ($command eq "rates") { |
109 | my @coins = cf::coin_archetypes; |
136 | my @coins = cf::coin_archetypes; |
110 | my $exchange_rates = "Here is a list of exchange rates:\n\n\n"; |
137 | my $exchange_rates = "Here is a list of exchange rates:\n\n"; |
111 | |
138 | |
112 | for my $f (0 .. $#coins) { |
139 | for my $f (0 .. $#coins) { |
113 | for my $t ($f + 1 .. $#coins) { |
140 | for my $t ($f + 1 .. $#coins) { |
114 | $exchange_rates .= |
141 | $exchange_rates .= |
115 | sprintf "One %s is %d %s.\n\n", |
142 | sprintf " One %s is %d %s.\n", |
116 | $coins[$f]->name, |
143 | $coins[$f]->name, |
117 | (calc_exchange_rate 1, $coins[$f], $coins[$t]), |
144 | (calc_exchange_rate 1, $coins[$f], $coins[$t]), |
118 | $coins[$t]->name_pl; |
145 | $coins[$t]->name_pl; |
119 | } |
146 | } |
120 | } |
147 | } |
121 | |
148 | |
122 | return "$exchange_rates\n\nThis may change a bit with the stock markets but don't expect it to be much."; |
149 | return "$exchange_rates\nThis may change a bit with the stock markets but don't expect it to be much."; |
123 | } |
150 | } |
124 | } |
151 | } |
125 | |
152 | |
126 | cf::register_script_function "bank::command" => sub { |
153 | cf::register_script_function "bank::command" => sub { |
127 | my ($who, $msg, $npc) = @_; |
154 | my ($who, $msg, $npc) = @_; |
… | |
… | |
133 | |
160 | |
134 | if ($cmd eq "balance") { |
161 | if ($cmd eq "balance") { |
135 | if ($who->flag (cf::FLAG_WIZ) && $arguments =~ /\S/) { |
162 | if ($who->flag (cf::FLAG_WIZ) && $arguments =~ /\S/) { |
136 | cf::async { |
163 | cf::async { |
137 | if (my $player = cf::player::find $arguments) { |
164 | if (my $player = cf::player::find $arguments) { |
138 | $who->contr->send_msg ( |
165 | $who->reply ($npc, |
139 | $ext::chat::SAY_CHANNEL, "Nancy says: " . balance ($player->ob, 1), |
166 | "Nancy says: " . balance ($player->ob, 1), |
140 | cf::NDI_BROWN | cf::NDI_REPLY |
167 | cf::NDI_BROWN | cf::NDI_REPLY |
141 | ) if $who->contr; |
168 | ); |
142 | } |
169 | } |
143 | }; |
170 | }; |
144 | } else { |
171 | } else { |
145 | $who->reply ($npc, balance ($who, 0)); |
172 | $who->reply ($npc, balance ($who, 0)); |
146 | } |
173 | } |
… | |
… | |
148 | } elsif ($cmd eq "balance" and !$arguments) { |
175 | } elsif ($cmd eq "balance" and !$arguments) { |
149 | $who->reply ($npc, "Balance of whom?"); |
176 | $who->reply ($npc, "Balance of whom?"); |
150 | |
177 | |
151 | } elsif ($cmd eq "deposit" and $currency) { |
178 | } elsif ($cmd eq "deposit" and $currency) { |
152 | $who->reply ($npc, transaction $who, "deposit", $amount, $currency); |
179 | $who->reply ($npc, transaction $who, "deposit", $amount, $currency); |
153 | |
180 | |
|
|
181 | } elsif ($cmd eq 'deposit' and $amount eq 'all') { |
|
|
182 | $who->reply ($npc, transaction_all $who, "deposit"); |
|
|
183 | |
154 | } elsif ($cmd eq "deposit" and $amount and $currency eq "") { |
184 | } elsif ($cmd eq "deposit" and $amount and $currency eq "") { |
155 | $who->reply ($npc, "deposit $amount of what: " . join ", ", cf::coin_names); |
185 | $who->reply ($npc, "deposit $amount of what: " . join ", ", cf::coin_names); |
156 | |
186 | |
157 | } elsif ($cmd eq "deposit" and $amount eq "" and $currency eq "") { |
187 | } elsif ($cmd eq "deposit" and $amount eq "" and $currency eq "") { |
158 | $who->reply ($npc, "How much do you want to deposit?"); |
188 | $who->reply ($npc, "How much do you want to deposit?"); |
159 | |
189 | |
|
|
190 | } elsif ($cmd eq 'withdraw' and $amount eq 'all') { |
|
|
191 | $who->reply ($npc, transaction_all $who, "withdraw"); |
|
|
192 | |
160 | } elsif ($cmd eq "withdraw" and $currency) { |
193 | } elsif ($cmd eq "withdraw" and $currency) { |
161 | $who->reply ($npc, transaction $who, "withdraw", $amount, $currency); |
194 | $who->reply ($npc, transaction $who, "withdraw", $amount, $currency); |
162 | |
195 | |
163 | } elsif ($cmd eq "withdraw" and $amount and $currency eq "") { |
196 | } elsif ($cmd eq "withdraw" and $amount and $currency eq "") { |
164 | $who->reply ($npc, "Withdraw $amount of what: " . join ", ", cf::coin_names); |
197 | $who->reply ($npc, "Withdraw $amount of what: " . join ", ", cf::coin_names); |