--- deliantra/server/ext/bank.ext 2007/10/05 08:14:23 1.3 +++ deliantra/server/ext/bank.ext 2008/04/10 15:35:16 1.5 @@ -13,19 +13,11 @@ # - Comment more steps # - Implement exchange() -# The units are being used in each sub so they are declared globally. -my %unit = ( - silver => 1, - gold => 10, - platina => 50, - royalty => 5000, -); - # Subroutine just to calculate exchange rates. sub calc_exchange_rate { my ($value, $from_cur, $to_cur) = @_; - $value * $unit{$from_cur} / $unit{$to_cur} + $value * $from_cur->value / $to_cur->value } # Calculates and returns how much money someone has starting with the @@ -35,7 +27,6 @@ sub balance { my ($who, $is_wiz) = @_; my $balance = $who->{bank_balance}; - warn "BALA $balance<<\n"; unless ($is_wiz) { return "Sorry, you have no balance." @@ -45,31 +36,33 @@ unless $balance >= 1; } - my $account_info = cf::cost_string_from_value ($balance); - - return "You have $account_info" - unless $is_wiz; + my $account_info = cf::cost_string_from_value $balance; - return $who->name." has $account_info"; + $is_wiz + ? $who->name . " has $account_info." + : "You have $account_info." } sub transaction { my ($who, $action, $amount, $currency) = @_; - my $pay = $amount * $unit{$currency}; - return "We don't trade in that currency." - unless exists $unit{$currency}; + my $coin = cf::coin_from_name $currency + or return "We don't trade in that currency.\n"; - return "You can not $action fractions." + 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 ($amount > 2**31) { - return "Sorry, we do not handle that kind of money for one ".$action."."; + if ($amount > 2**30) { + return "Sorry, we do not handle that kind of money for one $action."; } elsif ($amount == 0) { - return "You can not ".$action." nothing."; + return "You can not $action nothing."; } elsif ($amount < 0) { - return "You can not ".$action." negative values."; + return "You can not $action negative values."; } # Here we handle the transactions @@ -82,16 +75,12 @@ } } elsif ($action eq "withdraw") { if ($pay <= $who->{bank_balance}) { - my $paid = $who->pay_player_arch ( - $currency eq 'royalty' ? 'royalty' : $currency . "coin", - $amount - ); - if ($paid) { + if ($who->pay_player_arch ($coin->archname, $amount)) { $who->{bank_balance} -= $pay; + return "Withdrew $amount $currency"; } else { - $amount = 0; + return "You can't withdraw this many $currency in one go!"; } - return "Withdrew $amount $currency"; } else { return "You don't have that much money on your bank account."; } @@ -106,31 +95,31 @@ deposit withdraw balance - exchange - help "; + 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 \nSee help exchange for our currencies."; + deposit \nSee help rates for our currencies."; } elsif ($command eq "withdraw") { return "To get the money you stored in our system back, just say: withdraw "; } 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 "exchange") { - return "We exchange between royalties and platina coins at a very low price. To change from platina to royalties, say: - exchange platina royalty\nTo change from royalties to platina, say: - exchange royalty platina\nWe currently only exchange between these two currencies. If you want to know more about exchange rates, ask me: - help exchange rates\nThanks for using our bank."; - } elsif ($command eq "exchange rates") { - my @units = sort { $unit{$b} <=> $unit{$a} } keys %unit; - my $exchange_rates = "Here is a list of exchange rates:\n"; - - for my $from_cur_nr (0 .. $#units) { - for my $to_cur_nr ($from_cur_nr+1 .. $#units) { - $exchange_rates = $exchange_rates."One $units[$from_cur_nr] is ".calc_exchange_rate(1, $units[$from_cur_nr], $units[$to_cur_nr])." $units[$to_cur_nr]\n"; + } elsif ($command eq "rates") { + my @coins = cf::coin_archetypes; + my $exchange_rates = "Here is a list of exchange rates:\n\n\n"; + + for my $f (0 .. $#coins) { + for my $t ($f + 1 .. $#coins) { + $exchange_rates .= + sprintf "One %s is %d %s.\n\n", + $coins[$f]->name, + (calc_exchange_rate 1, $coins[$f], $coins[$t]), + $coins[$t]->name_pl; } } - return $exchange_rates."This may change a bit with the stock markets but don't expect it to be much."; + + return "$exchange_rates\n\nThis may change a bit with the stock markets but don't expect it to be much."; } } @@ -140,17 +129,8 @@ my ($cmd, $arguments) = split /\s+/, $msg, 2; my ($amount, $currency) = split /\s+/, $arguments, 2; my $service_charge = 5; - my @units = sort keys %unit; - my $units = join ", ", @units; my $fees = - ($service_charge / 100) + 1; - # Have some aliases for currencies - if ($currency eq "platinum") { - $currency = "platina"; - } elsif ($currency eq "royalties") { - $currency = "royalty"; - } - if ($cmd eq "balance") { if ($who->flag (cf::FLAG_WIZ) && $arguments =~ /\S/) { cf::async { @@ -170,27 +150,22 @@ } elsif ($cmd eq "deposit" and $currency) { $who->reply ($npc, transaction $who, "deposit", $amount, $currency); - $who->reply ($npc, balance $who); } elsif ($cmd eq "deposit" and $amount and $currency eq "") { - $who->reply ($npc, "deposit $amount of what: $units"); + $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 $currency) { $who->reply ($npc, transaction $who, "withdraw", $amount, $currency); - $who->reply ($npc, balance $who); } elsif ($cmd eq "withdraw" and $amount and $currency eq "") { - $who->reply ($npc, "Withdraw $amount of what: $units"); + $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 "exchange") { - $who->reply ($npc, "This function is not yet implemented. Ask me again later."); - } elsif ($cmd eq "help" or $cmd eq "yes" and $arguments eq "") { $who->reply ($npc, help $arguments);