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