#! perl use List::Util; my %apartment = ( "/scorn/apartment/apartments" => [ 1, "scorn", "skorn"], "/santo_dominion/sdomino_appartment" => [ 10, "santo dominion", "domino"], "/darcap/darcap/apartment" => [ 30, "darcap", "thecap"], "/navar_city/apartments/apartment" => [ 250, "navar", "navar"], "/azumauindo/ranbounagisatoshi/apartments/sapartment" => [ 100, "乱暴渚都市", "benjo"], "/azumauindo/suno-yamatoshi/apartments/lapartment1" => [ 1000, "スノー大和島根", "sama"], "/pup_land/nurnberg/apartment/main" => [ 300, "nürnberg", "sauerkraut"], "/pup_land/lone_town/apartment/groundfloor" => [50000, "lone town", "looney"], "/brest/apartments/brest_town_house" => [30000, "brest", "brecht"], ); sub teleport { my ($pl, $map, $x, $y) = @_; my $portal = cf::object::new "exit"; $portal->slaying ($map); $portal->stats->hp ($x); $portal->stats->sp ($y); $portal->apply ($pl->ob); $portal->destroy; } # 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/ && $pl->ob->map->path !~ m%/var/crossfire/players/%; } sub update_balance { my ($pl) = @_; my $NOW = time; # 1 silver per day per level per apartment of kingdom tax my $offline = (List::Util::min 30, ($NOW - $pl->{rent}{last_offline_check}) / 86400) * (cf::exp_to_level $pl->ob->stats->exp) * scalar keys %{ $pl->{rent}{apartment} }; # once per hour per map rented my $online = ($NOW - $pl->{rent}{last_online_check}) / 3600 * List::Util::sum map $apartment{$_}[0], keys %{ $pl->{rent}{apartment} }; $pl->{rent}{last_offline_check} = $NOW; $pl->{rent}{last_online_check} = $NOW; $pl->{rent}{balance} += $offline + $online; } sub pay_balance { my ($pl) = @_; 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}) { cf::db_put rent => balance => $deduct + cf::db_get rent => "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) = @_; 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 { my ($pl, $types) = @_; while (my ($k, $v) = each %apartment) { my $type = exists $pl->{rent}{apartment}{$k} ? 1 : 2; $pl->ob->reply (undef, "model \"$v->[2]\", situated in $v->[1] (" . (cf::cost_string_from_value $v->[0]) . "/hr)") if $type & $types; } }; cf::register_script_function "rent::status" => sub { my ($pl, $types) = @_; if ($pl->{rent}{balance} <= 0) { $pl->ob->reply (undef, "You have no debts to pay."); } else { $pl->ob->reply (undef, "You owe us " . (cf::cost_string_from_value $pl->{rent}{balance}) . "."); } }; cf::register_script_function "rent::rent" => sub { my ($pl, $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 $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, on_login => sub { my ($pl) = @_; $pl->{rent}{last_offline_check} ||= time; if ($pl->{rent}{last_online_check}) { $pl->{rent}{last_online_check} = time - List::Util::min 3600, $pl->ob->get_ob_key_value ("schmorplog_last_save") - $pl->{rent}{last_online_check}; } else { $pl->{rent}{last_online_check} = time; } update_balance $pl; }; cf::register_map_attachment rent => on_enter => sub { my ($map, $pl, $x, $y) = @_; # can freely enter homes of other people { my $path = sprintf "%s/%s/%s/", cf::localdir, cf::playerdir, $pl->ob->name; return if $path ne substr $map->path, 0, length $path; } 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; });