1 |
#! perl |
2 |
|
3 |
use List::Util; |
4 |
|
5 |
our %apartment = ( |
6 |
"/scorn/apartment/apartments" => [ 1, "scorn", "skorn"], |
7 |
"/santo_dominion/sdomino_appartment" => [ 10, "santo dominion", "domino"], |
8 |
"/darcap/darcap/apartment" => [ 30, "darcap", "thecap"], |
9 |
"/gotischerbereich/steinwandstadt/sapartment1" => [ 100, "steinwandstadt", "konkret"], |
10 |
"/navar_city/apartments/apartment" => [ 250, "navar", "navar"], |
11 |
"/celvear_port/tower2/tower2" => [ 300, "celvear port", "kevlar"], |
12 |
"/azumauindo/ranbounagisatoshi/apartments/sapartment" => [ 100, "乱暴渚都市", "benjo"], |
13 |
"/azumauindo/suno-yamatoshi/apartments/lapartment1" => [ 1000, "スノー大和島根", "sama"], |
14 |
"/valleynoy/jewelertown/jeweler_inn_upper" => [ 500, "jeweler town", "jewelor"], |
15 |
"/pup_land/nurnberg/apartment/main" => [ 300, "nürnberg", "sauerkraut"], |
16 |
"/lostwages/petapartment" => [ 5000, "lostwages", "losvegas"], |
17 |
"/gotischerbereich/towerapartment/tower1" => [10000, "gotisch", "retreat"], |
18 |
"/lake_country/Butakis/apartment" => [25000, "castle butakis", "expandor"], |
19 |
"/brest/apartments/brest_town_house" => [30000, "brest", "brecht"], |
20 |
"/pup_land/lone_town/apartment/groundfloor" => [50000, "lone town", "looney"], |
21 |
); |
22 |
|
23 |
# we have to special case some special cases :) |
24 |
sub reject_entry { |
25 |
my ($pl) = @_; |
26 |
|
27 |
my $prev_pos = $pl->ob->{_prev_pos}; |
28 |
$pl->ob->goto ($prev_pos ? @$prev_pos : ("/world/world_105_115", 2, 34)); |
29 |
|
30 |
cf::override; |
31 |
} |
32 |
|
33 |
sub update_balance { |
34 |
my ($pl) = @_; |
35 |
|
36 |
my $NOW = time; |
37 |
|
38 |
# 1 silver per day per level per apartment of kingdom tax |
39 |
my $offline = (List::Util::min 30, ($NOW - $pl->{rent}{last_offline_check}) / 86400) |
40 |
* (cf::exp_to_level $pl->ob->stats->exp) |
41 |
* scalar keys %{ $pl->{rent}{apartment} }; |
42 |
|
43 |
# once per hour per map rented |
44 |
my $online = ($NOW - $pl->{rent}{last_online_check}) / 3600 |
45 |
* List::Util::sum map $apartment{$_}[0], keys %{ $pl->{rent}{apartment} }; |
46 |
|
47 |
$pl->{rent}{last_offline_check} = $NOW; |
48 |
$pl->{rent}{last_online_check} = $NOW; |
49 |
|
50 |
$pl->{rent}{balance} += $offline + $online; |
51 |
} |
52 |
|
53 |
sub pay_balance { |
54 |
my ($pl) = @_; |
55 |
|
56 |
cf::cede_to_tick; |
57 |
|
58 |
update_balance $pl; |
59 |
|
60 |
return unless $pl->{rent}{balance} > 0; |
61 |
|
62 |
my $deduct = cf::ceil $pl->{rent}{balance}; |
63 |
|
64 |
my $deduct_string = cf::cost_string_from_value $deduct; |
65 |
|
66 |
if ($deduct <= $pl->ob->{bank_balance}) { |
67 |
cf::db_put rent => balance => $deduct + cf::db_get rent => "balance"; |
68 |
$pl->ob->{bank_balance} -= $deduct; |
69 |
$pl->{rent}{balance} -= $deduct; |
70 |
$pl->ob->reply (undef, "Something whispers into your ear: " |
71 |
. "Your highness, we deducted your apartment rent ($deduct_string) from your bank account."); |
72 |
} else { |
73 |
$pl->ob->reply (undef, "Something whispers into your ear: " |
74 |
. "Your highness, we want to deduct the apartment rent ($deduct_string), but the bank informed us that they cannot perform the transaction. " |
75 |
. "Please even out your balance so we can deduct the fees, otherwise we will be forced to shut down your access to the apartment."); |
76 |
} |
77 |
} |
78 |
|
79 |
sub check_balance { |
80 |
my ($pl) = @_; |
81 |
|
82 |
pay_balance $pl if $pl->{rent}{balance} > 0; |
83 |
|
84 |
$pl->{rent}{balance} <= 0 |
85 |
} |
86 |
|
87 |
sub find_apartment { |
88 |
my ($pl, $name) = @_; |
89 |
|
90 |
my $apartment = (grep $apartment{$_}[2] eq $name, keys %apartment)[0] |
91 |
or $pl->ob->reply (undef, "Sorry, but we do not offer model '$name' for rent."); |
92 |
|
93 |
$apartment |
94 |
} |
95 |
|
96 |
cf::register_script_function "rent::overview" => sub { |
97 |
my ($pl, $types) = @_; |
98 |
|
99 |
while (my ($k, $v) = each %apartment) { |
100 |
my $type = exists $pl->{rent}{apartment}{$k} ? 1 : 2; |
101 |
|
102 |
$pl->ob->reply (undef, "model \"$v->[2]\", located in $v->[1] (" |
103 |
. (cf::cost_string_from_value $v->[0]) . "/hr)\n") |
104 |
if $type & $types; |
105 |
} |
106 |
}; |
107 |
|
108 |
cf::register_script_function "rent::status" => sub { |
109 |
my ($pl, $types) = @_; |
110 |
|
111 |
if ($pl->{rent}{balance} <= 0) { |
112 |
$pl->ob->reply (undef, "You have no debts to pay."); |
113 |
} else { |
114 |
$pl->ob->reply (undef, "You owe us " . (cf::cost_string_from_value $pl->{rent}{balance}) . "."); |
115 |
} |
116 |
}; |
117 |
|
118 |
cf::register_script_function "rent::rent" => sub { |
119 |
my ($pl, $apartment) = @_; |
120 |
|
121 |
$apartment = find_apartment $pl, $apartment |
122 |
or return; |
123 |
|
124 |
update_balance $pl; |
125 |
|
126 |
$pl->{rent}{apartment}{$apartment} = undef; |
127 |
|
128 |
$pl->ob->reply (undef, "Wonderful decision, your highness! " |
129 |
. "We told the proprietor in $apartment{$apartment}[1] to expect you and let you in. " |
130 |
. "We are sure you will be satisfied!"); |
131 |
}; |
132 |
|
133 |
cf::register_script_function "rent::stop" => sub { |
134 |
my ($pl, $apartment) = @_; |
135 |
|
136 |
$apartment = find_apartment $pl, $apartment |
137 |
or return; |
138 |
|
139 |
update_balance $pl; |
140 |
|
141 |
delete $pl->{rent}{apartment}{$apartment}; |
142 |
|
143 |
$pl->ob->reply (undef, "I am sorry to hear that, we will immediately stop charging you for your apartment, of course."); |
144 |
}; |
145 |
|
146 |
cf::player->attach ( |
147 |
prio => 100, |
148 |
on_login => sub { |
149 |
my ($pl) = @_; |
150 |
|
151 |
$pl->{rent}{last_offline_check} ||= time; |
152 |
|
153 |
if ($pl->{rent}{last_online_check}) { |
154 |
$pl->{rent}{last_online_check} = time |
155 |
- List::Util::min 3600, |
156 |
$pl->ob->kv_get ("schmorplog_last_save") - $pl->{rent}{last_online_check}; |
157 |
} else { |
158 |
$pl->{rent}{last_online_check} = time; |
159 |
} |
160 |
|
161 |
update_balance $pl; |
162 |
}, |
163 |
); |
164 |
|
165 |
cf::map::attachment rent => |
166 |
on_enter => sub { |
167 |
my ($map, $pl, $x, $y) = @_; |
168 |
|
169 |
return if $pl->ob->flag (cf::FLAG_WIZ); |
170 |
|
171 |
my $pfx = sprintf "~%s/", $pl->ob->name; |
172 |
|
173 |
# only do something if entering ones own apartment |
174 |
if ($pfx eq substr $map->path, 0, length $pfx) { |
175 |
for my $path (keys %{ $pl->{rent}{apartment} }) { |
176 |
$path = sprintf "~%s%s", $pl->ob->name, $path; |
177 |
|
178 |
if ($map->path eq $path) { |
179 |
if (check_balance $pl) { |
180 |
$pl->ob->reply (undef, "Welcome to your apartment, your highness!"); |
181 |
} else { |
182 |
$pl->failmsg ("We are sorry, your highness, you have to pay your rent first."); |
183 |
reject_entry $pl; |
184 |
} |
185 |
|
186 |
return; |
187 |
} |
188 |
} |
189 |
|
190 |
$pl->failmsg ("Your highness, you have to rent this apartment in The Apartment Shop in Scorn or other apartment shops first!"); |
191 |
reject_entry $pl; |
192 |
} |
193 |
}, |
194 |
; |
195 |
|
196 |
our $RENT_TIMER = cf::periodic 3600, Coro::unblock_sub { |
197 |
pay_balance $_ for cf::player::list; |
198 |
}; |
199 |
|