#! 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"], "/elmex/jeweler/jeweler_inn_upper" => [ 500, "jeweler town", "jewelor"], ); # we have to special case some special cases :) sub reject_entry { my ($pl) = @_; my $prev_pos = $pl->ob->{_prev_pos}; $pl->ob->goto ($prev_pos ? @$prev_pos : ("/world/world_105_115", 2, 34)); cf::override; } 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) = @_; cf::cede_to_tick; 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: " . "Your highness, we deducted your apartment rent ($deduct_string) from your bank account."); } else { $pl->ob->reply (undef, "Something whispers into your ear: " . "Your highness, 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)\n") 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, your highness! " . "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::player->attach ( 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->kv_get ("schmorplog_last_save") - $pl->{rent}{last_online_check}; } else { $pl->{rent}{last_online_check} = time; } update_balance $pl; }, ); cf::map::attachment rent => on_enter => sub { my ($map, $pl, $x, $y) = @_; my $pfx = sprintf "~%s/", $pl->ob->name; # only do something if entering ones own apartment if ($pfx eq substr $map->path, 0, length $pfx) { for my $path (keys %{ $pl->{rent}{apartment} }) { $path = sprintf "~%s%s", $pl->ob->name, $path; if ($map->path eq $path) { if (check_balance $pl) { $pl->ob->reply (undef, "Welcome to your apartment, your highness!"); } else { $pl->ob->reply (undef, "We are sorry, your highness, you have to pay your rent first."); reject_entry $pl; } return; } } $pl->ob->reply (undef, "Your highness, you have to rent this apartment in The Apartment Shop in Scorn or other apartment shops first!"); reject_entry $pl; } }, ; our $RENT_TIMER = cf::periodic 3600, Coro::unblock_sub { pay_balance $_ for cf::player::list; };