#! perl # The international bank script. There are five commands one can # give the banker in-game: # - help, will show the usage information # - deposit, can deposit a certain amount of a given currency unit # - withdraw, can give out a certain amount of a given currency unit # provided, the bank account has this amount of money # - exchange, will exchange between royalties and platinum coins # - balance, simply shows how much money one has on his/her account. # # TODO: # - Comment more steps # - Implement exchange() # Subroutine just to calculate exchange rates. sub calc_exchange_rate { my ($value, $from_cur, $to_cur) = @_; $value * $from_cur->value / $to_cur->value } # Calculates and returns how much money someone has starting with the # highest value currency going down. Think of it like having the date # and time in the format of 2006-05-19 03:21:31 UTC instead of 1148001691 # seconds since 1970-01-01 00:00:00 UTC. sub balance { my ($who, $is_wiz) = @_; my $balance = $who->{bank_balance}; unless ($is_wiz) { return "Sorry, you have no balance." unless $balance >= 1; } else { return $who->name." has no balance." unless $balance >= 1; } my $account_info = cf::cost_string_from_value $balance; $is_wiz ? $who->name . " has $account_info." : "You have $account_info." } sub transaction_all { my ($who, $action) = @_; my $money = $who->query_money; if ($action eq 'deposit') { return "Sorry, you don't seem to have any money to deposit." unless $money > 0; if ($who->pay_amount ($money)) { $who->{bank_balance} += $money; my $account_info = cf::cost_string_from_value $money; return "$account_info received."; } else { return "Sorry, but we can't reach all your money. Something is wrong."; } } elsif ($action eq 'withdraw') { $who->pay_player ($who->{bank_balance}); my $account_info = cf::cost_string_from_value $who->{bank_balance}; $who->{bank_balance} = 0; return "You withdrew $account_info."; } return "Sorry, we don't know what you mean by '$action'." } sub transaction { my ($who, $action, $amount, $currency) = @_; my $coin = cf::coin_from_name $currency or return "We don't trade in that currency.\n"; return "You can not $action fractions or weirder numbers." unless $amount =~ /^\d+$/; $currency = $amount > 1 ? $coin->name_pl : $coin->name; my $pay = $amount * $coin->value; # First check for possible overflow and user stupidity if ($pay > 2**30) { return "Sorry, we do not handle that amount of money for one $action."; } elsif ($pay == 0) { return "You can not $action nothing."; } elsif ($pay < 0) { return "You can not $action negative values."; } # Here we handle the transactions if ($action eq "deposit") { if ($who->pay_amount ($pay)) { $who->{bank_balance} += $pay; return "$amount $currency received."; } else { return "You don't have enough money with you."; } } elsif ($action eq "withdraw") { if ($pay <= $who->{bank_balance}) { if ($who->pay_player_arch ($coin->archname, $amount)) { $who->{bank_balance} -= $pay; return "Withdrew $amount $currency"; } else { return "You can't withdraw this many $currency in one go!"; } } else { return "You don't have that much money on your bank account."; } } } sub help { my ($command) = @_; if ($command eq "") { return "You can: deposit withdraw balance help rates help "; } elsif ($command eq "deposit") { return "You can deposit some of the money you don't currently use in our bank. To do this, ask me to: deposit \nOr, to deposit all your money: deposit all\nSee help rates for our currencies."; } elsif ($command eq "withdraw") { return "To get the money you stored in our system back, just say: withdraw \nOr withdraw all to withdraw all your money."; } elsif ($command eq "balance") { return "I can tell you how much money you are saving in our bank, just ask me about your balance."; } elsif ($command eq "rates") { my @coins = cf::coin_archetypes; my $exchange_rates = "Here is a list of exchange rates:\n\n"; for my $f (0 .. $#coins) { for my $t ($f + 1 .. $#coins) { $exchange_rates .= sprintf " One %s is %d %s.\n", $coins[$f]->name, (calc_exchange_rate 1, $coins[$f], $coins[$t]), $coins[$t]->name_pl; } } return "$exchange_rates\nThis may change a bit with the stock markets but don't expect it to be much."; } } cf::register_script_function "bank::command" => sub { my ($who, $msg, $npc) = @_; my ($cmd, $arguments) = split /\s+/, $msg, 2; my ($amount, $currency) = split /\s+/, $arguments, 2; my $service_charge = 5; my $fees = - ($service_charge / 100) + 1; if ($cmd eq "balance") { if ($who->flag (cf::FLAG_WIZ) && $arguments =~ /\S/) { cf::async { if (my $player = cf::player::find $arguments) { $who->reply ($npc, "Nancy says: " . balance ($player->ob, 1), cf::NDI_BROWN | cf::NDI_REPLY ); } }; } else { $who->reply ($npc, balance ($who, 0)); } } elsif ($cmd eq "balance" and !$arguments) { $who->reply ($npc, "Balance of whom?"); } elsif ($cmd eq "deposit" and $currency) { $who->reply ($npc, transaction $who, "deposit", $amount, $currency); } elsif ($cmd eq 'deposit' and $amount eq 'all') { $who->reply ($npc, transaction_all $who, "deposit"); } elsif ($cmd eq "deposit" and $amount and $currency eq "") { $who->reply ($npc, "deposit $amount of what: " . join ", ", cf::coin_names); } elsif ($cmd eq "deposit" and $amount eq "" and $currency eq "") { $who->reply ($npc, "How much do you want to deposit?"); } elsif ($cmd eq 'withdraw' and $amount eq 'all') { $who->reply ($npc, transaction_all $who, "withdraw"); } elsif ($cmd eq "withdraw" and $currency) { $who->reply ($npc, transaction $who, "withdraw", $amount, $currency); } elsif ($cmd eq "withdraw" and $amount and $currency eq "") { $who->reply ($npc, "Withdraw $amount of what: " . join ", ", cf::coin_names); } elsif ($cmd eq "withdraw" and $amount eq "" and $currency eq "") { $who->reply ($npc, "How much do you want to withdraw?"); } elsif ($cmd eq "help" or $cmd eq "yes" and $arguments eq "") { $who->reply ($npc, help $arguments); } elsif ($cmd eq "help" or $cmd eq "yes" and $arguments ne "") { $who->reply ($npc, help); } else { $who->reply ($npc, "Do you need help?"); } }