--- deliantra/maps/perl/bank.ext 2006/05/21 22:00:19 1.3 +++ deliantra/maps/perl/bank.ext 2006/05/31 14:48:29 1.17 @@ -6,30 +6,26 @@ # - 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 imperials and platinum coins +# - exchange, will exchange between royalties and platinum coins # - balance, simply shows how much money one has on his/her account. # # TODO: -# - Continue modularising -# * transaction() # - 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, - imperial => 10000, + silver => 1, + gold => 10, + platina => 50, + royalty => 5000, ); -# Subroutine just to calculate exchange rates, I purposefully left in the -# exchange rates for imperial => imperial, platina => platina, etc. +# Subroutine just to calculate exchange rates. sub calc_exchange_rate { my ($value, $from_cur, $to_cur) = @_; - if ($unit{$from_cur} and $unit{$to_cur}) { - return $value * $unit{$from_cur} / $unit{$to_cur}; - } + $value * $unit{$from_cur} / $unit{$to_cur} } # Calculates and returns how much money someone has starting with the @@ -37,96 +33,80 @@ # 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) = @_; + my ($who, $is_wiz) = @_; my $balance = $who->{bank_balance}; - if (!$balance or int($balance) == 0) { - return "Sorry, you have no balance."; + unless ($is_wiz) { + return "Sorry, you have no balance." + unless $balance >= 1; } else { + return $who->name." has no balance." + unless $balance >= 1; + } - my $imperials = calc_exchange_rate($balance, "silver", "imperial"); - my $platinum = calc_exchange_rate($imperials - int ($imperials), "imperial", "platina"); - my $gold = calc_exchange_rate($platinum - int ($platinum), "platina", "gold"); - my $silver = calc_exchange_rate($gold - int ($gold), "gold", "silver"); + my $royalties = calc_exchange_rate $balance, "silver", "royalty"; + my $platinum = calc_exchange_rate $royalties - (int $royalties), "royalty", "platina"; + my $gold = calc_exchange_rate $platinum - (int $platinum), "platina", "gold"; + my $silver = calc_exchange_rate $gold - (int $gold), "gold", "silver"; - return "You have ".int($imperials)." imperials, ".int($platinum)." platinum, ".int($gold)." gold and about ".int($silver)." silver coins."; - } + my $account_info = + (int $royalties) . " royalties, " + . (int $platinum) . " platinum, " + . (int $gold) . " gold and about " + . (int $silver) ." silver coins."; + + return "You have $account_info" + unless $is_wiz; + + return $who->name." has $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}; + + return "You can not $action fractions." + unless $amount =~ /^\d+$/; + # First check for possible overflow and user stupidity - if ($amount > 10000) { - return "Sorry, we do not handle more than 10000 units for one ".$action."."; + if ($amount > 2**31) { + return "Sorry, we do not handle that kind of money for one ".$action."."; } elsif ($amount == 0) { return "You can not ".$action." nothing."; } elsif ($amount < 0) { return "You can not ".$action." negative values."; - } else { - # Here we handle the transactions - if ($action eq "deposit") { - if ($who->pay_amount ($pay)) { - $who->{bank_balance} += $pay; - return "$amount $currency received."; + } + + # 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}) { + $who->{bank_balance} -= $pay; + if ($currency eq "royalty") { + my $money = cf::object::new "royalty"; + $money->set_property (1, 24, $amount); + $money->insert_in_ob ($who); } else { - return "You don't have enough money with you."; - } - } elsif ($action eq "withdraw") { - if ($who->{bank_balance} - $pay > 0) { - $who->{bank_balance} -= $pay; - if ($currency eq "imperial") { - my $money = cf::object::new ("imperial"); - $money->set_property (1, 24, $amount); - $money->insert_in_ob ($who); - } else { - my $money = cf::object::new ($currency."coin"); - $money->set_property (1, 24, $amount); - $money->insert_in_ob ($who); - } - return "Withdrew $amount $currency"; + my $money = cf::object::new $currency . "coin"; + $money->set_property (1, 24, $amount); + $money->insert_in_ob ($who); } + return "Withdrew $amount $currency"; + } else { + return "You don't have that much money on your bank account."; } } } -sub on_say { - my ($event, $ob, $who, $msg) = @_; - 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; - - if ($cmd eq "balance") { - $who->message ($ob->name." says: ".balance ($who), cf::NDI_WHITE); - } - - elsif ($cmd eq "deposit" and $currency) { - $who->message ($ob->name." says: ".transaction($who, "deposit", $amount, $currency), cf::NDI_WHITE); - $who->message ($ob->name." says: ".balance ($who), cf::NDI_WHITE); - } elsif ($cmd eq "deposit" and $amount and $currency eq "") { - $who->message ($ob->name." says: Deposit $amount of what: ".$units.".", cf::NDI_WHITE); - } elsif ($cmd eq "deposit" and $amount eq "" and $currency eq "") { - $who->message ($ob->name." says: How much do you want to deposit?", cf::NDI_WHITE); - } - - elsif ($cmd eq "withdraw" and $currency) { - $who->message ($ob->name." says: ".transaction($who, "withdraw", $amount, $currency), cf::NDI_WHITE); - $who->message ($ob->name." says: ".balance ($who), cf::NDI_WHITE); - } elsif ($cmd eq "withdraw" and $amount and $currency eq "") { - $who->message ($ob->name." says: Withdraw $amount of what: ".$units.".", cf::NDI_WHITE); - } elsif ($cmd eq "withdraw" and $amount eq "" and $currency eq "") { - $who->message ($ob->name." says: How much do you want to withdraw?", cf::NDI_WHITE); - } - - elsif ($cmd eq "help" || "yes") { - $who->message ($ob->name." says: ".help($arguments), cf::NDI_WHITE); - } -} - sub help { my ($command) = @_; @@ -146,19 +126,83 @@ } 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 imperial notes and platinum coins at a very low price. To change from platinum to imperials, say: - exchange platina\nTo change from imperials to platinum, say: - exchange imperial\nWe currently only exchange between these two currencies. If you want to know more about exchange rates, ask me: + 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") { - return "Here is a list of exchange rates:\n" - ."imperial => ".calc_exchange_rate(1, "imperial", "platina")." platina\n" - ."imperial => ".calc_exchange_rate(1, "imperial", "gold")." gold\n" - ."imperial => ".calc_exchange_rate(1, "imperial", "silver")." silver\n" + my @units = sort { $unit{$b} <=> $unit{$a} } keys %unit; + my $exchange_rates = "Here is a list of exchange rates:\n"; - ."platina => ".calc_exchange_rate(1, "platina", "gold")." gold\n" - ."platina => ".calc_exchange_rate(1, "platina", "silver")." silver\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"; + } + } + return $exchange_rates."This may change a bit with the stock markets but don't expect it to be much."; + } +} + +sub on_say { + my ($event, $ob, $who, $msg) = @_; + my ($cmd, $arguments) = split /\s+/, $msg, 2; + my ($amount, $currency) = split /\s+/, $arguments, 2; + my $name = $ob->name; + 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)) { + if (my $player = cf::player::find $arguments) { + $who->message ("$name says: ".balance ($player->ob, 1), cf::NDI_WHITE); + } + } else { + $who->message ("$name says: ".balance ($who, 0), cf::NDI_WHITE); + } + + } elsif ($cmd eq "balance" and !$arguments) { + $who->message ("$name says: Balance of whom?", cf::NDI_WHITE); + + } elsif ($cmd eq "deposit" and $currency) { + $who->message ("$name says: " . (transaction $who, "deposit", $amount, $currency), cf::NDI_WHITE); + $who->message ("$name says: " . (balance $who), cf::NDI_WHITE); + + } elsif ($cmd eq "deposit" and $amount and $currency eq "") { + $who->message ("$name says: Deposit $amount of what: $units", cf::NDI_WHITE); - ."gold => ".calc_exchange_rate(1, "gold", "silver")." silver\nThis may change a bit with the stock markets but don't expect it to be much."; + } elsif ($cmd eq "deposit" and $amount eq "" and $currency eq "") { + $who->message ("$name says: How much do you want to deposit?", cf::NDI_WHITE); + + } elsif ($cmd eq "withdraw" and $currency) { + $who->message ("$name says: " . (transaction $who, "withdraw", $amount, $currency), cf::NDI_WHITE); + $who->message ("$name says: " . (balance $who), cf::NDI_WHITE); + + } elsif ($cmd eq "withdraw" and $amount and $currency eq "") { + $who->message ("$name says: Withdraw $amount of what: $units", cf::NDI_WHITE); + + } elsif ($cmd eq "withdraw" and $amount eq "" and $currency eq "") { + $who->message ("$name says: How much do you want to withdraw?", cf::NDI_WHITE); + + } elsif ($cmd eq "exchange") { + $who->message ("$name says: This function is not yet implemented. Ask me again later.", cf::NDI_WHITE); + + } elsif ($cmd eq "help" or $cmd eq "yes" and $arguments eq "") { + $who->message ("$name says: " . (help $arguments), cf::NDI_WHITE); + + } elsif ($cmd eq "help" or $cmd eq "yes" and $arguments ne "") { + $who->message ("$name says: " . (help), cf::NDI_WHITE); + + } else { + $who->message ("$name says: Do you need help?", cf::NDI_WHITE); } } +