--- deliantra/maps/perl/rent.ext 2006/09/18 00:40:30 1.1 +++ deliantra/maps/perl/rent.ext 2006/09/18 18:20:11 1.2 @@ -26,6 +26,16 @@ $portal->free; } +# we have to special case some special cases :) +sub reject_entry { + my ($pl) = @_; + + cf::override; + + teleport $pl, "/world/world_105_115", 2, 34 + unless $pl->ob->map && $pl->ob->map->path !~ /nimbus/; +} + sub update_balance { my ($pl) = @_; @@ -37,23 +47,53 @@ * scalar keys %{ $pl->{rent}{apartment} }; # once per hour per map rented - my $online = ($NOW - $pl->{rent}{last_offline_check}) / 3600 + my $online = ($NOW - $pl->{rent}{last_online_check}) / 3600 * List::Util::sum map $apartment{$_}[0], keys %{ $pl->{rent}{apartment} }; - #TODO: add to {balance} - warn "off $offline on $online\n";#d# + $pl->{rent}{last_offline_check} = $NOW; + $pl->{rent}{last_online_check} = $NOW; + + $pl->{rent}{balance} += $offline + $online; } sub pay_balance { my ($pl) = @_; - #TODO: rob the bank if balance > 0 + update_balance $pl; + + return unless $pl->{rent}{balance} > 0; + + my $deduct = cf::ceil $pl->{rent}{balance}; + + my $deduct_string = cf::cost_string_from_value $deduct; + + if ($deduct <= $pl->ob->{bank_balance}) { + $pl->ob->{bank_balance} -= $deduct; + $pl->{rent}{balance} -= $deduct; + $pl->ob->reply (undef, "Something whispers into your ear:\n" + . "Sir, we deducted your apartment rent ($deduct_string) from your bank account."); + } else { + $pl->ob->reply (undef, "Something whispers into your ear:\n" + . "Sir, we want to deduct the apartment rent ($deduct_string), but the bank informed us that they cannot perform the transaction. " + . "Please even out your balance so we can deduct the fees, otherwise we will be forced to shut down your access to the apartment."); + } +} + +sub check_balance { + my ($pl) = @_; + + pay_balance $pl if $pl->{rent}{balance} < 0; + + $pl->{rent}{balance} >= 0 } sub find_apartment { my ($pl, $name) = @_; - #TODO: find apartment or reply with error + my $apartment = (grep $apartment{$_}[2] eq $name, keys %apartment)[0] + or $pl->ob->reply (undef, "Sorry, but we do not offer model '$name' for rent."); + + $apartment } cf::register_script_function "rent::overview" => sub { @@ -81,23 +121,29 @@ cf::register_script_function "rent::rent" => sub { my ($pl, $apartment) = @_; - $apartment = find_apartment $apartment + $apartment = find_apartment $pl, $apartment or return; update_balance $pl; $pl->{rent}{apartment}{$apartment} = undef; + + $pl->ob->reply (undef, "Wonderful decision, sir! " + . "We told the proprietor in $apartment{$apartment}[1] to expect you and let you in. " + . "We are sure you will be satisfied!"); }; cf::register_script_function "rent::stop" => sub { my ($pl, $apartment) = @_; - $apartment = find_apartment $apartment + $apartment = find_apartment $pl, $apartment or return; update_balance $pl; delete $pl->{rent}{apartment}{$apartment}; + + $pl->ob->reply (undef, "I am sorry to hear that, we will immediately stop charging you for your apartment, of course."); }; cf::attach_to_players prio => 100, @@ -123,10 +169,31 @@ cf::register_map_attachment rent => on_enter => sub { - return; my ($map, $pl, $x, $y) = @_; -# teleport $pl, "/world/world_105_115", 2, 34; -# cf::override; - }; + for my $path (keys %{ $pl->{rent}{apartment} }) { + $path =~ y/\//_/; + $path = sprintf "%s/%s/%s/%s", + cf::localdir, cf::playerdir, $pl->ob->name, $path; + + if ($map->path eq $path) { + if (check_balance $pl) { + $pl->ob->reply (undef, "Welcome to your apartment, sir!"); + } else { + $pl->ob->reply (undef, "We are sorry, sir, you have to pay your rent first."); + reject_entry $pl; + } + + return; + } + } + + $pl->ob->reply (undef, "Sir, you have to rent this apartment in The Apartment Shop in Scorn first!"); + reject_entry $pl; + }, +; + +Event->timer (after => 60, interval => 3600, cb => sub { + pay_balance $_ for cf::player::list; +});